blob: 915410a94921053cb66db7ee0c2a4930063aea1f [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)
17
18if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND")
19 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'")
20endif()
21
22set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
Raef Coles9ec67e62020-07-10 09:40:35 +010023
Raef Coles69817322020-10-19 14:14:14 +010024set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
25set(COMPILER_CMSE_FLAG -mcmse)
Raef Coles9ec67e62020-07-10 09:40:35 +010026
27# This variable name is a bit of a misnomer. The file it is set to is included
28# at a particular step in the compiler initialisation. It is used here to
29# configure the extensions for object files. Despite the name, it also works
30# with the Ninja generator.
31set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
32
Raef Coles69817322020-10-19 14:14:14 +010033macro(tfm_toolchain_reset_compiler_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010034 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010035
Raef Coles9ec67e62020-07-10 09:40:35 +010036 add_compile_options(
37 --specs=nano.specs
38 -Wall
39 -Wno-format
40 -Wno-return-type
41 -Wno-unused-but-set-variable
42 -c
43 -fdata-sections
44 -ffunction-sections
45 -fno-builtin
46 -fshort-enums
Raef Coles9ec67e62020-07-10 09:40:35 +010047 -funsigned-char
48 -mthumb
49 -nostdlib
50 -std=c99
David Hu3ff26cf2021-11-15 18:12:59 +080051 $<$<OR:$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<BOOL:${TFM_CODE_COVERAGE}>>:-g>
Raef Coles9ec67e62020-07-10 09:40:35 +010052 )
Raef Coles69817322020-10-19 14:14:14 +010053endmacro()
54
Jimmy Brisson1f9b7c82021-12-14 10:53:36 -060055if(TFM_MEMORY_USAGE_QUIET)
56 set(MEMORY_USAGE_FLAG "")
57else()
58 set(MEMORY_USAGE_FLAG LINKER:--print-memory-usage)
59endif()
60
Raef Coles69817322020-10-19 14:14:14 +010061macro(tfm_toolchain_reset_linker_flags)
62 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
63
Raef Coles9ec67e62020-07-10 09:40:35 +010064 add_link_options(
65 --entry=Reset_Handler
66 --specs=nano.specs
67 LINKER:-check-sections
68 LINKER:-fatal-warnings
69 LINKER:--gc-sections
70 LINKER:--no-wchar-size-warning
Jimmy Brisson1f9b7c82021-12-14 10:53:36 -060071 ${MEMORY_USAGE_FLAG}
Raef Coles9ec67e62020-07-10 09:40:35 +010072 )
Raef Coles69817322020-10-19 14:14:14 +010073endmacro()
74
75macro(tfm_toolchain_set_processor_arch)
Gabor Abonyi866571c2021-10-07 13:56:19 +020076 if (DEFINED TFM_SYSTEM_PROCESSOR)
77 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
78
79 if (DEFINED TFM_SYSTEM_DSP)
80 if (NOT TFM_SYSTEM_DSP)
81 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
82 endif()
83 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +080084 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
Feder Liange851f6c2022-01-26 10:14:10 +080085 if (DEFINED CONFIG_TFM_FP)
86 if(CONFIG_TFM_FP STREQUAL "0" AND
David Hub8539092022-01-19 14:17:49 +080087 NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv6-m")
Feder Liangd4dbaa92021-09-07 15:34:46 +080088 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
89 endif()
90 endif()
91 endif()
Gabor Abonyi866571c2021-10-07 13:56:19 +020092 endif()
93
94 # CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
95 # set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
96 # The variable name is choosen to align with the ARMCLANG toolchain file.
97 set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
Raef Coles69817322020-10-19 14:14:14 +010098
99 if (DEFINED TFM_SYSTEM_DSP)
Gabor Abonyi866571c2021-10-07 13:56:19 +0200100 # +nodsp modifier is only supported from GCC version 8.
101 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
102 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
103 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
104 # armv8.1-m.main arch does not have +nodsp option
105 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
106 NOT TFM_SYSTEM_DSP)
107 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
108 endif()
Raef Coles69817322020-10-19 14:14:14 +0100109 endif()
110 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +0800111
112 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
Feder Liang55194382021-11-22 16:45:33 +0800113 if (DEFINED CONFIG_TFM_FP)
114 if(CONFIG_TFM_FP STREQUAL "hard")
Feder Liangd4dbaa92021-09-07 15:34:46 +0800115 string(APPEND CMAKE_SYSTEM_ARCH "+fp")
116 endif()
117 endif()
118 endif()
Raef Coles69817322020-10-19 14:14:14 +0100119endmacro()
120
121macro(tfm_toolchain_reload_compiler)
122 tfm_toolchain_set_processor_arch()
123 tfm_toolchain_reset_compiler_flags()
124 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +0100125
David Hufb4a8b72022-01-13 16:46:02 +0800126 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
127 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
128
129 if (GCC_VERSION VERSION_LESS 7.3.1)
130 message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
131 endif()
132
133 if (GCC_VERSION VERSION_EQUAL 10.2.1)
David Hu7f5070c2021-11-10 12:10:55 +0800134 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
135 " Select other GNU Arm compiler versions instead."
136 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
137 endif()
138
Raef Coles9ec67e62020-07-10 09:40:35 +0100139 unset(CMAKE_C_FLAGS_INIT)
140 unset(CMAKE_ASM_FLAGS_INIT)
141
Gabor Abonyi866571c2021-10-07 13:56:19 +0200142 if (DEFINED TFM_SYSTEM_PROCESSOR)
143 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
144 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
145 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
146 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
147 else()
148 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
149 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
150 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
151 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
152 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100153
154 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
155 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
Feder Liangd4dbaa92021-09-07 15:34:46 +0800156
157 set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft)
158
Feder Liang55194382021-11-22 16:45:33 +0800159 if (CONFIG_TFM_FP STREQUAL "hard")
Feder Liangd4dbaa92021-09-07 15:34:46 +0800160 set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
161 set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
Feder Liangd4dbaa92021-09-07 15:34:46 +0800162 else()
163 set(COMPILER_CP_FLAG -mfloat-abi=soft)
164 set(LINKER_CP_OPTION -mfloat-abi=soft)
165 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100166endmacro()
167
Raef Coles69817322020-10-19 14:14:14 +0100168# Configure environment for the compiler setup run by cmake at the first
169# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
170# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
171tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +0100172
173macro(target_add_scatter_file target)
174 target_link_options(${target}
175 PRIVATE
176 -T $<TARGET_OBJECTS:${target}_scatter>
177 )
178
179 add_dependencies(${target}
180 ${target}_scatter
181 )
182
183 add_library(${target}_scatter OBJECT)
184 foreach(scatter_file ${ARGN})
185 target_sources(${target}_scatter
186 PRIVATE
187 ${scatter_file}
188 )
189 # Cmake cannot use generator expressions in the
190 # set_source_file_properties command, so instead we just parse the regex
191 # for the filename and set the property on all files, regardless of if
192 # the generator expression would evaluate to true or not.
193 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
194 set_source_files_properties(${SCATTER_FILE_PATH}
195 PROPERTIES
196 LANGUAGE C
Sebastian Bøee6099c12021-10-25 15:11:26 +0200197 KEEP_EXTENSION True # Don't use .o extension for the preprocessed file
Raef Coles9ec67e62020-07-10 09:40:35 +0100198 )
199 endforeach()
200
Raef Coles9ec67e62020-07-10 09:40:35 +0100201 target_link_libraries(${target}_scatter
202 platform_region_defs
203 psa_interface
204 tfm_partition_defs
205 )
206
207 target_compile_options(${target}_scatter
208 PRIVATE
209 -E
210 -P
211 -xc
212 )
213endmacro()
214
215macro(add_convert_to_bin_target target)
216 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
217
218 add_custom_target(${target}_bin
219 SOURCES ${bin_dir}/${target}.bin
220 )
221 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
222 DEPENDS ${target}
223 COMMAND ${CMAKE_OBJCOPY}
224 -O binary $<TARGET_FILE:${target}>
225 ${bin_dir}/${target}.bin
226 )
227
228 add_custom_target(${target}_elf
229 SOURCES ${bin_dir}/${target}.elf
230 )
231 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
232 DEPENDS ${target}
233 COMMAND ${CMAKE_OBJCOPY}
234 -O elf32-littlearm $<TARGET_FILE:${target}>
235 ${bin_dir}/${target}.elf
236 )
237
238 add_custom_target(${target}_hex
239 SOURCES ${bin_dir}/${target}.hex
240 )
241 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
242 DEPENDS ${target}
243 COMMAND ${CMAKE_OBJCOPY}
244 -O ihex $<TARGET_FILE:${target}>
245 ${bin_dir}/${target}.hex
246 )
247
248 add_custom_target(${target}_binaries
249 ALL
250 DEPENDS ${target}_bin
251 DEPENDS ${target}_elf
252 DEPENDS ${target}_hex
253 )
254endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000255
256# Macro for sharing code among independent binaries. This function extracts
257# some parts of the code based on a symbol template file and creates a text
258# file, which contains the symbols with their absolute addresses, which can be
259# picked up by the linker when linking the other target.
260# INPUTS:
261# TARGET - - Target to extract the symbols/objects from
262# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
263macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
264 # Create a temporary file, which contains all extracted symbols from 'TARGET'
265 set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
266
267 # Find the CMake script doing the symbol filtering.
268 find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
269 find_file(STRIP_UNSHARED_CODE "StripUnsharedCode.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
270
271 find_program(GNUARM_NM arm-none-eabi-nm)
272 if (GNUARM_NM STREQUAL "GNUARM_NM-NOTFOUND")
273 message(FATAL_ERROR "toolchain_GNUARM.cmake: mandatory tool '${GNUARM_NM}' is missing.")
274 endif()
275
276 # Multiple steps are required:
277 # - Extract all symbols from sharing target
278 # - Filter the unwanted symbols from all_symbols.txt
279 # - Create a stripped shared_code.axf file which contains only the symbols which are meant to be shared
280 add_custom_command(TARGET ${TARGET}
281 POST_BUILD
282
283 COMMAND ${GNUARM_NM}
284 ARGS
285 ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf > ${ALL_SYMBOLS}
286 COMMENT "Dumping all symbols"
287
288 COMMAND ${CMAKE_COMMAND}
289 -DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
290 -DALL_SYMBOLS=${ALL_SYMBOLS}
291 -P ${FILTER_SYMBOLS_SCRIPT}
292 BYPRODUCTS
293 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
294 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
295 COMMENT "Filtering shared symbols"
296
297 COMMAND ${CMAKE_COMMAND}
298 -E copy ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
299 COMMENT "Copy and rename ${TARGET} to strip"
300
301 COMMAND ${CMAKE_COMMAND}
302 -DSHARED_SYMBOLS_FILE=${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
303 -DEXECUTABLE_TO_STRIP=${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
304 -P ${STRIP_UNSHARED_CODE}
305 COMMENT "Stripping unshared code from ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf"
306 )
307endmacro()
308
309macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
310 # Find the CMake scripts
311 find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
312
313 add_custom_command(TARGET ${TARGET}
314 PRE_LINK
315
316 COMMAND ${CMAKE_COMMAND}
317 -DLIB_LIST='${LIB_LIST}'
318 -DSHARED_CODE_PATH=${SHARED_CODE_PATH}
319 -P ${WEAKEN_SYMBOLS_SCRIPT}
320 COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
321
322 # If sharing target is defined by TF-M build then setup dependency
323 if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
324 add_dependencies(${TARGET} ${ORIG_TARGET})
325 endif()
326endmacro()
327
328# Macro for linking shared code to given target. Location of shared code could
329# be outside of the TF-M project. Its location can be defined with the CMake
330# command line argument "SHARED_CODE_PATH". The file containing the shared objects
331# must be named "shared_symbols_addr.txt".
332# INPUTS:
333# TARGET Target to link the shared code to
334# SHARED_CODE_PATH Shared code located in this folder
335# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
336# LIB_LIST List of libraries which are linked to top level target
337macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
338 # GNUARM requires to link a stripped version (only containing the shared symbols) of the
339 # original executable to the executable which want to rely on the shared symbols.
340 target_link_options(${TARGET} PRIVATE -Wl,-R${SHARED_CODE_PATH}/shared_code.axf)
341
342 compiler_weaken_symbols(${TARGET}
343 ${SHARED_CODE_PATH}
344 ${ORIG_TARGET}
345 "${LIB_LIST}"
346 )
Sebastian Bøee6099c12021-10-25 15:11:26 +0200347endmacro()