Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
Isabella Gottardi | c64f506 | 2022-01-21 15:27:13 +0000 | [diff] [blame] | 2 | # Copyright (c) 2021 - 2022 Arm Limited. All rights reserved. |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | #---------------------------------------------------------------------------- |
| 17 | # specify the cross compiler |
| 18 | set(TRIPLET arm-none-eabi) |
| 19 | |
| 20 | set(CMAKE_C_COMPILER ${TRIPLET}-gcc) |
| 21 | set(CMAKE_CXX_COMPILER ${TRIPLET}-g++) |
| 22 | |
| 23 | set(CMAKE_CROSSCOMPILING true) |
| 24 | set(CMAKE_SYSTEM_NAME Generic) |
| 25 | |
| 26 | set(MIN_GCC_VERSION 10.2.1) |
| 27 | |
| 28 | # Skip compiler test execution |
| 29 | set(CMAKE_C_COMPILER_WORKS 1) |
| 30 | set(CMAKE_CXX_COMPILER_WORKS 1) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 31 | |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 32 | if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH) |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 33 | set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 34 | endif() |
| 35 | |
| 36 | if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55) |
| 37 | # Flags for cortex-m55 |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 38 | set(CPU_ID M55) |
| 39 | set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID}) |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 40 | set(ARM_CPU "ARMC${CPU_ID}") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 41 | set(CPU_HEADER_FILE "${ARM_CPU}.h") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 42 | set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 43 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 44 | set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 45 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85) |
| 46 | # Flags for cortex-m85 |
| 47 | # @TODO: Current versions of GNU compiler do not support Cortex-M85, we compile for Cortex-M55 instead. |
| 48 | message(WARNING "Arm GNU Toolchain does not support Arm Cortex-M85 yet, switching to Cortex-M55.") |
| 49 | set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use" FORCE) |
| 50 | # No need to duplicate the definitions here. |
| 51 | # Flags from Cortex-M55 will be added as this toolchain file will be read by CMake again. |
| 52 | elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main) |
| 53 | # Flags for generic target armv8.1-m.main (will work for cortex-m55 and cortex-m85 |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 54 | set(CPU_ID ARMv81MML_DSP_DP_MVE_FP) |
| 55 | set(ARM_CPU "ARMv81MML") |
| 56 | set(CPU_COMPILE_DEF ${CPU_ID}) |
| 57 | set(FLOAT_ABI hard) |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 58 | set(CPU_HEADER_FILE "${CPU_ID}.h") |
| 59 | set(CPU_COMPILE_OPTION "-march=armv8.1-m.main+mve.fp+fp.dp") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 60 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 61 | set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 62 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33) |
| 63 | # Flags for cortex-m33 to go here |
| 64 | endif() |
| 65 | |
| 66 | set(${CPU_COMPILE_DEF} 1) |
| 67 | |
| 68 | # Warning options |
| 69 | add_compile_options( |
| 70 | -Wall |
| 71 | -Wextra |
| 72 | -Wvla |
| 73 | -Wno-psabi) |
| 74 | |
| 75 | # General purpose compile options: |
| 76 | add_compile_options( |
| 77 | -funsigned-char |
| 78 | -fno-function-sections |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 79 | -fdata-sections |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 80 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>") |
| 81 | |
| 82 | # Arch compile options: |
| 83 | add_compile_options( |
| 84 | -mthumb |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 85 | -mlittle-endian |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 86 | -MD |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 87 | ${FLOAT_ABI_COMPILE_OPTION} |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 88 | ${CPU_COMPILE_OPTION}) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 89 | |
| 90 | # Compile definitions: |
| 91 | add_compile_definitions( |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 92 | $<$<BOOL:${CPU_HEADER_FILE}>:CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"> |
| 93 | $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 94 | |
| 95 | # Link options: |
| 96 | add_link_options( |
| 97 | -mthumb |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 98 | ${CPU_COMPILE_OPTION} |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 99 | ${FLOAT_ABI_COMPILE_OPTION} |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 100 | -mlittle-endian |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 101 | --stats |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 102 | "SHELL:-Xlinker --gc-sections" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 103 | "$<$<CONFIG:RELEASE>:--no-debug>") |
| 104 | |
Kshitij Sisodia | acc6b85 | 2022-03-01 10:23:11 +0000 | [diff] [blame] | 105 | function(configure_semihosting TARGET_NAME SEMIHOSTING) |
| 106 | if (${SEMIHOSTING}) |
| 107 | target_link_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs") |
| 108 | target_compile_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs") |
| 109 | target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING) |
| 110 | else() |
| 111 | target_link_options(${TARGET_NAME} PUBLIC --specs=nosys.specs) |
| 112 | target_compile_options(${TARGET_NAME} PUBLIC "--specs=nosys.specs") |
| 113 | endif() |
| 114 | endfunction() |
| 115 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 116 | # Function to add a map file output for the linker to dump diagnostic information to. |
| 117 | function(add_target_map_file TARGET_NAME MAP_FILE_PATH) |
| 118 | target_link_options(${TARGET_NAME} PUBLIC |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 119 | "SHELL:-Xlinker -Map=${MAP_FILE_PATH}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 120 | endfunction() |
| 121 | |
| 122 | # Function to add linker option to use the chosen linker script. |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 123 | function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 124 | set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld |
| 125 | CACHE STRING "Linker script path") |
| 126 | if (NOT EXISTS ${LINKER_SCRIPT_PATH}) |
| 127 | message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}") |
| 128 | endif() |
| 129 | message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 130 | target_link_options(${TARGET_NAME} PUBLIC |
| 131 | "SHELL:-T ${LINKER_SCRIPT_PATH}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 132 | endfunction() |
| 133 | |
| 134 | # Function to set the command to copy/extract contents from an elf |
| 135 | # into a binary file. |
| 136 | function(add_bin_generation_command) |
| 137 | |
| 138 | set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES) |
| 139 | set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH) |
| 140 | cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 141 | |
| 142 | list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS) |
| 143 | list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES) |
| 144 | |
| 145 | if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES}) |
| 146 | message(FATAL_ERROR "Section patterns and the output binary names " |
| 147 | "should be of the same length") |
| 148 | endif() |
| 149 | |
| 150 | message(STATUS "${TRIPLET}-objcopy requested to generate " |
| 151 | "${N_OUTPUT_BIN_NAMES} bin files.") |
| 152 | |
| 153 | math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1") |
| 154 | |
| 155 | foreach(IDX RANGE ${MAX_IDX}) |
| 156 | |
| 157 | list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME) |
| 158 | list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN) |
| 159 | |
| 160 | add_custom_command(TARGET ${PARSED_TARGET_NAME} |
| 161 | POST_BUILD |
| 162 | COMMAND ${TRIPLET}-objcopy -O binary |
| 163 | --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH} |
| 164 | ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME}) |
| 165 | endforeach() |
| 166 | |
| 167 | endfunction() |
| 168 | |
| 169 | # Function to assert the compiler version |
| 170 | function(enforce_compiler_version) |
| 171 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION}) |
Kshitij Sisodia | ee98bd6 | 2022-01-17 13:38:54 +0000 | [diff] [blame] | 172 | message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater." ) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 173 | endif() |
| 174 | endfunction() |