-
Notifications
You must be signed in to change notification settings - Fork 5
/
toolchain_ARMCLANG.cmake
316 lines (271 loc) · 12.3 KB
/
toolchain_ARMCLANG.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#-------------------------------------------------------------------------------
# Copyright (c) 2020, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.15)
# Don't load this file if it is specified as a cmake toolchain file
if(NOT TFM_TOOLCHAIN_FILE)
message(DEPRECATION "SETTING CMAKE_TOOLCHAIN_FILE is deprecated. It has been replaced with TFM_TOOLCHAIN_FILE.")
return()
endif()
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER armclang)
set(CMAKE_ASM_COMPILER armasm)
set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out=)
set(COMPILER_CMSE_FLAG $<$<COMPILE_LANGUAGE:C>:-mcmse>)
# This variable name is a bit of a misnomer. The file it is set to is included
# at a particular step in the compiler initialisation. It is used here to
# configure the extensions for object files. Despite the name, it also works
# with the Ninja generator.
set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
macro(tfm_toolchain_reset_compiler_flags)
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
string(REGEX REPLACE "\\+nodsp" ".no_dsp" CMAKE_ASM_CPU_FLAG "${CMAKE_SYSTEM_PROCESSOR}")
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-Wno-ignored-optimization-argument>
$<$<COMPILE_LANGUAGE:C>:-Wno-unused-command-line-argument>
$<$<COMPILE_LANGUAGE:C>:-Wall>
# Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
$<$<COMPILE_LANGUAGE:C>:-Wno-error=cpp>
$<$<COMPILE_LANGUAGE:C>:-c>
$<$<COMPILE_LANGUAGE:C>:-fdata-sections>
$<$<COMPILE_LANGUAGE:C>:-ffunction-sections>
$<$<COMPILE_LANGUAGE:C>:-fno-builtin>
$<$<COMPILE_LANGUAGE:C>:-fshort-enums>
$<$<COMPILE_LANGUAGE:C>:-fshort-wchar>
$<$<COMPILE_LANGUAGE:C>:-funsigned-char>
$<$<COMPILE_LANGUAGE:C>:-masm=auto>
$<$<COMPILE_LANGUAGE:C>:-nostdlib>
$<$<COMPILE_LANGUAGE:C>:-std=c99>
$<$<AND:$<COMPILE_LANGUAGE:C>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:-mfpu=none>
$<$<AND:$<COMPILE_LANGUAGE:ASM>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:--fpu=none>
$<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}>
)
endmacro()
macro(tfm_toolchain_reset_linker_flags)
set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
add_link_options(
--info=summarysizes,sizes,totals,unused,veneers
--strict
--symbols
--xref
$<$<AND:$<VERSION_GREATER:${TFM_ISOLATION_LEVEL},1>,$<STREQUAL:"${TEST_PSA_API}","IPC">>:--no-merge>
# Suppress link warnings that are consistant (and therefore hopefully
# harmless)
# https://developer.arm.com/documentation/100074/0608/linker-errors-and-warnings/list-of-the-armlink-error-and-warning-messages
# Empty region description
--diag_suppress=6312
# Ns section matches pattern
--diag_suppress=6314
# Duplicate input files
--diag_suppress=6304
# Pattern only matches removed unused sections.
--diag_suppress=6329
$<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:--fpu=softvfp>
)
endmacro()
macro(tfm_toolchain_set_processor_arch)
set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE})
set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE})
if (DEFINED TFM_SYSTEM_MVE)
if(NOT TFM_SYSTEM_MVE)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
endif()
endif()
if (DEFINED TFM_SYSTEM_FP)
if(NOT TFM_SYSTEM_FP)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
endif()
endif()
if (DEFINED TFM_SYSTEM_DSP)
if(NOT TFM_SYSTEM_DSP)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
endif()
endif()
# Cmake's ARMClang support has several issues with compiler validation. To
# avoid these, we set the list of supported -mcpu and -march variables to
# the ones we intend to use so that the validation will never fail.
include(Compiler/ARMClang)
set(CMAKE_C_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_C_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_ASM_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_ASM_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_PROCESSOR})
endmacro()
macro(tfm_toolchain_reload_compiler)
tfm_toolchain_set_processor_arch()
tfm_toolchain_reset_compiler_flags()
tfm_toolchain_reset_linker_flags()
unset(CMAKE_C_FLAGS_INIT)
unset(CMAKE_C_LINK_FLAGS)
unset(CMAKE_ASM_FLAGS_INIT)
unset(CMAKE_ASM_LINK_FLAGS)
unset(__mcpu_flag_set)
unset(__march_flag_set)
include(Compiler/ARMClang)
__compiler_armclang(C)
include(Compiler/ARMCC)
__compiler_armcc(ASM)
# Cmake's armclang support will set either mcpu or march, but march gives
# better code size so we manually set it.
set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCHITECTURE}")
set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
set(CMAKE_C_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_ASM_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
# But armlink doesn't support this +dsp syntax
string(REGEX REPLACE "\\+nodsp" "" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
string(REGEX REPLACE "\\+nodsp" "" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
# And uses different syntax for +nofp
string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
# Workaround for issues with --depend-single-line with armasm and Ninja
if (CMAKE_GENERATOR STREQUAL "Ninja")
set( CMAKE_DEPFILE_FLAGS_ASM "--depend=<OBJECT>.d")
endif()
set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG")
endmacro()
# Configure environment for the compiler setup run by cmake at the first
# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
tfm_toolchain_set_processor_arch()
tfm_toolchain_reset_compiler_flags()
tfm_toolchain_reset_linker_flags()
# Behaviour for handling scatter files is so wildly divergent between compilers
# that this macro is required.
macro(target_add_scatter_file target)
target_link_options(${target}
PRIVATE
--scatter=$<TARGET_OBJECTS:${target}_scatter>
)
add_dependencies(${target}
${target}_scatter
)
add_library(${target}_scatter OBJECT)
foreach(scatter_file ${ARGN})
target_sources(${target}_scatter
PRIVATE
${scatter_file}
)
# Cmake cannot use generator expressions in the
# set_source_file_properties command, so instead we just parse the regex
# for the filename and set the property on all files, regardless of if
# the generator expression would evaluate to true or not.
string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
set_source_files_properties(${SCATTER_FILE_PATH}
PROPERTIES
LANGUAGE C
)
endforeach()
target_link_libraries(${target}_scatter
platform_region_defs
psa_interface
tfm_partition_defs
)
target_compile_options(${target}_scatter
PRIVATE
-E
-xc
)
endmacro()
macro(add_convert_to_bin_target target)
get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
add_custom_target(${target}_bin
SOURCES ${bin_dir}/${target}.bin
)
add_custom_command(OUTPUT ${bin_dir}/${target}.bin
DEPENDS ${target}
COMMAND fromelf
--bincombined $<TARGET_FILE:${target}>
--output=${bin_dir}/${target}.bin
)
add_custom_target(${target}_elf
SOURCES ${bin_dir}/${target}.elf
)
add_custom_command(OUTPUT ${bin_dir}/${target}.elf
DEPENDS ${target}
COMMAND fromelf
--elf $<TARGET_FILE:${target}>
--output=${bin_dir}/${target}.elf
)
add_custom_target(${target}_hex
SOURCES ${bin_dir}/${target}.hex
)
add_custom_command(OUTPUT ${bin_dir}/${target}.hex
DEPENDS ${target}
COMMAND fromelf
--i32combined $<TARGET_FILE:${target}>
--output=${bin_dir}/${target}.hex
)
add_custom_target(${target}_binaries
ALL
DEPENDS ${target}_bin
DEPENDS ${target}_elf
DEPENDS ${target}_hex
)
endmacro()
# Macro for sharing code among independent binaries. This function extracts
# some parts of the code based on a symbol template file and creates a text
# file, which contains the symbols with their absolute addresses, which can be
# picked up by the linker when linking the other target.
# INPUTS:
# TARGET - - Target to extract the symbols/objects from
# SHARED_SYMBOL_TEMPLATE - Template with names of symbols to share
macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
# Create a temporary file, which contains all extracted symbols from 'TARGET'
set(ALL_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/all_symbols.txt)
set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " --symdefs=${ALL_SYMBOLS}")
# Find the CMake script doing the symbol filtering.
find_file(FILTER_SYMBOLS_SCRIPT "FilterSharedSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
# Single step, just filter the unwanted symbols from symdefs file
add_custom_command(TARGET ${TARGET}
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DSHARED_SYMBOL_TEMPLATE=${SHARED_SYMBOL_TEMPLATE}
-DALL_SYMBOLS=${ALL_SYMBOLS}
-P ${FILTER_SYMBOLS_SCRIPT}
BYPRODUCTS
${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_name.txt
${CMAKE_CURRENT_BINARY_DIR}/shared_symbols_addr.txt
COMMENT "Filtering shared symbols"
)
endmacro()
macro(compiler_weaken_symbols TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
# Find the CMake scripts
find_file(WEAKEN_SYMBOLS_SCRIPT "WeakenSymbols.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
add_custom_command(TARGET ${TARGET}
PRE_LINK
COMMAND ${CMAKE_COMMAND}
-DLIB_LIST='${LIB_LIST}'
-DSHARED_CODE_PATH=${SHARED_CODE_PATH}
-P ${WEAKEN_SYMBOLS_SCRIPT}
COMMENT "Set conflicting symbols to be weak in the original libraries to avoid collision")
# If sharing target is defined by TF-M build then setup dependency
if(NOT ${ORIG_TARGET} STREQUAL "EXTERNAL_TARGET")
add_dependencies(${TARGET} ${ORIG_TARGET})
endif()
endmacro()
# Macro for linking shared code to given target. Location of shared code could
# be outside of the TF-M project. Its location can be defined with the CMake
# command line argument "SHARED_CODE_PATH". The file containing the shared objects
# must be named "shared_symbols_addr.txt".
# INPUTS:
# TARGET Target to link the shared code to
# SHARED_CODE_PATH Shared code located in this folder
# ORIG_TARGET Target that shared code was extraced from <TARGET | "EXTERNAL_TARGET">
# LIB_LIST List of libraries which are linked to top level target
macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
# ARMCLANG requires adding a symbol definition file to the source file list.
# This is the output of the -symdefs compiler switch.
set_source_files_properties(${SHARED_CODE_PATH}/shared_symbols_addr.txt PROPERTIES EXTERNAL_OBJECT true GENERATED true)
target_sources(${TARGET} PRIVATE ${SHARED_CODE_PATH}/shared_symbols_addr.txt)
compiler_weaken_symbols(${TARGET}
${SHARED_CODE_PATH}
${ORIG_TARGET}
"${LIB_LIST}"
)
endmacro()