forked from zephyrproject-rtos/trusted-firmware-m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolchain_GNUARM.cmake
391 lines (334 loc) · 13.3 KB
/
toolchain_GNUARM.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#-------------------------------------------------------------------------------
# Copyright (c) 2020-2024, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------
set(CMAKE_SYSTEM_NAME Generic)
find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
find_program(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND")
message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'")
endif()
if(CMAKE_CXX_COMPILER STREQUAL "CMAKE_CXX_COMPILER-NOTFOUND")
message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-g++'")
endif()
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
set(COMPILER_CMSE_FLAG -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)
# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
# ===================== SEt toolchain CPU and Arch =============================
if (DEFINED TFM_SYSTEM_PROCESSOR)
if(TFM_SYSTEM_PROCESSOR MATCHES "cortex-m85" AND GCC_VERSION VERSION_LESS "13.0.0")
# GNUARM until version 13 does not support the -mcpu=cortex-m85 flag
message(WARNING "Cortex-m85 is only supported from GCC13. "
"Falling back to -march usage for earlier versions.")
else()
set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
if (DEFINED TFM_SYSTEM_DSP)
if (NOT TFM_SYSTEM_DSP)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
endif()
endif()
# GCC specifies that '+nofp' is available on following M-profile cpus: 'cortex-m4',
# 'cortex-m7', 'cortex-m33', 'cortex-m35p' and 'cortex-m55'.
# Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
# Explicitly list those cpu to align with GCC description.
if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
if(NOT CONFIG_TFM_ENABLE_FP AND
(TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4"
OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7"
OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33"
OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p"
OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"))
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
endif()
endif()
if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
if(NOT CONFIG_TFM_ENABLE_MVE)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
endif()
if(NOT CONFIG_TFM_ENABLE_MVE_FP)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve.fp")
endif()
endif()
endif()
endif()
# CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
# set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
# The variable name is choosen to align with the ARMCLANG toolchain file.
set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
if(CONFIG_TFM_ENABLE_MVE)
string(APPEND CMAKE_SYSTEM_ARCH "+mve")
endif()
if(CONFIG_TFM_ENABLE_MVE_FP)
string(APPEND CMAKE_SYSTEM_ARCH "+mve.fp")
endif()
endif()
if (DEFINED TFM_SYSTEM_DSP)
# +nodsp modifier is only supported from GCC version 8.
if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
# armv8.1-m.main arch does not have +nodsp option
if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
NOT TFM_SYSTEM_DSP)
string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
endif()
endif()
endif()
if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
if(CONFIG_TFM_ENABLE_FP)
string(APPEND CMAKE_SYSTEM_ARCH "+fp")
endif()
endif()
add_compile_options(
-specs=nano.specs
-specs=nosys.specs
-Wall
-Wno-format
-Wno-return-type
-Wno-unused-but-set-variable
-c
-fdata-sections
-ffunction-sections
-fno-builtin
-fshort-enums
-funsigned-char
-mthumb
$<$<COMPILE_LANGUAGE:C>:-std=c99>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
# Force DWARF version 4 for zephyr as pyelftools does not support version 5 at present
-gdwarf-4
$<$<OR:$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<BOOL:${TFM_CODE_COVERAGE}>>:-g>
)
add_link_options(
--entry=Reset_Handler
-specs=nano.specs
-specs=nosys.specs
LINKER:-check-sections
LINKER:-fatal-warnings
LINKER:--gc-sections
LINKER:--no-wchar-size-warning
)
if(NOT CONFIG_TFM_MEMORY_USAGE_QUIET)
add_link_options(LINKER:--print-memory-usage)
endif()
if (GCC_VERSION VERSION_LESS 7.3.1)
message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
endif()
if (GCC_VERSION VERSION_EQUAL 10.2.1)
message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
" Select other GNU Arm compiler versions instead."
" See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
endif()
# GNU Arm compiler version greater equal than *11.3.Rel1*
# has a linker issue that required system calls are missing,
# such as _read and _write. Add stub functions of required
# system calls to solve this issue.
#
# READONLY linker script attribute is not supported in older
# GNU Arm compilers. For these version the preprocessor will
# remove the READONLY string from the linker scripts.
if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1)
set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
set(CONFIG_GNU_LINKER_READONLY_ATTRIBUTE TRUE)
endif()
if (CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_CXX_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
else()
set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
set(CMAKE_CXX_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
endif()
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_INIT})
set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft)
set(BL2_LINKER_CP_OPTION -mfloat-abi=soft)
set(BL1_COMPILER_CP_FLAG -mfloat-abi=soft)
set(BL1_LINKER_CP_OPTION -mfloat-abi=soft)
if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
set(COMPILER_CP_FLAG -mfloat-abi=hard)
set(LINKER_CP_OPTION -mfloat-abi=hard)
if (CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP)
set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
endif()
else()
set(COMPILER_CP_FLAG -mfloat-abi=soft)
set(LINKER_CP_OPTION -mfloat-abi=soft)
endif()
# For GNU Arm Embedded Toolchain doesn't emit __ARM_ARCH_8_1M_MAIN__, adding this macro manually.
add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__=1>)
macro(target_add_scatter_file target)
target_link_options(${target}
PRIVATE
-T $<TARGET_OBJECTS:${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
KEEP_EXTENSION True # Don't use .o extension for the preprocessed file
)
endforeach()
add_dependencies(${target}
${target}_scatter
)
set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
target_link_libraries(${target}_scatter
platform_region_defs
psa_interface
tfm_config
)
target_compile_options(${target}_scatter
PRIVATE
-E
-P
-xc
)
target_compile_definitions(${target}_scatter
PRIVATE
$<$<NOT:$<BOOL:${CONFIG_GNU_LINKER_READONLY_ATTRIBUTE}>>:READONLY=>
)
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 ${CMAKE_OBJCOPY}
-O binary $<TARGET_FILE:${target}>
${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 ${CMAKE_OBJCOPY}
-O elf32-littlearm $<TARGET_FILE:${target}>
${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 ${CMAKE_OBJCOPY}
-O ihex $<TARGET_FILE:${target}>
${bin_dir}/${target}.hex
)
add_custom_target(${target}_binaries
ALL
DEPENDS ${target}_bin
DEPENDS ${target}_elf
DEPENDS ${target}_hex
)
endmacro()
macro(target_share_symbols target)
get_target_property(TARGET_TYPE ${target} TYPE)
if (NOT TARGET_TYPE STREQUAL "EXECUTABLE")
message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.")
endif()
foreach(symbol_file ${ARGN})
FILE(STRINGS ${symbol_file} SYMBOLS
LENGTH_MINIMUM 1
)
list(APPEND KEEP_SYMBOL_LIST ${SYMBOLS})
endforeach()
set(STRIP_SYMBOL_KEEP_LIST ${KEEP_SYMBOL_LIST})
# Force the target to not remove the symbols if they're unused.
list(TRANSFORM KEEP_SYMBOL_LIST PREPEND "-Wl,--undefined=")
target_link_options(${target}
PRIVATE
${KEEP_SYMBOL_LIST}
)
list(TRANSFORM STRIP_SYMBOL_KEEP_LIST PREPEND --keep-symbol=)
# strip all the symbols except those proveded as arguments
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${target}> --wildcard ${STRIP_SYMBOL_KEEP_LIST} --strip-all $<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX}
)
endmacro()
macro(target_link_shared_code target)
foreach(symbol_provider ${ARGN})
if (TARGET ${symbol_provider})
get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE)
if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE")
message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.")
endif()
endif()
add_dependencies(${target} ${symbol_provider})
target_link_options(${target} PRIVATE LINKER:-R$<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}${CODE_SHARING_INPUT_FILE_SUFFIX})
endforeach()
endmacro()
macro(target_strip_symbols target)
set(SYMBOL_LIST "${ARGN}")
list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
)
endmacro()
macro(target_strip_symbols_from_dependency target dependency)
set(SYMBOL_LIST "${ARGN}")
list(TRANSFORM SYMBOL_LIST PREPEND --strip-symbol=)
add_custom_command(
TARGET ${target}
PRE_LINK
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
)
endmacro()
macro(target_weaken_symbols target)
set(SYMBOL_LIST "${ARGN}")
list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
)
endmacro()
macro(target_weaken_symbols_from_dependency target dependency)
set(SYMBOL_LIST "${ARGN}")
list(TRANSFORM SYMBOL_LIST PREPEND --weaken-symbol=)
add_custom_command(
TARGET ${target}
PRE_LINK
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
)
endmacro()
# A dummy macro to align with Armclang workaround
macro(tfm_toolchain_reload_compiler)
endmacro()