blob: 83eab4b2cc1391c13c4ccb4af4745635c5e30208 [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
David Hufb4a8b72022-01-13 16:46:02 +08002# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
Raef Coles9ec67e62020-07-10 09:40:35 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Raef Coles69817322020-10-19 14:14:14 +01008# Don't load this file if it is specified as a cmake toolchain file
9if(NOT TFM_TOOLCHAIN_FILE)
10 message(DEPRECATION "SETTING CMAKE_TOOLCHAIN_FILE is deprecated. It has been replaced with TFM_TOOLCHAIN_FILE.")
11 return()
12endif()
Raef Coles6e8f83d2020-09-28 10:44:06 +010013
Raef Coles9ec67e62020-07-10 09:40:35 +010014set(CMAKE_SYSTEM_NAME Generic)
Raef Coles9ec67e62020-07-10 09:40:35 +010015
Øyvind Rønningstad2f090f92021-02-23 15:59:22 +010016find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
David Brown12ab9732022-02-25 11:28:13 -070017find_program(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
Øyvind Rønningstad2f090f92021-02-23 15:59:22 +010018
19if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND")
20 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'")
21endif()
22
23set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
Raef Coles9ec67e62020-07-10 09:40:35 +010024
Raef Coles69817322020-10-19 14:14:14 +010025set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
26set(COMPILER_CMSE_FLAG -mcmse)
Raef Coles9ec67e62020-07-10 09:40:35 +010027
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.
32set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
33
Raef Coles69817322020-10-19 14:14:14 +010034macro(tfm_toolchain_reset_compiler_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010035 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010036
Raef Coles9ec67e62020-07-10 09:40:35 +010037 add_compile_options(
38 --specs=nano.specs
39 -Wall
40 -Wno-format
41 -Wno-return-type
42 -Wno-unused-but-set-variable
43 -c
44 -fdata-sections
45 -ffunction-sections
46 -fno-builtin
47 -fshort-enums
Raef Coles9ec67e62020-07-10 09:40:35 +010048 -funsigned-char
49 -mthumb
50 -nostdlib
51 -std=c99
David Hu3ff26cf2021-11-15 18:12:59 +080052 $<$<OR:$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<BOOL:${TFM_CODE_COVERAGE}>>:-g>
Raef Coles9ec67e62020-07-10 09:40:35 +010053 )
Raef Coles69817322020-10-19 14:14:14 +010054endmacro()
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 LINKER:--print-memory-usage)
60endif()
61
Raef Coles69817322020-10-19 14:14:14 +010062macro(tfm_toolchain_reset_linker_flags)
63 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
64
Raef Coles9ec67e62020-07-10 09:40:35 +010065 add_link_options(
66 --entry=Reset_Handler
67 --specs=nano.specs
68 LINKER:-check-sections
69 LINKER:-fatal-warnings
70 LINKER:--gc-sections
71 LINKER:--no-wchar-size-warning
Jimmy Brisson1f9b7c82021-12-14 10:53:36 -060072 ${MEMORY_USAGE_FLAG}
Raef Coles9ec67e62020-07-10 09:40:35 +010073 )
Raef Coles69817322020-10-19 14:14:14 +010074endmacro()
75
76macro(tfm_toolchain_set_processor_arch)
Gabor Abonyi866571c2021-10-07 13:56:19 +020077 if (DEFINED TFM_SYSTEM_PROCESSOR)
78 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
79
80 if (DEFINED TFM_SYSTEM_DSP)
81 if (NOT TFM_SYSTEM_DSP)
82 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
83 endif()
84 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +080085 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
Feder Liange851f6c2022-01-26 10:14:10 +080086 if (DEFINED CONFIG_TFM_FP)
87 if(CONFIG_TFM_FP STREQUAL "0" AND
David Hub8539092022-01-19 14:17:49 +080088 NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv6-m")
Feder Liangd4dbaa92021-09-07 15:34:46 +080089 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
90 endif()
91 endif()
92 endif()
Gabor Abonyi866571c2021-10-07 13:56:19 +020093 endif()
94
95 # CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
96 # set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
97 # The variable name is choosen to align with the ARMCLANG toolchain file.
98 set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
Raef Coles69817322020-10-19 14:14:14 +010099
100 if (DEFINED TFM_SYSTEM_DSP)
Gabor Abonyi866571c2021-10-07 13:56:19 +0200101 # +nodsp modifier is only supported from GCC version 8.
102 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
103 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
104 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
105 # armv8.1-m.main arch does not have +nodsp option
106 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
107 NOT TFM_SYSTEM_DSP)
108 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
109 endif()
Raef Coles69817322020-10-19 14:14:14 +0100110 endif()
111 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +0800112
113 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
Feder Liang55194382021-11-22 16:45:33 +0800114 if (DEFINED CONFIG_TFM_FP)
115 if(CONFIG_TFM_FP STREQUAL "hard")
Feder Liangd4dbaa92021-09-07 15:34:46 +0800116 string(APPEND CMAKE_SYSTEM_ARCH "+fp")
117 endif()
118 endif()
119 endif()
Raef Coles69817322020-10-19 14:14:14 +0100120endmacro()
121
122macro(tfm_toolchain_reload_compiler)
123 tfm_toolchain_set_processor_arch()
124 tfm_toolchain_reset_compiler_flags()
125 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +0100126
David Hufb4a8b72022-01-13 16:46:02 +0800127 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
128 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
129
130 if (GCC_VERSION VERSION_LESS 7.3.1)
131 message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
132 endif()
133
134 if (GCC_VERSION VERSION_EQUAL 10.2.1)
David Hu7f5070c2021-11-10 12:10:55 +0800135 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
136 " Select other GNU Arm compiler versions instead."
137 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
138 endif()
139
Raef Coles9ec67e62020-07-10 09:40:35 +0100140 unset(CMAKE_C_FLAGS_INIT)
141 unset(CMAKE_ASM_FLAGS_INIT)
142
Gabor Abonyi866571c2021-10-07 13:56:19 +0200143 if (DEFINED TFM_SYSTEM_PROCESSOR)
144 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
145 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
146 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
147 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
148 else()
149 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
150 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
151 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
152 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
153 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100154
155 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
156 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
Feder Liangd4dbaa92021-09-07 15:34:46 +0800157
158 set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft)
159
Feder Liang55194382021-11-22 16:45:33 +0800160 if (CONFIG_TFM_FP STREQUAL "hard")
Feder Liangd4dbaa92021-09-07 15:34:46 +0800161 set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
162 set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
Feder Liangd4dbaa92021-09-07 15:34:46 +0800163 else()
164 set(COMPILER_CP_FLAG -mfloat-abi=soft)
165 set(LINKER_CP_OPTION -mfloat-abi=soft)
166 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100167endmacro()
168
Raef Coles69817322020-10-19 14:14:14 +0100169# Configure environment for the compiler setup run by cmake at the first
170# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
171# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
172tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +0100173
174macro(target_add_scatter_file target)
175 target_link_options(${target}
176 PRIVATE
177 -T $<TARGET_OBJECTS:${target}_scatter>
178 )
179
Raef Coles9ec67e62020-07-10 09:40:35 +0100180 add_library(${target}_scatter OBJECT)
181 foreach(scatter_file ${ARGN})
182 target_sources(${target}_scatter
183 PRIVATE
184 ${scatter_file}
185 )
186 # Cmake cannot use generator expressions in the
187 # set_source_file_properties command, so instead we just parse the regex
188 # for the filename and set the property on all files, regardless of if
189 # the generator expression would evaluate to true or not.
190 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
191 set_source_files_properties(${SCATTER_FILE_PATH}
192 PROPERTIES
193 LANGUAGE C
Sebastian Bøee6099c12021-10-25 15:11:26 +0200194 KEEP_EXTENSION True # Don't use .o extension for the preprocessed file
Raef Coles9ec67e62020-07-10 09:40:35 +0100195 )
196 endforeach()
197
Anton Komlev1a103552022-02-11 15:40:26 +0000198 add_dependencies(${target}
199 ${target}_scatter
200 )
201
202 set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
203
Raef Coles9ec67e62020-07-10 09:40:35 +0100204 target_link_libraries(${target}_scatter
205 platform_region_defs
206 psa_interface
207 tfm_partition_defs
208 )
209
210 target_compile_options(${target}_scatter
211 PRIVATE
212 -E
213 -P
214 -xc
215 )
216endmacro()
217
218macro(add_convert_to_bin_target target)
219 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
220
221 add_custom_target(${target}_bin
222 SOURCES ${bin_dir}/${target}.bin
223 )
224 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
225 DEPENDS ${target}
226 COMMAND ${CMAKE_OBJCOPY}
227 -O binary $<TARGET_FILE:${target}>
228 ${bin_dir}/${target}.bin
229 )
230
231 add_custom_target(${target}_elf
232 SOURCES ${bin_dir}/${target}.elf
233 )
234 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
235 DEPENDS ${target}
236 COMMAND ${CMAKE_OBJCOPY}
237 -O elf32-littlearm $<TARGET_FILE:${target}>
238 ${bin_dir}/${target}.elf
239 )
240
241 add_custom_target(${target}_hex
242 SOURCES ${bin_dir}/${target}.hex
243 )
244 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
245 DEPENDS ${target}
246 COMMAND ${CMAKE_OBJCOPY}
247 -O ihex $<TARGET_FILE:${target}>
248 ${bin_dir}/${target}.hex
249 )
250
251 add_custom_target(${target}_binaries
252 ALL
253 DEPENDS ${target}_bin
254 DEPENDS ${target}_elf
255 DEPENDS ${target}_hex
256 )
257endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000258
259# Macro for sharing code among independent binaries. This function extracts
260# some parts of the code based on a symbol template file and creates a text
261# file, which contains the symbols with their absolute addresses, which can be
262# picked up by the linker when linking the other target.
263# INPUTS:
264# TARGET - - Target to extract the symbols/objects from
265# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
266macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
267 # Create a temporary file, which contains all extracted symbols from 'TARGET'
268 set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
269
270 # Find the CMake script doing the symbol filtering.
271 find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
272 find_file(STRIP_UNSHARED_CODE "StripUnsharedCode.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
273
274 find_program(GNUARM_NM arm-none-eabi-nm)
275 if (GNUARM_NM STREQUAL "GNUARM_NM-NOTFOUND")
276 message(FATAL_ERROR "toolchain_GNUARM.cmake: mandatory tool '${GNUARM_NM}' is missing.")
277 endif()
278
279 # Multiple steps are required:
280 # - Extract all symbols from sharing target
281 # - Filter the unwanted symbols from all_symbols.txt
282 # - Create a stripped shared_code.axf file which contains only the symbols which are meant to be shared
283 add_custom_command(TARGET ${TARGET}
284 POST_BUILD
285
286 COMMAND ${GNUARM_NM}
287 ARGS
288 ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf > ${ALL_SYMBOLS}
289 COMMENT "Dumping all symbols"
290
291 COMMAND ${CMAKE_COMMAND}
292 -DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
293 -DALL_SYMBOLS=${ALL_SYMBOLS}
294 -P ${FILTER_SYMBOLS_SCRIPT}
295 BYPRODUCTS
296 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
297 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
298 COMMENT "Filtering shared symbols"
299
300 COMMAND ${CMAKE_COMMAND}
301 -E copy ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
302 COMMENT "Copy and rename ${TARGET} to strip"
303
304 COMMAND ${CMAKE_COMMAND}
305 -DSHARED_SYMBOLS_FILE=${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
306 -DEXECUTABLE_TO_STRIP=${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
307 -P ${STRIP_UNSHARED_CODE}
308 COMMENT "Stripping unshared code from ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf"
309 )
310endmacro()
311
312macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
313 # Find the CMake scripts
314 find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
315
316 add_custom_command(TARGET ${TARGET}
317 PRE_LINK
318
319 COMMAND ${CMAKE_COMMAND}
320 -DLIB_LIST='${LIB_LIST}'
321 -DSHARED_CODE_PATH=${SHARED_CODE_PATH}
322 -P ${WEAKEN_SYMBOLS_SCRIPT}
323 COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
324
325 # If sharing target is defined by TF-M build then setup dependency
326 if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
327 add_dependencies(${TARGET} ${ORIG_TARGET})
328 endif()
329endmacro()
330
331# Macro for linking shared code to given target. Location of shared code could
332# be outside of the TF-M project. Its location can be defined with the CMake
333# command line argument "SHARED_CODE_PATH". The file containing the shared objects
334# must be named "shared_symbols_addr.txt".
335# INPUTS:
336# TARGET Target to link the shared code to
337# SHARED_CODE_PATH Shared code located in this folder
338# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
339# LIB_LIST List of libraries which are linked to top level target
340macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
341 # GNUARM requires to link a stripped version (only containing the shared symbols) of the
342 # original executable to the executable which want to rely on the shared symbols.
343 target_link_options(${TARGET} PRIVATE -Wl,-R${SHARED_CODE_PATH}/shared_code.axf)
344
345 compiler_weaken_symbols(${TARGET}
346 ${SHARED_CODE_PATH}
347 ${ORIG_TARGET}
348 "${LIB_LIST}"
349 )
Sebastian Bøee6099c12021-10-25 15:11:26 +0200350endmacro()