1#-------------------------------------------------------------------------------
2# Copyright (c) 2023, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.22)
9cmake_policy(SET CMP0115 NEW)
10
11SET(CMAKE_SYSTEM_NAME Generic)
12set(CMAKE_SYSTEM_PROCESSOR       ${TFM_SYSTEM_PROCESSOR})
13
14if(CROSS_COMPILE)
15    set(CMAKE_C_COMPILER_TARGET      arm-${CROSS_COMPILE})
16    set(CMAKE_ASM_COMPILER_TARGET    arm-${CROSS_COMPILE})
17else()
18    set(CMAKE_C_COMPILER_TARGET      arm-arm-none-eabi)
19    set(CMAKE_ASM_COMPILER_TARGET    arm-arm-none-eabi)
20endif()
21
22set(CMAKE_C_COMPILER iccarm)
23set(CMAKE_CXX_COMPILER iccarm)
24set(CMAKE_ASM_COMPILER iasmarm)
25
26set(CMAKE_C_FLAGS_DEBUG "-r -On")
27
28# This variable name is a bit of a misnomer. The file it is set to is included
29# at a particular step in the compiler initialisation. It is used here to
30# configure the extensions for object files. Despite the name, it also works
31# with the Ninja generator.
32set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/set_extensions.cmake)
33
34macro(tfm_toolchain_reset_compiler_flags)
35    set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
36
37    add_compile_options(
38        $<$<COMPILE_LANGUAGE:C,CXX>:-e>
39        $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full>
40        $<$<COMPILE_LANGUAGE:C,CXX>:--silent>
41        $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF>
42        $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES>
43        $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084>
44        $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros>
45        "$<$<COMPILE_LANGUAGE:C,CXX,ASM>:SHELL:--fpu none>"
46        $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r>
47    )
48endmacro()
49
50macro(tfm_toolchain_reset_linker_flags)
51    set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
52
53    add_link_options(
54      --silent
55      --semihosting
56      --redirect __write=__write_buffered
57      --diag_suppress=lp005
58      "SHELL:--fpu none"
59    )
60endmacro()
61
62macro(tfm_toolchain_set_processor_arch)
63    if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus")
64      set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+)
65    else()
66      set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
67    endif()
68
69    if (DEFINED TFM_SYSTEM_DSP)
70        if(NOT TFM_SYSTEM_DSP)
71            string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp")
72        endif()
73    endif()
74endmacro()
75
76macro(tfm_toolchain_reload_compiler)
77    tfm_toolchain_set_processor_arch()
78    tfm_toolchain_reset_compiler_flags()
79    tfm_toolchain_reset_linker_flags()
80
81    unset(CMAKE_C_FLAGS_INIT)
82    unset(CMAKE_C_LINK_FLAGS)
83    unset(CMAKE_ASM_FLAGS_INIT)
84    unset(CMAKE_ASM_LINK_FLAGS)
85
86    set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
87    set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
88    set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
89    set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
90
91    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
92    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
93
94    # Can't use the highest optimization with IAR on v8.1m arch because of the
95    # compilation bug in mbedcrypto
96    if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND
97        (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND
98        ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR
99         (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND
100        (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")))
101        message(FATAL_ERROR "Only debug build available for M55 and M85"
102                " cores with IAR version between 9.20 and 9.32.1")
103    endif()
104
105endmacro()
106
107# Configure environment for the compiler setup run by cmake at the first
108# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
109# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
110tfm_toolchain_reload_compiler()
111
112# Specify the scatter file used to link `target`.
113# Behaviour for handling scatter files is so wildly divergent between compilers
114# that this macro is required.
115#
116# Vendor platform can set a scatter file as property INTERFACE_LINK_DEPENDS of platform_ns.
117# `target` can fetch the scatter file from platform_ns.
118#
119# Alternatively, NS build can call target_add_scatter_file() with the install directory of
120# scatter files.
121#     target_add_scatter_file(target, install_dir)
122#
123# target_add_scatter_file() fetch a scatter file from the install directory.
124macro(target_add_scatter_file target)
125    # Try if scatter_file is passed from platform_ns
126    get_target_property(scatter_file
127                        platform_ns
128                        INTERFACE_LINK_DEPENDS
129    )
130
131    # If scatter_file is not passed from platform_ns
132    # Try if any scatter file is exported in install directory
133    # The intall directory is passed as an optinal argument
134    if(${scatter_file} STREQUAL "scatter_file-NOTFOUND")
135        set(install_dir ${ARGN})
136        list(LENGTH install_dir nr_install_dir)
137
138        # If nr_install_dir == 1, search for .icf file under install dir
139        if(${nr_install_dir} EQUAL 1)
140            file(GLOB scatter_file "${install_dir}/*.icf")
141        endif()
142    endif()
143
144    if(NOT EXISTS ${scatter_file})
145        message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}")
146    endif()
147
148    add_library(${target}_scatter OBJECT)
149    target_sources(${target}_scatter
150        PRIVATE
151            ${scatter_file}
152    )
153    # Cmake cannot use generator expressions in the
154    # set_source_file_properties command, so instead we just parse the regex
155    # for the filename and set the property on all files, regardless of if
156    # the generator expression would evaluate to true or not.
157    string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
158    set_source_files_properties(${SCATTER_FILE_PATH}
159        PROPERTIES
160        LANGUAGE C
161    )
162
163    target_link_options(${target}
164        PRIVATE
165            --config $<TARGET_OBJECTS:${target}_scatter>
166    )
167
168    add_dependencies(${target}
169        ${target}_scatter
170    )
171
172    set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
173
174    target_link_libraries(${target}_scatter
175        PRIVATE
176            platform_region_defs
177    )
178
179    target_compile_options(${target}_scatter
180        PRIVATE
181            --preprocess=sn $<TARGET_OBJECTS:${target}_scatter>
182    )
183endmacro()
184
185macro(add_convert_to_bin_target target)
186    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
187
188    add_custom_target(${target}_bin
189        SOURCES ${bin_dir}/${target}.bin
190    )
191    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
192        DEPENDS ${target}
193        COMMAND ielftool
194            --silent
195            --bin $<TARGET_FILE:${target}>
196            ${bin_dir}/${target}.bin
197    )
198
199    add_custom_target(${target}_elf
200        SOURCES ${bin_dir}/${target}.elf
201    )
202    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
203        DEPENDS ${target}
204        COMMAND ielftool
205            --silent
206            $<TARGET_FILE:${target}>
207            ${bin_dir}/${target}.elf
208    )
209
210    add_custom_target(${target}_hex
211        SOURCES ${bin_dir}/${target}.hex
212    )
213    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
214        DEPENDS ${target}
215        COMMAND ielftool
216            --silent
217            --ihex $<TARGET_FILE:${target}>
218            ${bin_dir}/${target}.hex
219    )
220
221    add_custom_target(${target}_binaries
222        ALL
223        DEPENDS ${target}_bin
224        DEPENDS ${target}_elf
225        DEPENDS ${target}_hex
226    )
227endmacro()
228