blob: 9c1cd4f440d794012214f8dc00469001bf94250e [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 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
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}")
ayamas0115f80702021-11-18 14:22:23 +000041 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010042 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010043 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
ayamas0115f80702021-11-18 14:22:23 +000044 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010045elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85)
46 # Flags for cortex-m85
47 # @TODO: Current versions of GNU compiler do not support Cortex-M85, we compile for Cortex-M55 instead.
48 message(WARNING "Arm GNU Toolchain does not support Arm Cortex-M85 yet, switching to Cortex-M55.")
49 set(CMAKE_SYSTEM_PROCESSOR cortex-m55 CACHE STRING "Cortex-M CPU to use" FORCE)
50 # No need to duplicate the definitions here.
51 # Flags from Cortex-M55 will be added as this toolchain file will be read by CMake again.
52elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main)
53 # Flags for generic target armv8.1-m.main (will work for cortex-m55 and cortex-m85
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010054 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
55 set(ARM_CPU "ARMv81MML")
56 set(CPU_COMPILE_DEF ${CPU_ID})
57 set(FLOAT_ABI hard)
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010058 set(CPU_HEADER_FILE "${CPU_ID}.h")
59 set(CPU_COMPILE_OPTION "-march=armv8.1-m.main+mve.fp+fp.dp")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010060 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010061 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010062elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
63 # Flags for cortex-m33 to go here
64endif()
65
66set(${CPU_COMPILE_DEF} 1)
67
68# Warning options
69add_compile_options(
70 -Wall
71 -Wextra
72 -Wvla
73 -Wno-psabi)
74
75# General purpose compile options:
76add_compile_options(
77 -funsigned-char
78 -fno-function-sections
Kshitij Sisodia661959c2021-11-24 10:39:52 +000079 -fdata-sections
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010080 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
81
82# Arch compile options:
83add_compile_options(
84 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010085 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010086 -MD
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010087 ${FLOAT_ABI_COMPILE_OPTION}
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010088 ${CPU_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089
90# Compile definitions:
91add_compile_definitions(
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010092 $<$<BOOL:${CPU_HEADER_FILE}>:CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\">
93 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094
95# Link options:
96add_link_options(
97 -mthumb
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010098 ${CPU_COMPILE_OPTION}
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010099 ${FLOAT_ABI_COMPILE_OPTION}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100100 -mlittle-endian
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100101 --stats
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000102 "SHELL:-Xlinker --gc-sections"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100103 "$<$<CONFIG:RELEASE>:--no-debug>")
104
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000105function(configure_semihosting TARGET_NAME SEMIHOSTING)
106 if (${SEMIHOSTING})
107 target_link_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
108 target_compile_options(${TARGET_NAME} PUBLIC "--specs=rdimon.specs")
109 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
110 else()
111 target_link_options(${TARGET_NAME} PUBLIC --specs=nosys.specs)
112 target_compile_options(${TARGET_NAME} PUBLIC "--specs=nosys.specs")
113 endif()
114endfunction()
115
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100116# Function to add a map file output for the linker to dump diagnostic information to.
117function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
118 target_link_options(${TARGET_NAME} PUBLIC
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000119 "SHELL:-Xlinker -Map=${MAP_FILE_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100120endfunction()
121
122# Function to add linker option to use the chosen linker script.
alexander31ae9f02022-02-10 16:15:54 +0000123function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100124 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld
125 CACHE STRING "Linker script path")
126 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
127 message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}")
128 endif()
129 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000130 target_link_options(${TARGET_NAME} PUBLIC
131 "SHELL:-T ${LINKER_SCRIPT_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100132endfunction()
133
134# Function to set the command to copy/extract contents from an elf
135# into a binary file.
136function(add_bin_generation_command)
137
138 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
139 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
140 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
141
142 list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS)
143 list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES)
144
145 if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES})
146 message(FATAL_ERROR "Section patterns and the output binary names "
147 "should be of the same length")
148 endif()
149
150 message(STATUS "${TRIPLET}-objcopy requested to generate "
151 "${N_OUTPUT_BIN_NAMES} bin files.")
152
153 math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1")
154
155 foreach(IDX RANGE ${MAX_IDX})
156
157 list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME)
158 list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN)
159
160 add_custom_command(TARGET ${PARSED_TARGET_NAME}
161 POST_BUILD
162 COMMAND ${TRIPLET}-objcopy -O binary
163 --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH}
164 ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME})
165 endforeach()
166
167endfunction()
168
169# Function to assert the compiler version
170function(enforce_compiler_version)
171 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000172 message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100173 endif()
174endfunction()