Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
Kshitij Sisodia | 26bc923 | 2023-03-10 16:33:23 +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(CMAKE_C_COMPILER armclang) |
| 19 | set(CMAKE_CXX_COMPILER armclang) |
| 20 | set(CMAKE_C_LINKER_PREFERENCE armlink) |
| 21 | set(CMAKE_ASM_LINKER_PREFERENCE armlink) |
| 22 | set(CMAKE_ASM_COMPILER armasm) |
| 23 | set(CMAKE_ASM_COMPILER_AR armar) |
| 24 | |
| 25 | set(CMAKE_CROSSCOMPILING true) |
| 26 | set(CMAKE_SYSTEM_NAME Generic) |
| 27 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 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 | |
Nina Drozd | 9263b51 | 2023-09-05 17:38:30 +0100 | [diff] [blame] | 32 | set(MIN_ARM_CLANG_VERSION 6.19) |
| 33 | |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 34 | if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH) |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 35 | 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] | 36 | endif() |
| 37 | |
| 38 | if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55) |
| 39 | # Flags for cortex-m55 |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 40 | set(CPU_ID M55) |
| 41 | set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID}) |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 42 | set(ARM_CPU "ARMC${CPU_ID}") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 43 | set(CPU_HEADER_FILE "${ARM_CPU}.h") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 44 | set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 45 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 46 | set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}") |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 47 | elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85) |
| 48 | # Flags for cortex-m85 |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 49 | set(CPU_ID M85) |
| 50 | set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID}) |
| 51 | set(ARM_CPU "ARMC${CPU_ID}") |
| 52 | set(CPU_HEADER_FILE "${ARM_CPU}.h") |
| 53 | set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 54 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
| 55 | set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}") |
| 56 | elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main) |
| 57 | # Flags for generic armv8.1-m profile |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 58 | set(CPU_ID ARMv81MML_DSP_DP_MVE_FP) |
| 59 | set(ARM_CPU "ARMv81MML") |
| 60 | set(CPU_COMPILE_DEF ${CPU_ID}) |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 61 | |
| 62 | # @TODO: Revise once we have the CPU file in CMSIS and CPU flags |
| 63 | # are supported by toolchains. |
| 64 | set(CPU_HEADER_FILE "${CPU_ID}.h") |
| 65 | 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] | 66 | set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard") |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 67 | set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 68 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33) |
| 69 | # Flags for cortex-m33 to go here |
| 70 | endif() |
| 71 | |
| 72 | set(${CPU_COMPILE_DEF} 1) |
| 73 | |
| 74 | # Warning options |
| 75 | add_compile_options( |
| 76 | -Wall |
| 77 | -Wextra |
| 78 | -Wvla) |
| 79 | |
| 80 | # General purpose compile options: |
| 81 | add_compile_options( |
| 82 | -funsigned-char |
| 83 | -fno-function-sections |
| 84 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>") |
| 85 | |
| 86 | # Arch compile options: |
| 87 | add_compile_options( |
| 88 | -mthumb |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 89 | --target=arm-arm-non-eabi |
| 90 | -mlittle-endian |
Kshitij Sisodia | 8c61c0a | 2022-05-17 11:16:22 +0100 | [diff] [blame] | 91 | -MD |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 92 | ${CPU_COMPILE_OPTION} |
| 93 | ${FLOAT_ABI_COMPILE_OPTION}) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 94 | |
| 95 | # Compile definitions: |
| 96 | add_compile_definitions( |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 97 | CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\" |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 98 | $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 99 | |
| 100 | # Link options: |
| 101 | add_link_options(${CPU_LINK_OPT}) |
| 102 | set(CMAKE_ASM_FLAGS "${CPU_LINK_OPT}") |
| 103 | |
Kshitij Sisodia | 7e56d8f | 2022-04-11 10:34:29 +0100 | [diff] [blame] | 104 | set(ARMCLANG_INFO_STR "sizes,totals,unused,veneers,summarysizes") |
| 105 | if(CMAKE_BUILD_TYPE STREQUAL Debug) |
| 106 | # For debug builds, we can add stack information too: |
| 107 | set(ARMCLANG_INFO_STR "${ARMCLANG_INFO_STR},stack,summarystack") |
| 108 | endif() |
| 109 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 110 | # Warnings to be ignored: |
| 111 | # L6314W = No section matches pattern |
| 112 | # L6439W = Multiply defined Global Symbol |
| 113 | add_link_options( |
| 114 | --diag_suppress=L6439W,L6314W |
Kshitij Sisodia | 7e56d8f | 2022-04-11 10:34:29 +0100 | [diff] [blame] | 115 | --info ${ARMCLANG_INFO_STR} |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 116 | --strict |
| 117 | --callgraph |
Kshitij Sisodia | 7e56d8f | 2022-04-11 10:34:29 +0100 | [diff] [blame] | 118 | --no_exceptions |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 119 | --load_addr_map_info |
| 120 | --xref |
| 121 | "$<$<CONFIG:RELEASE>:--no_debug>") |
| 122 | |
Kshitij Sisodia | acc6b85 | 2022-03-01 10:23:11 +0000 | [diff] [blame] | 123 | function(configure_semihosting TARGET_NAME SEMIHOSTING) |
| 124 | if (${SEMIHOSTING}) |
| 125 | target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING) |
| 126 | endif() |
| 127 | endfunction() |
| 128 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 129 | # Function to add a map file output for the linker to dump diagnostic information to. |
| 130 | function(add_target_map_file TARGET_NAME MAP_FILE_PATH) |
| 131 | target_link_options(${TARGET_NAME} PUBLIC |
| 132 | --map --symbols --list=${MAP_FILE_PATH}) |
| 133 | endfunction() |
| 134 | |
| 135 | # Function to add linker option to use the chosen linker script (scatter file). |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 136 | function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME) |
| 137 | set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.sct) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 138 | if (NOT EXISTS ${LINKER_SCRIPT_PATH}) |
| 139 | message(FATAL_ERROR "Scatter file not found: ${LINKER_SCRIPT_PATH}") |
| 140 | endif() |
| 141 | message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 142 | target_link_options(${TARGET_NAME} PUBLIC |
| 143 | --scatter=${LINKER_SCRIPT_PATH}) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 144 | endfunction() |
| 145 | |
| 146 | # Function to set the command to copy/extract contents from an elf |
| 147 | # into a binary file. |
| 148 | function(add_bin_generation_command) |
| 149 | |
| 150 | set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES) |
| 151 | set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH) |
| 152 | cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 153 | |
| 154 | add_custom_command(TARGET ${PARSED_TARGET_NAME} |
| 155 | POST_BUILD |
| 156 | COMMAND fromelf --bin --output=${PARSED_OUTPUT_DIR}/ |
| 157 | ${PARSED_AXF_PATH}) |
| 158 | |
| 159 | endfunction() |
| 160 | |
| 161 | # Function to assert the compiler version |
| 162 | function(enforce_compiler_version) |
| 163 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_ARM_CLANG_VERSION}) |
Kshitij Sisodia | ee98bd6 | 2022-01-17 13:38:54 +0000 | [diff] [blame] | 164 | message( FATAL_ERROR "Arm compiler version must be ${MIN_ARM_CLANG_VERSION} or greater." ) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 165 | endif() |
| 166 | endfunction() |