1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
4# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
5#
6# SPDX-License-Identifier: BSD-3-Clause
7#
8#-------------------------------------------------------------------------------
9cmake_minimum_required(VERSION 3.15)
10
11SET(CMAKE_SYSTEM_NAME Generic)
12
13set(CMAKE_C_COMPILER armclang)
14set(CMAKE_CXX_COMPILER armclang)
15set(CMAKE_ASM_COMPILER armasm)
16
17set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out=)
18set(COMPILER_CMSE_FLAG $<$<COMPILE_LANGUAGE:C>:-mcmse>)
19
20# This variable name is a bit of a misnomer. The file it is set to is included
21# at a particular step in the compiler initialisation. It is used here to
22# configure the extensions for object files. Despite the name, it also works
23# with the Ninja generator.
24set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
25
26if(NOT DEFINED CMAKE_OBJCOPY)
27    set(CMAKE_OBJCOPY ${CROSS_COMPILE}-objcopy CACHE FILEPATH "Path to objcopy")
28endif()
29
30macro(tfm_toolchain_reset_compiler_flags)
31    set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
32
33    add_compile_options(
34        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-ignored-optimization-argument>
35        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unused-command-line-argument>
36        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wall>
37        # Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
38        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-error=cpp>
39        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-c>
40        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fdata-sections>
41        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-ffunction-sections>
42        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fno-builtin>
43        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-enums>
44        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fshort-wchar>
45        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-funsigned-char>
46        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-masm=auto>
47        $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-nostdlib>
48        $<$<COMPILE_LANGUAGE:C>:-std=c99>
49        $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
50        $<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}>
51        $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
52        $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
53    )
54endmacro()
55
56if(CONFIG_TFM_MEMORY_USAGE_QUIET)
57    set(MEMORY_USAGE_FLAG "")
58else()
59    set(MEMORY_USAGE_FLAG --info=summarysizes,sizes,totals,unused,veneers)
60endif()
61
62macro(tfm_toolchain_reset_linker_flags)
63    set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
64
65    add_link_options(
66        ${MEMORY_USAGE_FLAG}
67        --strict
68        --symbols
69        --xref
70        $<$<AND:$<VERSION_GREATER:${TFM_ISOLATION_LEVEL},1>,$<STREQUAL:"${TEST_PSA_API}","IPC">>:--no-merge>
71        # Suppress link warnings that are consistant (and therefore hopefully
72        # harmless)
73        # https://developer.arm.com/documentation/100074/0608/linker-errors-and-warnings/list-of-the-armlink-error-and-warning-messages
74        # Empty region description
75        --diag_suppress=6312
76        # Ns section matches pattern
77        --diag_suppress=6314
78        # Duplicate input files
79        --diag_suppress=6304
80        # Pattern only matches removed unused sections.
81        --diag_suppress=6329
82    )
83endmacro()
84
85macro(tfm_toolchain_set_processor_arch)
86    if (DEFINED TFM_SYSTEM_PROCESSOR)
87        set(CMAKE_SYSTEM_PROCESSOR       ${TFM_SYSTEM_PROCESSOR})
88
89        if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
90            message(WARNING "MVE is not yet supported using ARMCLANG")
91            string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
92        endif()
93
94        if (DEFINED TFM_SYSTEM_DSP)
95            if(NOT TFM_SYSTEM_DSP)
96                string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
97            endif()
98        endif()
99
100        # ARMCLANG specifies that '+nofp' is available on following M-profile cpus:
101        # 'cortex-m4', 'cortex-m7', 'cortex-m33', 'cortex-m35p', 'cortex-m55' and 'cortex-m85'.
102        # Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
103        # Explicitly list those cpu to align with ARMCLANG description.
104        if (NOT CONFIG_TFM_FLOAT_ABI STREQUAL "hard" AND
105            (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4"
106            OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7"
107            OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33"
108            OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p"
109            OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"
110            OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85"))
111                string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
112        endif()
113
114        string(REGEX REPLACE "\\+nodsp" ".no_dsp" CMAKE_ASM_CPU_FLAG "${CMAKE_SYSTEM_PROCESSOR}")
115        string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
116        string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
117    else()
118        set(CMAKE_ASM_CPU_FLAG  ${TFM_SYSTEM_ARCHITECTURE})
119
120        # Armasm uses different syntax than armclang for architecture targets
121        string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
122        string(REGEX REPLACE "\\armv" "" CMAKE_ASM_CPU_FLAG "${CMAKE_ASM_CPU_FLAG}")
123
124        # Modifiers are additive instead of subtractive (.fp Vs .no_fp)
125        if (TFM_SYSTEM_DSP)
126            string(APPEND CMAKE_ASM_CPU_FLAG ".dsp")
127        endif()
128
129        if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
130            string(APPEND CMAKE_ASM_CPU_FLAG ".fp")
131        endif()
132    endif()
133
134    # CMAKE_SYSTEM_ARCH is an ARMCLANG CMAKE internal variable, used to set
135    # compile and linker flags up until CMake 3.21 where CMP0123 was introduced:
136    # https://cmake.org/cmake/help/latest/policy/CMP0123.html
137    # This behavior is overwritten by setting CMAKE_C_FLAGS in
138    # tfm_toolchain_reload_compiler.
139    # Another use of this variable is to statisfy a requirement for ARMCLANG to
140    # set either the target CPU or the Architecture. This variable needs to be
141    # set to allow targeting architectures without a specific CPU.
142    set(CMAKE_SYSTEM_ARCH            ${TFM_SYSTEM_ARCHITECTURE})
143
144    set(CMAKE_C_COMPILER_TARGET      arm-${CROSS_COMPILE})
145    set(CMAKE_CXX_COMPILER_TARGET    arm-${CROSS_COMPILE})
146    set(CMAKE_ASM_COMPILER_TARGET    arm-${CROSS_COMPILE})
147
148    # MVE is currently not supported in case of armclang
149    if (TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
150        string(APPEND CMAKE_SYSTEM_ARCH "+nomve")
151    endif()
152
153    if (DEFINED TFM_SYSTEM_DSP)
154        if(NOT TFM_SYSTEM_DSP)
155            string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
156        endif()
157    endif()
158
159    # Cmake's ARMClang support has several issues with compiler validation. To
160    # avoid these, we set the list of supported -mcpu and -march variables to
161    # the ones we intend to use so that the validation will never fail.
162    include(Compiler/ARMClang)
163    set(CMAKE_C_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
164    set(CMAKE_C_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
165    set(CMAKE_CXX_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
166    set(CMAKE_CXX_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
167    set(CMAKE_ASM_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR})
168    set(CMAKE_ASM_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_ARCH})
169endmacro()
170
171macro(tfm_toolchain_reload_compiler)
172    tfm_toolchain_set_processor_arch()
173    tfm_toolchain_reset_compiler_flags()
174    tfm_toolchain_reset_linker_flags()
175
176    unset(CMAKE_C_FLAGS_INIT)
177    unset(CMAKE_CXX_FLAGS_INIT)
178    unset(CMAKE_C_LINK_FLAGS)
179    unset(CMAKE_ASM_FLAGS_INIT)
180    unset(CMAKE_ASM_LINK_FLAGS)
181    unset(__mcpu_flag_set)
182    unset(__march_flag_set)
183
184    include(Compiler/ARMClang)
185    __compiler_armclang(C)
186    include(Compiler/ARMCC-ASM)
187    __compiler_armcc(ASM)
188
189    if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6.13)
190        message(FATAL_ERROR "Please select newer Arm compiler version starting from 6.13.")
191    endif()
192
193    if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.15 AND
194        CMAKE_C_COMPILER_VERSION VERSION_LESS 6.18)
195        message(FATAL_ERROR "Armclang 6.15~6.17 may cause MemManage fault."
196                            " This defect has been fixed since Armclang 6.18."
197                            " See [SDCOMP-59788] in Armclang 6.18 release note for details."
198                            " Please use other Armclang versions instead.")
199    endif()
200
201    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
202
203    if (DEFINED TFM_SYSTEM_PROCESSOR)
204        set(CMAKE_C_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
205        set(CMAKE_C_LINK_FLAGS   "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
206        set(CMAKE_CXX_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
207        set(CMAKE_ASM_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}")
208        # But armlink doesn't support this +dsp syntax
209        string(REGEX REPLACE "\\+nodsp" "" CMAKE_C_LINK_FLAGS   "${CMAKE_C_LINK_FLAGS}")
210        string(REGEX REPLACE "\\+nodsp" "" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
211        string(REGEX REPLACE "\\+nodsp" "" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
212        # And uses different syntax for +nofp
213        string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_C_LINK_FLAGS   "${CMAKE_C_LINK_FLAGS}")
214        string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
215        string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
216
217        string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_C_LINK_FLAGS   "${CMAKE_C_LINK_FLAGS}")
218        string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
219        string(REGEX REPLACE "\\+nomve" ".no_mve" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
220    else()
221        set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
222        set(CMAKE_CXX_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
223    endif()
224
225    set(BL2_COMPILER_CP_FLAG
226        $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
227        $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
228    )
229    # As BL2 does not use hardware FPU, specify '--fpu=SoftVFP' explicitly to use software
230    # library functions for BL2 to override any implicit FPU option, such as '--cpu' option.
231    # Because the implicit hardware FPU option enforces BL2 to initialize FPU but hardware FPU
232    # is not actually enabled in BL2, it will cause BL2 runtime fault.
233    set(BL2_LINKER_CP_OPTION --fpu=SoftVFP)
234
235    if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
236        set(COMPILER_CP_FLAG
237            $<$<COMPILE_LANGUAGE:C>:-mfloat-abi=hard>
238        )
239        if (CONFIG_TFM_ENABLE_FP)
240            set(COMPILER_CP_FLAG
241                $<$<COMPILE_LANGUAGE:C>:-mfpu=${CONFIG_TFM_FP_ARCH};-mfloat-abi=hard>
242                $<$<COMPILE_LANGUAGE:ASM>:--fpu=${CONFIG_TFM_FP_ARCH_ASM}>
243            )
244            # armasm and armlink have the same option "--fpu" and are both used to
245            # specify the target FPU architecture. So the supported FPU architecture
246            # names can be shared by armasm and armlink.
247            set(LINKER_CP_OPTION --fpu=${CONFIG_TFM_FP_ARCH_ASM})
248        endif()
249    else()
250        set(COMPILER_CP_FLAG
251            $<$<COMPILE_LANGUAGE:C>:-mfpu=softvfp>
252            $<$<COMPILE_LANGUAGE:ASM>:--fpu=softvfp>
253        )
254        set(LINKER_CP_OPTION --fpu=SoftVFP)
255    endif()
256
257    # Workaround for issues with --depend-single-line with armasm and Ninja
258    if (CMAKE_GENERATOR STREQUAL "Ninja")
259        set( CMAKE_DEPFILE_FLAGS_ASM "--depend=<OBJECT>.d")
260    endif()
261
262    set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG")
263endmacro()
264
265# Configure environment for the compiler setup run by cmake at the first
266# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
267# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
268tfm_toolchain_set_processor_arch()
269tfm_toolchain_reset_compiler_flags()
270tfm_toolchain_reset_linker_flags()
271
272# Behaviour for handling scatter files is so wildly divergent between compilers
273# that this macro is required.
274macro(target_add_scatter_file target)
275    target_link_options(${target}
276        PRIVATE
277        --scatter=$<TARGET_OBJECTS:${target}_scatter>
278    )
279
280    add_library(${target}_scatter OBJECT)
281    foreach(scatter_file ${ARGN})
282        target_sources(${target}_scatter
283            PRIVATE
284                ${scatter_file}
285        )
286        # Cmake cannot use generator expressions in the
287        # set_source_file_properties command, so instead we just parse the regex
288        # for the filename and set the property on all files, regardless of if
289        # the generator expression would evaluate to true or not.
290        string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
291        set_source_files_properties(${SCATTER_FILE_PATH}
292            PROPERTIES
293            LANGUAGE C
294        )
295    endforeach()
296
297    add_dependencies(${target}
298        ${target}_scatter
299    )
300
301    set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
302
303    target_link_libraries(${target}_scatter
304        platform_region_defs
305        psa_interface
306        tfm_config
307    )
308
309    target_compile_options(${target}_scatter
310        PRIVATE
311            -E
312            -xc
313    )
314endmacro()
315
316macro(add_convert_to_bin_target target)
317    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
318
319    add_custom_target(${target}_bin
320        SOURCES ${bin_dir}/${target}.bin
321    )
322    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
323        DEPENDS ${target}
324        COMMAND fromelf
325            --bincombined $<TARGET_FILE:${target}>
326            --output=${bin_dir}/${target}.bin
327    )
328
329    add_custom_target(${target}_elf
330        SOURCES ${bin_dir}/${target}.elf
331    )
332    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
333        DEPENDS ${target}
334        COMMAND fromelf
335            --elf $<TARGET_FILE:${target}>
336            --output=${bin_dir}/${target}.elf
337    )
338
339    add_custom_target(${target}_hex
340        SOURCES ${bin_dir}/${target}.hex
341    )
342    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
343        DEPENDS ${target}
344        COMMAND fromelf
345            --i32combined $<TARGET_FILE:${target}>
346            --output=${bin_dir}/${target}.hex
347    )
348
349    add_custom_target(${target}_binaries
350        ALL
351        DEPENDS ${target}_bin
352        DEPENDS ${target}_elf
353        DEPENDS ${target}_hex
354    )
355endmacro()
356
357macro(target_share_symbols target symbol_name_file)
358    get_target_property(TARGET_TYPE ${target} TYPE)
359    if (NOT TARGET_TYPE STREQUAL "EXECUTABLE")
360        message(FATAL_ERROR "${target} is not an executable. Symbols cannot be shared from libraries.")
361    endif()
362
363    FILE(STRINGS ${symbol_name_file} KEEP_SYMBOL_LIST
364        LENGTH_MINIMUM 1
365    )
366
367    # strip all the symbols except those proveded as arguments. Long inline
368    # python scripts aren't ideal, but this is both portable and possibly easier
369    # to maintain than trying to filter files at build time in cmake.
370    add_custom_command(TARGET ${target}
371        POST_BUILD
372        VERBATIM
373        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}_shared_symbols.txt ${KEEP_SYMBOL_LIST})
374
375    # Force the target to not remove the symbols if they're unused.
376    list(TRANSFORM KEEP_SYMBOL_LIST PREPEND --undefined=)
377    target_link_options(${target}
378        PRIVATE
379            ${KEEP_SYMBOL_LIST}
380    )
381
382    # Ask armclang to produce a symdefs file that will
383    target_link_options(${target}
384        PRIVATE
385            --symdefs=$<TARGET_FILE_DIR:${target}>/${target}_shared_symbols.txt
386    )
387endmacro()
388
389macro(target_link_shared_code target)
390    get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR)
391
392    foreach(symbol_provider ${ARGN})
393        if (TARGET ${symbol_provider})
394            get_target_property(SYMBOL_PROVIDER_TYPE ${symbol_provider} TYPE)
395            if (NOT SYMBOL_PROVIDER_TYPE STREQUAL "EXECUTABLE")
396                message(FATAL_ERROR "${symbol_provider} is not an executable. Symbols cannot be shared from libraries.")
397            endif()
398        endif()
399
400        add_dependencies(${target} ${symbol_provider})
401        # Some cmake functions don't allow generator expressions, so get the
402        # property as a backup. If the symbol provider hasn't been created yet,
403        # then use the output dir of the target.
404        if (TARGET ${symbol_provider})
405            get_target_property(SYMBOL_PROVIDER_OUTPUT_DIR ${symbol_provider} RUNTIME_OUTPUT_DIRECTORY)
406        else()
407            get_target_property(SYMBOL_PROVIDER_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY)
408        endif()
409        # Set these properties so that cmake will allow us to use a source file
410        # that doesn't exist at cmake-time, but we guarantee will exist at
411        # compile-time.
412        set_source_files_properties(${SYMBOL_PROVIDER_OUTPUT_DIR}/${symbol_provider}_shared_symbols.txt
413            DIRECTORY ${TARGET_SOURCE_DIR}
414            PROPERTIES
415                EXTERNAL_OBJECT true
416                GENERATED true
417        )
418        target_sources(${target}
419            PRIVATE
420                $<TARGET_FILE_DIR:${symbol_provider}>/${symbol_provider}_shared_symbols.txt
421        )
422    endforeach()
423endmacro()
424
425macro(target_strip_symbols target)
426    set(SYMBOL_LIST "${ARGN}")
427    list(TRANSFORM SYMBOL_LIST PREPEND  --strip-symbol=)
428
429    # TODO we assume that arm-none-eabi-objcopy is available - since we're using
430    # armclang this isn't necessarily true.
431    add_custom_command(
432        TARGET ${target}
433        POST_BUILD
434        COMMAND ${CMAKE_OBJCOPY}
435        ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
436    )
437endmacro()
438
439macro(target_strip_symbols_from_dependency target dependency)
440    set(SYMBOL_LIST "${ARGN}")
441    list(TRANSFORM SYMBOL_LIST PREPEND  --strip-symbol=)
442
443    # TODO we assume that arm-none-eabi-objcopy is available - since we're using
444    # armclang this isn't necessarily true.
445    add_custom_command(
446        TARGET ${target}
447        PRE_LINK
448        COMMAND ${CMAKE_OBJCOPY}
449        ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
450    )
451endmacro()
452
453macro(target_weaken_symbols target)
454    set(SYMBOL_LIST "${ARGN}")
455    list(TRANSFORM SYMBOL_LIST PREPEND  --weaken-symbol=)
456
457    # TODO we assume that arm-none-eabi-objcopy is available - since we're using
458    # armclang this isn't necessarily true.
459    add_custom_command(
460        TARGET ${target}
461        POST_BUILD
462        COMMAND ${CMAKE_OBJCOPY}
463        ARGS $<TARGET_FILE:${target}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${target}>
464    )
465endmacro()
466
467macro(target_weaken_symbols_from_dependency target dependency)
468    set(SYMBOL_LIST "${ARGN}")
469    list(TRANSFORM SYMBOL_LIST PREPEND  --weaken-symbol=)
470
471    # TODO we assume that arm-none-eabi-objcopy is available - since we're using
472    # armclang this isn't necessarily true.
473    add_custom_command(
474        TARGET ${target}
475        PRE_LINK
476        COMMAND ${CMAKE_OBJCOPY}
477        ARGS $<TARGET_FILE:${dependency}> --wildcard ${SYMBOL_LIST} $<TARGET_FILE:${dependency}>
478    )
479endmacro()
480