1#-------------------------------------------------------------------------------
2# Copyright (c) 2020, IAR Systems AB. All rights reserved.
3# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9cmake_minimum_required(VERSION 3.22)
10cmake_policy(SET CMP0115 NEW)
11
12SET(CMAKE_SYSTEM_NAME Generic)
13set(CMAKE_SYSTEM_PROCESSOR       ${TFM_SYSTEM_PROCESSOR})
14
15if(CROSS_COMPILE)
16    set(CMAKE_C_COMPILER_TARGET      arm-${CROSS_COMPILE})
17    set(CMAKE_ASM_COMPILER_TARGET    arm-${CROSS_COMPILE})
18else()
19    set(CMAKE_C_COMPILER_TARGET      arm-arm-none-eabi)
20    set(CMAKE_ASM_COMPILER_TARGET    arm-arm-none-eabi)
21endif()
22
23set(CMAKE_C_COMPILER iccarm)
24set(CMAKE_ASM_COMPILER iasmarm)
25
26set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= )
27set(COMPILER_CMSE_FLAG --cmse)
28
29# This variable name is a bit of a misnomer. The file it is set to is included
30# at a particular step in the compiler initialisation. It is used here to
31# configure the extensions for object files. Despite the name, it also works
32# with the Ninja generator.
33set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
34
35macro(tfm_toolchain_reset_compiler_flags)
36    set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
37
38    add_compile_options(
39        $<$<COMPILE_LANGUAGE:C,CXX>:-e>
40        $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full>
41        $<$<COMPILE_LANGUAGE:C,CXX>:--vla>
42        $<$<COMPILE_LANGUAGE:C,CXX>:--silent>
43        $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF>
44        $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES>
45        $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084>
46        $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros>
47        "$<$<COMPILE_LANGUAGE:C,CXX,ASM>:SHELL:--fpu none>"
48        $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r>
49    )
50endmacro()
51
52macro(tfm_toolchain_reset_linker_flags)
53    set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
54
55    add_link_options(
56      --silent
57      --semihosting
58      --redirect __write=__write_buffered
59      "SHELL:--fpu none"
60    )
61endmacro()
62
63macro(tfm_toolchain_set_processor_arch)
64    if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus")
65      set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+)
66    else()
67      set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
68    endif()
69
70    if (DEFINED TFM_SYSTEM_DSP)
71        if(NOT TFM_SYSTEM_DSP)
72            string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp")
73        endif()
74    endif()
75endmacro()
76
77macro(tfm_toolchain_reload_compiler)
78    tfm_toolchain_set_processor_arch()
79    tfm_toolchain_reset_compiler_flags()
80    tfm_toolchain_reset_linker_flags()
81
82    unset(CMAKE_C_FLAGS_INIT)
83    unset(CMAKE_C_LINK_FLAGS)
84    unset(CMAKE_ASM_FLAGS_INIT)
85    unset(CMAKE_ASM_LINK_FLAGS)
86
87    set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
88    set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
89    set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
90    set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
91
92    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
93    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
94endmacro()
95
96# Configure environment for the compiler setup run by cmake at the first
97# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
98# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
99tfm_toolchain_reload_compiler()
100
101# Behaviour for handling scatter files is so wildly divergent between compilers
102# that this macro is required.
103macro(target_add_scatter_file target)
104    target_link_options(${target}
105        PRIVATE
106        --config $<TARGET_OBJECTS:${target}_scatter>
107    )
108
109    add_library(${target}_scatter OBJECT)
110    foreach(scatter_file ${ARGN})
111        target_sources(${target}_scatter
112            PRIVATE
113                ${scatter_file}
114        )
115        # Cmake cannot use generator expressions in the
116        # set_source_file_properties command, so instead we just parse the regex
117        # for the filename and set the property on all files, regardless of if
118        # the generator expression would evaluate to true or not.
119        string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
120        set_source_files_properties(${SCATTER_FILE_PATH}
121            PROPERTIES
122            LANGUAGE C
123        )
124    endforeach()
125
126    add_dependencies(${target}
127        ${target}_scatter
128    )
129
130    set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
131
132    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            --preprocess=sn $<TARGET_OBJECTS:${target}_scatter>
141    )
142endmacro()
143
144macro(add_convert_to_bin_target target)
145    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
146
147    add_custom_target(${target}_bin
148        SOURCES ${bin_dir}/${target}.bin
149    )
150    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
151        DEPENDS ${target}
152        COMMAND ielftool
153            --silent
154            --bin $<TARGET_FILE:${target}>
155            ${bin_dir}/${target}.bin
156    )
157
158    add_custom_target(${target}_elf
159        SOURCES ${bin_dir}/${target}.elf
160    )
161    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
162        DEPENDS ${target}
163        COMMAND ielftool
164            --silent
165            $<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 ielftool
175            --silent
176            --ihex $<TARGET_FILE:${target}>
177            ${bin_dir}/${target}.hex
178    )
179
180    add_custom_target(${target}_binaries
181        ALL
182        DEPENDS ${target}_bin
183        DEPENDS ${target}_elf
184        DEPENDS ${target}_hex
185    )
186endmacro()
187
188macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
189    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
190endmacro()
191
192macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
193    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
194endmacro()
195