blob: dfce66fcd47c3cf45e694df99f27712d8726a6de [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
Raef Coles9ec67e62020-07-10 09:40:35 +010074 -funsigned-char
75 -mthumb
76 -nostdlib
77 -std=c99
78 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
79 )
80 add_link_options(
81 --entry=Reset_Handler
82 --specs=nano.specs
83 LINKER:-check-sections
84 LINKER:-fatal-warnings
85 LINKER:--gc-sections
86 LINKER:--no-wchar-size-warning
87 LINKER:--print-memory-usage
88 )
89
90 unset(CMAKE_C_FLAGS_INIT)
91 unset(CMAKE_ASM_FLAGS_INIT)
92
93 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
94 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
95 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
96 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
97
98 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
99 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
100endmacro()
101
102set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
103set(COMPILER_CMSE_FLAG -mcmse)
104
105macro(target_add_scatter_file target)
106 target_link_options(${target}
107 PRIVATE
108 -T $<TARGET_OBJECTS:${target}_scatter>
109 )
110
111 add_dependencies(${target}
112 ${target}_scatter
113 )
114
115 add_library(${target}_scatter OBJECT)
116 foreach(scatter_file ${ARGN})
117 target_sources(${target}_scatter
118 PRIVATE
119 ${scatter_file}
120 )
121 # Cmake cannot use generator expressions in the
122 # set_source_file_properties command, so instead we just parse the regex
123 # for the filename and set the property on all files, regardless of if
124 # the generator expression would evaluate to true or not.
125 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
126 set_source_files_properties(${SCATTER_FILE_PATH}
127 PROPERTIES
128 LANGUAGE C
129 )
130 endforeach()
131
Raef Coles9ec67e62020-07-10 09:40:35 +0100132 target_link_libraries(${target}_scatter
133 platform_region_defs
134 psa_interface
135 tfm_partition_defs
136 )
137
138 target_compile_options(${target}_scatter
139 PRIVATE
140 -E
141 -P
142 -xc
143 )
144endmacro()
145
146macro(add_convert_to_bin_target target)
147 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
148
149 add_custom_target(${target}_bin
150 SOURCES ${bin_dir}/${target}.bin
151 )
152 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
153 DEPENDS ${target}
154 COMMAND ${CMAKE_OBJCOPY}
155 -O binary $<TARGET_FILE:${target}>
156 ${bin_dir}/${target}.bin
157 )
158
159 add_custom_target(${target}_elf
160 SOURCES ${bin_dir}/${target}.elf
161 )
162 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
163 DEPENDS ${target}
164 COMMAND ${CMAKE_OBJCOPY}
165 -O elf32-littlearm $<TARGET_FILE:${target}>
166 ${bin_dir}/${target}.elf
167 )
168
169 add_custom_target(${target}_hex
170 SOURCES ${bin_dir}/${target}.hex
171 )
172 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
173 DEPENDS ${target}
174 COMMAND ${CMAKE_OBJCOPY}
175 -O ihex $<TARGET_FILE:${target}>
176 ${bin_dir}/${target}.hex
177 )
178
179 add_custom_target(${target}_binaries
180 ALL
181 DEPENDS ${target}_bin
182 DEPENDS ${target}_elf
183 DEPENDS ${target}_hex
184 )
185endmacro()