blob: 359f8468b0b3368bb33312f9bde6f92d6533c572 [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited. All rights reserved.
3#
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
Karl Zhangf897e9e2021-01-08 17:52:53 +080051 $<$<BOOL:${TFM_CODE_COVERAGE}>:-g>
Raef Coles9ec67e62020-07-10 09:40:35 +010052 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
53 )
Raef Coles69817322020-10-19 14:14:14 +010054endmacro()
55
56macro(tfm_toolchain_reset_linker_flags)
57 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
58
Raef Coles9ec67e62020-07-10 09:40:35 +010059 add_link_options(
60 --entry=Reset_Handler
61 --specs=nano.specs
62 LINKER:-check-sections
63 LINKER:-fatal-warnings
64 LINKER:--gc-sections
65 LINKER:--no-wchar-size-warning
66 LINKER:--print-memory-usage
67 )
Raef Coles69817322020-10-19 14:14:14 +010068endmacro()
69
70macro(tfm_toolchain_set_processor_arch)
Gabor Abonyi866571c2021-10-07 13:56:19 +020071 if (DEFINED TFM_SYSTEM_PROCESSOR)
72 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
73
74 if (DEFINED TFM_SYSTEM_DSP)
75 if (NOT TFM_SYSTEM_DSP)
76 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
77 endif()
78 endif()
79 endif()
80
81 # CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
82 # set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
83 # The variable name is choosen to align with the ARMCLANG toolchain file.
84 set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
Raef Coles69817322020-10-19 14:14:14 +010085
86 if (DEFINED TFM_SYSTEM_DSP)
Gabor Abonyi866571c2021-10-07 13:56:19 +020087 # +nodsp modifier is only supported from GCC version 8.
88 # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
89 EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
90 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
91 # armv8.1-m.main arch does not have +nodsp option
92 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
93 NOT TFM_SYSTEM_DSP)
94 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
95 endif()
Raef Coles69817322020-10-19 14:14:14 +010096 endif()
97 endif()
98endmacro()
99
100macro(tfm_toolchain_reload_compiler)
101 tfm_toolchain_set_processor_arch()
102 tfm_toolchain_reset_compiler_flags()
103 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +0100104
105 unset(CMAKE_C_FLAGS_INIT)
106 unset(CMAKE_ASM_FLAGS_INIT)
107
Gabor Abonyi866571c2021-10-07 13:56:19 +0200108 if (DEFINED TFM_SYSTEM_PROCESSOR)
109 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
110 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
111 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
112 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
113 else()
114 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
115 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
116 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
117 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
118 endif()
Raef Coles9ec67e62020-07-10 09:40:35 +0100119
120 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
121 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
122endmacro()
123
Raef Coles69817322020-10-19 14:14:14 +0100124# Configure environment for the compiler setup run by cmake at the first
125# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
126# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
127tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +0100128
129macro(target_add_scatter_file target)
130 target_link_options(${target}
131 PRIVATE
132 -T $<TARGET_OBJECTS:${target}_scatter>
133 )
134
135 add_dependencies(${target}
136 ${target}_scatter
137 )
138
139 add_library(${target}_scatter OBJECT)
140 foreach(scatter_file ${ARGN})
141 target_sources(${target}_scatter
142 PRIVATE
143 ${scatter_file}
144 )
145 # Cmake cannot use generator expressions in the
146 # set_source_file_properties command, so instead we just parse the regex
147 # for the filename and set the property on all files, regardless of if
148 # the generator expression would evaluate to true or not.
149 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
150 set_source_files_properties(${SCATTER_FILE_PATH}
151 PROPERTIES
152 LANGUAGE C
Sebastian Bøee6099c12021-10-25 15:11:26 +0200153 KEEP_EXTENSION True # Don't use .o extension for the preprocessed file
Raef Coles9ec67e62020-07-10 09:40:35 +0100154 )
155 endforeach()
156
Raef Coles9ec67e62020-07-10 09:40:35 +0100157 target_link_libraries(${target}_scatter
158 platform_region_defs
159 psa_interface
160 tfm_partition_defs
161 )
162
163 target_compile_options(${target}_scatter
164 PRIVATE
165 -E
166 -P
167 -xc
168 )
169endmacro()
170
171macro(add_convert_to_bin_target target)
172 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
173
174 add_custom_target(${target}_bin
175 SOURCES ${bin_dir}/${target}.bin
176 )
177 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
178 DEPENDS ${target}
179 COMMAND ${CMAKE_OBJCOPY}
180 -O binary $<TARGET_FILE:${target}>
181 ${bin_dir}/${target}.bin
182 )
183
184 add_custom_target(${target}_elf
185 SOURCES ${bin_dir}/${target}.elf
186 )
187 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
188 DEPENDS ${target}
189 COMMAND ${CMAKE_OBJCOPY}
190 -O elf32-littlearm $<TARGET_FILE:${target}>
191 ${bin_dir}/${target}.elf
192 )
193
194 add_custom_target(${target}_hex
195 SOURCES ${bin_dir}/${target}.hex
196 )
197 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
198 DEPENDS ${target}
199 COMMAND ${CMAKE_OBJCOPY}
200 -O ihex $<TARGET_FILE:${target}>
201 ${bin_dir}/${target}.hex
202 )
203
204 add_custom_target(${target}_binaries
205 ALL
206 DEPENDS ${target}_bin
207 DEPENDS ${target}_elf
208 DEPENDS ${target}_hex
209 )
210endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000211
212# Macro for sharing code among independent binaries. This function extracts
213# some parts of the code based on a symbol template file and creates a text
214# file, which contains the symbols with their absolute addresses, which can be
215# picked up by the linker when linking the other target.
216# INPUTS:
217# TARGET - - Target to extract the symbols/objects from
218# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
219macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
220 # Create a temporary file, which contains all extracted symbols from 'TARGET'
221 set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
222
223 # Find the CMake script doing the symbol filtering.
224 find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
225 find_file(STRIP_UNSHARED_CODE "StripUnsharedCode.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
226
227 find_program(GNUARM_NM arm-none-eabi-nm)
228 if (GNUARM_NM STREQUAL "GNUARM_NM-NOTFOUND")
229 message(FATAL_ERROR "toolchain_GNUARM.cmake: mandatory tool '${GNUARM_NM}' is missing.")
230 endif()
231
232 # Multiple steps are required:
233 # - Extract all symbols from sharing target
234 # - Filter the unwanted symbols from all_symbols.txt
235 # - Create a stripped shared_code.axf file which contains only the symbols which are meant to be shared
236 add_custom_command(TARGET ${TARGET}
237 POST_BUILD
238
239 COMMAND ${GNUARM_NM}
240 ARGS
241 ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf > ${ALL_SYMBOLS}
242 COMMENT "Dumping all symbols"
243
244 COMMAND ${CMAKE_COMMAND}
245 -DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
246 -DALL_SYMBOLS=${ALL_SYMBOLS}
247 -P ${FILTER_SYMBOLS_SCRIPT}
248 BYPRODUCTS
249 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
250 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
251 COMMENT "Filtering shared symbols"
252
253 COMMAND ${CMAKE_COMMAND}
254 -E copy ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
255 COMMENT "Copy and rename ${TARGET} to strip"
256
257 COMMAND ${CMAKE_COMMAND}
258 -DSHARED_SYMBOLS_FILE=${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
259 -DEXECUTABLE_TO_STRIP=${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
260 -P ${STRIP_UNSHARED_CODE}
261 COMMENT "Stripping unshared code from ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf"
262 )
263endmacro()
264
265macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
266 # Find the CMake scripts
267 find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
268
269 add_custom_command(TARGET ${TARGET}
270 PRE_LINK
271
272 COMMAND ${CMAKE_COMMAND}
273 -DLIB_LIST='${LIB_LIST}'
274 -DSHARED_CODE_PATH=${SHARED_CODE_PATH}
275 -P ${WEAKEN_SYMBOLS_SCRIPT}
276 COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
277
278 # If sharing target is defined by TF-M build then setup dependency
279 if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
280 add_dependencies(${TARGET} ${ORIG_TARGET})
281 endif()
282endmacro()
283
284# Macro for linking shared code to given target. Location of shared code could
285# be outside of the TF-M project. Its location can be defined with the CMake
286# command line argument "SHARED_CODE_PATH". The file containing the shared objects
287# must be named "shared_symbols_addr.txt".
288# INPUTS:
289# TARGET Target to link the shared code to
290# SHARED_CODE_PATH Shared code located in this folder
291# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
292# LIB_LIST List of libraries which are linked to top level target
293macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
294 # GNUARM requires to link a stripped version (only containing the shared symbols) of the
295 # original executable to the executable which want to rely on the shared symbols.
296 target_link_options(${TARGET} PRIVATE -Wl,-R${SHARED_CODE_PATH}/shared_code.axf)
297
298 compiler_weaken_symbols(${TARGET}
299 ${SHARED_CODE_PATH}
300 ${ORIG_TARGET}
301 "${LIB_LIST}"
302 )
Sebastian Bøee6099c12021-10-25 15:11:26 +0200303endmacro()