TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, IAR Systems AB. All rights reserved. |
| 3 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | #------------------------------------------------------------------------------- |
| 8 | |
| 9 | cmake_minimum_required(VERSION 3.14) |
| 10 | |
Raef Coles | 6e8f83d | 2020-09-28 10:44:06 +0100 | [diff] [blame^] | 11 | set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES) |
| 12 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 13 | SET(CMAKE_SYSTEM_NAME Generic) |
| 14 | # This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to |
| 15 | # any value here. |
| 16 | set(CMAKE_SYSTEM_PROCESSOR cortex-m23) |
| 17 | |
| 18 | set(CMAKE_C_COMPILER iccarm) |
| 19 | set(CMAKE_ASM_COMPILER iasmarm) |
| 20 | |
| 21 | set(COMPILER_CMSE_FLAG --cmse) |
| 22 | set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= ) |
| 23 | |
| 24 | # This variable name is a bit of a misnomer. The file it is set to is included |
| 25 | # at a particular step in the compiler initialisation. It is used here to |
| 26 | # configure the extensions for object files. Despite the name, it also works |
| 27 | # with the Ninja generator. |
| 28 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) |
| 29 | |
| 30 | # Cmake makes it _really_ hard to dynamically set the cmake_system_processor |
| 31 | # (used for setting mcpu flags etc). Instead we load |
| 32 | # platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro |
| 33 | # to reload the compiler autoconfig. Note that it can't be loaded in this file |
| 34 | # as cmake does not allow the use of command-line defined variables in toolchain |
| 35 | # files and the path is platform dependent. |
| 36 | macro(_compiler_reload) |
| 37 | if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus") |
| 38 | set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+) |
| 39 | else() |
| 40 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 41 | endif() |
| 42 | set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE}) |
| 43 | |
| 44 | if (DEFINED TFM_SYSTEM_FP) |
| 45 | if(TFM_SYSTEM_FP) |
| 46 | # TODO Whether a system requires these extensions appears to depend |
| 47 | # on the system in question, with no real rule. Since adding .fp |
| 48 | # will cause compile failures on systems that already have fp |
| 49 | # enabled, this is commented out for now to avoid those failures. In |
| 50 | # future, better handling should be implemented. |
| 51 | # string(APPEND CMAKE_SYSTEM_PROCESSOR ".fp") |
| 52 | endif() |
| 53 | endif() |
| 54 | |
| 55 | if (DEFINED TFM_SYSTEM_DSP) |
| 56 | if(TFM_SYSTEM_DSP) |
| 57 | # TODO Whether a system requires these extensions appears to depend |
| 58 | # on the system in question, with no real rule. Since adding .dsp |
| 59 | # will cause compile failures on systems that already have dsp |
| 60 | # enabled, this is commented out for now to avoid those failures. In |
| 61 | # future, better handling should be implemented. |
| 62 | # string(APPEND CMAKE_SYSTEM_PROCESSOR ".dsp") |
| 63 | else() |
| 64 | string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp") |
| 65 | endif() |
| 66 | endif() |
| 67 | |
| 68 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
| 69 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 70 | add_compile_options( |
| 71 | $<$<COMPILE_LANGUAGE:C,CXX>:-e> |
| 72 | $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full> |
| 73 | $<$<COMPILE_LANGUAGE:C,CXX>:--vla> |
| 74 | $<$<COMPILE_LANGUAGE:C,CXX>:--silent> |
| 75 | $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF> |
| 76 | $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES> |
| 77 | $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084> |
| 78 | ) |
| 79 | add_link_options( |
| 80 | --silent |
| 81 | --semihosting |
| 82 | --redirect __write=__write_buffered |
| 83 | ) |
| 84 | |
| 85 | unset(CMAKE_C_FLAGS_INIT) |
| 86 | unset(CMAKE_C_LINK_FLAGS) |
| 87 | unset(CMAKE_ASM_FLAGS_INIT) |
| 88 | unset(CMAKE_ASM_LINK_FLAGS) |
| 89 | |
| 90 | set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 91 | set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 92 | set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 93 | set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 94 | |
| 95 | set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) |
| 96 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
| 97 | |
| 98 | endmacro() |
| 99 | |
| 100 | # Behaviour for handling scatter files is so wildly divergent between compilers |
| 101 | # that this macro is required. |
| 102 | macro(target_add_scatter_file target) |
| 103 | target_link_options(${target} |
| 104 | PRIVATE |
| 105 | --config $<TARGET_OBJECTS:${target}_scatter> |
| 106 | --map $<TARGET_FILE:${target}>.map |
| 107 | ) |
| 108 | add_dependencies(${target} |
| 109 | ${target}_scatter |
| 110 | ) |
| 111 | |
| 112 | add_library(${target}_scatter OBJECT) |
| 113 | foreach(scatter_file ${ARGN}) |
| 114 | target_sources(${target}_scatter |
| 115 | PRIVATE |
| 116 | ${scatter_file} |
| 117 | ) |
| 118 | # Cmake cannot use generator expressions in the |
| 119 | # set_source_file_properties command, so instead we just parse the regex |
| 120 | # for the filename and set the property on all files, regardless of if |
| 121 | # the generator expression would evaluate to true or not. |
| 122 | string(REGEX REPLACE ".*:(.*)>$" "\\1" SCATTER_FILE_PATH ${scatter_file}) |
| 123 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 124 | PROPERTIES |
| 125 | LANGUAGE C |
| 126 | ) |
| 127 | endforeach() |
| 128 | |
| 129 | set_target_properties(${target}_scatter PROPERTIES |
| 130 | SUFFIX ".icf" |
| 131 | ) |
| 132 | |
| 133 | target_link_libraries(${target}_scatter |
| 134 | platform_region_defs |
| 135 | psa_interface |
| 136 | tfm_partition_defs |
| 137 | ) |
| 138 | |
| 139 | target_compile_options(${target}_scatter |
| 140 | PRIVATE |
| 141 | --preprocess=sn $<TARGET_OBJECTS:${target}_scatter> |
| 142 | ) |
| 143 | endmacro() |
| 144 | |
| 145 | macro(add_convert_to_bin_target target) |
| 146 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 147 | |
| 148 | add_custom_target(${target}_bin |
| 149 | SOURCES ${bin_dir}/${target}.bin |
| 150 | ) |
| 151 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 152 | DEPENDS ${target} |
| 153 | COMMAND ielftool |
| 154 | --silent |
| 155 | --bin $<TARGET_FILE:${target}> |
| 156 | ${bin_dir}/${target}.bin |
| 157 | ) |
| 158 | |
| 159 | add_custom_target(${target}_elf |
| 160 | SOURCES ${bin_dir}/${target}.elf |
| 161 | ) |
| 162 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 163 | DEPENDS ${target} |
| 164 | COMMAND ielftool |
| 165 | --silent |
| 166 | $<TARGET_FILE:${target}> |
| 167 | ${bin_dir}/${target}.elf |
| 168 | ) |
| 169 | |
| 170 | add_custom_target(${target}_hex |
| 171 | SOURCES ${bin_dir}/${target}.hex |
| 172 | ) |
| 173 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 174 | DEPENDS ${target} |
| 175 | COMMAND ielftool |
| 176 | --silent |
| 177 | --ihex $<TARGET_FILE:${target}> |
| 178 | ${bin_dir}/${target}.hex |
| 179 | ) |
| 180 | |
| 181 | add_custom_target(${target}_binaries |
| 182 | ALL |
| 183 | DEPENDS ${target}_bin |
| 184 | DEPENDS ${target}_elf |
| 185 | DEPENDS ${target}_hex |
| 186 | ) |
| 187 | endmacro() |