blob: e530bf7d39c1a0dc15665c809638cccd6d1a6f68 [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 Coles69817322020-10-19 14:14:14 +01008# Don't load this file if it is specified as a cmake toolchain file
9if(NOT TFM_TOOLCHAIN_FILE)
10 message(DEPRECATION "SETTING CMAKE_TOOLCHAIN_FILE is deprecated. It has been replaced with TFM_TOOLCHAIN_FILE.")
11 return()
12endif()
Raef Coles6e8f83d2020-09-28 10:44:06 +010013
Raef Coles9ec67e62020-07-10 09:40:35 +010014set(CMAKE_SYSTEM_NAME Generic)
Raef Coles9ec67e62020-07-10 09:40:35 +010015
Raef Coles69817322020-10-19 14:14:14 +010016set(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
17set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}-gcc)
Raef Coles9ec67e62020-07-10 09:40:35 +010018
Raef Coles69817322020-10-19 14:14:14 +010019set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
20set(COMPILER_CMSE_FLAG -mcmse)
Raef Coles9ec67e62020-07-10 09:40:35 +010021
22# This variable name is a bit of a misnomer. The file it is set to is included
23# at a particular step in the compiler initialisation. It is used here to
24# configure the extensions for object files. Despite the name, it also works
25# with the Ninja generator.
26set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
27
Raef Coles69817322020-10-19 14:14:14 +010028macro(tfm_toolchain_reset_compiler_flags)
Raef Coles9ec67e62020-07-10 09:40:35 +010029 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
Raef Coles69817322020-10-19 14:14:14 +010030
Raef Coles9ec67e62020-07-10 09:40:35 +010031 add_compile_options(
32 --specs=nano.specs
33 -Wall
34 -Wno-format
35 -Wno-return-type
36 -Wno-unused-but-set-variable
37 -c
38 -fdata-sections
39 -ffunction-sections
40 -fno-builtin
41 -fshort-enums
Raef Coles9ec67e62020-07-10 09:40:35 +010042 -funsigned-char
43 -mthumb
44 -nostdlib
45 -std=c99
46 $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
47 )
Raef Coles69817322020-10-19 14:14:14 +010048endmacro()
49
50macro(tfm_toolchain_reset_linker_flags)
51 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
52
Raef Coles9ec67e62020-07-10 09:40:35 +010053 add_link_options(
54 --entry=Reset_Handler
55 --specs=nano.specs
56 LINKER:-check-sections
57 LINKER:-fatal-warnings
58 LINKER:--gc-sections
59 LINKER:--no-wchar-size-warning
60 LINKER:--print-memory-usage
61 )
Raef Coles69817322020-10-19 14:14:14 +010062endmacro()
63
64macro(tfm_toolchain_set_processor_arch)
65 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
66 set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
67
68 if (DEFINED TFM_SYSTEM_DSP)
69 if(NOT TFM_SYSTEM_DSP)
70 string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
71 endif()
72 endif()
73endmacro()
74
75macro(tfm_toolchain_reload_compiler)
76 tfm_toolchain_set_processor_arch()
77 tfm_toolchain_reset_compiler_flags()
78 tfm_toolchain_reset_linker_flags()
Raef Coles9ec67e62020-07-10 09:40:35 +010079
80 unset(CMAKE_C_FLAGS_INIT)
81 unset(CMAKE_ASM_FLAGS_INIT)
82
83 set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
84 set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
85 set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
86 set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
87
88 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
89 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
90endmacro()
91
Raef Coles69817322020-10-19 14:14:14 +010092# Configure environment for the compiler setup run by cmake at the first
93# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
94# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
95tfm_toolchain_reload_compiler()
Raef Coles9ec67e62020-07-10 09:40:35 +010096
97macro(target_add_scatter_file target)
98 target_link_options(${target}
99 PRIVATE
100 -T $<TARGET_OBJECTS:${target}_scatter>
101 )
102
103 add_dependencies(${target}
104 ${target}_scatter
105 )
106
107 add_library(${target}_scatter OBJECT)
108 foreach(scatter_file ${ARGN})
109 target_sources(${target}_scatter
110 PRIVATE
111 ${scatter_file}
112 )
113 # Cmake cannot use generator expressions in the
114 # set_source_file_properties command, so instead we just parse the regex
115 # for the filename and set the property on all files, regardless of if
116 # the generator expression would evaluate to true or not.
117 string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
118 set_source_files_properties(${SCATTER_FILE_PATH}
119 PROPERTIES
120 LANGUAGE C
121 )
122 endforeach()
123
Raef Coles9ec67e62020-07-10 09:40:35 +0100124 target_link_libraries(${target}_scatter
125 platform_region_defs
126 psa_interface
127 tfm_partition_defs
128 )
129
130 target_compile_options(${target}_scatter
131 PRIVATE
132 -E
133 -P
134 -xc
135 )
136endmacro()
137
138macro(add_convert_to_bin_target target)
139 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
140
141 add_custom_target(${target}_bin
142 SOURCES ${bin_dir}/${target}.bin
143 )
144 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
145 DEPENDS ${target}
146 COMMAND ${CMAKE_OBJCOPY}
147 -O binary $<TARGET_FILE:${target}>
148 ${bin_dir}/${target}.bin
149 )
150
151 add_custom_target(${target}_elf
152 SOURCES ${bin_dir}/${target}.elf
153 )
154 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
155 DEPENDS ${target}
156 COMMAND ${CMAKE_OBJCOPY}
157 -O elf32-littlearm $<TARGET_FILE:${target}>
158 ${bin_dir}/${target}.elf
159 )
160
161 add_custom_target(${target}_hex
162 SOURCES ${bin_dir}/${target}.hex
163 )
164 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
165 DEPENDS ${target}
166 COMMAND ${CMAKE_OBJCOPY}
167 -O ihex $<TARGET_FILE:${target}>
168 ${bin_dir}/${target}.hex
169 )
170
171 add_custom_target(${target}_binaries
172 ALL
173 DEPENDS ${target}_bin
174 DEPENDS ${target}_elf
175 DEPENDS ${target}_hex
176 )
177endmacro()