1#------------------------------------------------------------------------------- 2# Copyright (c) 2020-2023, Arm Limited. All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5# 6#------------------------------------------------------------------------------- 7 8set(CMAKE_SYSTEM_NAME Generic) 9 10find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc) 11find_program(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++) 12 13if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND") 14 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'") 15endif() 16 17if(CMAKE_CXX_COMPILER STREQUAL "CMAKE_CXX_COMPILER-NOTFOUND") 18 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-g++'") 19endif() 20 21set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) 22 23set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=) 24set(COMPILER_CMSE_FLAG -mcmse) 25 26# This variable name is a bit of a misnomer. The file it is set to is included 27# at a particular step in the compiler initialisation. It is used here to 28# configure the extensions for object files. Despite the name, it also works 29# with the Ninja generator. 30set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) 31 32# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined. 33EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION ) 34 35# ===================== SEt toolchain CPU and Arch ============================= 36 37if (DEFINED TFM_SYSTEM_PROCESSOR) 38 if(TFM_SYSTEM_PROCESSOR MATCHES "cortex-m85" AND GCC_VERSION VERSION_LESS "13.0.0") 39 # GNUARM until version 13 does not support the -mcpu=cortex-m85 flag 40 message(WARNING "Cortex-m85 is only supported from GCC13. " 41 "Falling back to -march usage for earlier versions.") 42 else() 43 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) 44 45 if (DEFINED TFM_SYSTEM_DSP) 46 if (NOT TFM_SYSTEM_DSP) 47 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp") 48 endif() 49 endif() 50 # GCC specifies that '+nofp' is available on following M-profile cpus: 'cortex-m4', 51 # 'cortex-m7', 'cortex-m33', 'cortex-m35p' and 'cortex-m55'. 52 # Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'. 53 # Explicitly list those cpu to align with GCC description. 54 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0") 55 if(NOT CONFIG_TFM_ENABLE_FP AND 56 (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4" 57 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7" 58 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33" 59 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p" 60 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) 61 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp") 62 endif() 63 endif() 64 65 if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") 66 if(NOT CONFIG_TFM_ENABLE_MVE) 67 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve") 68 endif() 69 if(NOT CONFIG_TFM_ENABLE_MVE_FP) 70 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve.fp") 71 endif() 72 endif() 73 endif() 74 75endif() 76 77# CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to 78# set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified. 79# The variable name is choosen to align with the ARMCLANG toolchain file. 80set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE}) 81 82if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") 83 if(CONFIG_TFM_ENABLE_MVE) 84 string(APPEND CMAKE_SYSTEM_ARCH "+mve") 85 endif() 86 if(CONFIG_TFM_ENABLE_MVE_FP) 87 string(APPEND CMAKE_SYSTEM_ARCH "+mve.fp") 88 endif() 89endif() 90 91if (DEFINED TFM_SYSTEM_DSP) 92 # +nodsp modifier is only supported from GCC version 8. 93 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0") 94 # armv8.1-m.main arch does not have +nodsp option 95 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND 96 NOT TFM_SYSTEM_DSP) 97 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp") 98 endif() 99 endif() 100endif() 101 102if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0") 103 if(CONFIG_TFM_ENABLE_FP) 104 string(APPEND CMAKE_SYSTEM_ARCH "+fp") 105 endif() 106endif() 107 108add_compile_options( 109 -specs=nano.specs 110 -Wall 111 -Wno-format 112 -Wno-return-type 113 -Wno-unused-but-set-variable 114 -c 115 -fdata-sections 116 -ffunction-sections 117 -fno-builtin 118 -fshort-enums 119 -funsigned-char 120 -mthumb 121 -nostdlib 122 $<$<COMPILE_LANGUAGE:C>:-std=c99> 123 $<$<COMPILE_LANGUAGE:CXX>:-std=c++11> 124 # Force DWARF version 4 for zephyr as pyelftools does not support version 5 at present 125 -gdwarf-4 126 $<$<OR:$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<BOOL:${TFM_CODE_COVERAGE}>>:-g> 127) 128 129add_link_options( 130 --entry=Reset_Handler 131 -specs=nano.specs 132 LINKER:-check-sections 133 LINKER:-fatal-warnings 134 LINKER:--gc-sections 135 LINKER:--no-wchar-size-warning 136) 137 138if(NOT CONFIG_TFM_MEMORY_USAGE_QUIET) 139 add_link_options(LINKER:--print-memory-usage) 140endif() 141 142if (GCC_VERSION VERSION_LESS 7.3.1) 143 message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.") 144endif() 145 146if (GCC_VERSION VERSION_EQUAL 10.2.1) 147 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support." 148 " Select other GNU Arm compiler versions instead." 149 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.") 150endif() 151 152# GNU Arm compiler version greater equal than *11.3.Rel1* 153# has a linker issue that required system calls are missing, 154# such as _read and _write. Add stub functions of required 155# system calls to solve this issue. 156if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1) 157 set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE) 158endif() 159 160if (CMAKE_SYSTEM_PROCESSOR) 161 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") 162 set(CMAKE_CXX_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") 163 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") 164 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") 165 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") 166else() 167 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}") 168 set(CMAKE_CXX_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}") 169 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}") 170 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}") 171 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}") 172endif() 173 174set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) 175set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_INIT}) 176set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) 177 178set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft) 179 180if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard") 181 set(COMPILER_CP_FLAG -mfloat-abi=hard) 182 set(LINKER_CP_OPTION -mfloat-abi=hard) 183 if (CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP) 184 set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH}) 185 set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH}) 186 endif() 187else() 188 set(COMPILER_CP_FLAG -mfloat-abi=soft) 189 set(LINKER_CP_OPTION -mfloat-abi=soft) 190endif() 191 192# For GNU Arm Embedded Toolchain doesn't emit __ARM_ARCH_8_1M_MAIN__, adding this macro manually. 193add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__>) 194 195macro(target_add_scatter_file target) 196 target_link_options(${target} 197 PRIVATE 198 -T $<TARGET_OBJECTS:${target}_scatter> 199 ) 200 201 add_library(${target}_scatter OBJECT) 202 foreach(scatter_file ${ARGN}) 203 target_sources(${target}_scatter 204 PRIVATE 205 ${scatter_file} 206 ) 207 # Cmake cannot use generator expressions in the 208 # set_source_file_properties command, so instead we just parse the regex 209 # for the filename and set the property on all files, regardless of if 210 # the generator expression would evaluate to true or not. 211 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") 212 set_source_files_properties(${SCATTER_FILE_PATH} 213 PROPERTIES 214 LANGUAGE C 215 KEEP_EXTENSION True # Don't use .o extension for the preprocessed file 216 ) 217 endforeach() 218 219 add_dependencies(${target} 220 ${target}_scatter 221 ) 222 223 set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>) 224 225 target_link_libraries(${target}_scatter 226 platform_region_defs 227 psa_interface 228 tfm_config 229 ) 230 231 target_compile_options(${target}_scatter 232 PRIVATE 233 -E 234 -P 235 -xc 236 ) 237endmacro() 238 239macro(add_convert_to_bin_target target) 240 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) 241 242 add_custom_target(${target}_bin 243 SOURCES ${bin_dir}/${target}.bin 244 ) 245 add_custom_command(OUTPUT ${bin_dir}/${target}.bin 246 DEPENDS ${target} 247 COMMAND ${CMAKE_OBJCOPY} 248 -O binary $<TARGET_FILE:${target}> 249 ${bin_dir}/${target}.bin 250 ) 251 252 add_custom_target(${target}_elf 253 SOURCES ${bin_dir}/${target}.elf 254 ) 255 add_custom_command(OUTPUT ${bin_dir}/${target}.elf 256 DEPENDS ${target} 257 COMMAND ${CMAKE_OBJCOPY} 258 -O elf32-littlearm $<TARGET_FILE:${target}> 259 ${bin_dir}/${target}.elf 260 ) 261 262 add_custom_target(${target}_hex 263 SOURCES ${bin_dir}/${target}.hex 264 ) 265 add_custom_command(OUTPUT ${bin_dir}/${target}.hex 266 DEPENDS ${target} 267 COMMAND ${CMAKE_OBJCOPY} 268 -O ihex $<TARGET_FILE:${target}> 269 ${bin_dir}/${target}.hex 270 ) 271 272 add_custom_target(${target}_binaries 273 ALL 274 DEPENDS ${target}_bin 275 DEPENDS ${target}_elf 276 DEPENDS ${target}_hex 277 ) 278endmacro() 279 280macro(target_share_symbols target symbol_name_file) 281 get_target_property(TARGET_TYPE ${target} TYPE) 282 if (NOT TARGET_TYPE STREQUAL "EXECUTABLE") 283 message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.") 284 endif() 285 286 FILE(STRINGS ${symbol_name_file} KEEP_SYMBOL_LIST 287 LENGTH_MINIMUM 1 288 ) 289 set(STRIP_SYMBOL_KEEP_LIST ${KEEP_SYMBOL_LIST}) 290 291 # Force the target to not remove the symbols if they're unused. 292 list(TRANSFORM KEEP_SYMBOL_LIST PREPEND "-Wl,--undefined=") 293 target_link_options(${target} 294 PRIVATE 295 ${KEEP_SYMBOL_LIST} 296 ) 297 298 list(TRANSFORM STRIP_SYMBOL_KEEP_LIST PREPEND --keep-symbol=) 299 # strip all the symbols except those proveded as arguments 300 add_custom_command( 301 TARGET ${target} 302 POST_BUILD 303 COMMAND ${CMAKE_OBJCOPY} 304 ARGS $<TARGET_FILE:${target}> --wildcard ${STRIP_SYMBOL_KEEP_LIST} --strip-all $<TARGET_FILE_DIR:${target}>/${target}_shared_symbols.axf 305 ) 306endmacro() 307 308macro(target_link_shared_code target) 309 foreach(symbol_provider ${ARGN}) 310 if (TARGET ${symbol_provider}) 311 get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE) 312 if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE") 313 message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.") 314 endif() 315 endif() 316 317 add_dependencies(${target} ${symbol_provider}) 318 target_link_options(${target} PRIVATE LINKER:-R$<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}_shared_symbols.axf) 319 endforeach() 320endmacro() 321 322macro(target_strip_symbols target) 323 set(SYMBOL_LIST "${ARGN}") 324 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=) 325 326 add_custom_command( 327 TARGET ${target} 328 POST_BUILD 329 COMMAND ${CMAKE_OBJCOPY} 330 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}> 331 ) 332endmacro() 333 334macro(target_strip_symbols_from_dependency target dependency) 335 set(SYMBOL_LIST "${ARGN}") 336 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=) 337 338 add_custom_command( 339 TARGET ${target} 340 PRE_LINK 341 COMMAND ${CMAKE_OBJCOPY} 342 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}> 343 ) 344endmacro() 345 346macro(target_weaken_symbols target) 347 set(SYMBOL_LIST "${ARGN}") 348 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=) 349 350 add_custom_command( 351 TARGET ${target} 352 POST_BUILD 353 COMMAND ${CMAKE_OBJCOPY} 354 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}> 355 ) 356endmacro() 357 358macro(target_weaken_symbols_from_dependency target dependency) 359 set(SYMBOL_LIST "${ARGN}") 360 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=) 361 362 add_custom_command( 363 TARGET ${target} 364 PRE_LINK 365 COMMAND ${CMAKE_OBJCOPY} 366 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}> 367 ) 368endmacro() 369 370# A dummy macro to align with Armclang workaround 371macro(tfm_toolchain_reload_compiler) 372endmacro() 373