blob: cbe82604b49a929473e2bacaf17c201b647d16c1 [file] [log] [blame]
TTornblom18b3bf02020-09-03 17:42:11 +02001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, IAR Systems AB. All rights reserved.
3# Copyright (c) 2020, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9cmake_minimum_required(VERSION 3.14)
10
11SET(CMAKE_SYSTEM_NAME Generic)
12# This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to
13# any value here.
14set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
15
16set(CMAKE_C_COMPILER iccarm)
17set(CMAKE_ASM_COMPILER iasmarm)
18
19set(COMPILER_CMSE_FLAG --cmse)
20set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= )
21
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
28# Cmake makes it _really_ hard to dynamically set the cmake_system_processor
29# (used for setting mcpu flags etc). Instead we load
30# platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro
31# to reload the compiler autoconfig. Note that it can't be loaded in this file
32# as cmake does not allow the use of command-line defined variables in toolchain
33# files and the path is platform dependent.
34macro(_compiler_reload)
35 if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus")
36 set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+)
37 else()
38 set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
39 endif()
40 set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
41
42 if (DEFINED TFM_SYSTEM_FP)
43 if(TFM_SYSTEM_FP)
44 # TODO Whether a system requires these extensions appears to depend
45 # on the system in question, with no real rule. Since adding .fp
46 # will cause compile failures on systems that already have fp
47 # enabled, this is commented out for now to avoid those failures. In
48 # future, better handling should be implemented.
49 # string(APPEND CMAKE_SYSTEM_PROCESSOR ".fp")
50 endif()
51 endif()
52
53 if (DEFINED TFM_SYSTEM_DSP)
54 if(TFM_SYSTEM_DSP)
55 # TODO Whether a system requires these extensions appears to depend
56 # on the system in question, with no real rule. Since adding .dsp
57 # will cause compile failures on systems that already have dsp
58 # enabled, this is commented out for now to avoid those failures. In
59 # future, better handling should be implemented.
60 # string(APPEND CMAKE_SYSTEM_PROCESSOR ".dsp")
61 else()
62 string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp")
63 endif()
64 endif()
65
66 set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
67 set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
68 add_compile_options(
69 $<$<COMPILE_LANGUAGE:C,CXX>:-e>
70 $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full>
71 $<$<COMPILE_LANGUAGE:C,CXX>:--vla>
72 $<$<COMPILE_LANGUAGE:C,CXX>:--silent>
73 $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF>
74 $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES>
75 $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084>
76 )
77 add_link_options(
78 --silent
79 --semihosting
80 --redirect __write=__write_buffered
81 )
82
83 unset(CMAKE_C_FLAGS_INIT)
84 unset(CMAKE_C_LINK_FLAGS)
85 unset(CMAKE_ASM_FLAGS_INIT)
86 unset(CMAKE_ASM_LINK_FLAGS)
87
88 set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
89 set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
90 set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
91 set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
92
93 set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
94 set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
95
96endmacro()
97
98# Behaviour for handling scatter files is so wildly divergent between compilers
99# that this macro is required.
100macro(target_add_scatter_file target)
101 target_link_options(${target}
102 PRIVATE
103 --config $<TARGET_OBJECTS:${target}_scatter>
104 --map $<TARGET_FILE:${target}>.map
105 )
106 add_dependencies(${target}
107 ${target}_scatter
108 )
109
110 add_library(${target}_scatter OBJECT)
111 foreach(scatter_file ${ARGN})
112 target_sources(${target}_scatter
113 PRIVATE
114 ${scatter_file}
115 )
116 # Cmake cannot use generator expressions in the
117 # set_source_file_properties command, so instead we just parse the regex
118 # for the filename and set the property on all files, regardless of if
119 # the generator expression would evaluate to true or not.
120 string(REGEX REPLACE ".*:(.*)>$" "\\1" SCATTER_FILE_PATH ${scatter_file})
121 set_source_files_properties(${SCATTER_FILE_PATH}
122 PROPERTIES
123 LANGUAGE C
124 )
125 endforeach()
126
127 set_target_properties(${target}_scatter PROPERTIES
128 SUFFIX ".icf"
129 )
130
131 target_link_libraries(${target}_scatter
132 platform_region_defs
133 psa_interface
134 tfm_partition_defs
135 )
136
137 target_compile_options(${target}_scatter
138 PRIVATE
139 --preprocess=sn $<TARGET_OBJECTS:${target}_scatter>
140 )
141endmacro()
142
143macro(add_convert_to_bin_target target)
144 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
145
146 add_custom_target(${target}_bin
147 SOURCES ${bin_dir}/${target}.bin
148 )
149 add_custom_command(OUTPUT ${bin_dir}/${target}.bin
150 DEPENDS ${target}
151 COMMAND ielftool
152 --silent
153 --bin $<TARGET_FILE:${target}>
154 ${bin_dir}/${target}.bin
155 )
156
157 add_custom_target(${target}_elf
158 SOURCES ${bin_dir}/${target}.elf
159 )
160 add_custom_command(OUTPUT ${bin_dir}/${target}.elf
161 DEPENDS ${target}
162 COMMAND ielftool
163 --silent
164 $<TARGET_FILE:${target}>
165 ${bin_dir}/${target}.elf
166 )
167
168 add_custom_target(${target}_hex
169 SOURCES ${bin_dir}/${target}.hex
170 )
171 add_custom_command(OUTPUT ${bin_dir}/${target}.hex
172 DEPENDS ${target}
173 COMMAND ielftool
174 --silent
175 --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()