1#-------------------------------------------------------------------------------
2# Copyright (c) 2020, IAR Systems AB. All rights reserved.
3# Copyright (c) 2020-2023, 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_CXX_COMPILER iccarm)
25set(CMAKE_ASM_COMPILER iasmarm)
26
27set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= )
28set(COMPILER_CMSE_FLAG --cmse)
29
30set(CMAKE_C_FLAGS_DEBUG "-r -On")
31
32# This variable name is a bit of a misnomer. The file it is set to is included
33# at a particular step in the compiler initialisation. It is used here to
34# configure the extensions for object files. Despite the name, it also works
35# with the Ninja generator.
36set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
37
38macro(tfm_toolchain_reset_compiler_flags)
39    set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
40
41    add_compile_options(
42        $<$<COMPILE_LANGUAGE:C,CXX>:-e>
43        $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full>
44        $<$<COMPILE_LANGUAGE:C,CXX>:--silent>
45        $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF>
46        $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES>
47        $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084>
48        $<$<COMPILE_LANGUAGE:C,CXX>:--no_path_in_file_macros>
49        "$<$<COMPILE_LANGUAGE:C,CXX,ASM>:SHELL:--fpu none>"
50        $<$<AND:$<COMPILE_LANGUAGE:C,CXX,ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>,$<CONFIG:Release,MinSizeRel>>:-r>
51    )
52endmacro()
53
54macro(tfm_toolchain_reset_linker_flags)
55    set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
56
57    add_link_options(
58      --silent
59      --semihosting
60      --redirect __write=__write_buffered
61      "SHELL:--fpu none"
62    )
63endmacro()
64
65macro(tfm_toolchain_set_processor_arch)
66    if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus")
67      set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+)
68    else()
69      set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
70    endif()
71
72    if (DEFINED TFM_SYSTEM_DSP)
73        if(NOT TFM_SYSTEM_DSP)
74            string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp")
75        endif()
76    endif()
77endmacro()
78
79macro(tfm_toolchain_reload_compiler)
80    tfm_toolchain_set_processor_arch()
81    tfm_toolchain_reset_compiler_flags()
82    tfm_toolchain_reset_linker_flags()
83
84    unset(CMAKE_C_FLAGS_INIT)
85    unset(CMAKE_C_LINK_FLAGS)
86    unset(CMAKE_ASM_FLAGS_INIT)
87    unset(CMAKE_ASM_LINK_FLAGS)
88
89    set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
90    set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
91    set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
92    set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
93
94    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
95    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
96
97    # Can't use the highest optimization with IAR on v8.1m arch because of the
98    # compilation bug in mbedcrypto
99    if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND
100        (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND
101        ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR
102         (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND
103        (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")))
104        message(FATAL_ERROR "Only debug build available for M55 and M85"
105                " cores with IAR version between 9.20 and 9.32.1")
106    endif()
107
108endmacro()
109
110# Configure environment for the compiler setup run by cmake at the first
111# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
112# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
113tfm_toolchain_reload_compiler()
114
115# Behaviour for handling scatter files is so wildly divergent between compilers
116# that this macro is required.
117macro(target_add_scatter_file target)
118    target_link_options(${target}
119        PRIVATE
120        --config $<TARGET_OBJECTS:${target}_scatter>
121    )
122
123    add_library(${target}_scatter OBJECT)
124    foreach(scatter_file ${ARGN})
125        target_sources(${target}_scatter
126            PRIVATE
127                ${scatter_file}
128        )
129        # Cmake cannot use generator expressions in the
130        # set_source_file_properties command, so instead we just parse the regex
131        # for the filename and set the property on all files, regardless of if
132        # the generator expression would evaluate to true or not.
133        string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
134        set_source_files_properties(${SCATTER_FILE_PATH}
135            PROPERTIES
136            LANGUAGE C
137        )
138    endforeach()
139
140    add_dependencies(${target}
141        ${target}_scatter
142    )
143
144    set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
145
146    target_link_libraries(${target}_scatter
147        platform_region_defs
148        psa_interface
149        tfm_partition_defs
150    )
151
152    target_compile_options(${target}_scatter
153        PRIVATE
154            --preprocess=sn $<TARGET_OBJECTS:${target}_scatter>
155    )
156endmacro()
157
158macro(add_convert_to_bin_target target)
159    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
160
161    add_custom_target(${target}_bin
162        SOURCES ${bin_dir}/${target}.bin
163    )
164    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
165        DEPENDS ${target}
166        COMMAND ielftool
167            --silent
168            --bin $<TARGET_FILE:${target}>
169            ${bin_dir}/${target}.bin
170    )
171
172    add_custom_target(${target}_elf
173        SOURCES ${bin_dir}/${target}.elf
174    )
175    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
176        DEPENDS ${target}
177        COMMAND ielftool
178            --silent
179            $<TARGET_FILE:${target}>
180            ${bin_dir}/${target}.elf
181    )
182
183    add_custom_target(${target}_hex
184        SOURCES ${bin_dir}/${target}.hex
185    )
186    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
187        DEPENDS ${target}
188        COMMAND ielftool
189            --silent
190            --ihex $<TARGET_FILE:${target}>
191            ${bin_dir}/${target}.hex
192    )
193
194    add_custom_target(${target}_binaries
195        ALL
196        DEPENDS ${target}_bin
197        DEPENDS ${target}_elf
198        DEPENDS ${target}_hex
199    )
200endmacro()
201
202macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
203    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
204endmacro()
205
206macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
207    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
208endmacro()
209