blob: 89201fb7e65fa10b91d20bbe78cefcf28df75749 [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
32if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
33 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})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010040 set(CPU_NAME ${CMAKE_SYSTEM_PROCESSOR})
ayamas0115f80702021-11-18 14:22:23 +000041 set(ARM_CPU "ARMC${CPU_ID}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010042 set(FLOAT_ABI hard)
43 set(ARM_MATH_DSP 1)
44 set(ARM_MATH_LOOPUNROLL 1)
ayamas0115f80702021-11-18 14:22:23 +000045 set(CPU_HEADER_FILE "${ARM_CPU}.h")
46 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010047elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
48 # Flags for cortex-m33 to go here
49endif()
50
51set(${CPU_COMPILE_DEF} 1)
52
53# Warning options
54add_compile_options(
55 -Wall
56 -Wextra
57 -Wvla
58 -Wno-psabi)
59
60# General purpose compile options:
61add_compile_options(
62 -funsigned-char
63 -fno-function-sections
Kshitij Sisodia661959c2021-11-24 10:39:52 +000064 -fdata-sections
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010065 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
66
67# Arch compile options:
68add_compile_options(
69 -mthumb
70 -mcpu=${CPU_NAME}
71 -mfloat-abi=${FLOAT_ABI}
72 -mlittle-endian
73 -MD)
74
75# Compile definitions:
76add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +000077 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
78 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010079 $<$<BOOL:${ARM_MATH_DSP}>:ARM_MATH_DSP>
80 $<$<BOOL:${ARM_MATH_LOOPUNROLL}>:ARM_MATH_LOOPUNROLL>)
81
82# Link options:
83add_link_options(
84 -mthumb
85 -mcpu=${CPU_NAME}
86 -mfloat-abi=${FLOAT_ABI}
87 -mlittle-endian
88 --specs=nosys.specs
89 --stats
Kshitij Sisodia661959c2021-11-24 10:39:52 +000090 "SHELL:-Xlinker --gc-sections"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010091 "$<$<CONFIG:RELEASE>:--no-debug>")
92
93# Function to add a map file output for the linker to dump diagnostic information to.
94function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
95 target_link_options(${TARGET_NAME} PUBLIC
Kshitij Sisodia661959c2021-11-24 10:39:52 +000096 "SHELL:-Xlinker -Map=${MAP_FILE_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010097endfunction()
98
99# Function to add linker option to use the chosen linker script.
alexander31ae9f02022-02-10 16:15:54 +0000100function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100101 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld
102 CACHE STRING "Linker script path")
103 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
104 message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}")
105 endif()
106 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000107 target_link_options(${TARGET_NAME} PUBLIC
108 "SHELL:-T ${LINKER_SCRIPT_PATH}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100109endfunction()
110
111# Function to set the command to copy/extract contents from an elf
112# into a binary file.
113function(add_bin_generation_command)
114
115 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
116 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
117 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
118
119 list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS)
120 list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES)
121
122 if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES})
123 message(FATAL_ERROR "Section patterns and the output binary names "
124 "should be of the same length")
125 endif()
126
127 message(STATUS "${TRIPLET}-objcopy requested to generate "
128 "${N_OUTPUT_BIN_NAMES} bin files.")
129
130 math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1")
131
132 foreach(IDX RANGE ${MAX_IDX})
133
134 list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME)
135 list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN)
136
137 add_custom_command(TARGET ${PARSED_TARGET_NAME}
138 POST_BUILD
139 COMMAND ${TRIPLET}-objcopy -O binary
140 --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH}
141 ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME})
142 endforeach()
143
144endfunction()
145
146# Function to assert the compiler version
147function(enforce_compiler_version)
148 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000149 message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100150 endif()
151endfunction()