TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, IAR Systems AB. All rights reserved. |
Dávid Házi | 19ec2d3 | 2023-02-08 11:23:34 +0100 | [diff] [blame] | 3 | # Copyright (c) 2020-2023, Arm Limited. All rights reserved. |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | #------------------------------------------------------------------------------- |
| 8 | |
Michel Jaouen | 57248b3 | 2021-11-19 13:27:13 +0100 | [diff] [blame] | 9 | cmake_minimum_required(VERSION 3.22) |
TTornblom | c0f33ce | 2021-12-02 08:49:06 +0100 | [diff] [blame] | 10 | cmake_policy(SET CMP0115 NEW) |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 11 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 12 | SET(CMAKE_SYSTEM_NAME Generic) |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 13 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 14 | |
| 15 | if(CROSS_COMPILE) |
| 16 | set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 17 | set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 18 | else() |
| 19 | set(CMAKE_C_COMPILER_TARGET arm-arm-none-eabi) |
| 20 | set(CMAKE_ASM_COMPILER_TARGET arm-arm-none-eabi) |
| 21 | endif() |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 22 | |
| 23 | set(CMAKE_C_COMPILER iccarm) |
Dávid Házi | 19ec2d3 | 2023-02-08 11:23:34 +0100 | [diff] [blame] | 24 | set(CMAKE_CXX_COMPILER iccarm) |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 25 | set(CMAKE_ASM_COMPILER iasmarm) |
| 26 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 27 | set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= ) |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 28 | set(COMPILER_CMSE_FLAG --cmse) |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 29 | |
Dávid Házi | 2d4aa52 | 2022-11-28 09:29:49 +0100 | [diff] [blame] | 30 | set(CMAKE_C_FLAGS_DEBUG "-r -On") |
| 31 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 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. |
| 36 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) |
| 37 | |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 38 | macro(tfm_toolchain_reset_compiler_flags) |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 39 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 40 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 41 | add_compile_options( |
| 42 | $<$<COMPILE_LANGUAGE:C,CXX>:-e> |
| 43 | $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full> |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 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> |
Sebastian Bøe | 196673c | 2022-09-19 15:35:32 +0200 | [diff] [blame] | 48 | $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros> |
Dávid Házi | 9d3b541 | 2022-08-23 11:22:14 +0200 | [diff] [blame] | 49 | "$<$<COMPILE_LANGUAGE:C,CXX,ASM>:SHELL:--fpu none>" |
TTornblom | 428de67 | 2021-11-11 15:32:51 +0100 | [diff] [blame] | 50 | $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r> |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 51 | ) |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 52 | endmacro() |
| 53 | |
| 54 | macro(tfm_toolchain_reset_linker_flags) |
| 55 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 56 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 57 | add_link_options( |
| 58 | --silent |
| 59 | --semihosting |
| 60 | --redirect __write=__write_buffered |
Dávid Házi | 9d3b541 | 2022-08-23 11:22:14 +0200 | [diff] [blame] | 61 | "SHELL:--fpu none" |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 62 | ) |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 63 | endmacro() |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 64 | |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 65 | macro(tfm_toolchain_set_processor_arch) |
| 66 | if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus") |
| 67 | set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+) |
| 68 | else() |
| 69 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 70 | endif() |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 71 | |
| 72 | if (DEFINED TFM_SYSTEM_DSP) |
| 73 | if(NOT TFM_SYSTEM_DSP) |
| 74 | string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp") |
| 75 | endif() |
| 76 | endif() |
| 77 | endmacro() |
| 78 | |
| 79 | macro(tfm_toolchain_reload_compiler) |
TTornblom | 330e476 | 2020-11-03 13:13:52 +0100 | [diff] [blame] | 80 | tfm_toolchain_set_processor_arch() |
| 81 | tfm_toolchain_reset_compiler_flags() |
| 82 | tfm_toolchain_reset_linker_flags() |
| 83 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 84 | unset(CMAKE_C_FLAGS_INIT) |
| 85 | unset(CMAKE_C_LINK_FLAGS) |
| 86 | unset(CMAKE_ASM_FLAGS_INIT) |
| 87 | unset(CMAKE_ASM_LINK_FLAGS) |
| 88 | |
| 89 | set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 90 | set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 91 | set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 92 | set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 93 | |
| 94 | set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) |
| 95 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
Dávid Házi | ab54b48 | 2023-02-22 13:54:52 +0100 | [diff] [blame] | 96 | |
| 97 | # Can't use the highest optimization with IAR on v8.1m arch because of the |
| 98 | # compilation bug in mbedcrypto |
| 99 | if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND |
Dávid Házi | 614a3dd | 2023-03-20 19:13:03 +0100 | [diff] [blame] | 100 | (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND |
Dávid Házi | ab54b48 | 2023-02-22 13:54:52 +0100 | [diff] [blame] | 101 | ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR |
| 102 | (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND |
Dávid Házi | 614a3dd | 2023-03-20 19:13:03 +0100 | [diff] [blame] | 103 | (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))) |
| 104 | message(FATAL_ERROR "Only debug build available for M55 and M85" |
| 105 | " cores with IAR version between 9.20 and 9.32.1") |
Dávid Házi | ab54b48 | 2023-02-22 13:54:52 +0100 | [diff] [blame] | 106 | endif() |
| 107 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 108 | endmacro() |
| 109 | |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 110 | # Configure environment for the compiler setup run by cmake at the first |
| 111 | # `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is |
| 112 | # done, all further compiler setup is done via tfm_toolchain_reload_compiler() |
| 113 | tfm_toolchain_reload_compiler() |
| 114 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 115 | # Behaviour for handling scatter files is so wildly divergent between compilers |
| 116 | # that this macro is required. |
| 117 | macro(target_add_scatter_file target) |
| 118 | target_link_options(${target} |
| 119 | PRIVATE |
| 120 | --config $<TARGET_OBJECTS:${target}_scatter> |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 121 | ) |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 122 | |
| 123 | add_library(${target}_scatter OBJECT) |
| 124 | foreach(scatter_file ${ARGN}) |
| 125 | target_sources(${target}_scatter |
| 126 | PRIVATE |
| 127 | ${scatter_file} |
| 128 | ) |
| 129 | # Cmake cannot use generator expressions in the |
| 130 | # set_source_file_properties command, so instead we just parse the regex |
| 131 | # for the filename and set the property on all files, regardless of if |
| 132 | # the generator expression would evaluate to true or not. |
TTornblom | af19ae9 | 2020-09-29 13:26:29 +0200 | [diff] [blame] | 133 | string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 134 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 135 | PROPERTIES |
| 136 | LANGUAGE C |
| 137 | ) |
| 138 | endforeach() |
| 139 | |
Anton Komlev | 1a10355 | 2022-02-11 15:40:26 +0000 | [diff] [blame] | 140 | add_dependencies(${target} |
| 141 | ${target}_scatter |
| 142 | ) |
| 143 | |
| 144 | set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>) |
| 145 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 146 | target_link_libraries(${target}_scatter |
| 147 | platform_region_defs |
| 148 | psa_interface |
Xinyu Zhang | eeb19ac | 2023-06-19 18:09:20 +0800 | [diff] [blame] | 149 | tfm_config |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 150 | ) |
| 151 | |
| 152 | target_compile_options(${target}_scatter |
| 153 | PRIVATE |
| 154 | --preprocess=sn $<TARGET_OBJECTS:${target}_scatter> |
| 155 | ) |
| 156 | endmacro() |
| 157 | |
| 158 | macro(add_convert_to_bin_target target) |
| 159 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 160 | |
| 161 | add_custom_target(${target}_bin |
| 162 | SOURCES ${bin_dir}/${target}.bin |
| 163 | ) |
| 164 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 165 | DEPENDS ${target} |
| 166 | COMMAND ielftool |
| 167 | --silent |
| 168 | --bin $<TARGET_FILE:${target}> |
| 169 | ${bin_dir}/${target}.bin |
| 170 | ) |
| 171 | |
| 172 | add_custom_target(${target}_elf |
| 173 | SOURCES ${bin_dir}/${target}.elf |
| 174 | ) |
| 175 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 176 | DEPENDS ${target} |
| 177 | COMMAND ielftool |
| 178 | --silent |
| 179 | $<TARGET_FILE:${target}> |
| 180 | ${bin_dir}/${target}.elf |
| 181 | ) |
| 182 | |
| 183 | add_custom_target(${target}_hex |
| 184 | SOURCES ${bin_dir}/${target}.hex |
| 185 | ) |
| 186 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 187 | DEPENDS ${target} |
| 188 | COMMAND ielftool |
| 189 | --silent |
| 190 | --ihex $<TARGET_FILE:${target}> |
| 191 | ${bin_dir}/${target}.hex |
| 192 | ) |
| 193 | |
| 194 | add_custom_target(${target}_binaries |
| 195 | ALL |
| 196 | DEPENDS ${target}_bin |
| 197 | DEPENDS ${target}_elf |
| 198 | DEPENDS ${target}_hex |
| 199 | ) |
| 200 | endmacro() |
Tamas Ban | f8b0b2d | 2020-10-26 13:03:13 +0000 | [diff] [blame] | 201 | |
| 202 | macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE) |
| 203 | message(FATAL_ERROR "Code sharing support is not implemented by IAR.") |
| 204 | endmacro() |
| 205 | |
| 206 | macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST) |
| 207 | message(FATAL_ERROR "Code sharing support is not implemented by IAR.") |
Sebastian Bøe | b73f176 | 2021-10-28 14:23:13 +0200 | [diff] [blame] | 208 | endmacro() |