1#------------------------------------------------------------------------------- 2# Copyright (c) 2020, IAR Systems AB. All rights reserved. 3# Copyright (c) 2020-2024, Arm Limited. All rights reserved. 4# 5# SPDX-License-Identifier: BSD-3-Clause 6# 7#------------------------------------------------------------------------------- 8 9cmake_minimum_required(VERSION 3.22) 10cmake_policy(SET CMP0115 NEW) 11 12SET(CMAKE_SYSTEM_NAME Generic) 13set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) 14 15if(CROSS_COMPILE) 16 set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE}) 17 set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE}) 18else() 19 set(CMAKE_C_COMPILER_TARGET arm-arm-none-eabi) 20 set(CMAKE_ASM_COMPILER_TARGET arm-arm-none-eabi) 21endif() 22 23set(CMAKE_C_COMPILER iccarm) 24set(CMAKE_CXX_COMPILER iccarm) 25set(CMAKE_ASM_COMPILER iasmarm) 26 27set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= ) 28set(COMPILER_CMSE_FLAG --cmse) 29 30set(CMAKE_C_FLAGS_DEBUG "-r -On") 31 32# This variable name is a bit of a misnomer. The file it is set to is included 33# at a particular step in the compiler initialisation. It is used here to 34# configure the extensions for object files. Despite the name, it also works 35# with the Ninja generator. 36set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) 37 38macro(tfm_toolchain_reset_compiler_flags) 39 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") 40 41 add_compile_options( 42 $<$<COMPILE_LANGUAGE:C,CXX>:-e> 43 $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full> 44 $<$<COMPILE_LANGUAGE:C,CXX>:--silent> 45 $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF> 46 $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES> 47 $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084> 48 $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros> 49 $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r> 50 ) 51endmacro() 52 53macro(tfm_toolchain_reset_linker_flags) 54 set_property(DIRECTORY PROPERTY LINK_OPTIONS "") 55 56 add_link_options( 57 --silent 58 --semihosting 59 --redirect __write=__write_buffered 60 --diag_suppress=lp005,Lp023 61 ) 62endmacro() 63 64macro(tfm_toolchain_set_processor_arch) 65 if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus") 66 set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+) 67 else() 68 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) 69 endif() 70 71 if (DEFINED TFM_SYSTEM_DSP) 72 if(NOT TFM_SYSTEM_DSP) 73 string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp") 74 endif() 75 endif() 76endmacro() 77 78macro(tfm_toolchain_reload_compiler) 79 tfm_toolchain_set_processor_arch() 80 tfm_toolchain_reset_compiler_flags() 81 tfm_toolchain_reset_linker_flags() 82 83 unset(CMAKE_C_FLAGS_INIT) 84 unset(CMAKE_C_LINK_FLAGS) 85 unset(CMAKE_ASM_FLAGS_INIT) 86 unset(CMAKE_ASM_LINK_FLAGS) 87 88 set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") 89 set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") 90 set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") 91 set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") 92 93 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) 94 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) 95 96 # Can't use the highest optimization with IAR on v8.1m arch because of the 97 # compilation bug in mbedcrypto 98 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND 99 (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND 100 ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR 101 (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND 102 (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))) 103 message(FATAL_ERROR "Only debug build available for M55 and M85" 104 " cores with IAR version between 9.20 and 9.32.1") 105 endif() 106 107 set(BL2_COMPILER_CP_FLAG 108 $<$<COMPILE_LANGUAGE:C>:--fpu=none> 109 $<$<COMPILE_LANGUAGE:ASM>:--fpu=none> 110 ) 111 # As BL2 does not use hardware FPU, specify '--fpu=none' explicitly to use software 112 # library functions for BL2 to override any implicit FPU option, such as '--cpu' option. 113 # Because the implicit hardware FPU option enforces BL2 to initialize FPU but hardware FPU 114 # is not actually enabled in BL2, it will cause BL2 runtime fault. 115 set(BL2_LINKER_CP_OPTION --fpu=none) 116 117 set(BL1_COMPILER_CP_FLAG 118 $<$<COMPILE_LANGUAGE:C>:--fpu=none> 119 $<$<COMPILE_LANGUAGE:ASM>:--fpu=none> 120 ) 121 set(BL1_LINKER_CP_OPTION --fpu=none) 122 123 if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard") 124 set(COMPILER_CP_FLAG 125 $<$<COMPILE_LANGUAGE:C>:-mfloat-abi=hard> 126 ) 127 128 if (CONFIG_TFM_ENABLE_FP) 129 set(COMPILER_CP_FLAG 130 $<$<COMPILE_LANGUAGE:C>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}> 131 $<$<COMPILE_LANGUAGE:ASM>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}> 132 ) 133 # armasm and armlink have the same option "--fpu" and are both used to 134 # specify the target FPU architecture. So the supported FPU architecture 135 # names can be shared by armasm and armlink. 136 set(LINKER_CP_OPTION --fpu=${CONFIG_TFM_FP_ARCH_ASM}) 137 endif() 138 else() 139 set(COMPILER_CP_FLAG 140 $<$<COMPILE_LANGUAGE:C>:--fpu=none> 141 $<$<COMPILE_LANGUAGE:ASM>:--fpu=none> 142 ) 143 set(LINKER_CP_OPTION --fpu=none) 144 endif() 145 146 add_compile_definitions( 147 $<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv6-m>:__ARM_ARCH_6M__=1> 148 $<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv7-m>:__ARM_ARCH_7M__=1> 149 $<$<AND:$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv7-m>,$<BOOL:__ARM_FEATURE_DSP>>:__ARM_ARCH_7EM__=1> 150 $<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8-m.base>:__ARM_ARCH_8M_BASE__=1> 151 $<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8-m.main>:__ARM_ARCH_8M_MAIN__=1> 152 $<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__=1> 153 ) 154endmacro() 155 156# Configure environment for the compiler setup run by cmake at the first 157# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is 158# done, all further compiler setup is done via tfm_toolchain_reload_compiler() 159tfm_toolchain_reload_compiler() 160 161# Behaviour for handling scatter files is so wildly divergent between compilers 162# that this macro is required. 163macro(target_add_scatter_file target) 164 target_link_options(${target} 165 PRIVATE 166 --config $<TARGET_OBJECTS:${target}_scatter> 167 ) 168 169 add_library(${target}_scatter OBJECT) 170 foreach(scatter_file ${ARGN}) 171 target_sources(${target}_scatter 172 PRIVATE 173 ${scatter_file} 174 ) 175 # Cmake cannot use generator expressions in the 176 # set_source_file_properties command, so instead we just parse the regex 177 # for the filename and set the property on all files, regardless of if 178 # the generator expression would evaluate to true or not. 179 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") 180 set_source_files_properties(${SCATTER_FILE_PATH} 181 PROPERTIES 182 LANGUAGE C 183 ) 184 endforeach() 185 186 add_dependencies(${target} 187 ${target}_scatter 188 ) 189 190 set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>) 191 192 target_link_libraries(${target}_scatter 193 platform_region_defs 194 psa_interface 195 tfm_config 196 ) 197 198 target_compile_options(${target}_scatter 199 PRIVATE 200 --preprocess=sn $<TARGET_OBJECTS:${target}_scatter> 201 ) 202endmacro() 203 204macro(add_convert_to_bin_target target) 205 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) 206 207 add_custom_target(${target}_bin 208 SOURCES ${bin_dir}/${target}.bin 209 ) 210 add_custom_command(OUTPUT ${bin_dir}/${target}.bin 211 DEPENDS ${target} 212 COMMAND ielftool 213 --silent 214 --bin $<TARGET_FILE:${target}> 215 ${bin_dir}/${target}.bin 216 ) 217 218 add_custom_target(${target}_elf 219 SOURCES ${bin_dir}/${target}.elf 220 ) 221 add_custom_command(OUTPUT ${bin_dir}/${target}.elf 222 DEPENDS ${target} 223 COMMAND ielftool 224 --silent 225 $<TARGET_FILE:${target}> 226 ${bin_dir}/${target}.elf 227 ) 228 229 add_custom_target(${target}_hex 230 SOURCES ${bin_dir}/${target}.hex 231 ) 232 add_custom_command(OUTPUT ${bin_dir}/${target}.hex 233 DEPENDS ${target} 234 COMMAND ielftool 235 --silent 236 --ihex $<TARGET_FILE:${target}> 237 ${bin_dir}/${target}.hex 238 ) 239 240 add_custom_target(${target}_binaries 241 ALL 242 DEPENDS ${target}_bin 243 DEPENDS ${target}_elf 244 DEPENDS ${target}_hex 245 ) 246endmacro() 247 248macro(target_share_symbols target) 249 get_target_property(TARGET_TYPE ${target} TYPE) 250 if (NOT TARGET_TYPE STREQUAL "EXECUTABLE") 251 message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.") 252 endif() 253 254 foreach(symbol_file ${ARGN}) 255 FILE(STRINGS ${symbol_file} SYMBOLS 256 LENGTH_MINIMUM 1 257 ) 258 list(APPEND KEEP_SYMBOL_LIST ${SYMBOLS}) 259 endforeach() 260 261 set(IAR_STEERING_FILE ${KEEP_SYMBOL_LIST}) 262 263 list(TRANSFORM IAR_STEERING_FILE PREPEND "show ") 264 list(TRANSFORM IAR_STEERING_FILE APPEND " \n") 265 list(INSERT IAR_STEERING_FILE 0 "hide *\n") 266 string(REPLACE ";" "" IAR_STEERING_FILE ${IAR_STEERING_FILE}) 267 file( GENERATE OUTPUT "$<TARGET_FILE_DIR:${target}>/iar_steering_file" CONTENT "${IAR_STEERING_FILE}") 268 269 add_custom_command( 270 TARGET ${target} 271 POST_BUILD 272 COMMAND ${CMAKE_IAR_SYMEXPORT} 273 ARGS --edit $<TARGET_FILE_DIR:${target}>/iar_steering_file $<TARGET_FILE:${target}> $<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX} 274 ) 275 276 # Force the target to not remove the symbols if they're unused. 277 list(TRANSFORM KEEP_SYMBOL_LIST PREPEND --keep=) 278 target_link_options(${target} 279 PRIVATE 280 ${KEEP_SYMBOL_LIST} 281 ) 282endmacro() 283 284macro(target_link_shared_code target) 285 get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR) 286 287 foreach(symbol_provider ${ARGN}) 288 if (TARGET ${symbol_provider}) 289 get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE) 290 if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE") 291 message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.") 292 endif() 293 endif() 294 295 add_dependencies(${target} ${symbol_provider}) 296 target_link_options(${target} PRIVATE LINKER:$<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}${CODE_SHARING_INPUT_FILE_SUFFIX}) 297 endforeach() 298endmacro() 299 300macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE) 301 message(FATAL_ERROR "Code sharing support is not implemented by IAR.") 302endmacro() 303 304macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST) 305 message(FATAL_ERROR "Code sharing support is not implemented by IAR.") 306endmacro() 307