blob: 015f962d145e0f2c2b9e0842214473b758698780 [file] [log] [blame]
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +01001#----------------------------------------------------------------------------
Richard Burtonbcec6752023-11-03 16:21:58 +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(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
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010026# Skip compiler test execution
27set(CMAKE_C_COMPILER_WORKS 1)
28set(CMAKE_CXX_COMPILER_WORKS 1)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010029
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010030if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED CMAKE_SYSTEM_ARCH)
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010031 set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010032endif()
33
34if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55)
35 # Flags for cortex-m55
Richard Burtonbcec6752023-11-03 16:21:58 +000036 set(MIN_GCC_VERSION 10.2.1)
ayamas0115f80702021-11-18 14:22:23 +000037 set(CPU_ID M55)
38 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
ayamas0115f80702021-11-18 14:22:23 +000039 set(ARM_CPU "ARMC${CPU_ID}")
ayamas0115f80702021-11-18 14:22:23 +000040 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010041 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010042 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
ayamas0115f80702021-11-18 14:22:23 +000043 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010044elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85)
45 # Flags for cortex-m85
Richard Burtonbcec6752023-11-03 16:21:58 +000046 set(MIN_GCC_VERSION 13.2.1)
47 set(CPU_ID M85)
48 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
49 set(ARM_CPU "ARMC${CPU_ID}")
50 set(CPU_HEADER_FILE "${ARM_CPU}.h")
51 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
52 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
53 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010054elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main)
55 # Flags for generic target armv8.1-m.main (will work for cortex-m55 and cortex-m85
Richard Burtonbcec6752023-11-03 16:21:58 +000056 set(MIN_GCC_VERSION 10.2.1)
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010057 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
58 set(ARM_CPU "ARMv81MML")
59 set(CPU_COMPILE_DEF ${CPU_ID})
60 set(FLOAT_ABI hard)
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010061 set(CPU_HEADER_FILE "${CPU_ID}.h")
62 set(CPU_COMPILE_OPTION "-march=armv8.1-m.main+mve.fp+fp.dp")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010063 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010064 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010065elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
66 # Flags for cortex-m33 to go here
67endif()
68
Richard Burtonbcec6752023-11-03 16:21:58 +000069if (NOT DEFINED MIN_GCC_VERSION)
70 set(MIN_GCC_VERSION 10.2.1)
71endif()
72
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010073set(${CPU_COMPILE_DEF} 1)
74
75# Warning options
76add_compile_options(
77 -Wall
78 -Wextra
79 -Wvla
80 -Wno-psabi)
81
82# General purpose compile options:
83add_compile_options(
84 -funsigned-char
85 -fno-function-sections
Kshitij Sisodia661959c2021-11-24 10:39:52 +000086 -fdata-sections
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010087 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
88
89# Arch compile options:
90add_compile_options(
91 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010092 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010093 -MD
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010094 ${FLOAT_ABI_COMPILE_OPTION}
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010095 ${CPU_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010096
97# Compile definitions:
98add_compile_definitions(
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010099 $<$<BOOL:${CPU_HEADER_FILE}>:CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\">
100 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100101
102# Link options:
103add_link_options(
104 -mthumb
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +0100105 ${CPU_COMPILE_OPTION}
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100106 ${FLOAT_ABI_COMPILE_OPTION}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100107 -mlittle-endian
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108 --stats
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000109 "SHELL:-Xlinker --gc-sections"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100110 "$<$<CONFIG:RELEASE>:--no-debug>")
111
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000112function(configure_semihosting TARGET_NAME SEMIHOSTING)
113 if (${SEMIHOSTING})
114 target_link_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
115 target_compile_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
116 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
117 else()
118 target_link_options(${TARGET_NAME} PUBLIC --specs=nosys.specs)
119 target_compile_options(${TARGET_NAME} PUBLIC "--specs=nosys.specs")
120 endif()
121endfunction()
122
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100123# Function to add a map file output for the linker to dump diagnostic information to.
124function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
125 target_link_options(${TARGET_NAME} PUBLIC
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000126 "SHELL:-Xlinker -Map=${MAP_FILE_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100127endfunction()
128
129# Function to add linker option to use the chosen linker script.
alexander31ae9f02022-02-10 16:15:54 +0000130function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100131 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld
132 CACHE STRING "Linker script path")
133 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
134 message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}")
135 endif()
136 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000137 target_link_options(${TARGET_NAME} PUBLIC
138 "SHELL:-T ${LINKER_SCRIPT_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100139endfunction()
140
141# Function to set the command to copy/extract contents from an elf
142# into a binary file.
143function(add_bin_generation_command)
144
145 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
146 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
147 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
148
149 list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS)
150 list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES)
151
152 if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES})
153 message(FATAL_ERROR "Section patterns and the output binary names "
154 "should be of the same length")
155 endif()
156
157 message(STATUS "${TRIPLET}-objcopy requested to generate "
158 "${N_OUTPUT_BIN_NAMES} bin files.")
159
160 math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1")
161
162 foreach(IDX RANGE ${MAX_IDX})
163
164 list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME)
165 list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN)
166
167 add_custom_command(TARGET ${PARSED_TARGET_NAME}
168 POST_BUILD
169 COMMAND ${TRIPLET}-objcopy -O binary
170 --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH}
171 ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME})
172 endforeach()
173
174endfunction()
175
176# Function to assert the compiler version
177function(enforce_compiler_version)
178 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000179 message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100180 endif()
181endfunction()