blob: db533728bb953edc49e5c7d9f82320f300926fe3 [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
Raef Coles9ec67e62020-07-10 09:40:35 +010051 -funsigned-char
52 -masm=auto
53 -nostdlib
54 -std=c99
55 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-mfpu=none>
56 )
57 else()
58 # Compile options for legacy compiler 6.10.1. To be removed as soon that
59 # that compiler can be deprecated.
60 add_compile_options(
61 $<$<COMPILE_LANGUAGE:C>:--target=arm-arm-none-eabi>
62 $<$<COMPILE_LANGUAGE:C>:-Wno-ignored-optimization-argument>
63 $<$<COMPILE_LANGUAGE:C>:-Wno-unused-command-line-argument>
64 $<$<COMPILE_LANGUAGE:C>:-Wall>
65 # Don't error when the MBEDTLS_NULL_ENTROPY warning is shown
66 $<$<COMPILE_LANGUAGE:C>:-Wno-error=cpp>
67 $<$<COMPILE_LANGUAGE:C>:-c>
68 $<$<COMPILE_LANGUAGE:C>:-fdata-sections>
69 $<$<COMPILE_LANGUAGE:C>:-ffunction-sections>
70 $<$<COMPILE_LANGUAGE:C>:-fno-builtin>
71 $<$<COMPILE_LANGUAGE:C>:-fshort-enums>
Raef Coles9ec67e62020-07-10 09:40:35 +010072 $<$<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
Raef Coles9ec67e62020-07-10 09:40:35 +0100197 target_link_libraries(${target}_scatter
198 platform_region_defs
199 psa_interface
200 tfm_partition_defs
201 )
202
203 target_compile_options(${target}_scatter
204 PRIVATE
205 -E
206 -xc
207 )
208endmacro()
209
210macro(add_convert_to_bin_target target)
211 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
212
213 add_custom_target(${target}_bin
214 SOURCES ${bin_dir}/${target}.bin
215 )
216 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
217 DEPENDS ${target}
218 COMMAND fromelf
219 --bincombined $<TARGET_FILE:${target}>
220 --output=${bin_dir}/${target}.bin
221 )
222
223 add_custom_target(${target}_elf
224 SOURCES ${bin_dir}/${target}.elf
225 )
226 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
227 DEPENDS ${target}
228 COMMAND fromelf
229 --elf $<TARGET_FILE:${target}>
230 --output=${bin_dir}/${target}.elf
231 )
232
233 add_custom_target(${target}_hex
234 SOURCES ${bin_dir}/${target}.hex
235 )
236 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
237 DEPENDS ${target}
238 COMMAND fromelf
239 --i32combined $<TARGET_FILE:${target}>
240 --output=${bin_dir}/${target}.hex
241 )
242
243 add_custom_target(${target}_binaries
244 ALL
245 DEPENDS ${target}_bin
246 DEPENDS ${target}_elf
247 DEPENDS ${target}_hex
248 )
249endmacro()