Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 26 | # Skip compiler test execution |
| 27 | set(CMAKE_C_COMPILER_WORKS 1) |
| 28 | set(CMAKE_CXX_COMPILER_WORKS 1) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 29 | |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 30 | if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH) |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 31 | 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] | 32 | endif() |
| 33 | |
| 34 | if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55) |
| 35 | # Flags for cortex-m55 |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 36 | set(MIN_GCC_VERSION 10.2.1) |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 37 | set(CPU_ID M55) |
| 38 | set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID}) |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 39 | set(ARM_CPU "ARMC${CPU_ID}") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 40 | set(CPU_HEADER_FILE "${ARM_CPU}.h") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 41 | set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 42 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 43 | set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 44 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85) |
| 45 | # Flags for cortex-m85 |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 46 | set(MIN_GCC_VERSION 13.2.1) |
| 47 | set(CPU_ID M85) |
| 48 | set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID}) |
| 49 | set(ARM_CPU "ARMC${CPU_ID}") |
| 50 | set(CPU_HEADER_FILE "${ARM_CPU}.h") |
| 51 | set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 52 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
| 53 | set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 54 | elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main) |
| 55 | # Flags for generic target armv8.1-m.main (will work for cortex-m55 and cortex-m85 |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 56 | set(MIN_GCC_VERSION 10.2.1) |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 57 | set(CPU_ID ARMv81MML_DSP_DP_MVE_FP) |
| 58 | set(ARM_CPU "ARMv81MML") |
| 59 | set(CPU_COMPILE_DEF ${CPU_ID}) |
| 60 | set(FLOAT_ABI hard) |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 61 | set(CPU_HEADER_FILE "${CPU_ID}.h") |
| 62 | 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] | 63 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 64 | set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 65 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33) |
| 66 | # Flags for cortex-m33 to go here |
| 67 | endif() |
| 68 | |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 69 | if (NOT DEFINED MIN_GCC_VERSION) |
| 70 | set(MIN_GCC_VERSION 10.2.1) |
| 71 | endif() |
| 72 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 73 | set(${CPU_COMPILE_DEF} 1) |
| 74 | |
| 75 | # Warning options |
| 76 | add_compile_options( |
| 77 | -Wall |
| 78 | -Wextra |
| 79 | -Wvla |
| 80 | -Wno-psabi) |
| 81 | |
| 82 | # General purpose compile options: |
| 83 | add_compile_options( |
| 84 | -funsigned-char |
| 85 | -fno-function-sections |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 86 | -fdata-sections |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 87 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>") |
| 88 | |
| 89 | # Arch compile options: |
| 90 | add_compile_options( |
| 91 | -mthumb |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 92 | -mlittle-endian |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 93 | -MD |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 94 | ${FLOAT_ABI_COMPILE_OPTION} |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 95 | ${CPU_COMPILE_OPTION}) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 96 | |
| 97 | # Compile definitions: |
| 98 | add_compile_definitions( |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 99 | $<$<BOOL:${CPU_HEADER_FILE}>:CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"> |
| 100 | $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 101 | |
| 102 | # Link options: |
| 103 | add_link_options( |
| 104 | -mthumb |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 105 | ${CPU_COMPILE_OPTION} |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 106 | ${FLOAT_ABI_COMPILE_OPTION} |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 107 | -mlittle-endian |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 108 | --stats |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 109 | "SHELL:-Xlinker --gc-sections" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 110 | "$<$<CONFIG:RELEASE>:--no-debug>") |
| 111 | |
Kshitij Sisodia | acc6b85 | 2022-03-01 10:23:11 +0000 | [diff] [blame] | 112 | function(configure_semihosting TARGET_NAME SEMIHOSTING) |
| 113 | if (${SEMIHOSTING}) |
| 114 | target_link_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs") |
| 115 | target_compile_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs") |
| 116 | target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING) |
| 117 | else() |
| 118 | target_link_options(${TARGET_NAME} PUBLIC --specs=nosys.specs) |
| 119 | target_compile_options(${TARGET_NAME} PUBLIC "--specs=nosys.specs") |
| 120 | endif() |
| 121 | endfunction() |
| 122 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 123 | # Function to add a map file output for the linker to dump diagnostic information to. |
| 124 | function(add_target_map_file TARGET_NAME MAP_FILE_PATH) |
| 125 | target_link_options(${TARGET_NAME} PUBLIC |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 126 | "SHELL:-Xlinker -Map=${MAP_FILE_PATH}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 127 | endfunction() |
| 128 | |
| 129 | # Function to add linker option to use the chosen linker script. |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 130 | function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 131 | set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld |
| 132 | CACHE STRING "Linker script path") |
| 133 | if (NOT EXISTS ${LINKER_SCRIPT_PATH}) |
| 134 | message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}") |
| 135 | endif() |
| 136 | message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 137 | target_link_options(${TARGET_NAME} PUBLIC |
| 138 | "SHELL:-T ${LINKER_SCRIPT_PATH}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 139 | endfunction() |
| 140 | |
| 141 | # Function to set the command to copy/extract contents from an elf |
| 142 | # into a binary file. |
| 143 | function(add_bin_generation_command) |
| 144 | |
| 145 | set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES) |
| 146 | set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH) |
| 147 | cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 148 | |
| 149 | list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS) |
| 150 | list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES) |
| 151 | |
| 152 | if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES}) |
| 153 | message(FATAL_ERROR "Section patterns and the output binary names " |
| 154 | "should be of the same length") |
| 155 | endif() |
| 156 | |
| 157 | message(STATUS "${TRIPLET}-objcopy requested to generate " |
| 158 | "${N_OUTPUT_BIN_NAMES} bin files.") |
| 159 | |
| 160 | math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1") |
| 161 | |
| 162 | foreach(IDX RANGE ${MAX_IDX}) |
| 163 | |
| 164 | list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME) |
| 165 | list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN) |
| 166 | |
| 167 | add_custom_command(TARGET ${PARSED_TARGET_NAME} |
| 168 | POST_BUILD |
| 169 | COMMAND ${TRIPLET}-objcopy -O binary |
| 170 | --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH} |
| 171 | ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME}) |
| 172 | endforeach() |
| 173 | |
| 174 | endfunction() |
| 175 | |
| 176 | # Function to assert the compiler version |
| 177 | function(enforce_compiler_version) |
| 178 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION}) |
Kshitij Sisodia | ee98bd6 | 2022-01-17 13:38:54 +0000 | [diff] [blame] | 179 | 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] | 180 | endif() |
| 181 | endfunction() |