forked from TrustedFirmware-M/trusted-firmware-m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolchain_ARMCLANG.cmake
524 lines (453 loc) · 21.4 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#-------------------------------------------------------------------------------
# Copyright (c) 2020-2024, Arm Limited. All rights reserved.
# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.21)
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER armclang)
set(CMAKE_CXX_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)
if(NOT DEFINED CMAKE_OBJCOPY)
set(CMAKE_OBJCOPY ${CROSS_COMPILE}-objcopy CACHE FILEPATH "Path to objcopy")
endif()
macro(tfm_toolchain_reset_compiler_flags)
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
add_compile_options(
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-ignored-optimization-argument>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unused-command-line-argument>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wall>
# Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-error=cpp>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-c>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fdata-sections>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-ffunction-sections>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fno-builtin>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-enums>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-wchar>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-funsigned-char>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-masm=auto>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-nostdlib>
$<$<COMPILE_LANGUAGE:C>:-std=c99>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
$<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}>
$<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
)
endmacro()
if(CONFIG_TFM_MEMORY_USAGE_QUIET)
set(MEMORY_USAGE_FLAG "")
else()
set(MEMORY_USAGE_FLAG --info=summarysizes,sizes,totals,unused,veneers)
endif()
macro(tfm_toolchain_reset_linker_flags)
set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
add_link_options(
${MEMORY_USAGE_FLAG}
--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
)
endmacro()
macro(tfm_toolchain_set_processor_arch)
if (DEFINED TFM_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
message(WARNING "MVE is not yet supported using ARMCLANG")
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
endif()
if (DEFINED TFM_SYSTEM_DSP)
if(NOT TFM_SYSTEM_DSP)
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
endif()
endif()
# ARMCLANG specifies that '+nofp' is available on following M-profile cpus:
# 'cortex-m4', 'cortex-m7', 'cortex-m33', 'cortex-m35p', 'cortex-m55' and 'cortex-m85'.
# Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
# Explicitly list those cpu to align with ARMCLANG description.
if (NOT CONFIG_TFM_FLOAT_ABI STREQUAL "hard" 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"
OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85"))
string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
endif()
string(REGEX REPLACE "\\+nodsp" ".no_dsp" CMAKE_ASM_CPU_FLAG "${CMAKE_SYSTEM_PROCESSOR}")
string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
else()
set(CMAKE_ASM_CPU_FLAG ${TFM_SYSTEM_ARCHITECTURE})
# Armasm uses different syntax than armclang for architecture targets
string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
# Modifiers are additive instead of subtractive (.fp Vs .no_fp)
if (TFM_SYSTEM_DSP)
string(APPEND CMAKE_ASM_CPU_FLAG ".dsp")
endif()
if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
string(APPEND CMAKE_ASM_CPU_FLAG ".fp")
endif()
endif()
# CMAKE_SYSTEM_ARCH is an ARMCLANG CMAKE internal variable, used to set
# compile and linker flags up until CMake 3.21 where CMP0123 was introduced:
# https://cmake.org/cmake/help/latest/policy/CMP0123.html
# This behavior is overwritten by setting CMAKE_C_FLAGS in
# tfm_toolchain_reload_compiler.
# Another use of this variable is to statisfy a requirement for ARMCLANG to
# set either the target CPU or the Architecture. This variable needs to be
# set to allow targeting architectures without a specific CPU.
set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE})
set(CMAKE_CXX_COMPILER_TARGET arm-${CROSS_COMPILE})
set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE})
# MVE is currently not supported in case of armclang
if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
string(APPEND CMAKE_SYSTEM_ARCH "+nomve")
endif()
if (DEFINED TFM_SYSTEM_DSP)
if(NOT TFM_SYSTEM_DSP)
string(APPEND CMAKE_SYSTEM_ARCH "+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_ARCH})
set(CMAKE_CXX_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_CXX_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
set(CMAKE_ASM_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_ASM_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
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_CXX_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-ASM)
__compiler_armcc(ASM)
if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6.13)
message(FATAL_ERROR "Please select newer Arm compiler version starting from 6.13.")
endif()
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.15 AND
CMAKE_C_COMPILER_VERSION VERSION_LESS 6.18)
message(FATAL_ERROR "Armclang 6.15~6.17 may cause MemManage fault."
" This defect has been fixed since Armclang 6.18."
" See [SDCOMP-59788] in Armclang 6.18 release note for details."
" Please use other Armclang versions instead.")
endif()
set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
if (DEFINED TFM_SYSTEM_PROCESSOR)
set(CMAKE_C_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_CXX_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_C_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
set(CMAKE_CXX_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_CXX_LINK_FLAGS "${CMAKE_CXX_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_CXX_LINK_FLAGS "${CMAKE_CXX_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_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
else()
set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
set(CMAKE_CXX_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
endif()
set(BL2_COMPILER_CP_FLAG
$<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
$<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
)
# As BL2 does not use hardware FPU, specify '--fpu=SoftVFP' explicitly to use software
# library functions for BL2 to override any implicit FPU option, such as '--cpu' option.
# Because the implicit hardware FPU option enforces BL2 to initialize FPU but hardware FPU
# is not actually enabled in BL2, it will cause BL2 runtime fault.
set(BL2_LINKER_CP_OPTION --fpu=SoftVFP)
set(BL1_COMPILER_CP_FLAG
$<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
$<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
)
set(BL1_LINKER_CP_OPTION --fpu=SoftVFP)
if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
set(COMPILER_CP_FLAG
$<$<COMPILE_LANGUAGE:C>:-mfloat-abi=hard>
)
if (CONFIG_TFM_ENABLE_FP)
set(COMPILER_CP_FLAG
$<$<COMPILE_LANGUAGE:C>:-mfpu=${CONFIG_TFM_FP_ARCH};-mfloat-abi=hard>
$<$<COMPILE_LANGUAGE:ASM>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}>
)
# armasm and armlink have the same option "--fpu" and are both used to
# specify the target FPU architecture. So the supported FPU architecture
# names can be shared by armasm and armlink.
set(LINKER_CP_OPTION --fpu=${CONFIG_TFM_FP_ARCH_ASM})
endif()
else()
set(COMPILER_CP_FLAG
$<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
$<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
)
set(LINKER_CP_OPTION --fpu=SoftVFP)
endif()
# 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")
#
# Pointer Authentication Code and Branch Target Identification (PACBTI) Options
#
if (${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_NONE)
set(BRANCH_PROTECTION_OPTIONS "none")
elseif(${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_STANDARD)
set(BRANCH_PROTECTION_OPTIONS "standard")
elseif(${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_PACRET)
set(BRANCH_PROTECTION_OPTIONS "pac-ret")
elseif(${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_PACRET_LEAF)
set(BRANCH_PROTECTION_OPTIONS "pac-ret+leaf")
elseif(${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_BTI)
set(BRANCH_PROTECTION_OPTIONS "bti")
endif()
if(NOT ${CONFIG_TFM_BRANCH_PROTECTION_FEAT} STREQUAL BRANCH_PROTECTION_DISABLED)
if(CMAKE_C_COMPILER_VERSION VERSION_LESS 6.18)
message(FATAL_ERROR "Your compiler does not support BRANCH_PROTECTION")
else()
if((TFM_SYSTEM_PROCESSOR MATCHES "cortex-m85") AND
(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main"))
message(NOTICE "BRANCH_PROTECTION enabled with: ${BRANCH_PROTECTION_OPTIONS}")
string(APPEND CMAKE_C_FLAGS " -mbranch-protection=${BRANCH_PROTECTION_OPTIONS}")
string(APPEND CMAKE_CXX_FLAGS " -mbranch-protection=${BRANCH_PROTECTION_OPTIONS}")
add_link_options(--library_security=pacbti-m)
else()
message(FATAL_ERROR "Your architecture does not support BRANCH_PROTECTION")
endif()
endif()
endif()
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_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()
add_dependencies(${target}
${target}_scatter
)
set_property(TARGET ${target} APPEND PROPERTY 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
-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(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()
# strip all the symbols except those provided as arguments. Long inline
# python scripts aren't ideal, but this is both portable and possibly easier
# to maintain than trying to filter files at build time in cmake.
add_custom_target(${target}_shared_symbols
VERBATIM
COMMAND python3
-c "from sys import argv; import re; f = open(argv[1], 'rt'); p = [x.replace('*', '.*') for x in argv[2:]]; l = [x for x in f.readlines() if re.search(r'(?=('+'$|'.join(p + ['SYMDEFS']) + r'))', x)]; f.close(); f = open(argv[1], 'wt'); f.writelines(l); f.close();"
$<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX}
${KEEP_SYMBOL_LIST}
)
# Ensure ${target} is build before $<TARGET_FILE:${target}> is used to generate ${target}_shared_symbols
add_dependencies(${target}_shared_symbols ${target})
# Allow the global clean target to rm the ${target}_shared_symbols created
set_target_properties(${target}_shared_symbols PROPERTIES
ADDITIONAL_CLEAN_FILES $<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX}
)
# Force the target to not remove the symbols if they're unused.
list(TRANSFORM KEEP_SYMBOL_LIST PREPEND --undefined=)
target_link_options(${target}
PRIVATE
${KEEP_SYMBOL_LIST}
# This is needed because the symbol file can contain functions
# that are not defined in every build configuration.
# The L6474E is:
# "Symbol referenced by --undefined or --undefined_and_export
# switch could not be resolved by a static library."
--diag_warning 6474
)
# Ask armclang to produce a symdefs file
target_link_options(${target}
PRIVATE
--symdefs=$<TARGET_FILE_DIR:${target}>/${target}${CODE_SHARING_OUTPUT_FILE_SUFFIX}
)
endmacro()
macro(target_link_shared_code target)
get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR)
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()
# Ensure ${symbol_provider}_shared_symbols is built before ${target}
add_dependencies(${target} ${symbol_provider}_shared_symbols)
# ${symbol_provider}_shared_symbols - a custom target is always considered out-of-date
# To only link when necessary, depend on ${symbol_provider} instead
set_property(TARGET ${target} APPEND PROPERTY LINK_DEPENDS $<TARGET_OBJECTS:${symbol_provider}>)
target_link_options(${target} PRIVATE LINKER:$<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=)
# TODO we assume that arm-none-eabi-objcopy is available - since we're using
# armclang this isn't necessarily true.
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=)
# TODO we assume that arm-none-eabi-objcopy is available - since we're using
# armclang this isn't necessarily true.
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=)
# TODO we assume that arm-none-eabi-objcopy is available - since we're using
# armclang this isn't necessarily true.
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=)
# TODO we assume that arm-none-eabi-objcopy is available - since we're using
# armclang this isn't necessarily true.
add_custom_command(
TARGET ${target}
PRE_LINK
COMMAND ${CMAKE_OBJCOPY}
ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
)
endmacro()