blob: 92acbf9534f28f8534688530469b8ef1781316c8 [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
Raef Coles69817322020-10-19 14:14:14 +010016set(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
17set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}-gcc)
Raef Coles9ec67e62020-07-10 09:40:35 +010018
Raef Coles69817322020-10-19 14:14:14 +010019set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
20set(COMPILER_CMSE_FLAG -mcmse)
Raef Coles9ec67e62020-07-10 09:40:35 +010021
22# This variable name is a bit of a misnomer. The file it is set to is included
23# at a particular step in the compiler initialisation. It is used here to
24# configure the extensions for object files. Despite the name, it also works
25# with the Ninja generator.
26set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
27
Raef Coles69817322020-10-19 14:14:14 +010028macro(tfm_toolchain_reset_compiler_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010029 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010030
Raef Coles9ec67e62020-07-10 09:40:35 +010031 add_compile_options(
32 --specs=nano.specs
33 -Wall
34 -Wno-format
35 -Wno-return-type
36 -Wno-unused-but-set-variable
37 -c
38 -fdata-sections
39 -ffunction-sections
40 -fno-builtin
41 -fshort-enums
Raef Coles9ec67e62020-07-10 09:40:35 +010042 -funsigned-char
43 -mthumb
44 -nostdlib
45 -std=c99
Karl Zhangf897e9e2021-01-08 17:52:53 +080046 $<$<BOOL:${TFM_CODE_COVERAGE}>:-g>
Raef Coles9ec67e62020-07-10 09:40:35 +010047 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
48 )
Raef Coles69817322020-10-19 14:14:14 +010049endmacro()
50
51macro(tfm_toolchain_reset_linker_flags)
52 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
53
Raef Coles9ec67e62020-07-10 09:40:35 +010054 add_link_options(
55 --entry=Reset_Handler
56 --specs=nano.specs
57 LINKER:-check-sections
58 LINKER:-fatal-warnings
59 LINKER:--gc-sections
60 LINKER:--no-wchar-size-warning
61 LINKER:--print-memory-usage
62 )
Raef Coles69817322020-10-19 14:14:14 +010063endmacro()
64
65macro(tfm_toolchain_set_processor_arch)
66 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
67 set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
68
69 if (DEFINED TFM_SYSTEM_DSP)
70 if(NOT TFM_SYSTEM_DSP)
71 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
72 endif()
73 endif()
74endmacro()
75
76macro(tfm_toolchain_reload_compiler)
77 tfm_toolchain_set_processor_arch()
78 tfm_toolchain_reset_compiler_flags()
79 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +010080
81 unset(CMAKE_C_FLAGS_INIT)
82 unset(CMAKE_ASM_FLAGS_INIT)
83
84 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
85 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
86 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
87 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
88
89 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
90 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
91endmacro()
92
Raef Coles69817322020-10-19 14:14:14 +010093# Configure environment for the compiler setup run by cmake at the first
94# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
95# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
96tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +010097
98macro(target_add_scatter_file target)
99 target_link_options(${target}
100 PRIVATE
101 -T $<TARGET_OBJECTS:${target}_scatter>
102 )
103
104 add_dependencies(${target}
105 ${target}_scatter
106 )
107
108 add_library(${target}_scatter OBJECT)
109 foreach(scatter_file ${ARGN})
110 target_sources(${target}_scatter
111 PRIVATE
112 ${scatter_file}
113 )
114 # Cmake cannot use generator expressions in the
115 # set_source_file_properties command, so instead we just parse the regex
116 # for the filename and set the property on all files, regardless of if
117 # the generator expression would evaluate to true or not.
118 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
119 set_source_files_properties(${SCATTER_FILE_PATH}
120 PROPERTIES
121 LANGUAGE C
122 )
123 endforeach()
124
Raef Coles9ec67e62020-07-10 09:40:35 +0100125 target_link_libraries(${target}_scatter
126 platform_region_defs
127 psa_interface
128 tfm_partition_defs
129 )
130
131 target_compile_options(${target}_scatter
132 PRIVATE
133 -E
134 -P
135 -xc
136 )
137endmacro()
138
139macro(add_convert_to_bin_target target)
140 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
141
142 add_custom_target(${target}_bin
143 SOURCES ${bin_dir}/${target}.bin
144 )
145 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
146 DEPENDS ${target}
147 COMMAND ${CMAKE_OBJCOPY}
148 -O binary $<TARGET_FILE:${target}>
149 ${bin_dir}/${target}.bin
150 )
151
152 add_custom_target(${target}_elf
153 SOURCES ${bin_dir}/${target}.elf
154 )
155 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
156 DEPENDS ${target}
157 COMMAND ${CMAKE_OBJCOPY}
158 -O elf32-littlearm $<TARGET_FILE:${target}>
159 ${bin_dir}/${target}.elf
160 )
161
162 add_custom_target(${target}_hex
163 SOURCES ${bin_dir}/${target}.hex
164 )
165 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
166 DEPENDS ${target}
167 COMMAND ${CMAKE_OBJCOPY}
168 -O ihex $<TARGET_FILE:${target}>
169 ${bin_dir}/${target}.hex
170 )
171
172 add_custom_target(${target}_binaries
173 ALL
174 DEPENDS ${target}_bin
175 DEPENDS ${target}_elf
176 DEPENDS ${target}_hex
177 )
178endmacro()
Tamas Banf8b0b2d2020-10-26 13:03:13 +0000179
180# Macro for sharing code among independent binaries. This function extracts
181# some parts of the code based on a symbol template file and creates a text
182# file, which contains the symbols with their absolute addresses, which can be
183# picked up by the linker when linking the other target.
184# INPUTS:
185# TARGET - - Target to extract the symbols/objects from
186# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
187macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
188 # Create a temporary file, which contains all extracted symbols from 'TARGET'
189 set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
190
191 # Find the CMake script doing the symbol filtering.
192 find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
193 find_file(STRIP_UNSHARED_CODE "StripUnsharedCode.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
194
195 find_program(GNUARM_NM arm-none-eabi-nm)
196 if (GNUARM_NM STREQUAL "GNUARM_NM-NOTFOUND")
197 message(FATAL_ERROR "toolchain_GNUARM.cmake: mandatory tool '${GNUARM_NM}' is missing.")
198 endif()
199
200 # Multiple steps are required:
201 # - Extract all symbols from sharing target
202 # - Filter the unwanted symbols from all_symbols.txt
203 # - Create a stripped shared_code.axf file which contains only the symbols which are meant to be shared
204 add_custom_command(TARGET ${TARGET}
205 POST_BUILD
206
207 COMMAND ${GNUARM_NM}
208 ARGS
209 ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf > ${ALL_SYMBOLS}
210 COMMENT "Dumping all symbols"
211
212 COMMAND ${CMAKE_COMMAND}
213 -DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
214 -DALL_SYMBOLS=${ALL_SYMBOLS}
215 -P ${FILTER_SYMBOLS_SCRIPT}
216 BYPRODUCTS
217 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
218 ${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
219 COMMENT "Filtering shared symbols"
220
221 COMMAND ${CMAKE_COMMAND}
222 -E copy ${CMAKE_BINARY_DIR}/bin/${TARGET}.axf ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
223 COMMENT "Copy and rename ${TARGET} to strip"
224
225 COMMAND ${CMAKE_COMMAND}
226 -DSHARED_SYMBOLS_FILE=${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
227 -DEXECUTABLE_TO_STRIP=${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf
228 -P ${STRIP_UNSHARED_CODE}
229 COMMENT "Stripping unshared code from ${CMAKE_CURRENT_BINARY_DIR}/shared_code.axf"
230 )
231endmacro()
232
233macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
234 # Find the CMake scripts
235 find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
236
237 add_custom_command(TARGET ${TARGET}
238 PRE_LINK
239
240 COMMAND ${CMAKE_COMMAND}
241 -DLIB_LIST='${LIB_LIST}'
242 -DSHARED_CODE_PATH=${SHARED_CODE_PATH}
243 -P ${WEAKEN_SYMBOLS_SCRIPT}
244 COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
245
246 # If sharing target is defined by TF-M build then setup dependency
247 if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
248 add_dependencies(${TARGET} ${ORIG_TARGET})
249 endif()
250endmacro()
251
252# Macro for linking shared code to given target. Location of shared code could
253# be outside of the TF-M project. Its location can be defined with the CMake
254# command line argument "SHARED_CODE_PATH". The file containing the shared objects
255# must be named "shared_symbols_addr.txt".
256# INPUTS:
257# TARGET Target to link the shared code to
258# SHARED_CODE_PATH Shared code located in this folder
259# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
260# LIB_LIST List of libraries which are linked to top level target
261macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
262 # GNUARM requires to link a stripped version (only containing the shared symbols) of the
263 # original executable to the executable which want to rely on the shared symbols.
264 target_link_options(${TARGET} PRIVATE -Wl,-R${SHARED_CODE_PATH}/shared_code.axf)
265
266 compiler_weaken_symbols(${TARGET}
267 ${SHARED_CODE_PATH}
268 ${ORIG_TARGET}
269 "${LIB_LIST}"
270 )
271endmacro()