blob: fcea11873756458cdb0bcb9505eda2244c2a62f4 [file] [log] [blame]
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +01001#----------------------------------------------------------------------------
Kshitij Sisodia26bc9232023-03-10 16:33:23 +00002# SPDX-FileCopyrightText: Copyright 2021 - 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +01003# 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
18set(CMAKE_C_COMPILER armclang)
19set(CMAKE_CXX_COMPILER armclang)
20set(CMAKE_C_LINKER_PREFERENCE armlink)
21set(CMAKE_ASM_LINKER_PREFERENCE armlink)
22set(CMAKE_ASM_COMPILER armasm)
23set(CMAKE_ASM_COMPILER_AR armar)
24
25set(CMAKE_CROSSCOMPILING true)
26set(CMAKE_SYSTEM_NAME Generic)
27
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010028# Skip compiler test execution
29set(CMAKE_C_COMPILER_WORKS 1)
30set(CMAKE_CXX_COMPILER_WORKS 1)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010031
Nina Drozd9263b512023-09-05 17:38:30 +010032set(MIN_ARM_CLANG_VERSION 6.19)
33
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010034if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH)
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010035 set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010036endif()
37
38if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55)
39 # Flags for cortex-m55
ayamas0115f80702021-11-18 14:22:23 +000040 set(CPU_ID M55)
41 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
ayamas0115f80702021-11-18 14:22:23 +000042 set(ARM_CPU "ARMC${CPU_ID}")
ayamas0115f80702021-11-18 14:22:23 +000043 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010044 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010045 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
ayamas0115f80702021-11-18 14:22:23 +000046 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010047elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85)
48 # Flags for cortex-m85
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010049 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}")
56elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main)
57 # Flags for generic armv8.1-m profile
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010058 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
59 set(ARM_CPU "ARMv81MML")
60 set(CPU_COMPILE_DEF ${CPU_ID})
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010061
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 Sisodia9c6f9f82022-05-20 14:30:02 +010066 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010067 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010068elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
69 # Flags for cortex-m33 to go here
70endif()
71
72set(${CPU_COMPILE_DEF} 1)
73
74# Warning options
75add_compile_options(
76 -Wall
77 -Wextra
78 -Wvla)
79
80# General purpose compile options:
81add_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:
87add_compile_options(
88 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089 --target=arm-arm-non-eabi
90 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010091 -MD
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010092 ${CPU_COMPILE_OPTION}
93 ${FLOAT_ABI_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094
95# Compile definitions:
96add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +000097 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010098 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010099
100# Link options:
101add_link_options(${CPU_LINK_OPT})
102set(CMAKE_ASM_FLAGS "${CPU_LINK_OPT}")
103
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100104set(ARMCLANG_INFO_STR "sizes,totals,unused,veneers,summarysizes")
105if(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")
108endif()
109
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100110# Warnings to be ignored:
111# L6314W = No section matches pattern
112# L6439W = Multiply defined Global Symbol
113add_link_options(
114 --diag_suppress=L6439W,L6314W
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100115 --info ${ARMCLANG_INFO_STR}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100116 --strict
117 --callgraph
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100118 --no_exceptions
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100119 --load_addr_map_info
120 --xref
121 "$<$<CONFIG:RELEASE>:--no_debug>")
122
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000123function(configure_semihosting TARGET_NAME SEMIHOSTING)
124 if (${SEMIHOSTING})
125 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
126 endif()
127endfunction()
128
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100129# Function to add a map file output for the linker to dump diagnostic information to.
130function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
131 target_link_options(${TARGET_NAME} PUBLIC
132 --map --symbols --list=${MAP_FILE_PATH})
133endfunction()
134
135# Function to add linker option to use the chosen linker script (scatter file).
alexander31ae9f02022-02-10 16:15:54 +0000136function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
137 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.sct)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100138 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}")
alexander31ae9f02022-02-10 16:15:54 +0000142 target_link_options(${TARGET_NAME} PUBLIC
143 --scatter=${LINKER_SCRIPT_PATH})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100144endfunction()
145
146# Function to set the command to copy/extract contents from an elf
147# into a binary file.
148function(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
159endfunction()
160
161# Function to assert the compiler version
162function(enforce_compiler_version)
163 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_ARM_CLANG_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000164 message( FATAL_ERROR "Arm compiler version must be ${MIN_ARM_CLANG_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100165 endif()
166endfunction()