blob: f8297628e3145952c71b459062db101a9318a019 [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
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010032if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH)
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010033 set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010034endif()
35
36if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55)
37 # Flags for cortex-m55
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010038 set(MIN_ARM_CLANG_VERSION 6.16)
ayamas0115f80702021-11-18 14:22:23 +000039 set(CPU_ID M55)
40 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
ayamas0115f80702021-11-18 14:22:23 +000041 set(ARM_CPU "ARMC${CPU_ID}")
ayamas0115f80702021-11-18 14:22:23 +000042 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010043 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010044 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
ayamas0115f80702021-11-18 14:22:23 +000045 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010046elseif (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}")
56elseif (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 Sisodia8c61c0a2022-05-17 11:16:22 +010059 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
60 set(ARM_CPU "ARMv81MML")
61 set(CPU_COMPILE_DEF ${CPU_ID})
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010062
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 Sisodia9c6f9f82022-05-20 14:30:02 +010067 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010068 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010069elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
70 # Flags for cortex-m33 to go here
71endif()
72
Kshitij Sisodia26bc9232023-03-10 16:33:23 +000073if (NOT DEFINED MIN_ARM_CLANG_VERSION)
74 set(MIN_ARM_CLANG_VERSION 6.16)
75endif()
76
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010077set(${CPU_COMPILE_DEF} 1)
78
79# Warning options
80add_compile_options(
81 -Wall
82 -Wextra
83 -Wvla)
84
85# General purpose compile options:
86add_compile_options(
87 -funsigned-char
88 -fno-function-sections
89 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
90
91# Arch compile options:
92add_compile_options(
93 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094 --target=arm-arm-non-eabi
95 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010096 -MD
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010097 ${CPU_COMPILE_OPTION}
98 ${FLOAT_ABI_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010099
100# Compile definitions:
101add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +0000102 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100103 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100104
105# Link options:
106add_link_options(${CPU_LINK_OPT})
107set(CMAKE_ASM_FLAGS "${CPU_LINK_OPT}")
108
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100109set(ARMCLANG_INFO_STR "sizes,totals,unused,veneers,summarysizes")
110if(CMAKE_BUILD_TYPE STREQUAL Debug)
111 # For debug builds, we can add stack information too:
112 set(ARMCLANG_INFO_STR "${ARMCLANG_INFO_STR},stack,summarystack")
113endif()
114
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115# Warnings to be ignored:
116# L6314W = No section matches pattern
117# L6439W = Multiply defined Global Symbol
118add_link_options(
119 --diag_suppress=L6439W,L6314W
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100120 --info ${ARMCLANG_INFO_STR}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100121 --strict
122 --callgraph
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100123 --no_exceptions
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100124 --load_addr_map_info
125 --xref
126 "$<$<CONFIG:RELEASE>:--no_debug>")
127
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000128function(configure_semihosting TARGET_NAME SEMIHOSTING)
129 if (${SEMIHOSTING})
130 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
131 endif()
132endfunction()
133
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100134# Function to add a map file output for the linker to dump diagnostic information to.
135function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
136 target_link_options(${TARGET_NAME} PUBLIC
137 --map --symbols --list=${MAP_FILE_PATH})
138endfunction()
139
140# Function to add linker option to use the chosen linker script (scatter file).
alexander31ae9f02022-02-10 16:15:54 +0000141function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
142 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.sct)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100143 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
144 message(FATAL_ERROR "Scatter file not found: ${LINKER_SCRIPT_PATH}")
145 endif()
146 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000147 target_link_options(${TARGET_NAME} PUBLIC
148 --scatter=${LINKER_SCRIPT_PATH})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100149endfunction()
150
151# Function to set the command to copy/extract contents from an elf
152# into a binary file.
153function(add_bin_generation_command)
154
155 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
156 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
157 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
158
159 add_custom_command(TARGET ${PARSED_TARGET_NAME}
160 POST_BUILD
161 COMMAND fromelf --bin --output=${PARSED_OUTPUT_DIR}/
162 ${PARSED_AXF_PATH})
163
164endfunction()
165
166# Function to assert the compiler version
167function(enforce_compiler_version)
168 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_ARM_CLANG_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000169 message( FATAL_ERROR "Arm compiler version must be ${MIN_ARM_CLANG_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100170 endif()
171endfunction()