blob: 97da76fec95376c68352a93d49202a438e501f83 [file] [log] [blame]
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +01001#----------------------------------------------------------------------------
Isabella Gottardic64f5062022-01-21 15:27:13 +00002# Copyright (c) 2021 - 2022 Arm Limited. All rights reserved.
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 Sisodiaee98bd62022-01-17 13:38:54 +000028set(MIN_ARM_CLANG_VERSION 6.16)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010029
30# Skip compiler test execution
31set(CMAKE_C_COMPILER_WORKS 1)
32set(CMAKE_CXX_COMPILER_WORKS 1)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010033
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010034if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010035 set(CMAKE_SYSTEM_PROCESSOR cortex-m55)
36endif()
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}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010043 set(FLOAT_ABI hard)
44 set(ARM_MATH_DSP 1)
45 set(ARM_MATH_LOOPUNROLL 1)
ayamas0115f80702021-11-18 14:22:23 +000046 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010047 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
ayamas0115f80702021-11-18 14:22:23 +000048 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010049elseif (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 Sisodiaf9c19ea2021-05-07 16:08:14 +010063elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
64 # Flags for cortex-m33 to go here
65endif()
66
67set(${CPU_COMPILE_DEF} 1)
68
69# Warning options
70add_compile_options(
71 -Wall
72 -Wextra
73 -Wvla)
74
75# General purpose compile options:
76add_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:
82add_compile_options(
83 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010084 -mfloat-abi=${FLOAT_ABI}
85 --target=arm-arm-non-eabi
86 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010087 -MD
88 ${CPU_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089
90# Compile definitions:
91add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +000092 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
93 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094 $<$<BOOL:${ARM_MATH_DSP}>:ARM_MATH_DSP>
95 $<$<BOOL:${ARM_MATH_LOOPUNROLL}>:ARM_MATH_LOOPUNROLL>)
96
97# Link options:
98add_link_options(${CPU_LINK_OPT})
99set(CMAKE_ASM_FLAGS "${CPU_LINK_OPT}")
100
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100101set(ARMCLANG_INFO_STR "sizes,totals,unused,veneers,summarysizes")
102if(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")
105endif()
106
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100107# Warnings to be ignored:
108# L6314W = No section matches pattern
109# L6439W = Multiply defined Global Symbol
110add_link_options(
111 --diag_suppress=L6439W,L6314W
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100112 --info ${ARMCLANG_INFO_STR}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100113 --strict
114 --callgraph
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100115 --no_exceptions
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100116 --load_addr_map_info
117 --xref
118 "$<$<CONFIG:RELEASE>:--no_debug>")
119
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000120function(configure_semihosting TARGET_NAME SEMIHOSTING)
121 if (${SEMIHOSTING})
122 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
123 endif()
124endfunction()
125
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100126# Function to add a map file output for the linker to dump diagnostic information to.
127function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
128 target_link_options(${TARGET_NAME} PUBLIC
129 --map --symbols --list=${MAP_FILE_PATH})
130endfunction()
131
132# Function to add linker option to use the chosen linker script (scatter file).
alexander31ae9f02022-02-10 16:15:54 +0000133function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
134 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.sct)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100135 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}")
alexander31ae9f02022-02-10 16:15:54 +0000139 target_link_options(${TARGET_NAME} PUBLIC
140 --scatter=${LINKER_SCRIPT_PATH})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100141endfunction()
142
143# Function to set the command to copy/extract contents from an elf
144# into a binary file.
145function(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
156endfunction()
157
158# Function to assert the compiler version
159function(enforce_compiler_version)
160 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_ARM_CLANG_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000161 message( FATAL_ERROR "Arm compiler version must be ${MIN_ARM_CLANG_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100162 endif()
163endfunction()