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