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