David Hu | 2bbcb96 | 2023-11-01 14:29:53 +0800 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Dávid Házi | cc45a7b | 2024-04-15 22:30:28 +0200 | [diff] [blame^] | 2 | # Copyright (c) 2023-2024, Arm Limited. All rights reserved. |
David Hu | 2bbcb96 | 2023-11-01 14:29:53 +0800 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | cmake_minimum_required(VERSION 3.22) |
| 9 | cmake_policy(SET CMP0115 NEW) |
| 10 | |
| 11 | SET(CMAKE_SYSTEM_NAME Generic) |
| 12 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 13 | |
| 14 | if(CROSS_COMPILE) |
| 15 | set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 16 | set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 17 | else() |
| 18 | set(CMAKE_C_COMPILER_TARGET arm-arm-none-eabi) |
| 19 | set(CMAKE_ASM_COMPILER_TARGET arm-arm-none-eabi) |
| 20 | endif() |
| 21 | |
| 22 | set(CMAKE_C_COMPILER iccarm) |
| 23 | set(CMAKE_CXX_COMPILER iccarm) |
| 24 | set(CMAKE_ASM_COMPILER iasmarm) |
| 25 | |
| 26 | set(CMAKE_C_FLAGS_DEBUG "-r -On") |
| 27 | |
| 28 | # This variable name is a bit of a misnomer. The file it is set to is included |
| 29 | # at a particular step in the compiler initialisation. It is used here to |
| 30 | # configure the extensions for object files. Despite the name, it also works |
| 31 | # with the Ninja generator. |
| 32 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/set_extensions.cmake) |
| 33 | |
| 34 | macro(tfm_toolchain_reset_compiler_flags) |
| 35 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
| 36 | |
| 37 | add_compile_options( |
| 38 | $<$<COMPILE_LANGUAGE:C,CXX>:-e> |
| 39 | $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full> |
| 40 | $<$<COMPILE_LANGUAGE:C,CXX>:--silent> |
| 41 | $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF> |
| 42 | $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES> |
| 43 | $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084> |
| 44 | $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros> |
David Hu | 2bbcb96 | 2023-11-01 14:29:53 +0800 | [diff] [blame] | 45 | $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r> |
| 46 | ) |
| 47 | endmacro() |
| 48 | |
| 49 | macro(tfm_toolchain_reset_linker_flags) |
| 50 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 51 | |
| 52 | add_link_options( |
| 53 | --silent |
| 54 | --semihosting |
| 55 | --redirect __write=__write_buffered |
| 56 | --diag_suppress=lp005 |
David Hu | 2bbcb96 | 2023-11-01 14:29:53 +0800 | [diff] [blame] | 57 | ) |
| 58 | endmacro() |
| 59 | |
| 60 | macro(tfm_toolchain_set_processor_arch) |
| 61 | if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus") |
| 62 | set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+) |
| 63 | else() |
| 64 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 65 | endif() |
| 66 | |
| 67 | if (DEFINED TFM_SYSTEM_DSP) |
| 68 | if(NOT TFM_SYSTEM_DSP) |
| 69 | string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp") |
| 70 | endif() |
| 71 | endif() |
| 72 | endmacro() |
| 73 | |
| 74 | macro(tfm_toolchain_reload_compiler) |
| 75 | tfm_toolchain_set_processor_arch() |
| 76 | tfm_toolchain_reset_compiler_flags() |
| 77 | tfm_toolchain_reset_linker_flags() |
| 78 | |
| 79 | unset(CMAKE_C_FLAGS_INIT) |
| 80 | unset(CMAKE_C_LINK_FLAGS) |
| 81 | unset(CMAKE_ASM_FLAGS_INIT) |
| 82 | unset(CMAKE_ASM_LINK_FLAGS) |
| 83 | |
| 84 | set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 85 | set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 86 | set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 87 | set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 88 | |
| 89 | set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) |
| 90 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
| 91 | |
| 92 | # Can't use the highest optimization with IAR on v8.1m arch because of the |
| 93 | # compilation bug in mbedcrypto |
| 94 | if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND |
| 95 | (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND |
| 96 | ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR |
| 97 | (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND |
| 98 | (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))) |
| 99 | message(FATAL_ERROR "Only debug build available for M55 and M85" |
| 100 | " cores with IAR version between 9.20 and 9.32.1") |
| 101 | endif() |
| 102 | |
Dávid Házi | cc45a7b | 2024-04-15 22:30:28 +0200 | [diff] [blame^] | 103 | if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard") |
| 104 | if (CONFIG_TFM_ENABLE_FP) |
| 105 | set(COMPILER_CP_C_FLAG "--fpu=${CONFIG_TFM_FP_ARCH_ASM}") |
| 106 | set(COMPILER_CP_ASM_FLAG "--fpu=${CONFIG_TFM_FP_ARCH_ASM}") |
| 107 | # armasm and armlink have the same option "--fpu" and are both used to |
| 108 | # specify the target FPU architecture. So the supported FPU architecture |
| 109 | # names can be shared by armasm and armlink. |
| 110 | set(LINKER_CP_OPTION "--fpu=${CONFIG_TFM_FP_ARCH_ASM}") |
| 111 | endif() |
| 112 | else() |
| 113 | set(COMPILER_CP_C_FLAG "--fpu=none") |
| 114 | set(COMPILER_CP_ASM_FLAG "--fpu=none") |
| 115 | set(LINKER_CP_OPTION "--fpu=none") |
| 116 | endif() |
| 117 | |
| 118 | string(APPEND CMAKE_C_FLAGS " " ${COMPILER_CP_C_FLAG}) |
| 119 | string(APPEND CMAKE_ASM_FLAGS " " ${COMPILER_CP_ASM_FLAG}) |
| 120 | string(APPEND CMAKE_C_LINK_FLAGS " " ${LINKER_CP_OPTION}) |
| 121 | string(APPEND CMAKE_ASM_LINK_FLAGS " " ${LINKER_CP_OPTION}) |
| 122 | |
David Hu | 2bbcb96 | 2023-11-01 14:29:53 +0800 | [diff] [blame] | 123 | endmacro() |
| 124 | |
| 125 | # Configure environment for the compiler setup run by cmake at the first |
| 126 | # `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is |
| 127 | # done, all further compiler setup is done via tfm_toolchain_reload_compiler() |
| 128 | tfm_toolchain_reload_compiler() |
| 129 | |
| 130 | # Specify the scatter file used to link `target`. |
| 131 | # Behaviour for handling scatter files is so wildly divergent between compilers |
| 132 | # that this macro is required. |
| 133 | # |
| 134 | # Vendor platform can set a scatter file as property INTERFACE_LINK_DEPENDS of platform_ns. |
| 135 | # `target` can fetch the scatter file from platform_ns. |
| 136 | # |
| 137 | # Alternatively, NS build can call target_add_scatter_file() with the install directory of |
| 138 | # scatter files. |
| 139 | # target_add_scatter_file(target, install_dir) |
| 140 | # |
| 141 | # target_add_scatter_file() fetch a scatter file from the install directory. |
| 142 | macro(target_add_scatter_file target) |
| 143 | # Try if scatter_file is passed from platform_ns |
| 144 | get_target_property(scatter_file |
| 145 | platform_ns |
| 146 | INTERFACE_LINK_DEPENDS |
| 147 | ) |
| 148 | |
| 149 | # If scatter_file is not passed from platform_ns |
| 150 | # Try if any scatter file is exported in install directory |
| 151 | # The intall directory is passed as an optinal argument |
| 152 | if(${scatter_file} STREQUAL "scatter_file-NOTFOUND") |
| 153 | set(install_dir ${ARGN}) |
| 154 | list(LENGTH install_dir nr_install_dir) |
| 155 | |
| 156 | # If nr_install_dir == 1, search for .icf file under install dir |
| 157 | if(${nr_install_dir} EQUAL 1) |
| 158 | file(GLOB scatter_file "${install_dir}/*.icf") |
| 159 | endif() |
| 160 | endif() |
| 161 | |
| 162 | if(NOT EXISTS ${scatter_file}) |
| 163 | message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}") |
| 164 | endif() |
| 165 | |
| 166 | add_library(${target}_scatter OBJECT) |
| 167 | target_sources(${target}_scatter |
| 168 | PRIVATE |
| 169 | ${scatter_file} |
| 170 | ) |
| 171 | # Cmake cannot use generator expressions in the |
| 172 | # set_source_file_properties command, so instead we just parse the regex |
| 173 | # for the filename and set the property on all files, regardless of if |
| 174 | # the generator expression would evaluate to true or not. |
| 175 | string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") |
| 176 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 177 | PROPERTIES |
| 178 | LANGUAGE C |
| 179 | ) |
| 180 | |
| 181 | target_link_options(${target} |
| 182 | PRIVATE |
| 183 | --config $<TARGET_OBJECTS:${target}_scatter> |
| 184 | ) |
| 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 | PRIVATE |
| 194 | platform_region_defs |
| 195 | ) |
| 196 | |
| 197 | target_compile_options(${target}_scatter |
| 198 | PRIVATE |
| 199 | --preprocess=sn $<TARGET_OBJECTS:${target}_scatter> |
| 200 | ) |
| 201 | endmacro() |
| 202 | |
| 203 | macro(add_convert_to_bin_target target) |
| 204 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 205 | |
| 206 | add_custom_target(${target}_bin |
| 207 | SOURCES ${bin_dir}/${target}.bin |
| 208 | ) |
| 209 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 210 | DEPENDS ${target} |
| 211 | COMMAND ielftool |
| 212 | --silent |
| 213 | --bin $<TARGET_FILE:${target}> |
| 214 | ${bin_dir}/${target}.bin |
| 215 | ) |
| 216 | |
| 217 | add_custom_target(${target}_elf |
| 218 | SOURCES ${bin_dir}/${target}.elf |
| 219 | ) |
| 220 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 221 | DEPENDS ${target} |
| 222 | COMMAND ielftool |
| 223 | --silent |
| 224 | $<TARGET_FILE:${target}> |
| 225 | ${bin_dir}/${target}.elf |
| 226 | ) |
| 227 | |
| 228 | add_custom_target(${target}_hex |
| 229 | SOURCES ${bin_dir}/${target}.hex |
| 230 | ) |
| 231 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 232 | DEPENDS ${target} |
| 233 | COMMAND ielftool |
| 234 | --silent |
| 235 | --ihex $<TARGET_FILE:${target}> |
| 236 | ${bin_dir}/${target}.hex |
| 237 | ) |
| 238 | |
| 239 | add_custom_target(${target}_binaries |
| 240 | ALL |
| 241 | DEPENDS ${target}_bin |
| 242 | DEPENDS ${target}_elf |
| 243 | DEPENDS ${target}_hex |
| 244 | ) |
| 245 | endmacro() |