blob: c5d930ca57edade5d30db5dfe793bb204077bb40 [file] [log] [blame]
Anton Komlevaee4b612023-05-14 17:38:36 +01001#-------------------------------------------------------------------------------
Jackson Cooper-Driver070a6e42025-03-18 17:03:56 +00002# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
Anton Komlevaee4b612023-05-14 17:38:36 +01003#
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
David Hu9e1b2632023-10-14 13:35:24 +080013if(NOT DEFINED CROSS_COMPILE)
14 set(CROSS_COMPILE arm-none-eabi CACHE STRING "Cross-compiler prefix")
15endif()
16
Anton Komlevaee4b612023-05-14 17:38:36 +010017find_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)
Anton Komlevdd0a7222023-09-19 18:49:39 +010032
David Hu9af4d7d2023-11-01 07:52:17 +080033# 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
David Hu681a8ee2023-10-13 14:40:02 +080039# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
40EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
41
Jianliang Shenbd624ed2023-10-24 15:42:59 +080042# ===================== Set toolchain CPU and Arch =============================
David Hu681a8ee2023-10-13 14:40:02 +080043
44if (DEFINED TFM_SYSTEM_PROCESSOR)
Dávid Házif0d23a62023-11-07 11:07:31 +010045 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.")
David Hu681a8ee2023-10-13 14:40:02 +080049 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',
Dávid Házi50c18282024-10-10 12:37:28 +000058 # 'cortex-m7', 'cortex-m33', 'cortex-m35p', 'cortex-m55' and 'cortex-m85'.
David Hu681a8ee2023-10-13 14:40:02 +080059 # 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"
Dávid Házi50c18282024-10-10 12:37:28 +000067 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"
68 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85"))
David Hu681a8ee2023-10-13 14:40:02 +080069 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
70 endif()
71 endif()
72
73 if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
74 if(NOT CONFIG_TFM_ENABLE_MVE)
75 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
76 endif()
77 if(NOT CONFIG_TFM_ENABLE_MVE_FP)
78 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve.fp")
79 endif()
80 endif()
81 endif()
82
83endif()
84
85# CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
86# set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
87# The variable name is choosen to align with the ARMCLANG toolchain file.
88set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
89
90if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
91 if(CONFIG_TFM_ENABLE_MVE)
92 string(APPEND CMAKE_SYSTEM_ARCH "+mve")
93 endif()
94 if(CONFIG_TFM_ENABLE_MVE_FP)
95 string(APPEND CMAKE_SYSTEM_ARCH "+mve.fp")
96 endif()
97endif()
98
99if (DEFINED TFM_SYSTEM_DSP)
100 # +nodsp modifier is only supported from GCC version 8.
101 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
102 # armv8.1-m.main arch does not have +nodsp option
103 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
104 NOT TFM_SYSTEM_DSP)
105 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
106 endif()
107 endif()
108endif()
109
110if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
111 if(CONFIG_TFM_ENABLE_FP)
112 string(APPEND CMAKE_SYSTEM_ARCH "+fp")
113 endif()
114endif()
115
116if (GCC_VERSION VERSION_LESS 7.3.1)
117 message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
118endif()
119
120if (GCC_VERSION VERSION_EQUAL 10.2.1)
121 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
122 " Select other GNU Arm compiler versions instead."
123 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
124endif()
125
126# GNU Arm compiler version greater equal than *11.3.Rel1*
127# has a linker issue that required system calls are missing,
128# such as _read and _write. Add stub functions of required
129# system calls to solve this issue.
Dávid Házi50c18282024-10-10 12:37:28 +0000130#
131# READONLY linker script attribute is not supported in older
132# GNU Arm compilers. For these version the preprocessor will
133# remove the READONLY string from the linker scripts.
David Hu681a8ee2023-10-13 14:40:02 +0800134if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1)
135 set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
Dávid Házi50c18282024-10-10 12:37:28 +0000136 set(CONFIG_GNU_LINKER_READONLY_ATTRIBUTE TRUE)
David Hu681a8ee2023-10-13 14:40:02 +0800137endif()
138
139if (CMAKE_SYSTEM_PROCESSOR)
140 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
141 set(CMAKE_CXX_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
142 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
143 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
144 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
145else()
146 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
147 set(CMAKE_CXX_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
148 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
149 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
150 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
151endif()
152
153set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
154set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_INIT})
155set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
156
Jianliang Shenbd624ed2023-10-24 15:42:59 +0800157set(COMPILER_CP_FLAG "-mfloat-abi=soft")
158set(LINKER_CP_OPTION "-mfloat-abi=soft")
David Hu681a8ee2023-10-13 14:40:02 +0800159
Jianliang Shenbd624ed2023-10-24 15:42:59 +0800160if(CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
161 set(COMPILER_CP_FLAG "-mfloat-abi=hard")
162 set(LINKER_CP_OPTION "-mfloat-abi=hard")
163 if(CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP)
164 string(APPEND COMPILER_CP_FLAG " " "-mfpu=${CONFIG_TFM_FP_ARCH}")
165 string(APPEND LINKER_CP_OPTION " " "-mfpu=${CONFIG_TFM_FP_ARCH}")
David Hu681a8ee2023-10-13 14:40:02 +0800166 endif()
David Hu681a8ee2023-10-13 14:40:02 +0800167endif()
168
Jianliang Shenbd624ed2023-10-24 15:42:59 +0800169string(APPEND CMAKE_C_FLAGS " " ${COMPILER_CP_FLAG})
Gergely Kovacs96cc0442024-12-03 14:05:22 +0000170string(APPEND CMAKE_CXX_FLAGS " " ${COMPILER_CP_FLAG})
Jianliang Shenbd624ed2023-10-24 15:42:59 +0800171string(APPEND CMAKE_ASM_FLAGS " " ${COMPILER_CP_FLAG})
172string(APPEND CMAKE_C_LINK_FLAGS " " ${LINKER_CP_OPTION})
Gergely Kovacs96cc0442024-12-03 14:05:22 +0000173string(APPEND CMAKE_CXX_LINK_FLAGS " " ${LINKER_CP_OPTION})
Jianliang Shenbd624ed2023-10-24 15:42:59 +0800174string(APPEND CMAKE_ASM_LINK_FLAGS " " ${LINKER_CP_OPTION})
175
David Hu681a8ee2023-10-13 14:40:02 +0800176# For GNU Arm Embedded Toolchain doesn't emit __ARM_ARCH_8_1M_MAIN__, adding this macro manually.
Dávid Házie6dadd12024-04-25 17:50:52 +0200177add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__=1>)
Anton Komlevaee4b612023-05-14 17:38:36 +0100178
179add_compile_options(
180 -specs=nano.specs
Jamie Fox13feddb2024-01-23 18:58:42 +0000181 -specs=nosys.specs
Anton Komlevaee4b612023-05-14 17:38:36 +0100182 -Wall
183 -Wno-format
184 -Wno-return-type
185 -Wno-unused-but-set-variable
186 -c
187 -fdata-sections
188 -ffunction-sections
189 -fno-builtin
190 -fshort-enums
191 -funsigned-char
Jackson Cooper-Driver070a6e42025-03-18 17:03:56 +0000192 -fmacro-prefix-map=${CMAKE_SOURCE_DIR}/=
Anton Komlevaee4b612023-05-14 17:38:36 +0100193 -mthumb
Dávid Házi777dcbe2023-11-15 09:51:39 +0100194 $<$<COMPILE_LANGUAGE:C>:-std=c99>
195 $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
196 $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
197 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
Bohdan Hunkoc7d222b2024-11-05 16:37:21 +0200198 $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${CONFIG_TFM_WARNINGS_ARE_ERRORS}>>:-Werror>
Anton Komlevaee4b612023-05-14 17:38:36 +0100199)
200
201add_link_options(
202 --entry=Reset_Handler
203 -specs=nano.specs
Jamie Fox13feddb2024-01-23 18:58:42 +0000204 -specs=nosys.specs
Anton Komlevaee4b612023-05-14 17:38:36 +0100205 LINKER:-check-sections
206 LINKER:-fatal-warnings
207 LINKER:--gc-sections
208 LINKER:--no-wchar-size-warning
209 LINKER:--print-memory-usage
Anton Komlevaee4b612023-05-14 17:38:36 +0100210)
211
David Hu500ea2f2023-10-13 12:15:08 +0800212# Specify the linker script used to link `target`.
213# Behaviour for handling linker scripts is so wildly divergent between compilers
214# that this macro is required.
215#
216# Vendor platform can set a linker script as property INTERFACE_LINK_DEPENDS of platform_ns.
217# `target` can fetch the linker script from platform_ns.
218#
219# Alternatively, NS build can call target_add_scatter_file() with the install directory of
220# linker script.
221# target_add_scatter_file(target, install_dir)
222#
223# target_add_scatter_file() fetch a linker script from the install directory.
224macro(target_add_scatter_file target)
Anton Komlevaee4b612023-05-14 17:38:36 +0100225
David Hu500ea2f2023-10-13 12:15:08 +0800226 get_target_property(scatter_file
227 platform_ns
228 INTERFACE_LINK_DEPENDS
Anton Komlevaee4b612023-05-14 17:38:36 +0100229 )
230
David Hu500ea2f2023-10-13 12:15:08 +0800231 # If scatter_file is not passed from platform_ns
232 # Try if any linker script is exported in install directory
233 # The intall directory is passed as an optinal argument
234 if(${scatter_file} STREQUAL "scatter_file-NOTFOUND")
235 set(install_dir ${ARGN})
236 list(LENGTH install_dir nr_install_dir)
237
238 # If nr_install_dir == 1, search for sct file under install dir
239 if(${nr_install_dir} EQUAL 1)
240 file(GLOB scatter_file "${install_dir}/*.ld")
241 endif()
242 endif()
243
244 if(NOT EXISTS ${scatter_file})
245 message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}")
246 endif()
247
248 add_library(${target}_scatter OBJECT)
249 target_sources(${target}_scatter
250 PRIVATE
251 ${scatter_file}
252 )
253
254 set_source_files_properties(${scatter_file} PROPERTIES
Anton Komlevaee4b612023-05-14 17:38:36 +0100255 LANGUAGE C
256 KEEP_EXTENSION True)
257
David Hu500ea2f2023-10-13 12:15:08 +0800258 target_compile_options(${target}_scatter
Anton Komlevaee4b612023-05-14 17:38:36 +0100259 PRIVATE
260 -E
261 -P
262 -xc
263 )
264
Dávid Házi80427ea2024-04-17 21:07:17 +0200265 target_compile_definitions(${target}_scatter
266 PRIVATE
267 $<$<NOT:$<BOOL:${CONFIG_GNU_LINKER_READONLY_ATTRIBUTE}>>:READONLY=>
268 )
269
David Hu35aa1a52023-10-24 23:04:04 +0800270 target_link_libraries(${target}_scatter
271 PRIVATE
272 platform_region_defs
273 )
Anton Komlevaee4b612023-05-14 17:38:36 +0100274
David Hu500ea2f2023-10-13 12:15:08 +0800275 add_dependencies(${target} ${target}_scatter)
Anton Komlevaee4b612023-05-14 17:38:36 +0100276
David Hu99d82922023-10-29 13:35:51 +0800277 target_link_options(${target}
278 PRIVATE
279 -T $<TARGET_OBJECTS:${target}_scatter>
280 )
281
Anton Komlevaee4b612023-05-14 17:38:36 +0100282endmacro()
David Hu9e1b2632023-10-14 13:35:24 +0800283
284macro(add_convert_to_bin_target target)
285 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
286
287 add_custom_target(${target}_bin
288 SOURCES ${bin_dir}/${target}.bin
289 )
290 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
291 DEPENDS ${target}
292 COMMAND ${CMAKE_OBJCOPY}
293 -O binary $<TARGET_FILE:${target}>
294 ${bin_dir}/${target}.bin
295 )
296
297 add_custom_target(${target}_elf
298 SOURCES ${bin_dir}/${target}.elf
299 )
300 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
301 DEPENDS ${target}
302 COMMAND ${CMAKE_OBJCOPY}
303 -O elf32-littlearm $<TARGET_FILE:${target}>
304 ${bin_dir}/${target}.elf
305 )
306
307 add_custom_target(${target}_hex
308 SOURCES ${bin_dir}/${target}.hex
309 )
310 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
311 DEPENDS ${target}
312 COMMAND ${CMAKE_OBJCOPY}
313 -O ihex $<TARGET_FILE:${target}>
314 ${bin_dir}/${target}.hex
315 )
316
317 add_custom_target(${target}_binaries
318 ALL
319 DEPENDS ${target}_bin
320 DEPENDS ${target}_elf
321 DEPENDS ${target}_hex
322 )
323endmacro()