Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Jackson Cooper-Driver | 070a6e4 | 2025-03-18 17:03:56 +0000 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | set(CMAKE_SYSTEM_NAME Generic) |
| 9 | |
David Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 10 | if(NOT DEFINED CROSS_COMPILE) |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 11 | set(CROSS_COMPILE "arm-none-eabi" CACHE STRING "Cross-compiler prefix") |
David Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 12 | endif() |
| 13 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 14 | set(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc) |
| 15 | set(CMAKE_C_COMPILER_FORCED TRUE) |
| 16 | set(CMAKE_C_STANDARD 99) |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 17 | |
| 18 | set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) |
| 19 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 20 | set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++) |
| 21 | set(CMAKE_CXX_COMPILER_FORCED TRUE) |
| 22 | set(CMAKE_CXX_STANDARD 11) |
Anton Komlev | dd0a722 | 2023-09-19 18:49:39 +0100 | [diff] [blame] | 23 | |
David Hu | 9af4d7d | 2023-11-01 07:52:17 +0800 | [diff] [blame] | 24 | # 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. |
| 28 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/set_extensions.cmake) |
| 29 | |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 30 | # CMAKE_C_COMPILER_VERSION is not guaranteed to be defined. |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 31 | EXECUTE_PROCESS( COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE CMAKE_C_COMPILER_VERSION) |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 32 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 33 | if (${CMAKE_C_COMPILER_VERSION} VERSION_LESS 10.3.1) |
| 34 | message(FATAL_ERROR "Please use GNU Arm toolchain version 10.3.1 or later") |
| 35 | endif() |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 36 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 37 | function(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 Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 43 | endif() |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 44 | endfunction() |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 45 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 46 | # the lowest supported GCC version for a specific cpu |
| 47 | min_toolchain_version("cortex-m85" "13.0.0") |
| 48 | min_toolchain_version("cortex-m52" "14.2.0") |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 49 | |
| 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ázi | 50c1828 | 2024-10-10 12:37:28 +0000 | [diff] [blame] | 54 | # |
| 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 Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 58 | if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3.1) |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 59 | set(CONFIG_GNU_SYSCALL_STUB_ENABLED TRUE) |
Dávid Házi | 50c1828 | 2024-10-10 12:37:28 +0000 | [diff] [blame] | 60 | set(CONFIG_GNU_LINKER_READONLY_ATTRIBUTE TRUE) |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 61 | endif() |
| 62 | |
Jianliang Shen | bd624ed | 2023-10-24 15:42:59 +0800 | [diff] [blame] | 63 | if(CONFIG_TFM_FLOAT_ABI STREQUAL "hard") |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 64 | add_compile_options(-mfloat-abi=hard) |
| 65 | add_link_options(-mfloat-abi=hard) |
Jianliang Shen | bd624ed | 2023-10-24 15:42:59 +0800 | [diff] [blame] | 66 | if(CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP) |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 67 | add_compile_options(-mfpu=${CONFIG_TFM_FP_ARCH}) |
| 68 | add_link_options(-mfpu=${CONFIG_TFM_FP_ARCH}) |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 69 | endif() |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 70 | else() |
| 71 | add_compile_options(-mfloat-abi=soft) |
| 72 | add_link_options(-mfloat-abi=soft) |
David Hu | 681a8ee | 2023-10-13 14:40:02 +0800 | [diff] [blame] | 73 | endif() |
| 74 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 75 | include(mcpu_features) |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 76 | |
Jackson Cooper-Driver | 9f7ab5b | 2025-03-31 14:27:53 +0100 | [diff] [blame] | 77 | file(REAL_PATH "${CMAKE_SOURCE_DIR}/../" TOP_LEVEL_PROJECT_DIR) |
| 78 | |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 79 | add_compile_options( |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 80 | -Wall |
| 81 | -Wno-format |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 82 | -Wno-unused-but-set-variable |
Antonio de Angelis | 86d1f71 | 2025-05-02 11:51:31 +0100 | [diff] [blame] | 83 | -Wnull-dereference |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 84 | -c |
| 85 | -fdata-sections |
| 86 | -ffunction-sections |
| 87 | -fno-builtin |
| 88 | -fshort-enums |
| 89 | -funsigned-char |
Jackson Cooper-Driver | 9f7ab5b | 2025-03-31 14:27:53 +0100 | [diff] [blame] | 90 | # Strip /workspace/ |
| 91 | -fmacro-prefix-map=${TOP_LEVEL_PROJECT_DIR}/= |
| 92 | # Strip /workspace/trusted-firmware-m |
Jackson Cooper-Driver | 070a6e4 | 2025-03-18 17:03:56 +0000 | [diff] [blame] | 93 | -fmacro-prefix-map=${CMAKE_SOURCE_DIR}/= |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 94 | -mthumb |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 95 | $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g> |
Jackson Cooper-Driver | df542cf | 2025-04-01 14:34:11 +0100 | [diff] [blame] | 96 | $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${TFM_DEBUG_OPTIMISATION}>,$<CONFIG:Debug>>:-Og> |
Bohdan Hunko | c7d222b | 2024-11-05 16:37:21 +0200 | [diff] [blame] | 97 | $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${CONFIG_TFM_WARNINGS_ARE_ERRORS}>>:-Werror> |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 98 | ) |
| 99 | |
| 100 | add_link_options( |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 101 | -mcpu=${TFM_SYSTEM_PROCESSOR} |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 102 | -specs=nano.specs |
Jamie Fox | 13feddb | 2024-01-23 18:58:42 +0000 | [diff] [blame] | 103 | -specs=nosys.specs |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 104 | LINKER:-check-sections |
| 105 | LINKER:-fatal-warnings |
| 106 | LINKER:--gc-sections |
| 107 | LINKER:--no-wchar-size-warning |
| 108 | LINKER:--print-memory-usage |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 109 | ) |
| 110 | |
David Hu | 500ea2f | 2023-10-13 12:15:08 +0800 | [diff] [blame] | 111 | # 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. |
| 123 | macro(target_add_scatter_file target) |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 124 | |
David Hu | 500ea2f | 2023-10-13 12:15:08 +0800 | [diff] [blame] | 125 | get_target_property(scatter_file |
| 126 | platform_ns |
| 127 | INTERFACE_LINK_DEPENDS |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 128 | ) |
| 129 | |
David Hu | 500ea2f | 2023-10-13 12:15:08 +0800 | [diff] [blame] | 130 | # 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 Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 154 | LANGUAGE C |
| 155 | KEEP_EXTENSION True) |
| 156 | |
David Hu | 500ea2f | 2023-10-13 12:15:08 +0800 | [diff] [blame] | 157 | target_compile_options(${target}_scatter |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 158 | PRIVATE |
| 159 | -E |
| 160 | -P |
| 161 | -xc |
| 162 | ) |
| 163 | |
Dávid Házi | 80427ea | 2024-04-17 21:07:17 +0200 | [diff] [blame] | 164 | target_compile_definitions(${target}_scatter |
| 165 | PRIVATE |
| 166 | $<$<NOT:$<BOOL:${CONFIG_GNU_LINKER_READONLY_ATTRIBUTE}>>:READONLY=> |
| 167 | ) |
| 168 | |
David Hu | 35aa1a5 | 2023-10-24 23:04:04 +0800 | [diff] [blame] | 169 | target_link_libraries(${target}_scatter |
| 170 | PRIVATE |
| 171 | platform_region_defs |
| 172 | ) |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 173 | |
David Hu | 500ea2f | 2023-10-13 12:15:08 +0800 | [diff] [blame] | 174 | add_dependencies(${target} ${target}_scatter) |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 175 | |
David Hu | 99d8292 | 2023-10-29 13:35:51 +0800 | [diff] [blame] | 176 | target_link_options(${target} |
| 177 | PRIVATE |
| 178 | -T $<TARGET_OBJECTS:${target}_scatter> |
| 179 | ) |
| 180 | |
Anton Komlev | aee4b61 | 2023-05-14 17:38:36 +0100 | [diff] [blame] | 181 | endmacro() |
David Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 182 | |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 183 | # Macro for converting the output *.axf file to finary files: bin, elf, hex |
David Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 184 | macro(add_convert_to_bin_target target) |
| 185 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
David Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 186 | add_custom_target(${target}_bin |
Anton Komlev | 8ab4f04 | 2025-06-03 21:13:07 +0100 | [diff] [blame] | 187 | 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 Hu | 9e1b263 | 2023-10-14 13:35:24 +0800 | [diff] [blame] | 191 | ) |
| 192 | endmacro() |