blob: 99c3ccfa910b2fef63b3d586f37c49423afd0a08 [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
Raef Coles88ff7992024-01-11 10:27:05 +00002# Copyright (c) 2020-2024, 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#-------------------------------------------------------------------------------
David Hu1249f0d2023-12-04 22:57:56 +08009cmake_minimum_required(VERSION 3.21)
Raef Coles9ec67e62020-07-10 09:40:35 +010010
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}")
Kevin Peng8a355d82023-11-15 16:04:18 +0800205 set(CMAKE_CXX_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200206 set(CMAKE_C_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200207 set(CMAKE_CXX_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200208 set(CMAKE_ASM_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
209 # But armlink doesn't support this +dsp syntax
210 string(REGEX REPLACE "\\+nodsp" "" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200211 string(REGEX REPLACE "\\+nodsp" "" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200212 string(REGEX REPLACE "\\+nodsp" "" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
213 # And uses different syntax for +nofp
214 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200215 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200216 string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
Raef Coles69817322020-10-19 14:14:14 +0100217
Gabor Abonyi866571c2021-10-07 13:56:19 +0200218 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200219 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200220 string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
Dávid Házie50f0a92022-07-06 14:46:01 +0200221 else()
222 set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
Rajkumar Kanagarajcd831592022-05-04 11:44:03 +0200223 set(CMAKE_CXX_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
Gabor Abonyi866571c2021-10-07 13:56:19 +0200224 endif()
Raef Coles8db4ddf2020-11-19 09:35:37 +0000225
chesun01a94a9c72023-02-14 16:19:51 +0800226 set(BL2_COMPILER_CP_FLAG
227 $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
228 $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
229 )
chesun01ea11c822022-12-15 15:53:05 +0800230 # As BL2 does not use hardware FPU, specify '--fpu=SoftVFP' explicitly to use software
231 # library functions for BL2 to override any implicit FPU option, such as '--cpu' option.
232 # Because the implicit hardware FPU option enforces BL2 to initialize FPU but hardware FPU
233 # is not actually enabled in BL2, it will cause BL2 runtime fault.
234 set(BL2_LINKER_CP_OPTION --fpu=SoftVFP)
235
Dávid Házi5575f922024-04-15 21:14:11 +0200236 set(BL1_COMPILER_CP_FLAG
237 $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
238 $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
239 )
240 set(BL1_LINKER_CP_OPTION --fpu=SoftVFP)
241
chesun01ea11c822022-12-15 15:53:05 +0800242 if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
chesun01a94a9c72023-02-14 16:19:51 +0800243 set(COMPILER_CP_FLAG
244 $<$<COMPILE_LANGUAGE:C>:-mfloat-abi=hard>
245 )
chesun01ea11c822022-12-15 15:53:05 +0800246 if (CONFIG_TFM_ENABLE_FP)
chesun01a94a9c72023-02-14 16:19:51 +0800247 set(COMPILER_CP_FLAG
248 $<$<COMPILE_LANGUAGE:C>:-mfpu=${CONFIG_TFM_FP_ARCH};-mfloat-abi=hard>
249 $<$<COMPILE_LANGUAGE:ASM>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}>
250 )
251 # armasm and armlink have the same option "--fpu" and are both used to
252 # specify the target FPU architecture. So the supported FPU architecture
253 # names can be shared by armasm and armlink.
254 set(LINKER_CP_OPTION --fpu=${CONFIG_TFM_FP_ARCH_ASM})
chesun01ea11c822022-12-15 15:53:05 +0800255 endif()
256 else()
chesun01a94a9c72023-02-14 16:19:51 +0800257 set(COMPILER_CP_FLAG
258 $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
259 $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
260 )
chesun01ea11c822022-12-15 15:53:05 +0800261 set(LINKER_CP_OPTION --fpu=SoftVFP)
262 endif()
263
Raef Coles69817322020-10-19 14:14:14 +0100264 # Workaround for issues with --depend-single-line with armasm and Ninja
265 if (CMAKE_GENERATOR STREQUAL "Ninja")
266 set( CMAKE_DEPFILE_FLAGS_ASM "--depend=<OBJECT>.d")
267 endif()
Raef Colesd29c7f72020-11-05 14:12:06 +0000268
269 set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG")
Raef Coles9ec67e62020-07-10 09:40:35 +0100270endmacro()
271
Raef Coles69817322020-10-19 14:14:14 +0100272# Configure environment for the compiler setup run by cmake at the first
273# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
274# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
275tfm_toolchain_set_processor_arch()
276tfm_toolchain_reset_compiler_flags()
277tfm_toolchain_reset_linker_flags()
278
Raef Coles9ec67e62020-07-10 09:40:35 +0100279# Behaviour for handling scatter files is so wildly divergent between compilers
280# that this macro is required.
281macro(target_add_scatter_file target)
282 target_link_options(${target}
283 PRIVATE
284 --scatter=$<TARGET_OBJECTS:${target}_scatter>
285 )
286
Raef Coles9ec67e62020-07-10 09:40:35 +0100287 add_library(${target}_scatter OBJECT)
288 foreach(scatter_file ${ARGN})
289 target_sources(${target}_scatter
290 PRIVATE
291 ${scatter_file}
292 )
293 # Cmake cannot use generator expressions in the
294 # set_source_file_properties command, so instead we just parse the regex
295 # for the filename and set the property on all files, regardless of if
296 # the generator expression would evaluate to true or not.
297 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
298 set_source_files_properties(${SCATTER_FILE_PATH}
299 PROPERTIES
300 LANGUAGE C
301 )
302 endforeach()
303
Anton Komlev1a103552022-02-11 15:40:26 +0000304 add_dependencies(${target}
305 ${target}_scatter
306 )
307
308 set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
309
Raef Coles9ec67e62020-07-10 09:40:35 +0100310 target_link_libraries(${target}_scatter
311 platform_region_defs
312 psa_interface
Xinyu Zhangeeb19ac2023-06-19 18:09:20 +0800313 tfm_config
Raef Coles9ec67e62020-07-10 09:40:35 +0100314 )
315
316 target_compile_options(${target}_scatter
317 PRIVATE
318 -E
319 -xc
320 )
321endmacro()
322
323macro(add_convert_to_bin_target target)
324 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
325
326 add_custom_target(${target}_bin
327 SOURCES ${bin_dir}/${target}.bin
328 )
329 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
330 DEPENDS ${target}
331 COMMAND fromelf
332 --bincombined $<TARGET_FILE:${target}>
333 --output=${bin_dir}/${target}.bin
334 )
335
336 add_custom_target(${target}_elf
337 SOURCES ${bin_dir}/${target}.elf
338 )
339 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
340 DEPENDS ${target}
341 COMMAND fromelf
342 --elf $<TARGET_FILE:${target}>
343 --output=${bin_dir}/${target}.elf
344 )
345
346 add_custom_target(${target}_hex
347 SOURCES ${bin_dir}/${target}.hex
348 )
349 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
350 DEPENDS ${target}
351 COMMAND fromelf
352 --i32combined $<TARGET_FILE:${target}>
353 --output=${bin_dir}/${target}.hex
354 )
355
356 add_custom_target(${target}_binaries
357 ALL
358 DEPENDS ${target}_bin
359 DEPENDS ${target}_elf
360 DEPENDS ${target}_hex
361 )
362endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000363
Anubhav Raina54c359a2023-12-07 14:09:06 +0000364macro(target_share_symbols target)
Raef Coles4351ec22021-04-26 09:20:50 +0100365 get_target_property(TARGET_TYPE ${target} TYPE)
366 if (NOT TARGET_TYPE STREQUAL "EXECUTABLE")
367 message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.")
368 endif()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000369
Bence Balogh027b6ba2024-04-09 16:34:06 +0200370 foreach(symbol_file ${ARGN})
Anubhav Raina54c359a2023-12-07 14:09:06 +0000371 FILE(STRINGS ${symbol_file} SYMBOLS
372 LENGTH_MINIMUM 1
373 )
374 list(APPEND KEEP_SYMBOL_LIST ${SYMBOLS})
375 endforeach()
376
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000377
Raef Coles4351ec22021-04-26 09:20:50 +0100378 # strip all the symbols except those proveded as arguments. Long inline
379 # python scripts aren't ideal, but this is both portable and possibly easier
380 # to maintain than trying to filter files at build time in cmake.
381 add_custom_command(TARGET ${target}
382 POST_BUILD
383 VERBATIM
Raef Coles88ff7992024-01-11 10:27:05 +0000384 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}${CODE_SHARING_OUTPUT_FILE_SUFFIX} ${KEEP_SYMBOL_LIST})
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000385
Raef Coles08b0c5c2023-05-03 11:56:28 +0100386 # Force the target to not remove the symbols if they're unused.
387 list(TRANSFORM KEEP_SYMBOL_LIST PREPEND --undefined=)
Raef Coles4351ec22021-04-26 09:20:50 +0100388 target_link_options(${target}
389 PRIVATE
390 ${KEEP_SYMBOL_LIST}
391 )
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000392
Raef Coles4351ec22021-04-26 09:20:50 +0100393 # Ask armclang to produce a symdefs file that will
394 target_link_options(${target}
395 PRIVATE
Raef Coles88ff7992024-01-11 10:27:05 +0000396 --symdefs=$<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX}
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000397 )
398endmacro()
399
Raef Coles4351ec22021-04-26 09:20:50 +0100400macro(target_link_shared_code target)
401 get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR)
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000402
Raef Coles4351ec22021-04-26 09:20:50 +0100403 foreach(symbol_provider ${ARGN})
404 if (TARGET ${symbol_provider})
405 get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE)
406 if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE")
407 message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.")
408 endif()
409 endif()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000410
Raef Coles4351ec22021-04-26 09:20:50 +0100411 add_dependencies(${target} ${symbol_provider})
Dávid Házif7ef8f02024-02-20 22:04:53 +0100412 target_link_options(${target} PRIVATE LINKER:$<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}${CODE_SHARING_INPUT_FILE_SUFFIX})
Raef Coles4351ec22021-04-26 09:20:50 +0100413 endforeach()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000414endmacro()
415
Raef Coles4351ec22021-04-26 09:20:50 +0100416macro(target_strip_symbols target)
417 set(SYMBOL_LIST "${ARGN}")
418 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000419
Raef Coles4351ec22021-04-26 09:20:50 +0100420 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
421 # armclang this isn't necessarily true.
422 add_custom_command(
423 TARGET ${target}
424 POST_BUILD
Raef Coles6d987ba2023-07-24 12:06:15 +0100425 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100426 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
427 )
428endmacro()
429
430macro(target_strip_symbols_from_dependency target dependency)
431 set(SYMBOL_LIST "${ARGN}")
432 list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
433
434 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
435 # armclang this isn't necessarily true.
436 add_custom_command(
437 TARGET ${target}
438 PRE_LINK
Raef Coles6d987ba2023-07-24 12:06:15 +0100439 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100440 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
441 )
442endmacro()
443
444macro(target_weaken_symbols target)
445 set(SYMBOL_LIST "${ARGN}")
446 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
447
448 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
449 # armclang this isn't necessarily true.
450 add_custom_command(
451 TARGET ${target}
452 POST_BUILD
Raef Coles6d987ba2023-07-24 12:06:15 +0100453 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100454 ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
455 )
456endmacro()
457
458macro(target_weaken_symbols_from_dependency target dependency)
459 set(SYMBOL_LIST "${ARGN}")
460 list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
461
462 # TODO we assume that arm-none-eabi-objcopy is available - since we're using
463 # armclang this isn't necessarily true.
464 add_custom_command(
465 TARGET ${target}
466 PRE_LINK
Raef Coles6d987ba2023-07-24 12:06:15 +0100467 COMMAND ${CMAKE_OBJCOPY}
Raef Coles4351ec22021-04-26 09:20:50 +0100468 ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000469 )
470endmacro()