blob: fa5d5b52485b215760c07dd7cae26c08f76ec820 [file] [log] [blame]
Anton Komlevaee4b612023-05-14 17:38:36 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2023, 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
13find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
14find_program(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
15
16if(CMAKE_C_COMPILER STREQUAL "CMAKE_C_COMPILER-NOTFOUND")
17 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-gcc'")
18endif()
19
20if(CMAKE_CXX_COMPILER STREQUAL "CMAKE_CXX_COMPILER-NOTFOUND")
21 message(FATAL_ERROR "Could not find compiler: '${CROSS_COMPILE}-g++'")
22endif()
23
24set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
25
26# Set compiler ID explicitly as it's not detected at this moment
27set(CMAKE_C_COMPILER_ID GNU)
Anton Komlevdd0a7222023-09-19 18:49:39 +010028
David Hu681a8ee2023-10-13 14:40:02 +080029# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
30EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION )
31
32# ===================== SEt toolchain CPU and Arch =============================
33
34if (DEFINED TFM_SYSTEM_PROCESSOR)
35 if(TFM_SYSTEM_PROCESSOR MATCHES "cortex-m85")
36 # GNUARM does not support the -mcpu=cortex-m85 flag yet
37 # TODO: Remove this exception when the cortex-m85 support comes out.
38 message(WARNING "Cortex-m85 is not supported by GCC. Falling back to -march usage.")
39 else()
40 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
41
42 if (DEFINED TFM_SYSTEM_DSP)
43 if (NOT TFM_SYSTEM_DSP)
44 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
45 endif()
46 endif()
47 # GCC specifies that '+nofp' is available on following M-profile cpus: 'cortex-m4',
48 # 'cortex-m7', 'cortex-m33', 'cortex-m35p' and 'cortex-m55'.
49 # Build fails if other M-profile cpu, such as 'cortex-m23', is added with '+nofp'.
50 # Explicitly list those cpu to align with GCC description.
51 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
52 if(NOT CONFIG_TFM_ENABLE_FP AND
53 (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m4"
54 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m7"
55 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m33"
56 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m35p"
57 OR TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55"))
58 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
59 endif()
60 endif()
61
62 if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
63 if(NOT CONFIG_TFM_ENABLE_MVE)
64 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve")
65 endif()
66 if(NOT CONFIG_TFM_ENABLE_MVE_FP)
67 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nomve.fp")
68 endif()
69 endif()
70 endif()
71
72endif()
73
74# CMAKE_SYSTEM_ARCH variable is not a built-in CMAKE variable. It is used to
75# set the compile and link flags when TFM_SYSTEM_PROCESSOR is not specified.
76# The variable name is choosen to align with the ARMCLANG toolchain file.
77set(CMAKE_SYSTEM_ARCH ${TFM_SYSTEM_ARCHITECTURE})
78
79if(TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main")
80 if(CONFIG_TFM_ENABLE_MVE)
81 string(APPEND CMAKE_SYSTEM_ARCH "+mve")
82 endif()
83 if(CONFIG_TFM_ENABLE_MVE_FP)
84 string(APPEND CMAKE_SYSTEM_ARCH "+mve.fp")
85 endif()
86endif()
87
88if (DEFINED TFM_SYSTEM_DSP)
89 # +nodsp modifier is only supported from GCC version 8.
90 if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
91 # armv8.1-m.main arch does not have +nodsp option
92 if ((NOT TFM_SYSTEM_ARCHITECTURE STREQUAL "armv8.1-m.main") AND
93 NOT TFM_SYSTEM_DSP)
94 string(APPEND CMAKE_SYSTEM_ARCH "+nodsp")
95 endif()
96 endif()
97endif()
98
99if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
100 if(CONFIG_TFM_ENABLE_FP)
101 string(APPEND CMAKE_SYSTEM_ARCH "+fp")
102 endif()
103endif()
104
105if (GCC_VERSION VERSION_LESS 7.3.1)
106 message(FATAL_ERROR "Please use newer GNU Arm compiler version starting from 7.3.1.")
107endif()
108
109if (GCC_VERSION VERSION_EQUAL 10.2.1)
110 message(FATAL_ERROR "GNU Arm compiler version 10-2020-q4-major has an issue in CMSE support."
111 " Select other GNU Arm compiler versions instead."
112 " See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99157 for the issue detail.")
113endif()
114
115# GNU Arm compiler version greater equal than *11.3.Rel1*
116# has a linker issue that required system calls are missing,
117# such as _read and _write. Add stub functions of required
118# system calls to solve this issue.
119if (GCC_VERSION VERSION_GREATER_EQUAL 11.3.1)
120 set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
121endif()
122
123if (CMAKE_SYSTEM_PROCESSOR)
124 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
125 set(CMAKE_CXX_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
126 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
127 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
128 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
129else()
130 set(CMAKE_C_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
131 set(CMAKE_CXX_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
132 set(CMAKE_ASM_FLAGS_INIT "-march=${CMAKE_SYSTEM_ARCH}")
133 set(CMAKE_C_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
134 set(CMAKE_ASM_LINK_FLAGS "-march=${CMAKE_SYSTEM_ARCH}")
135endif()
136
137set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
138set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_INIT})
139set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
140
141set(BL2_COMPILER_CP_FLAG -mfloat-abi=soft)
142
143if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
144 set(COMPILER_CP_FLAG -mfloat-abi=hard)
145 set(LINKER_CP_OPTION -mfloat-abi=hard)
146 if (CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP)
147 set(COMPILER_CP_FLAG -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
148 set(LINKER_CP_OPTION -mfloat-abi=hard -mfpu=${CONFIG_TFM_FP_ARCH})
149 endif()
150else()
151 set(COMPILER_CP_FLAG -mfloat-abi=soft)
152 set(LINKER_CP_OPTION -mfloat-abi=soft)
153endif()
154
155# For GNU Arm Embedded Toolchain doesn't emit __ARM_ARCH_8_1M_MAIN__, adding this macro manually.
156add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__>)
Anton Komlevaee4b612023-05-14 17:38:36 +0100157
158add_compile_options(
159 -specs=nano.specs
160 -Wall
161 -Wno-format
162 -Wno-return-type
163 -Wno-unused-but-set-variable
164 -c
165 -fdata-sections
166 -ffunction-sections
167 -fno-builtin
168 -fshort-enums
169 -funsigned-char
170 -mthumb
171 -nostdlib
172)
173
174add_link_options(
175 --entry=Reset_Handler
176 -specs=nano.specs
177 LINKER:-check-sections
178 LINKER:-fatal-warnings
179 LINKER:--gc-sections
180 LINKER:--no-wchar-size-warning
181 LINKER:--print-memory-usage
182 LINKER:-Map=tfm_ns.map
183 -T $<TARGET_OBJECTS:tfm_ns_scatter>
184)
185
David Hu500ea2f2023-10-13 12:15:08 +0800186# Specify the linker script used to link `target`.
187# Behaviour for handling linker scripts is so wildly divergent between compilers
188# that this macro is required.
189#
190# Vendor platform can set a linker script as property INTERFACE_LINK_DEPENDS of platform_ns.
191# `target` can fetch the linker script from platform_ns.
192#
193# Alternatively, NS build can call target_add_scatter_file() with the install directory of
194# linker script.
195# target_add_scatter_file(target, install_dir)
196#
197# target_add_scatter_file() fetch a linker script from the install directory.
198macro(target_add_scatter_file target)
Anton Komlevaee4b612023-05-14 17:38:36 +0100199
David Hu500ea2f2023-10-13 12:15:08 +0800200 get_target_property(scatter_file
201 platform_ns
202 INTERFACE_LINK_DEPENDS
Anton Komlevaee4b612023-05-14 17:38:36 +0100203 )
204
David Hu500ea2f2023-10-13 12:15:08 +0800205 # If scatter_file is not passed from platform_ns
206 # Try if any linker script is exported in install directory
207 # The intall directory is passed as an optinal argument
208 if(${scatter_file} STREQUAL "scatter_file-NOTFOUND")
209 set(install_dir ${ARGN})
210 list(LENGTH install_dir nr_install_dir)
211
212 # If nr_install_dir == 1, search for sct file under install dir
213 if(${nr_install_dir} EQUAL 1)
214 file(GLOB scatter_file "${install_dir}/*.ld")
215 endif()
216 endif()
217
218 if(NOT EXISTS ${scatter_file})
219 message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}")
220 endif()
221
222 add_library(${target}_scatter OBJECT)
223 target_sources(${target}_scatter
224 PRIVATE
225 ${scatter_file}
226 )
227
228 set_source_files_properties(${scatter_file} PROPERTIES
Anton Komlevaee4b612023-05-14 17:38:36 +0100229 LANGUAGE C
230 KEEP_EXTENSION True)
231
David Hu500ea2f2023-10-13 12:15:08 +0800232 target_compile_options(${target}_scatter
Anton Komlevaee4b612023-05-14 17:38:36 +0100233 PRIVATE
234 -E
235 -P
236 -xc
237 )
238
David Hu500ea2f2023-10-13 12:15:08 +0800239 target_link_libraries(${target}_scatter platform_ns)
Anton Komlevaee4b612023-05-14 17:38:36 +0100240
David Hu500ea2f2023-10-13 12:15:08 +0800241 add_dependencies(${target} ${target}_scatter)
Anton Komlevaee4b612023-05-14 17:38:36 +0100242
243endmacro()