blob: 2ed640b0d5cdcfebcac55f93e3236d8bff292b9b [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
Feder Liangd4dbaa92021-09-07 15:34:46 +08002# Copyright (c) 2020-2021, 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
Sebastian Bøeb73f1762021-10-28 14:23:13 +020051 $<$<BOOL:${TFM_DEBUG_SYMBOLS}>:-g>
Raef Coles9ec67e62020-07-10 09:40:35 +010052 )
Raef Coles69817322020-10-19 14:14:14 +010053endmacro()
54
55macro(tfm_toolchain_reset_linker_flags)
56 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
57
Raef Coles9ec67e62020-07-10 09:40:35 +010058 add_link_options(
59 --entry=Reset_Handler
60 --specs=nano.specs
61 LINKER:-check-sections
62 LINKER:-fatal-warnings
63 LINKER:--gc-sections
64 LINKER:--no-wchar-size-warning
65 LINKER:--print-memory-usage
66 )
Raef Coles69817322020-10-19 14:14:14 +010067endmacro()
68
69macro(tfm_toolchain_set_processor_arch)
Gabor Abonyi866571c2021-10-07 13:56:19 +020070 if (DEFINED TFM_SYSTEM_PROCESSOR)
71 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
72
73 if (DEFINED TFM_SYSTEM_DSP)
74 if (NOT TFM_SYSTEM_DSP)
75 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
76 endif()
77 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +080078 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
79 if (DEFINED CONFIG_TFM_SPE_FP)
80 if(CONFIG_TFM_SPE_FP STREQUAL "0")
81 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
82 endif()
83 endif()
84 endif()
Gabor Abonyi866571c2021-10-07 13:56:19 +020085 endif()
86
87 # CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
88 # set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
89 # The variable name is choosen to align with the ARMCLANG toolchain file.
90 set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
Raef Coles69817322020-10-19 14:14:14 +010091
92 if (DEFINED TFM_SYSTEM_DSP)
Gabor Abonyi866571c2021-10-07 13:56:19 +020093 # +nodsp modifier is only supported from GCC version 8.
94 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
95 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
96 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
97 # armv8.1-m.main arch does not have +nodsp option
98 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
99 NOT TFM_SYSTEM_DSP)
100 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
101 endif()
Raef Coles69817322020-10-19 14:14:14 +0100102 endif()
103 endif()
Feder Liangd4dbaa92021-09-07 15:34:46 +0800104
105 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
106 if (DEFINED CONFIG_TFM_SPE_FP)
107 if(CONFIG_TFM_SPE_FP STRGREATER 0)
108 string(APPEND CMAKE_SYSTEM_ARCH "+fp")
109 endif()
110 endif()
111 endif()
Raef Coles69817322020-10-19 14:14:14 +0100112endmacro()
113
114macro(tfm_toolchain_reload_compiler)
115 tfm_toolchain_set_processor_arch()
116 tfm_toolchain_reset_compiler_flags()
117 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +0100118
David Hu7f5070c2021-11-10 12:10:55 +0800119 if (CMAKE_C_COMPILER_VERSION VERSION_EQUAL 10.2.1)
120 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
121 " Select other GNU Arm compiler versions instead."
122 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
123 endif()
124
Raef Coles9ec67e62020-07-10 09:40:35 +0100125 unset(CMAKE_C_FLAGS_INIT)
126 unset(CMAKE_ASM_FLAGS_INIT)
127
Gabor Abonyi866571c2021-10-07 13:56:19 +0200128 if (DEFINED TFM_SYSTEM_PROCESSOR)
129 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
130 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
131 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
132 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
133 else()
134 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
135 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
136 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
137 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
138 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100139
140 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
141 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
Feder Liangd4dbaa92021-09-07 15:34:46 +0800142
143 set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft)
144
145 if (CONFIG_TFM_SPE_FP STREQUAL "2")
146 set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
147 set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
148 elseif (CONFIG_TFM_SPE_FP STREQUAL "1")
149 set(COMPILER_CP_FLAG -mfloat-abi=softfp -mfpu=${CONFIG_TFM_FP_ARCH})
150 set(LINKER_CP_OPTION -mfloat-abi=softfp -mfpu=${CONFIG_TFM_FP_ARCH})
151 else()
152 set(COMPILER_CP_FLAG -mfloat-abi=soft)
153 set(LINKER_CP_OPTION -mfloat-abi=soft)
154 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100155endmacro()
156
Raef Coles69817322020-10-19 14:14:14 +0100157# Configure environment for the compiler setup run by cmake at the first
158# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
159# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
160tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +0100161
162macro(target_add_scatter_file target)
163 target_link_options(${target}
164 PRIVATE
165 -T $<TARGET_OBJECTS:${target}_scatter>
166 )
167
168 add_dependencies(${target}
169 ${target}_scatter
170 )
171
172 add_library(${target}_scatter OBJECT)
173 foreach(scatter_file ${ARGN})
174 target_sources(${target}_scatter
175 PRIVATE
176 ${scatter_file}
177 )
178 # Cmake cannot use generator expressions in the
179 # set_source_file_properties command, so instead we just parse the regex
180 # for the filename and set the property on all files, regardless of if
181 # the generator expression would evaluate to true or not.
182 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
183 set_source_files_properties(${SCATTER_FILE_PATH}
184 PROPERTIES
185 LANGUAGE C
Sebastian Bøee6099c12021-10-25 15:11:26 +0200186 KEEP_EXTENSION True # Don't use .o extension for the preprocessed file
Raef Coles9ec67e62020-07-10 09:40:35 +0100187 )
188 endforeach()
189
Raef Coles9ec67e62020-07-10 09:40:35 +0100190 target_link_libraries(${target}_scatter
191 platform_region_defs
192 psa_interface
193 tfm_partition_defs
194 )
195
196 target_compile_options(${target}_scatter
197 PRIVATE
198 -E
199 -P
200 -xc
201 )
202endmacro()
203
204macro(add_convert_to_bin_target target)
205 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
206
207 add_custom_target(${target}_bin
208 SOURCES ${bin_dir}/${target}.bin
209 )
210 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
211 DEPENDS ${target}
212 COMMAND ${CMAKE_OBJCOPY}
213 -O binary $<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 ${CMAKE_OBJCOPY}
223 -O elf32-littlearm $<TARGET_FILE:${target}>
224 ${bin_dir}/${target}.elf
225 )
226
227 add_custom_target(${target}_hex
228 SOURCES ${bin_dir}/${target}.hex
229 )
230 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
231 DEPENDS ${target}
232 COMMAND ${CMAKE_OBJCOPY}
233 -O ihex $<TARGET_FILE:${target}>
234 ${bin_dir}/${target}.hex
235 )
236
237 add_custom_target(${target}_binaries
238 ALL
239 DEPENDS ${target}_bin
240 DEPENDS ${target}_elf
241 DEPENDS ${target}_hex
242 )
243endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000244
245# Macro for sharing code among independent binaries. This function extracts
246# some parts of the code based on a symbol template file and creates a text
247# file, which contains the symbols with their absolute addresses, which can be
248# picked up by the linker when linking the other target.
249# INPUTS:
250# TARGET - - Target to extract the symbols/objects from
251# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
252macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
253 # Create a temporary file, which contains all extracted symbols from 'TARGET'
254 set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
255
256 # Find the CMake script doing the symbol filtering.
257 find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
258 find_file(STRIP_UNSHARED_CODE "StripUnsharedCode.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
259
260 find_program(GNUARM_NM arm-none-eabi-nm)
261 if (GNUARM_NM STREQUAL "GNUARM_NM-NOTFOUND")
262 message(FATAL_ERROR "toolchain_GNUARM.cmake: mandatory tool '${GNUARM_NM}' is missing.")
263 endif()
264
265 # Multiple steps are required:
266 # - Extract all symbols from sharing target
267 # - Filter the unwanted symbols from all_symbols.txt
268 # - Create a stripped shared_code.axf file which contains only the symbols which are meant to be shared
269 add_custom_command(TARGET ${TARGET}
270 POST_BUILD
271
272 COMMAND ${GNUARM_NM}
273 ARGS
274 ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf > ${ALL_SYMBOLS}
275 COMMENT "Dumping all symbols"
276
277 COMMAND ${CMAKE_COMMAND}
278 -DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
279 -DALL_SYMBOLS=${ALL_SYMBOLS}
280 -P ${FILTER_SYMBOLS_SCRIPT}
281 BYPRODUCTS
282 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
283 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
284 COMMENT "Filtering shared symbols"
285
286 COMMAND ${CMAKE_COMMAND}
287 -E copy ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
288 COMMENT "Copy and rename ${TARGET} to strip"
289
290 COMMAND ${CMAKE_COMMAND}
291 -DSHARED_SYMBOLS_FILE=${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
292 -DEXECUTABLE_TO_STRIP=${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
293 -P ${STRIP_UNSHARED_CODE}
294 COMMENT "Stripping unshared code from ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf"
295 )
296endmacro()
297
298macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
299 # Find the CMake scripts
300 find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
301
302 add_custom_command(TARGET ${TARGET}
303 PRE_LINK
304
305 COMMAND ${CMAKE_COMMAND}
306 -DLIB_LIST='${LIB_LIST}'
307 -DSHARED_CODE_PATH=${SHARED_CODE_PATH}
308 -P ${WEAKEN_SYMBOLS_SCRIPT}
309 COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
310
311 # If sharing target is defined by TF-M build then setup dependency
312 if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
313 add_dependencies(${TARGET} ${ORIG_TARGET})
314 endif()
315endmacro()
316
317# Macro for linking shared code to given target. Location of shared code could
318# be outside of the TF-M project. Its location can be defined with the CMake
319# command line argument "SHARED_CODE_PATH". The file containing the shared objects
320# must be named "shared_symbols_addr.txt".
321# INPUTS:
322# TARGET Target to link the shared code to
323# SHARED_CODE_PATH Shared code located in this folder
324# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
325# LIB_LIST List of libraries which are linked to top level target
326macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
327 # GNUARM requires to link a stripped version (only containing the shared symbols) of the
328 # original executable to the executable which want to rely on the shared symbols.
329 target_link_options(${TARGET} PRIVATE -Wl,-R${SHARED_CODE_PATH}/shared_code.axf)
330
331 compiler_weaken_symbols(${TARGET}
332 ${SHARED_CODE_PATH}
333 ${ORIG_TARGET}
334 "${LIB_LIST}"
335 )
Sebastian Bøee6099c12021-10-25 15:11:26 +0200336endmacro()