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