blob: d1b4e1f423a059be413601c15c12d656f4a9d082 [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
Raef Coles6e8f83d2020-09-28 10:44:06 +01008set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES)
9
Raef Coles9ec67e62020-07-10 09:40:35 +010010set(CMAKE_SYSTEM_NAME Generic)
11set(CMAKE_SYSTEM_PROCESSOR arm)
12
13set(CMAKE_C_COMPILER arm-none-eabi-gcc)
14set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
15
16# Tell CMake not to try to link executables during its checks
17set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
18
19# This variable name is a bit of a misnomer. The file it is set to is included
20# at a particular step in the compiler initialisation. It is used here to
21# configure the extensions for object files. Despite the name, it also works
22# with the Ninja generator.
23set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
24
25# Cmake makes it _really_ hard to dynamically set the cmake_system_processor
26# (used for setting mcpu flags etc). Instead we load
27# platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro
28# to reload the compiler autoconfig. Note that it can't be loaded in this file
29# as cmake does not allow the use of command-line defined variables in toolchain
30# files and the path is platform dependent.
31macro(_compiler_reload)
32 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
33 set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
34
35 if (DEFINED TFM_SYSTEM_FP)
36 if(TFM_SYSTEM_FP)
37 # TODO Whether a system requires these extensions appears to depend
38 # on the system in question, with no real rule. Since adding +fp
39 # will cause compile failures on systems that already have fp
40 # enabled, this is commented out for now to avoid those failures. In
41 # future, better handling should be implemented.
42 # string(APPEND CMAKE_SYSTEM_PROCESSOR "+fp")
43 else()
44 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
45 endif()
46 endif()
47
48 if (DEFINED TFM_SYSTEM_DSP)
49 if(TFM_SYSTEM_DSP)
50 # TODO Whether a system requires these extensions appears to depend
51 # on the system in question, with no real rule. Since adding +dsp
52 # will cause compile failures on systems that already have dsp
53 # enabled, this is commented out for now to avoid those failures. In
54 # future, better handling should be implemented.
55 # string(APPEND CMAKE_SYSTEM_PROCESSOR "+dsp")
56 else()
57 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
58 endif()
59 endif()
60
61 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
62 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
63 add_compile_options(
64 --specs=nano.specs
65 -Wall
66 -Wno-format
67 -Wno-return-type
68 -Wno-unused-but-set-variable
69 -c
70 -fdata-sections
71 -ffunction-sections
72 -fno-builtin
73 -fshort-enums
74 -fshort-wchar
75 -funsigned-char
76 -mthumb
77 -nostdlib
78 -std=c99
79 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
80 )
81 add_link_options(
82 --entry=Reset_Handler
83 --specs=nano.specs
84 LINKER:-check-sections
85 LINKER:-fatal-warnings
86 LINKER:--gc-sections
87 LINKER:--no-wchar-size-warning
88 LINKER:--print-memory-usage
89 )
90
91 unset(CMAKE_C_FLAGS_INIT)
92 unset(CMAKE_ASM_FLAGS_INIT)
93
94 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
95 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
96 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
97 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
98
99 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
100 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
101endmacro()
102
103set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
104set(COMPILER_CMSE_FLAG -mcmse)
105
106macro(target_add_scatter_file target)
107 target_link_options(${target}
108 PRIVATE
109 -T $<TARGET_OBJECTS:${target}_scatter>
110 )
111
112 add_dependencies(${target}
113 ${target}_scatter
114 )
115
116 add_library(${target}_scatter OBJECT)
117 foreach(scatter_file ${ARGN})
118 target_sources(${target}_scatter
119 PRIVATE
120 ${scatter_file}
121 )
122 # Cmake cannot use generator expressions in the
123 # set_source_file_properties command, so instead we just parse the regex
124 # for the filename and set the property on all files, regardless of if
125 # the generator expression would evaluate to true or not.
126 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
127 set_source_files_properties(${SCATTER_FILE_PATH}
128 PROPERTIES
129 LANGUAGE C
130 )
131 endforeach()
132
133 set_target_properties(${target}_scatter PROPERTIES
134 SUFFIX ".ld"
135 )
136
137 target_link_libraries(${target}_scatter
138 platform_region_defs
139 psa_interface
140 tfm_partition_defs
141 )
142
143 target_compile_options(${target}_scatter
144 PRIVATE
145 -E
146 -P
147 -xc
148 )
149endmacro()
150
151macro(add_convert_to_bin_target target)
152 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
153
154 add_custom_target(${target}_bin
155 SOURCES ${bin_dir}/${target}.bin
156 )
157 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
158 DEPENDS ${target}
159 COMMAND ${CMAKE_OBJCOPY}
160 -O binary $<TARGET_FILE:${target}>
161 ${bin_dir}/${target}.bin
162 )
163
164 add_custom_target(${target}_elf
165 SOURCES ${bin_dir}/${target}.elf
166 )
167 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
168 DEPENDS ${target}
169 COMMAND ${CMAKE_OBJCOPY}
170 -O elf32-littlearm $<TARGET_FILE:${target}>
171 ${bin_dir}/${target}.elf
172 )
173
174 add_custom_target(${target}_hex
175 SOURCES ${bin_dir}/${target}.hex
176 )
177 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
178 DEPENDS ${target}
179 COMMAND ${CMAKE_OBJCOPY}
180 -O ihex $<TARGET_FILE:${target}>
181 ${bin_dir}/${target}.hex
182 )
183
184 add_custom_target(${target}_binaries
185 ALL
186 DEPENDS ${target}_bin
187 DEPENDS ${target}_elf
188 DEPENDS ${target}_hex
189 )
190endmacro()