blob: 956b10f40b3bd5158aba415fffe1e5fd4f5e41bc [file] [log] [blame]
Raef Coles9ec67e62020-07-10 09:40:35 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.15)
9
Raef Coles6e8f83d2020-09-28 10:44:06 +010010set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES)
11
Raef Coles9ec67e62020-07-10 09:40:35 +010012SET(CMAKE_SYSTEM_NAME Generic)
13# This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to
14# any value here.
15set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
16
17set(CMAKE_C_COMPILER armclang)
18set(CMAKE_ASM_COMPILER armclang)
19
20set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out=)
21set(COMPILER_CMSE_FLAG -mcmse)
22
23# This variable name is a bit of a misnomer. The file it is set to is included
24# at a particular step in the compiler initialisation. It is used here to
25# configure the extensions for object files. Despite the name, it also works
26# with the Ninja generator.
27set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
28
29# Cmake makes it _really_ hard to dynamically set the cmake_system_processor
30# (used for setting mcpu flags etc). Instead we load
31# platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro
32# to reload the compiler autoconfig. Note that it can't be loaded in this file
33# as cmake does not allow the use of command-line defined variables in toolchain
34# files and the path is platform dependent.
35macro(_compiler_reload)
36 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
37 set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
38
39 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
40 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
41 if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER_EQUAL "6.11")
42 add_compile_options(
43 --target=arm-arm-none-eabi
44 -Wno-ignored-optimization-argument
45 -Wno-unused-command-line-argument
46 -c
47 -fdata-sections
48 -ffunction-sections
49 -fno-builtin
50 -fshort-enums
51 -fshort-wchar
52 -funsigned-char
53 -masm=auto
54 -nostdlib
55 -std=c99
56 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-mfpu=none>
57 )
58 else()
59 # Compile options for legacy compiler 6.10.1. To be removed as soon that
60 # that compiler can be deprecated.
61 add_compile_options(
62 $<$<COMPILE_LANGUAGE:C>:--target=arm-arm-none-eabi>
63 $<$<COMPILE_LANGUAGE:C>:-Wno-ignored-optimization-argument>
64 $<$<COMPILE_LANGUAGE:C>:-Wno-unused-command-line-argument>
65 $<$<COMPILE_LANGUAGE:C>:-Wall>
66 # Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
67 $<$<COMPILE_LANGUAGE:C>:-Wno-error=cpp>
68 $<$<COMPILE_LANGUAGE:C>:-c>
69 $<$<COMPILE_LANGUAGE:C>:-fdata-sections>
70 $<$<COMPILE_LANGUAGE:C>:-ffunction-sections>
71 $<$<COMPILE_LANGUAGE:C>:-fno-builtin>
72 $<$<COMPILE_LANGUAGE:C>:-fshort-enums>
73 $<$<COMPILE_LANGUAGE:C>:-fshort-wchar>
74 $<$<COMPILE_LANGUAGE:C>:-funsigned-char>
75 $<$<COMPILE_LANGUAGE:C>:-masm=auto>
76 $<$<COMPILE_LANGUAGE:C>:-nostdlib>
77 $<$<COMPILE_LANGUAGE:C>:-std=c99>
78 $<$<AND:$<COMPILE_LANGUAGE:C>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:-mfpu=none>
79 $<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_SYSTEM_PROCESSOR}>
80 $<$<AND:$<COMPILE_LANGUAGE:ASM>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:--fpu=none>
81 )
82 set(COMPILER_CMSE_FLAG $<$<COMPILE_LANGUAGE:C>:-mcmse>)
83 endif()
84 add_link_options(
85 --info=summarysizes,sizes,totals,unused,veneers
86 --strict
87 --symbols
88 --xref
89 # Suppress link warnings that are consistant (and therefore hopefully
90 # harmless)
91 # https://developer.arm.com/documentation/100074/0608/linker-errors-and-warnings/list-of-the-armlink-error-and-warning-messages
92 # Empty region description
93 --diag_suppress=6312
94 # Ns section matches pattern
95 --diag_suppress=6314
96 # Duplicate input files
97 --diag_suppress=6304
98 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:--fpu=softvfp>
99 )
100
101 if (DEFINED TFM_SYSTEM_FP)
102 if(TFM_SYSTEM_FP)
103 # TODO Whether a system requires these extensions appears to depend
104 # on the system in question, with no real rule. Since adding +fp
105 # will cause compile failures on systems that already have fp
106 # enabled, this is commented out for now to avoid those failures. In
107 # future, better handling should be implemented.
108 # string(APPEND CMAKE_SYSTEM_PROCESSOR "+fp")
109 else()
110 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
111 endif()
112 endif()
113
114 if (DEFINED TFM_SYSTEM_DSP)
115 if(TFM_SYSTEM_DSP)
116 # TODO Whether a system requires these extensions appears to depend
117 # on the system in question, with no real rule. Since adding +dsp
118 # will cause compile failures on systems that already have dsp
119 # enabled, this is commented out for now to avoid those failures. In
120 # future, better handling should be implemented.
121 # string(APPEND CMAKE_SYSTEM_PROCESSOR "+dsp")
122 else()
123 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
124 endif()
125 endif()
126
127 unset(CMAKE_C_FLAGS_INIT)
128 unset(CMAKE_C_LINK_FLAGS)
129 unset(CMAKE_ASM_FLAGS_INIT)
130 unset(CMAKE_ASM_LINK_FLAGS)
131
132 # cmake does not allow the use of +dsp / +nofpu syntax. An override is added
133 # here to enable it. First reset the supported CPU list in case this has
134 # already been run.
135 __armclang_set_processor_list(C CMAKE_C_COMPILER_PROCESSOR_LIST)
136 __armclang_set_processor_list(ASM CMAKE_ASM_COMPILER_PROCESSOR_LIST)
137 # Then use regex to add +dsp +nodsp +fpu and +nofp options
138 # TODO generate this combinatorially
139 list(TRANSFORM CMAKE_C_COMPILER_PROCESSOR_LIST REPLACE "^(.+)$" "\\1;\\1+fp;\\1+nofp;")
140 list(TRANSFORM CMAKE_C_COMPILER_PROCESSOR_LIST REPLACE "^(.+)$" "\\1;\\1+dsp;\\1+nodsp;")
141 list(TRANSFORM CMAKE_ASM_COMPILER_PROCESSOR_LIST REPLACE "^(.+)$" "\\1;\\1+fp;\\1+nofp;")
142 list(TRANSFORM CMAKE_ASM_COMPILER_PROCESSOR_LIST REPLACE "^(.+)$" "\\1;\\1+dsp;\\1+nodsp;")
143
144 __compiler_armclang(C)
145
146 if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER_EQUAL "6.11")
147 __compiler_armclang(ASM)
148 else()
149 # Because armclang<6.11 does not have an integrated assembler that
150 # supports the ASM syntax used by the CMSIS startup files, it is
151 # required to manuall invoke armasm. CMake does not support that when
152 # using armclang as the declared compiler, so instead we use some of the
153 # internals from ArmCC which uses armasm by default.
154 find_program(ARMASM_PATH armasm HINTS "${_CMAKE_C_TOOLCHAIN_LOCATION}")
155 set(CMAKE_ASM_COMPILER ${ARMASM_PATH})
156 include(${CMAKE_ROOT}/Modules/Compiler/ARMCC.cmake)
157 include(${CMAKE_ROOT}/Modules/Compiler/ARMCC-ASM.cmake)
158 __compiler_armcc(ASM)
159 endif()
160
161 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
162 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
163
164 # But armlink doesn't support this +dsp syntax, so take the cpu flag and
165 # throw away the plus and everything after.
166 string(REGEX REPLACE "(--cpu=.*)\\+[a-z\\+]*[ ]?" "\\1" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}")
167 string(REGEX REPLACE "(--cpu=.*)\\+[a-z\\+]*[ ]?" "\\1" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}")
168endmacro()
169
170# Behaviour for handling scatter files is so wildly divergent between compilers
171# that this macro is required.
172macro(target_add_scatter_file target)
173 target_link_options(${target}
174 PRIVATE
175 --scatter=$<TARGET_OBJECTS:${target}_scatter>
176 )
177
178 add_dependencies(${target}
179 ${target}_scatter
180 )
181
182 add_library(${target}_scatter OBJECT)
183 foreach(scatter_file ${ARGN})
184 target_sources(${target}_scatter
185 PRIVATE
186 ${scatter_file}
187 )
188 # Cmake cannot use generator expressions in the
189 # set_source_file_properties command, so instead we just parse the regex
190 # for the filename and set the property on all files, regardless of if
191 # the generator expression would evaluate to true or not.
192 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
193 set_source_files_properties(${SCATTER_FILE_PATH}
194 PROPERTIES
195 LANGUAGE C
196 )
197 endforeach()
198
199 set_target_properties(${target}_scatter PROPERTIES
200 SUFFIX ".ld"
201 )
202
203 target_link_libraries(${target}_scatter
204 platform_region_defs
205 psa_interface
206 tfm_partition_defs
207 )
208
209 target_compile_options(${target}_scatter
210 PRIVATE
211 -E
212 -xc
213 )
214endmacro()
215
216macro(add_convert_to_bin_target target)
217 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
218
219 add_custom_target(${target}_bin
220 SOURCES ${bin_dir}/${target}.bin
221 )
222 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
223 DEPENDS ${target}
224 COMMAND fromelf
225 --bincombined $<TARGET_FILE:${target}>
226 --output=${bin_dir}/${target}.bin
227 )
228
229 add_custom_target(${target}_elf
230 SOURCES ${bin_dir}/${target}.elf
231 )
232 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
233 DEPENDS ${target}
234 COMMAND fromelf
235 --elf $<TARGET_FILE:${target}>
236 --output=${bin_dir}/${target}.elf
237 )
238
239 add_custom_target(${target}_hex
240 SOURCES ${bin_dir}/${target}.hex
241 )
242 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
243 DEPENDS ${target}
244 COMMAND fromelf
245 --i32combined $<TARGET_FILE:${target}>
246 --output=${bin_dir}/${target}.hex
247 )
248
249 add_custom_target(${target}_binaries
250 ALL
251 DEPENDS ${target}_bin
252 DEPENDS ${target}_elf
253 DEPENDS ${target}_hex
254 )
255endmacro()