1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2024, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8set(CMAKE_SYSTEM_NAME Generic)
9
10set(CMAKE_C_COMPILER_FORCED TRUE)
11set(CMAKE_CXX_COMPILER_FORCED TRUE)
12
13if(NOT DEFINED CROSS_COMPILE)
14    set(CROSS_COMPILE    arm-none-eabi CACHE STRING "Cross-compiler prefix")
15endif()
16
17find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
18find_program(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
19
20if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND")
21    message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'")
22endif()
23
24if(CMAKE_CXX_COMPILER STREQUAL "CMAKE_CXX_COMPILER-NOTFOUND")
25    message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-g++'")
26endif()
27
28set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
29
30# Set compiler ID explicitly as it's not detected at this moment
31set(CMAKE_C_COMPILER_ID GNU)
32
33# This variable name is a bit of a misnomer. The file it is set to is included
34# at a particular step in the compiler initialisation. It is used here to
35# configure the extensions for object files. Despite the name, it also works
36# with the Ninja generator.
37set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/set_extensions.cmake)
38
39# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
40EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
41
42# ===================== Set toolchain CPU and Arch =============================
43
44if (DEFINED TFM_SYSTEM_PROCESSOR)
45    if(TFM_SYSTEM_PROCESSOR MATCHES "cortex-m85" AND GCC_VERSION VERSION_LESS "13.0.0")
46        # GNUARM until version 13 does not support the -mcpu=cortex-m85 flag
47        message(WARNING "Cortex-m85 is only supported from GCC13. "
48                        "Falling back to -march usage for earlier versions.")
49    else()
50        set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
51
52        if (DEFINED TFM_SYSTEM_DSP)
53            if (NOT TFM_SYSTEM_DSP)
54                string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
55            endif()
56        endif()
57        # GCC specifies that '+nofp' is available on following M-profile cpus: 'cortex-m4',
58        # 'cortex-m7', 'cortex-m33', 'cortex-m35p' and 'cortex-m55'.
59        # Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
60        # Explicitly list those cpu to align with GCC description.
61        if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
62            if(NOT CONFIG_TFM_ENABLE_FP AND
63                (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4"
64                OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7"
65                OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33"
66                OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p"
67                OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"))
68                    string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
69            endif()
70        endif()
71
72        if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
73            if(NOT CONFIG_TFM_ENABLE_MVE)
74                string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
75            endif()
76            if(NOT CONFIG_TFM_ENABLE_MVE_FP)
77                string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve.fp")
78            endif()
79        endif()
80    endif()
81
82endif()
83
84# CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
85# set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
86# The variable name is choosen to align with the ARMCLANG toolchain file.
87set(CMAKE_SYSTEM_ARCH         ${TFM_SYSTEM_ARCHITECTURE})
88
89if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
90    if(CONFIG_TFM_ENABLE_MVE)
91        string(APPEND CMAKE_SYSTEM_ARCH "+mve")
92    endif()
93    if(CONFIG_TFM_ENABLE_MVE_FP)
94        string(APPEND CMAKE_SYSTEM_ARCH "+mve.fp")
95    endif()
96endif()
97
98if (DEFINED TFM_SYSTEM_DSP)
99    # +nodsp modifier is only supported from GCC version 8.
100    if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
101        # armv8.1-m.main arch does not have +nodsp option
102        if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
103            NOT TFM_SYSTEM_DSP)
104            string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
105        endif()
106    endif()
107endif()
108
109if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
110    if(CONFIG_TFM_ENABLE_FP)
111        string(APPEND CMAKE_SYSTEM_ARCH "+fp")
112    endif()
113endif()
114
115if (GCC_VERSION VERSION_LESS 7.3.1)
116    message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
117endif()
118
119if (GCC_VERSION VERSION_EQUAL 10.2.1)
120    message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
121                        " Select other GNU Arm compiler versions instead."
122                        " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
123endif()
124
125# GNU Arm compiler version greater equal than *11.3.Rel1*
126# has a linker issue that required system calls are missing,
127# such as _read and _write. Add stub functions of required
128# system calls to solve this issue.
129if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1)
130    set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
131endif()
132
133if (CMAKE_SYSTEM_PROCESSOR)
134    set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
135    set(CMAKE_CXX_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
136    set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
137    set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
138    set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
139else()
140    set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
141    set(CMAKE_CXX_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
142    set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
143    set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
144    set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
145endif()
146
147set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
148set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_INIT})
149set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
150
151set(COMPILER_CP_FLAG "-mfloat-abi=soft")
152set(LINKER_CP_OPTION "-mfloat-abi=soft")
153
154if(CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
155    set(COMPILER_CP_FLAG "-mfloat-abi=hard")
156    set(LINKER_CP_OPTION "-mfloat-abi=hard")
157    if(CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP)
158        string(APPEND COMPILER_CP_FLAG " " "-mfpu=${CONFIG_TFM_FP_ARCH}")
159        string(APPEND LINKER_CP_OPTION " " "-mfpu=${CONFIG_TFM_FP_ARCH}")
160    endif()
161endif()
162
163string(APPEND CMAKE_C_FLAGS " " ${COMPILER_CP_FLAG})
164string(APPEND CMAKE_ASM_FLAGS " " ${COMPILER_CP_FLAG})
165string(APPEND CMAKE_C_LINK_FLAGS " " ${LINKER_CP_OPTION})
166string(APPEND CMAKE_ASM_LINK_FLAGS " " ${LINKER_CP_OPTION})
167
168# For GNU Arm Embedded Toolchain doesn't emit __ARM_ARCH_8_1M_MAIN__, adding this macro manually.
169add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__=1>)
170
171# GNU Arm compiler version greater equal than *11.3.Rel1*
172# has a linker issue that required system calls are missing,
173# such as _read and _write. Add stub functions of required
174# system calls to solve this issue.
175#
176# READONLY linker script attribute is not supported in older
177# GNU Arm compilers. For these version the preprocessor will
178# remove the READONLY string from the linker scripts.
179if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1)
180    set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
181    set(CONFIG_GNU_LINKER_READONLY_ATTRIBUTE TRUE)
182endif()
183
184add_compile_options(
185    -specs=nano.specs
186    -specs=nosys.specs
187    -Wall
188    -Wno-format
189    -Wno-return-type
190    -Wno-unused-but-set-variable
191    -c
192    -fdata-sections
193    -ffunction-sections
194    -fno-builtin
195    -fshort-enums
196    -funsigned-char
197    -mthumb
198    $<$<COMPILE_LANGUAGE:C>:-std=c99>
199    $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
200    $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
201    $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
202)
203
204add_link_options(
205    --entry=Reset_Handler
206    -specs=nano.specs
207    -specs=nosys.specs
208    LINKER:-check-sections
209    LINKER:-fatal-warnings
210    LINKER:--gc-sections
211    LINKER:--no-wchar-size-warning
212    LINKER:--print-memory-usage
213    LINKER:-Map=tfm_ns.map
214)
215
216# Specify the linker script used to link `target`.
217# Behaviour for handling linker scripts is so wildly divergent between compilers
218# that this macro is required.
219#
220# Vendor platform can set a linker script as property INTERFACE_LINK_DEPENDS of platform_ns.
221# `target` can fetch the linker script from platform_ns.
222#
223# Alternatively, NS build can call target_add_scatter_file() with the install directory of
224# linker script.
225#     target_add_scatter_file(target, install_dir)
226#
227# target_add_scatter_file() fetch a linker script from the install directory.
228macro(target_add_scatter_file target)
229
230    get_target_property(scatter_file
231                        platform_ns
232                        INTERFACE_LINK_DEPENDS
233    )
234
235    # If scatter_file is not passed from platform_ns
236    # Try if any linker script is exported in install directory
237    # The intall directory is passed as an optinal argument
238    if(${scatter_file} STREQUAL "scatter_file-NOTFOUND")
239        set(install_dir ${ARGN})
240        list(LENGTH install_dir nr_install_dir)
241
242        # If nr_install_dir == 1, search for sct file under install dir
243        if(${nr_install_dir} EQUAL 1)
244            file(GLOB scatter_file "${install_dir}/*.ld")
245        endif()
246    endif()
247
248    if(NOT EXISTS ${scatter_file})
249        message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}")
250    endif()
251
252    add_library(${target}_scatter OBJECT)
253    target_sources(${target}_scatter
254        PRIVATE
255            ${scatter_file}
256    )
257
258    set_source_files_properties(${scatter_file} PROPERTIES
259        LANGUAGE C
260        KEEP_EXTENSION True)
261
262    target_compile_options(${target}_scatter
263        PRIVATE
264            -E
265            -P
266            -xc
267    )
268
269    target_compile_definitions(${target}_scatter
270        PRIVATE
271            $<$<NOT:$<BOOL:${CONFIG_GNU_LINKER_READONLY_ATTRIBUTE}>>:READONLY=>
272    )
273
274    target_link_libraries(${target}_scatter
275        PRIVATE
276            platform_region_defs
277    )
278
279    add_dependencies(${target} ${target}_scatter)
280
281    target_link_options(${target}
282        PRIVATE
283            -T $<TARGET_OBJECTS:${target}_scatter>
284    )
285
286endmacro()
287
288macro(add_convert_to_bin_target target)
289    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
290
291    add_custom_target(${target}_bin
292        SOURCES ${bin_dir}/${target}.bin
293    )
294    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
295        DEPENDS ${target}
296        COMMAND ${CMAKE_OBJCOPY}
297            -O binary $<TARGET_FILE:${target}>
298            ${bin_dir}/${target}.bin
299    )
300
301    add_custom_target(${target}_elf
302        SOURCES ${bin_dir}/${target}.elf
303    )
304    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
305        DEPENDS ${target}
306        COMMAND ${CMAKE_OBJCOPY}
307            -O elf32-littlearm $<TARGET_FILE:${target}>
308            ${bin_dir}/${target}.elf
309    )
310
311    add_custom_target(${target}_hex
312        SOURCES ${bin_dir}/${target}.hex
313    )
314    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
315        DEPENDS ${target}
316        COMMAND ${CMAKE_OBJCOPY}
317            -O ihex $<TARGET_FILE:${target}>
318            ${bin_dir}/${target}.hex
319    )
320
321    add_custom_target(${target}_binaries
322        ALL
323        DEPENDS ${target}_bin
324        DEPENDS ${target}_elf
325        DEPENDS ${target}_hex
326    )
327endmacro()
328
329# A dummy macro to align with Armclang workaround
330macro(tfm_toolchain_reload_compiler)
331endmacro()
332