blob: 2ffc1bb3919b543eb858d46755f879a60039df88 [file] [log] [blame]
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +01001#----------------------------------------------------------------------------
2# Copyright (c) 2021 Arm Limited. All rights reserved.
3# 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)
31set(PLATFORM_HAL 1)
32
33if (NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
34 set(CMAKE_SYSTEM_PROCESSOR cortex-m55)
35endif()
36
37if (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m55)
38 # Flags for cortex-m55
39 set(CPU_COMPILE_DEF CPU_CORTEX_M55)
40 set(CPU_NAME ${CMAKE_SYSTEM_PROCESSOR})
41 set(FLOAT_ABI hard)
42 set(ARM_MATH_DSP 1)
43 set(ARM_MATH_LOOPUNROLL 1)
44elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
45 # Flags for cortex-m33 to go here
46endif()
47
48set(${CPU_COMPILE_DEF} 1)
49
50# Warning options
51add_compile_options(
52 -Wall
53 -Wextra
54 -Wvla
55 -Wno-psabi)
56
57# General purpose compile options:
58add_compile_options(
59 -funsigned-char
60 -fno-function-sections
61 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
62
63# Arch compile options:
64add_compile_options(
65 -mthumb
66 -mcpu=${CPU_NAME}
67 -mfloat-abi=${FLOAT_ABI}
68 -mlittle-endian
69 -MD)
70
71# Compile definitions:
72add_compile_definitions(
73 PLATFORM_HAL=${PLATFORM_HAL}
74 ${CPU_COMPILE_DEF}=1
75 $<$<BOOL:${ARM_MATH_DSP}>:ARM_MATH_DSP>
76 $<$<BOOL:${ARM_MATH_LOOPUNROLL}>:ARM_MATH_LOOPUNROLL>)
77
78# Link options:
79add_link_options(
80 -mthumb
81 -mcpu=${CPU_NAME}
82 -mfloat-abi=${FLOAT_ABI}
83 -mlittle-endian
84 --specs=nosys.specs
85 --stats
86 "$<$<CONFIG:RELEASE>:--no-debug>")
87
88# Function to add a map file output for the linker to dump diagnostic information to.
89function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
90 target_link_options(${TARGET_NAME} PUBLIC
91 -Xlinker -Map=${MAP_FILE_PATH})
92endfunction()
93
94# Function to add linker option to use the chosen linker script.
95function(add_linker_script SCRIPT_DIR SCRIPT_NAME)
96 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.ld
97 CACHE STRING "Linker script path")
98 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
99 message(FATAL_ERROR "Linker script not found: ${LINKER_SCRIPT_PATH}")
100 endif()
101 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
102 add_link_options("SHELL:-T ${LINKER_SCRIPT_PATH}")
103endfunction()
104
105# Function to set the command to copy/extract contents from an elf
106# into a binary file.
107function(add_bin_generation_command)
108
109 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
110 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
111 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
112
113 list(LENGTH PARSED_SECTION_PATTERNS N_SECTION_PATTERNS)
114 list(LENGTH PARSED_OUTPUT_BIN_NAMES N_OUTPUT_BIN_NAMES)
115
116 if (NOT ${N_SECTION_PATTERNS} STREQUAL ${N_OUTPUT_BIN_NAMES})
117 message(FATAL_ERROR "Section patterns and the output binary names "
118 "should be of the same length")
119 endif()
120
121 message(STATUS "${TRIPLET}-objcopy requested to generate "
122 "${N_OUTPUT_BIN_NAMES} bin files.")
123
124 math(EXPR MAX_IDX "${N_SECTION_PATTERNS} - 1")
125
126 foreach(IDX RANGE ${MAX_IDX})
127
128 list(GET PARSED_OUTPUT_BIN_NAMES ${IDX} OUTPUT_BIN_NAME)
129 list(GET PARSED_SECTION_PATTERNS ${IDX} SECTION_PATTERN)
130
131 add_custom_command(TARGET ${PARSED_TARGET_NAME}
132 POST_BUILD
133 COMMAND ${TRIPLET}-objcopy -O binary
134 --only-section ${SECTION_PATTERN} ${PARSED_AXF_PATH}
135 ${PARSED_OUTPUT_DIR}/${OUTPUT_BIN_NAME})
136 endforeach()
137
138endfunction()
139
140# Function to assert the compiler version
141function(enforce_compiler_version)
142 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_GCC_VERSION})
143 message( FATAL_ERROR "arm-none-eabi-gcc version must be ${MIN_GCC_VERSION} or greater to support ${CMAKE_SYSTEM_PROCESSOR} architecture." )
144 endif()
145endfunction()