blob: a6d6c0e2240261a9def32099512fe2d79a33b14b [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(TRIPLET arm-none-eabi)
19
20set(CMAKE_C_COMPILER ${TRIPLET}-gcc)
21set(CMAKE_CXX_COMPILER ${TRIPLET}-g++)
22
23set(CMAKE_CROSSCOMPILING true)
24set(CMAKE_SYSTEM_NAME Generic)
25
26set(MIN_GCC_VERSION 10.2.1)
27
28# 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 Sisodiaf9c19ea2021-05-07 16:08:14 +010033 set(CMAKE_SYSTEM_PROCESSOR cortex-m55)
34endif()
35
36if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55)
37 # Flags for cortex-m55
ayamas0115f80702021-11-18 14:22:23 +000038 set(CPU_ID M55)
39 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
ayamas0115f80702021-11-18 14:22:23 +000040 set(ARM_CPU "ARMC${CPU_ID}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010041 set(FLOAT_ABI hard)
42 set(ARM_MATH_DSP 1)
43 set(ARM_MATH_LOOPUNROLL 1)
ayamas0115f80702021-11-18 14:22:23 +000044 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010045 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
ayamas0115f80702021-11-18 14:22:23 +000046 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010047elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85 OR CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main)
48 # Flags for Cortex-M85
49 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
50 set(ARM_CPU "ARMv81MML")
51 set(CPU_COMPILE_DEF ${CPU_ID})
52 set(FLOAT_ABI hard)
53 set(ARM_MATH_DSP 1)
54 set(ARM_MATH_LOOPUNROLL 1)
55
56 # @TODO: Revise once we have the CPU file in CMSIS and CPU flags
57 # are supported by toolchains.
58 set(CPU_HEADER_FILE "${CPU_ID}.h")
59 set(CPU_COMPILE_OPTION "-march=armv8.1-m.main+mve.fp+fp.dp")
60 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010061elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
62 # Flags for cortex-m33 to go here
63endif()
64
65set(${CPU_COMPILE_DEF} 1)
66
67# Warning options
68add_compile_options(
69 -Wall
70 -Wextra
71 -Wvla
72 -Wno-psabi)
73
74# General purpose compile options:
75add_compile_options(
76 -funsigned-char
77 -fno-function-sections
Kshitij Sisodia661959c2021-11-24 10:39:52 +000078 -fdata-sections
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010079 "$<$<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 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010086 -MD
87 ${CPU_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010088
89# Compile definitions:
90add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +000091 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
92 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093 $<$<BOOL:${ARM_MATH_DSP}>:ARM_MATH_DSP>
94 $<$<BOOL:${ARM_MATH_LOOPUNROLL}>:ARM_MATH_LOOPUNROLL>)
95
96# Link options:
97add_link_options(
98 -mthumb
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010099 ${CPU_COMPILE_OPTION}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100100 -mfloat-abi=${FLOAT_ABI}
101 -mlittle-endian
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100102 --stats
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000103 "SHELL:-Xlinker --gc-sections"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100104 "$<$<CONFIG:RELEASE>:--no-debug>")
105
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000106function(configure_semihosting TARGET_NAME SEMIHOSTING)
107 if (${SEMIHOSTING})
108 target_link_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
109 target_compile_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
110 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
111 else()
112 target_link_options(${TARGET_NAME} PUBLIC --specs=nosys.specs)
113 target_compile_options(${TARGET_NAME} PUBLIC "--specs=nosys.specs")
114 endif()
115endfunction()
116
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100117# Function to add a map file output for the linker to dump diagnostic information to.
118function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
119 target_link_options(${TARGET_NAME} PUBLIC
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000120 "SHELL:-Xlinker -Map=${MAP_FILE_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100121endfunction()
122
123# Function to add linker option to use the chosen linker script.
alexander31ae9f02022-02-10 16:15:54 +0000124function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100125 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld
126 CACHE STRING "Linker script path")
127 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
128 message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}")
129 endif()
130 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000131 target_link_options(${TARGET_NAME} PUBLIC
132 "SHELL:-T ${LINKER_SCRIPT_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100133endfunction()
134
135# Function to set the command to copy/extract contents from an elf
136# into a binary file.
137function(add_bin_generation_command)
138
139 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
140 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
141 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
142
143 list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS)
144 list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES)
145
146 if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES})
147 message(FATAL_ERROR "Section patterns and the output binary names "
148 "should be of the same length")
149 endif()
150
151 message(STATUS "${TRIPLET}-objcopy requested to generate "
152 "${N_OUTPUT_BIN_NAMES} bin files.")
153
154 math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1")
155
156 foreach(IDX RANGE ${MAX_IDX})
157
158 list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME)
159 list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN)
160
161 add_custom_command(TARGET ${PARSED_TARGET_NAME}
162 POST_BUILD
163 COMMAND ${TRIPLET}-objcopy -O binary
164 --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH}
165 ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME})
166 endforeach()
167
168endfunction()
169
170# Function to assert the compiler version
171function(enforce_compiler_version)
172 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000173 message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100174 endif()
175endfunction()