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      --diag_suppress=lp005
62      "SHELL:--fpu none"
63    )
64endmacro()
65
66macro(tfm_toolchain_set_processor_arch)
67    if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus")
68      set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+)
69    else()
70      set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
71    endif()
72
73    if (DEFINED TFM_SYSTEM_DSP)
74        if(NOT TFM_SYSTEM_DSP)
75            string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp")
76        endif()
77    endif()
78endmacro()
79
80macro(tfm_toolchain_reload_compiler)
81    tfm_toolchain_set_processor_arch()
82    tfm_toolchain_reset_compiler_flags()
83    tfm_toolchain_reset_linker_flags()
84
85    unset(CMAKE_C_FLAGS_INIT)
86    unset(CMAKE_C_LINK_FLAGS)
87    unset(CMAKE_ASM_FLAGS_INIT)
88    unset(CMAKE_ASM_LINK_FLAGS)
89
90    set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
91    set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
92    set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
93    set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}")
94
95    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
96    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
97
98    # Can't use the highest optimization with IAR on v8.1m arch because of the
99    # compilation bug in mbedcrypto
100    if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9.20") AND
101        (CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "9.32.1") AND
102        ((TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m85") OR
103         (TFM_SYSTEM_PROCESSOR STREQUAL "cortex-m55")) AND
104        (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")))
105        message(FATAL_ERROR "Only debug build available for M55 and M85"
106                " cores with IAR version between 9.20 and 9.32.1")
107    endif()
108
109endmacro()
110
111# Configure environment for the compiler setup run by cmake at the first
112# `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is
113# done, all further compiler setup is done via tfm_toolchain_reload_compiler()
114tfm_toolchain_reload_compiler()
115
116# Behaviour for handling scatter files is so wildly divergent between compilers
117# that this macro is required.
118macro(target_add_scatter_file target)
119    target_link_options(${target}
120        PRIVATE
121        --config $<TARGET_OBJECTS:${target}_scatter>
122    )
123
124    add_library(${target}_scatter OBJECT)
125    foreach(scatter_file ${ARGN})
126        target_sources(${target}_scatter
127            PRIVATE
128                ${scatter_file}
129        )
130        # Cmake cannot use generator expressions in the
131        # set_source_file_properties command, so instead we just parse the regex
132        # for the filename and set the property on all files, regardless of if
133        # the generator expression would evaluate to true or not.
134        string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
135        set_source_files_properties(${SCATTER_FILE_PATH}
136            PROPERTIES
137            LANGUAGE C
138        )
139    endforeach()
140
141    add_dependencies(${target}
142        ${target}_scatter
143    )
144
145    set_target_properties(${target} PROPERTIES LINK_DEPENDS $<TARGET_OBJECTS:${target}_scatter>)
146
147    target_link_libraries(${target}_scatter
148        platform_region_defs
149        psa_interface
150        tfm_config
151    )
152
153    target_compile_options(${target}_scatter
154        PRIVATE
155            --preprocess=sn $<TARGET_OBJECTS:${target}_scatter>
156    )
157endmacro()
158
159macro(add_convert_to_bin_target target)
160    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
161
162    add_custom_target(${target}_bin
163        SOURCES ${bin_dir}/${target}.bin
164    )
165    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
166        DEPENDS ${target}
167        COMMAND ielftool
168            --silent
169            --bin $<TARGET_FILE:${target}>
170            ${bin_dir}/${target}.bin
171    )
172
173    add_custom_target(${target}_elf
174        SOURCES ${bin_dir}/${target}.elf
175    )
176    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
177        DEPENDS ${target}
178        COMMAND ielftool
179            --silent
180            $<TARGET_FILE:${target}>
181            ${bin_dir}/${target}.elf
182    )
183
184    add_custom_target(${target}_hex
185        SOURCES ${bin_dir}/${target}.hex
186    )
187    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
188        DEPENDS ${target}
189        COMMAND ielftool
190            --silent
191            --ihex $<TARGET_FILE:${target}>
192            ${bin_dir}/${target}.hex
193    )
194
195    add_custom_target(${target}_binaries
196        ALL
197        DEPENDS ${target}_bin
198        DEPENDS ${target}_elf
199        DEPENDS ${target}_hex
200    )
201endmacro()
202
203macro(compiler_create_shared_code TARGET SHARED_SYMBOL_TEMPLATE)
204    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
205endmacro()
206
207macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
208    message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
209endmacro()
210