blob: b87f43feadf53936bc71752478932312e4049b65 [file] [log] [blame]
Anton Komlevaee4b612023-05-14 17:38:36 +01001#-------------------------------------------------------------------------------
Jackson Cooper-Driver070a6e42025-03-18 17:03:56 +00002# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
Anton Komlevaee4b612023-05-14 17:38:36 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8set(CMAKE_SYSTEM_NAME Generic)
9
David Hu9e1b2632023-10-14 13:35:24 +080010if(NOT DEFINED CROSS_COMPILE)
Anton Komlev8ab4f042025-06-03 21:13:07 +010011 set(CROSS_COMPILE "arm-none-eabi" CACHE STRING "Cross-compiler prefix")
David Hu9e1b2632023-10-14 13:35:24 +080012endif()
13
Anton Komlev8ab4f042025-06-03 21:13:07 +010014set(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
15set(CMAKE_C_COMPILER_FORCED TRUE)
16set(CMAKE_C_STANDARD 99)
Anton Komlevaee4b612023-05-14 17:38:36 +010017
18set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
19
Anton Komlev8ab4f042025-06-03 21:13:07 +010020set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
21set(CMAKE_CXX_COMPILER_FORCED TRUE)
22set(CMAKE_CXX_STANDARD 11)
Anton Komlevdd0a7222023-09-19 18:49:39 +010023
David Hu9af4d7d2023-11-01 07:52:17 +080024# This variable name is a bit of a misnomer. The file it is set to is included
25# at a particular step in the compiler initialisation. It is used here to
26# configure the extensions for object files. Despite the name, it also works
27# with the Ninja generator.
28set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/set_extensions.cmake)
29
David Hu681a8ee2023-10-13 14:40:02 +080030# CMAKE_C_COMPILER_VERSION is not guaranteed to be defined.
Anton Komlev8ab4f042025-06-03 21:13:07 +010031EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE CMAKE_C_COMPILER_VERSION)
David Hu681a8ee2023-10-13 14:40:02 +080032
Anton Komlev8ab4f042025-06-03 21:13:07 +010033if (${CMAKE_C_COMPILER_VERSION} VERSION_LESS 10.3.1)
34 message(FATAL_ERROR "Please use GNU Arm toolchain version 10.3.1 or later")
35endif()
David Hu681a8ee2023-10-13 14:40:02 +080036
Anton Komlev8ab4f042025-06-03 21:13:07 +010037function(min_toolchain_version mcpu gnu-version)
38 if (${TFM_SYSTEM_PROCESSOR} MATCHES ${mcpu} AND
39 ${CMAKE_C_COMPILER_VERSION} VERSION_LESS ${gnu-version})
40 message(FATAL_ERROR "-mcpu=${mcpu} is supported in GNU Arm version ${gnu-version} and later.\n"
41 "Please upgrade your toolchain to a supported version from: "
42 "https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads")
David Hu681a8ee2023-10-13 14:40:02 +080043 endif()
Anton Komlev8ab4f042025-06-03 21:13:07 +010044endfunction()
David Hu681a8ee2023-10-13 14:40:02 +080045
Anton Komlev8ab4f042025-06-03 21:13:07 +010046# the lowest supported GCC version for a specific cpu
47min_toolchain_version("cortex-m85" "13.0.0")
48min_toolchain_version("cortex-m52" "14.2.0")
David Hu681a8ee2023-10-13 14:40:02 +080049
50# GNU Arm compiler version greater equal than *11.3.Rel1*
51# has a linker issue that required system calls are missing,
52# such as _read and _write. Add stub functions of required
53# system calls to solve this issue.
Dávid Házi50c18282024-10-10 12:37:28 +000054#
55# READONLY linker script attribute is not supported in older
56# GNU Arm compilers. For these version the preprocessor will
57# remove the READONLY string from the linker scripts.
Anton Komlev8ab4f042025-06-03 21:13:07 +010058if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3.1)
David Hu681a8ee2023-10-13 14:40:02 +080059 set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE)
Dávid Házi50c18282024-10-10 12:37:28 +000060 set(CONFIG_GNU_LINKER_READONLY_ATTRIBUTE TRUE)
David Hu681a8ee2023-10-13 14:40:02 +080061endif()
62
Jianliang Shenbd624ed2023-10-24 15:42:59 +080063if(CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
Anton Komlev8ab4f042025-06-03 21:13:07 +010064 add_compile_options(-mfloat-abi=hard)
65 add_link_options(-mfloat-abi=hard)
Jianliang Shenbd624ed2023-10-24 15:42:59 +080066 if(CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP)
Anton Komlev8ab4f042025-06-03 21:13:07 +010067 add_compile_options(-mfpu=${CONFIG_TFM_FP_ARCH})
68 add_link_options(-mfpu=${CONFIG_TFM_FP_ARCH})
David Hu681a8ee2023-10-13 14:40:02 +080069 endif()
Anton Komlev8ab4f042025-06-03 21:13:07 +010070else()
71 add_compile_options(-mfloat-abi=soft)
72 add_link_options(-mfloat-abi=soft)
David Hu681a8ee2023-10-13 14:40:02 +080073endif()
74
Anton Komlev8ab4f042025-06-03 21:13:07 +010075include(mcpu_features)
Anton Komlevaee4b612023-05-14 17:38:36 +010076
Jackson Cooper-Driver9f7ab5b2025-03-31 14:27:53 +010077file(REAL_PATH "${CMAKE_SOURCE_DIR}/../" TOP_LEVEL_PROJECT_DIR)
78
Anton Komlevaee4b612023-05-14 17:38:36 +010079add_compile_options(
Anton Komlevaee4b612023-05-14 17:38:36 +010080 -Wall
81 -Wno-format
Anton Komlevaee4b612023-05-14 17:38:36 +010082 -Wno-unused-but-set-variable
Antonio de Angelis86d1f712025-05-02 11:51:31 +010083 -Wnull-dereference
Anton Komlevaee4b612023-05-14 17:38:36 +010084 -c
85 -fdata-sections
86 -ffunction-sections
87 -fno-builtin
88 -fshort-enums
89 -funsigned-char
Jackson Cooper-Driver9f7ab5b2025-03-31 14:27:53 +010090 # Strip /workspace/
91 -fmacro-prefix-map=${TOP_LEVEL_PROJECT_DIR}/=
92 # Strip /workspace/trusted-firmware-m
Jackson Cooper-Driver070a6e42025-03-18 17:03:56 +000093 -fmacro-prefix-map=${CMAKE_SOURCE_DIR}/=
Anton Komlevaee4b612023-05-14 17:38:36 +010094 -mthumb
Anton Komlev8ab4f042025-06-03 21:13:07 +010095 $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
Jackson Cooper-Driverdf542cf2025-04-01 14:34:11 +010096 $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${TFM_DEBUG_OPTIMISATION}>,$<CONFIG:Debug>>:-Og>
Bohdan Hunkoc7d222b2024-11-05 16:37:21 +020097 $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${CONFIG_TFM_WARNINGS_ARE_ERRORS}>>:-Werror>
Anton Komlevaee4b612023-05-14 17:38:36 +010098)
99
100add_link_options(
Anton Komlev8ab4f042025-06-03 21:13:07 +0100101 -mcpu=${TFM_SYSTEM_PROCESSOR}
Anton Komlevaee4b612023-05-14 17:38:36 +0100102 -specs=nano.specs
Jamie Fox13feddb2024-01-23 18:58:42 +0000103 -specs=nosys.specs
Anton Komlevaee4b612023-05-14 17:38:36 +0100104 LINKER:-check-sections
105 LINKER:-fatal-warnings
106 LINKER:--gc-sections
107 LINKER:--no-wchar-size-warning
108 LINKER:--print-memory-usage
Anton Komlevaee4b612023-05-14 17:38:36 +0100109)
110
David Hu500ea2f2023-10-13 12:15:08 +0800111# Specify the linker script used to link `target`.
112# Behaviour for handling linker scripts is so wildly divergent between compilers
113# that this macro is required.
114#
115# Vendor platform can set a linker script as property INTERFACE_LINK_DEPENDS of platform_ns.
116# `target` can fetch the linker script from platform_ns.
117#
118# Alternatively, NS build can call target_add_scatter_file() with the install directory of
119# linker script.
120# target_add_scatter_file(target, install_dir)
121#
122# target_add_scatter_file() fetch a linker script from the install directory.
123macro(target_add_scatter_file target)
Anton Komlevaee4b612023-05-14 17:38:36 +0100124
David Hu500ea2f2023-10-13 12:15:08 +0800125 get_target_property(scatter_file
126 platform_ns
127 INTERFACE_LINK_DEPENDS
Anton Komlevaee4b612023-05-14 17:38:36 +0100128 )
129
David Hu500ea2f2023-10-13 12:15:08 +0800130 # If scatter_file is not passed from platform_ns
131 # Try if any linker script is exported in install directory
132 # The intall directory is passed as an optinal argument
133 if(${scatter_file} STREQUAL "scatter_file-NOTFOUND")
134 set(install_dir ${ARGN})
135 list(LENGTH install_dir nr_install_dir)
136
137 # If nr_install_dir == 1, search for sct file under install dir
138 if(${nr_install_dir} EQUAL 1)
139 file(GLOB scatter_file "${install_dir}/*.ld")
140 endif()
141 endif()
142
143 if(NOT EXISTS ${scatter_file})
144 message(FATAL_ERROR "Unable to find NS scatter file ${scatter_file}")
145 endif()
146
147 add_library(${target}_scatter OBJECT)
148 target_sources(${target}_scatter
149 PRIVATE
150 ${scatter_file}
151 )
152
153 set_source_files_properties(${scatter_file} PROPERTIES
Anton Komlevaee4b612023-05-14 17:38:36 +0100154 LANGUAGE C
155 KEEP_EXTENSION True)
156
David Hu500ea2f2023-10-13 12:15:08 +0800157 target_compile_options(${target}_scatter
Anton Komlevaee4b612023-05-14 17:38:36 +0100158 PRIVATE
159 -E
160 -P
161 -xc
162 )
163
Dávid Házi80427ea2024-04-17 21:07:17 +0200164 target_compile_definitions(${target}_scatter
165 PRIVATE
166 $<$<NOT:$<BOOL:${CONFIG_GNU_LINKER_READONLY_ATTRIBUTE}>>:READONLY=>
167 )
168
David Hu35aa1a52023-10-24 23:04:04 +0800169 target_link_libraries(${target}_scatter
170 PRIVATE
171 platform_region_defs
172 )
Anton Komlevaee4b612023-05-14 17:38:36 +0100173
David Hu500ea2f2023-10-13 12:15:08 +0800174 add_dependencies(${target} ${target}_scatter)
Anton Komlevaee4b612023-05-14 17:38:36 +0100175
David Hu99d82922023-10-29 13:35:51 +0800176 target_link_options(${target}
177 PRIVATE
178 -T $<TARGET_OBJECTS:${target}_scatter>
179 )
180
Anton Komlevaee4b612023-05-14 17:38:36 +0100181endmacro()
David Hu9e1b2632023-10-14 13:35:24 +0800182
Anton Komlev8ab4f042025-06-03 21:13:07 +0100183# Macro for converting the output *.axf file to finary files: bin, elf, hex
David Hu9e1b2632023-10-14 13:35:24 +0800184macro(add_convert_to_bin_target target)
185 get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
David Hu9e1b2632023-10-14 13:35:24 +0800186 add_custom_target(${target}_bin
Anton Komlev8ab4f042025-06-03 21:13:07 +0100187 ALL DEPENDS ${target}
188 COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${target}> ${bin_dir}/${target}.bin
189 COMMAND ${CMAKE_OBJCOPY} -O elf32-littlearm $<TARGET_FILE:${target}> ${bin_dir}/${target}.elf
190 COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${target}> ${bin_dir}/${target}.hex
David Hu9e1b2632023-10-14 13:35:24 +0800191 )
192endmacro()