blob: e6a6a95471c6edf57d68a392e12822e3d5b27561 [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
chesun01ea11c822022-12-15 15:53:05 +08002# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
Chris Brand4b381f82022-12-01 16:30:23 -08003# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
4# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
Raef Coles9ec67e62020-07-10 09:40:35 +01005#
6# SPDX-License-Identifier: BSD-3-Clause
7#
8#-------------------------------------------------------------------------------
Raef Coles9ec67e62020-07-10 09:40:35 +01009cmake_minimum_required(VERSION 3.15)
10
Raef Coles9ec67e62020-07-10 09:40:35 +010011SET(CMAKE_SYSTEM_NAME Generic)
Raef Coles9ec67e62020-07-10 09:40:35 +010012
13set(CMAKE_C_COMPILER armclang)
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +020014set(CMAKE_CXX_COMPILER armclang)
Raef Coles69817322020-10-19 14:14:14 +010015set(CMAKE_ASM_COMPILER armasm)
Raef Coles9ec67e62020-07-10 09:40:35 +010016
17set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out=)
Raef Coles69817322020-10-19 14:14:14 +010018set(COMPILER_CMSE_FLAG $<$<COMPILE_LANGUAGE:C>:-mcmse>)
Raef Coles9ec67e62020-07-10 09:40:35 +010019
20# This variable name is a bit of a misnomer. The file it is set to is included
21# at a particular step in the compiler initialisation. It is used here to
22# configure the extensions for object files. Despite the name, it also works
23# with the Ninja generator.
24set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
25
Raef Coles6d987ba2023-07-24 12:06:15 +010026if(NOT DEFINED CMAKE_OBJCOPY)
27 set(CMAKE_OBJCOPY ${CROSS_COMPILE}-objcopy CACHE FILEPATH "Path to objcopy")
28endif()
29
Raef Coles69817322020-10-19 14:14:14 +010030macro(tfm_toolchain_reset_compiler_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010031 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010032
33 add_compile_options(
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +020034 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-ignored-optimization-argument>
35 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unused-command-line-argument>
36 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wall>
Raef Coles69817322020-10-19 14:14:14 +010037 # Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +020038 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-error=cpp>
39 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-c>
40 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fdata-sections>
41 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-ffunction-sections>
42 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fno-builtin>
43 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-enums>
44 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-wchar>
45 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-funsigned-char>
46 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-masm=auto>
47 $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-nostdlib>
Raef Coles69817322020-10-19 14:14:14 +010048 $<$<COMPILE_LANGUAGE:C>:-std=c99>
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +020049 $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
Raef Coles69817322020-10-19 14:14:14 +010050 $<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}>
Sebastian Bøeb73f1762021-10-28 14:23:13 +020051 $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +020052 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
Raef Coles69817322020-10-19 14:14:14 +010053 )
54endmacro()
55
Kevin Pengc32279d2022-02-10 11:11:55 +080056if(CONFIG_TFM_MEMORY_USAGE_QUIET)
Jimmy Brisson1f9b7c82021-12-14 10:53:36 -060057 set(MEMORY_USAGE_FLAG "")
58else()
59 set(MEMORY_USAGE_FLAG --info=summarysizes,sizes,totals,unused,veneers)
60endif()
61
Raef Coles69817322020-10-19 14:14:14 +010062macro(tfm_toolchain_reset_linker_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010063 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010064
Raef Coles9ec67e62020-07-10 09:40:35 +010065 add_link_options(
Jimmy Brisson1f9b7c82021-12-14 10:53:36 -060066 ${MEMORY_USAGE_FLAG}
Raef Coles9ec67e62020-07-10 09:40:35 +010067 --strict
68 --symbols
69 --xref
Shawn Shan95fb8762021-06-25 16:48:40 +080070 $<$<AND:$<VERSION_GREATER:${TFM_ISOLATION_LEVEL},1>,$<STREQUAL:"${TEST_PSA_API}","IPC">>:--no-merge>
Raef Coles9ec67e62020-07-10 09:40:35 +010071 # Suppress link warnings that are consistant (and therefore hopefully
72 # harmless)
73 # https://developer.arm.com/documentation/100074/0608/linker-errors-and-warnings/list-of-the-armlink-error-and-warning-messages
74 # Empty region description
75 --diag_suppress=6312
76 # Ns section matches pattern
77 --diag_suppress=6314
78 # Duplicate input files
79 --diag_suppress=6304
Xinyu Zhang5b095db2021-06-04 15:17:26 +080080 # Pattern only matches removed unused sections.
81 --diag_suppress=6329
Raef Coles9ec67e62020-07-10 09:40:35 +010082 )
Raef Coles69817322020-10-19 14:14:14 +010083endmacro()
Raef Coles9ec67e62020-07-10 09:40:35 +010084
Raef Coles69817322020-10-19 14:14:14 +010085macro(tfm_toolchain_set_processor_arch)
Gabor Abonyi866571c2021-10-07 13:56:19 +020086 if (DEFINED TFM_SYSTEM_PROCESSOR)
87 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
88
Gabor Toth4d414112021-11-10 17:44:50 +010089 if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
90 message(WARNING "MVE is not yet supported using ARMCLANG")
91 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
Gabor Abonyi866571c2021-10-07 13:56:19 +020092 endif()
93
Gabor Abonyi866571c2021-10-07 13:56:19 +020094 if (DEFINED TFM_SYSTEM_DSP)
95 if(NOT TFM_SYSTEM_DSP)
96 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
97 endif()
98 endif()
99
chesun01ea11c822022-12-15 15:53:05 +0800100 # ARMCLANG specifies that '+nofp' is available on following M-profile cpus:
Dávid Házi3b385332023-03-06 14:17:42 +0100101 # 'cortex-m4', 'cortex-m7', 'cortex-m33', 'cortex-m35p', 'cortex-m55' and 'cortex-m85'.
chesun01ea11c822022-12-15 15:53:05 +0800102 # Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
103 # Explicitly list those cpu to align with ARMCLANG description.
104 if (NOT CONFIG_TFM_FLOAT_ABI STREQUAL "hard" AND
105 (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4"
106 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7"
107 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33"
108 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p"
Dávid Házi3b385332023-03-06 14:17:42 +0100109 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"
110 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85"))
chesun01ea11c822022-12-15 15:53:05 +0800111 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
112 endif()
113
Gabor Abonyi866571c2021-10-07 13:56:19 +0200114 string(REGEX REPLACE "\\+nodsp" ".no_dsp" CMAKE_ASM_CPU_FLAG "${CMAKE_SYSTEM_PROCESSOR}")
Bohdan Hunko9535af62023-06-22 15:50:38 +0300115 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
chesun01ea11c822022-12-15 15:53:05 +0800116 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200117 else()
118 set(CMAKE_ASM_CPU_FLAG ${TFM_SYSTEM_ARCHITECTURE})
119
120 # Armasm uses different syntax than armclang for architecture targets
121 string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
122 string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
123
124 # Modifiers are additive instead of subtractive (.fp Vs .no_fp)
125 if (TFM_SYSTEM_DSP)
126 string(APPEND CMAKE_ASM_CPU_FLAG ".dsp")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200127 endif()
chesun01ea11c822022-12-15 15:53:05 +0800128
129 if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
130 string(APPEND CMAKE_ASM_CPU_FLAG ".fp")
131 endif()
Gabor Abonyi866571c2021-10-07 13:56:19 +0200132 endif()
133
134 # CMAKE_SYSTEM_ARCH is an ARMCLANG CMAKE internal variable, used to set
135 # compile and linker flags up until CMake 3.21 where CMP0123 was introduced:
136 # https://cmake.org/cmake/help/latest/policy/CMP0123.html
137 # This behavior is overwritten by setting CMAKE_C_FLAGS in
138 # tfm_toolchain_reload_compiler.
139 # Another use of this variable is to statisfy a requirement for ARMCLANG to
140 # set either the target CPU or the Architecture. This variable needs to be
141 # set to allow targeting architectures without a specific CPU.
142 set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
Raef Coles69817322020-10-19 14:14:14 +0100143
144 set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE})
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200145 set(CMAKE_CXX_COMPILER_TARGET arm-${CROSS_COMPILE})
Raef Coles69817322020-10-19 14:14:14 +0100146 set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE})
Raef Coles9ec67e62020-07-10 09:40:35 +0100147
Gabor Toth4d414112021-11-10 17:44:50 +0100148 # MVE is currently not supported in case of armclang
149 if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
150 string(APPEND CMAKE_SYSTEM_ARCH "+nomve")
Raef Coles8db4ddf2020-11-19 09:35:37 +0000151 endif()
152
Raef Coles9ec67e62020-07-10 09:40:35 +0100153 if (DEFINED TFM_SYSTEM_DSP)
Raef Coles69817322020-10-19 14:14:14 +0100154 if(NOT TFM_SYSTEM_DSP)
Gabor Abonyi866571c2021-10-07 13:56:19 +0200155 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
Raef Coles9ec67e62020-07-10 09:40:35 +0100156 endif()
Raef Coles8db4ddf2020-11-19 09:35:37 +0000157 endif()
Raef Coles69817322020-10-19 14:14:14 +0100158
Raef Colesd853fa52020-11-06 14:20:12 +0000159 # Cmake's ARMClang support has several issues with compiler validation. To
160 # avoid these, we set the list of supported -mcpu and -march variables to
161 # the ones we intend to use so that the validation will never fail.
Raef Coles69817322020-10-19 14:14:14 +0100162 include(Compiler/ARMClang)
Raef Colesd853fa52020-11-06 14:20:12 +0000163 set(CMAKE_C_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
Gabor Abonyi866571c2021-10-07 13:56:19 +0200164 set(CMAKE_C_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200165 set(CMAKE_CXX_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
166 set(CMAKE_CXX_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
Raef Colesd853fa52020-11-06 14:20:12 +0000167 set(CMAKE_ASM_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
Gabor Abonyi866571c2021-10-07 13:56:19 +0200168 set(CMAKE_ASM_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
Raef Coles69817322020-10-19 14:14:14 +0100169endmacro()
170
171macro(tfm_toolchain_reload_compiler)
172 tfm_toolchain_set_processor_arch()
173 tfm_toolchain_reset_compiler_flags()
174 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +0100175
176 unset(CMAKE_C_FLAGS_INIT)
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200177 unset(CMAKE_CXX_FLAGS_INIT)
Raef Coles9ec67e62020-07-10 09:40:35 +0100178 unset(CMAKE_C_LINK_FLAGS)
179 unset(CMAKE_ASM_FLAGS_INIT)
180 unset(CMAKE_ASM_LINK_FLAGS)
Raef Colesfef2e0c2020-10-22 11:42:19 +0100181 unset(__mcpu_flag_set)
182 unset(__march_flag_set)
Raef Coles9ec67e62020-07-10 09:40:35 +0100183
Raef Coles69817322020-10-19 14:14:14 +0100184 include(Compiler/ARMClang)
Raef Coles9ec67e62020-07-10 09:40:35 +0100185 __compiler_armclang(C)
David Hu7075e312021-11-22 18:13:16 +0800186 include(Compiler/ARMCC-ASM)
Raef Coles69817322020-10-19 14:14:14 +0100187 __compiler_armcc(ASM)
Raef Coles9ec67e62020-07-10 09:40:35 +0100188
Chris Brand4b381f82022-12-01 16:30:23 -0800189 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6.13)
190 message(FATAL_ERROR "Please select newer Arm compiler version starting from 6.13.")
David Hufb4a8b72022-01-13 16:46:02 +0800191 endif()
192
David Hu9dd91b52022-04-06 17:00:56 +0800193 if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.15 AND
194 CMAKE_C_COMPILER_VERSION VERSION_LESS 6.18)
David Hue5f56682022-04-06 17:17:11 +0800195 message(FATAL_ERROR "Armclang 6.15~6.17 may cause MemManage fault."
196 " This defect has been fixed since Armclang 6.18."
197 " See [SDCOMP-59788] in Armclang 6.18 release note for details."
198 " Please use other Armclang versions instead.")
David Hufb4a8b72022-01-13 16:46:02 +0800199 endif()
200
Raef Coles9ec67e62020-07-10 09:40:35 +0100201 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
202
Gabor Abonyi866571c2021-10-07 13:56:19 +0200203 if (DEFINED TFM_SYSTEM_PROCESSOR)
Dávid Házie50f0a92022-07-06 14:46:01 +0200204 set(CMAKE_C_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200205 set(CMAKE_C_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200206 set(CMAKE_CXX_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200207 set(CMAKE_ASM_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
208 # But armlink doesn't support this +dsp syntax
209 string(REGEX REPLACE "\\+nodsp" "" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200210 string(REGEX REPLACE "\\+nodsp" "" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200211 string(REGEX REPLACE "\\+nodsp" "" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
212 # And uses different syntax for +nofp
213 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200214 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200215 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
Raef Coles69817322020-10-19 14:14:14 +0100216
Gabor Abonyi866571c2021-10-07 13:56:19 +0200217 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200218 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200219 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
Dávid Házie50f0a92022-07-06 14:46:01 +0200220 else()
221 set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200222 set(CMAKE_CXX_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200223 endif()
Raef Coles8db4ddf2020-11-19 09:35:37 +0000224
chesun01a94a9c72023-02-14 16:19:51 +0800225 set(BL2_COMPILER_CP_FLAG
226 $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
227 $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
228 )
chesun01ea11c822022-12-15 15:53:05 +0800229 # As BL2 does not use hardware FPU, specify '--fpu=SoftVFP' explicitly to use software
230 # library functions for BL2 to override any implicit FPU option, such as '--cpu' option.
231 # Because the implicit hardware FPU option enforces BL2 to initialize FPU but hardware FPU
232 # is not actually enabled in BL2, it will cause BL2 runtime fault.
233 set(BL2_LINKER_CP_OPTION --fpu=SoftVFP)
234
235 if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
chesun01a94a9c72023-02-14 16:19:51 +0800236 set(COMPILER_CP_FLAG
237 $<$<COMPILE_LANGUAGE:C>:-mfloat-abi=hard>
238 )
chesun01ea11c822022-12-15 15:53:05 +0800239 if (CONFIG_TFM_ENABLE_FP)
chesun01a94a9c72023-02-14 16:19:51 +0800240 set(COMPILER_CP_FLAG
241 $<$<COMPILE_LANGUAGE:C>:-mfpu=${CONFIG_TFM_FP_ARCH};-mfloat-abi=hard>
242 $<$<COMPILE_LANGUAGE:ASM>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}>
243 )
244 # armasm and armlink have the same option "--fpu" and are both used to
245 # specify the target FPU architecture. So the supported FPU architecture
246 # names can be shared by armasm and armlink.
247 set(LINKER_CP_OPTION --fpu=${CONFIG_TFM_FP_ARCH_ASM})
chesun01ea11c822022-12-15 15:53:05 +0800248 endif()
249 else()
chesun01a94a9c72023-02-14 16:19:51 +0800250 set(COMPILER_CP_FLAG
251 $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
252 $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
253 )
chesun01ea11c822022-12-15 15:53:05 +0800254 set(LINKER_CP_OPTION --fpu=SoftVFP)
255 endif()
256
Raef Coles69817322020-10-19 14:14:14 +0100257 # Workaround for issues with --depend-single-line with armasm and Ninja
258 if (CMAKE_GENERATOR STREQUAL "Ninja")
259 set( CMAKE_DEPFILE_FLAGS_ASM "--depend=<OBJECT>.d")
260 endif()
Raef Colesd29c7f72020-11-05 14:12:06 +0000261
262 set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG")
Raef Coles9ec67e62020-07-10 09:40:35 +0100263endmacro()
264
Raef Coles69817322020-10-19 14:14:14 +0100265# Configure environment for the compiler setup run by cmake at the first
266# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
267# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
268tfm_toolchain_set_processor_arch()
269tfm_toolchain_reset_compiler_flags()
270tfm_toolchain_reset_linker_flags()
271
Raef Coles9ec67e62020-07-10 09:40:35 +0100272# Behaviour for handling scatter files is so wildly divergent between compilers
273# that this macro is required.
274macro(target_add_scatter_file target)
275 target_link_options(${target}
276 PRIVATE
277 --scatter=$<TARGET_OBJECTS:${target}_scatter>
278 )
279
Raef Coles9ec67e62020-07-10 09:40:35 +0100280 add_library(${target}_scatter OBJECT)
281 foreach(scatter_file ${ARGN})
282 target_sources(${target}_scatter
283 PRIVATE
284 ${scatter_file}
285 )
286 # Cmake cannot use generator expressions in the
287 # set_source_file_properties command, so instead we just parse the regex
288 # for the filename and set the property on all files, regardless of if
289 # the generator expression would evaluate to true or not.
290 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
291 set_source_files_properties(${SCATTER_FILE_PATH}
292 PROPERTIES
293 LANGUAGE C
294 )
295 endforeach()
296
Anton Komlev1a103552022-02-11 15:40:26 +0000297 add_dependencies(${target}
298 ${target}_scatter
299 )
300
301 set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
302
Raef Coles9ec67e62020-07-10 09:40:35 +0100303 target_link_libraries(${target}_scatter
304 platform_region_defs
305 psa_interface
Xinyu Zhangeeb19ac2023-06-19 18:09:20 +0800306 tfm_config
Raef Coles9ec67e62020-07-10 09:40:35 +0100307 )
308
309 target_compile_options(${target}_scatter
310 PRIVATE
311 -E
312 -xc
313 )
314endmacro()
315
316macro(add_convert_to_bin_target target)
317 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
318
319 add_custom_target(${target}_bin
320 SOURCES ${bin_dir}/${target}.bin
321 )
322 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
323 DEPENDS ${target}
324 COMMAND fromelf
325 --bincombined $<TARGET_FILE:${target}>
326 --output=${bin_dir}/${target}.bin
327 )
328
329 add_custom_target(${target}_elf
330 SOURCES ${bin_dir}/${target}.elf
331 )
332 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
333 DEPENDS ${target}
334 COMMAND fromelf
335 --elf $<TARGET_FILE:${target}>
336 --output=${bin_dir}/${target}.elf
337 )
338
339 add_custom_target(${target}_hex
340 SOURCES ${bin_dir}/${target}.hex
341 )
342 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
343 DEPENDS ${target}
344 COMMAND fromelf
345 --i32combined $<TARGET_FILE:${target}>
346 --output=${bin_dir}/${target}.hex
347 )
348
349 add_custom_target(${target}_binaries
350 ALL
351 DEPENDS ${target}_bin
352 DEPENDS ${target}_elf
353 DEPENDS ${target}_hex
354 )
355endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000356
Raef Coles4351ec22021-04-26 09:20:50 +0100357macro(target_share_symbols target symbol_name_file)
358 get_target_property(TARGET_TYPE ${target} TYPE)
359 if (NOT TARGET_TYPE STREQUAL "EXECUTABLE")
360 message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.")
361 endif()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000362
Raef Coles4351ec22021-04-26 09:20:50 +0100363 FILE(STRINGS ${symbol_name_file} KEEP_SYMBOL_LIST
364 LENGTH_MINIMUM 1
365 )
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000366
Raef Coles4351ec22021-04-26 09:20:50 +0100367 # strip all the symbols except those proveded as arguments. Long inline
368 # python scripts aren't ideal, but this is both portable and possibly easier
369 # to maintain than trying to filter files at build time in cmake.
370 add_custom_command(TARGET ${target}
371 POST_BUILD
372 VERBATIM
373 COMMAND python3 -c "from sys import argv; import re; f = open(argv[1], 'rt'); p = [x.replace('*', '.*') for x in argv[2:]]; l = [x for x in f.readlines() if re.search(r'(?=('+'$|'.join(p + ['SYMDEFS']) + r'))', x)]; f.close(); f = open(argv[1], 'wt'); f.writelines(l); f.close();" $<TARGET_FILE_DIR:${target}>/${target}_shared_symbols.txt ${KEEP_SYMBOL_LIST})
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000374
Raef Coles08b0c5c2023-05-03 11:56:28 +0100375 # Force the target to not remove the symbols if they're unused.
376 list(TRANSFORM KEEP_SYMBOL_LIST PREPEND --undefined=)
Raef Coles4351ec22021-04-26 09:20:50 +0100377 target_link_options(${target}
378 PRIVATE
379 ${KEEP_SYMBOL_LIST}
380 )
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000381
Raef Coles4351ec22021-04-26 09:20:50 +0100382 # Ask armclang to produce a symdefs file that will
383 target_link_options(${target}
384 PRIVATE
385 --symdefs=$<TARGET_FILE_DIR:${target}>/${target}_shared_symbols.txt
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000386 )
387endmacro()
388
Raef Coles4351ec22021-04-26 09:20:50 +0100389macro(target_link_shared_code target)
390 get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR)
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000391
Raef Coles4351ec22021-04-26 09:20:50 +0100392 foreach(symbol_provider ${ARGN})
393 if (TARGET ${symbol_provider})
394 get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE)
395 if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE")
396 message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.")
397 endif()
398 endif()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000399
Raef Coles4351ec22021-04-26 09:20:50 +0100400 add_dependencies(${target} ${symbol_provider})
401 # Some cmake functions don't allow generator expressions, so get the
402 # property as a backup. If the symbol provider hasn't been created yet,
403 # then use the output dir of the target.
404 if (TARGET ${symbol_provider})
405 get_target_property(SYMBOL_PROVIDER_OUTPUT_DIR ${symbol_provider} RUNTIME_OUTPUT_DIRECTORY)
406 else()
407 get_target_property(SYMBOL_PROVIDER_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY)
408 endif()
409 # Set these properties so that cmake will allow us to use a source file
410 # that doesn't exist at cmake-time, but we guarantee will exist at
411 # compile-time.
412 set_source_files_properties(${SYMBOL_PROVIDER_OUTPUT_DIR}/${symbol_provider}_shared_symbols.txt
413 DIRECTORY ${TARGET_SOURCE_DIR}
414 PROPERTIES
415 EXTERNAL_OBJECT true
416 GENERATED true
417 )
418 target_sources(${target}
419 PRIVATE
420 $<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}_shared_symbols.txt
421 )
422 endforeach()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000423endmacro()
424
Raef Coles4351ec22021-04-26 09:20:50 +0100425macro(target_strip_symbols target)
426 set(SYMBOL_LIST "${ARGN}")
427 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000428
Raef Coles4351ec22021-04-26 09:20:50 +0100429 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
430 # armclang this isn't necessarily true.
431 add_custom_command(
432 TARGET ${target}
433 POST_BUILD
Raef Coles6d987ba2023-07-24 12:06:15 +0100434 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100435 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
436 )
437endmacro()
438
439macro(target_strip_symbols_from_dependency target dependency)
440 set(SYMBOL_LIST "${ARGN}")
441 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
442
443 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
444 # armclang this isn't necessarily true.
445 add_custom_command(
446 TARGET ${target}
447 PRE_LINK
Raef Coles6d987ba2023-07-24 12:06:15 +0100448 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100449 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
450 )
451endmacro()
452
453macro(target_weaken_symbols target)
454 set(SYMBOL_LIST "${ARGN}")
455 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
456
457 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
458 # armclang this isn't necessarily true.
459 add_custom_command(
460 TARGET ${target}
461 POST_BUILD
Raef Coles6d987ba2023-07-24 12:06:15 +0100462 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100463 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
464 )
465endmacro()
466
467macro(target_weaken_symbols_from_dependency target dependency)
468 set(SYMBOL_LIST "${ARGN}")
469 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
470
471 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
472 # armclang this isn't necessarily true.
473 add_custom_command(
474 TARGET ${target}
475 PRE_LINK
Raef Coles6d987ba2023-07-24 12:06:15 +0100476 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100477 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000478 )
479endmacro()