blob: 3db332041ba531212cb7c74664fe47edca63ef49 [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(CMAKE_C_COMPILER armclang)
19set(CMAKE_CXX_COMPILER armclang)
20set(CMAKE_C_LINKER_PREFERENCE armlink)
21set(CMAKE_ASM_LINKER_PREFERENCE armlink)
22set(CMAKE_ASM_COMPILER armasm)
23set(CMAKE_ASM_COMPILER_AR armar)
24
25set(CMAKE_CROSSCOMPILING true)
26set(CMAKE_SYSTEM_NAME Generic)
27
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010028# 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
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010038 set(MIN_ARM_CLANG_VERSION 6.16)
ayamas0115f80702021-11-18 14:22:23 +000039 set(CPU_ID M55)
40 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
ayamas0115f80702021-11-18 14:22:23 +000041 set(ARM_CPU "ARMC${CPU_ID}")
ayamas0115f80702021-11-18 14:22:23 +000042 set(CPU_HEADER_FILE "${ARM_CPU}.h")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010043 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010044 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
ayamas0115f80702021-11-18 14:22:23 +000045 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010046elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m85)
47 # Flags for cortex-m85
48 set(MIN_ARM_CLANG_VERSION 6.18)
49 set(CPU_ID M85)
50 set(CPU_COMPILE_DEF CPU_CORTEX_${CPU_ID})
51 set(ARM_CPU "ARMC${CPU_ID}")
52 set(CPU_HEADER_FILE "${ARM_CPU}.h")
53 set(CPU_COMPILE_OPTION "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
54 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
55 set(CPU_LINK_OPT "--cpu=Cortex-${CPU_ID}")
56elseif (CMAKE_SYSTEM_ARCH STREQUAL armv8.1-m.main)
57 # Flags for generic armv8.1-m profile
58 set(MIN_ARM_CLANG_VERSION 6.16)
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010059 set(CPU_ID ARMv81MML_DSP_DP_MVE_FP)
60 set(ARM_CPU "ARMv81MML")
61 set(CPU_COMPILE_DEF ${CPU_ID})
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010062
63 # @TODO: Revise once we have the CPU file in CMSIS and CPU flags
64 # are supported by toolchains.
65 set(CPU_HEADER_FILE "${CPU_ID}.h")
66 set(CPU_COMPILE_OPTION "-march=armv8.1-m.main+mve.fp+fp.dp")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010067 set(FLOAT_ABI_COMPILE_OPTION "-mfloat-abi=hard")
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010068 set(CPU_LINK_OPT "--cpu=8.1-M.Main.mve.fp")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010069elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m33)
70 # Flags for cortex-m33 to go here
71endif()
72
73set(${CPU_COMPILE_DEF} 1)
74
75# Warning options
76add_compile_options(
77 -Wall
78 -Wextra
79 -Wvla)
80
81# General purpose compile options:
82add_compile_options(
83 -funsigned-char
84 -fno-function-sections
85 "$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>")
86
87# Arch compile options:
88add_compile_options(
89 -mthumb
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010090 --target=arm-arm-non-eabi
91 -mlittle-endian
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +010092 -MD
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010093 ${CPU_COMPILE_OPTION}
94 ${FLOAT_ABI_COMPILE_OPTION})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010095
96# Compile definitions:
97add_compile_definitions(
ayamas0115f80702021-11-18 14:22:23 +000098 CPU_HEADER_FILE=\"${CPU_HEADER_FILE}\"
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010099 $<$<BOOL:${CPU_COMPILE_DEF}>:${CPU_COMPILE_DEF}>)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100100
101# Link options:
102add_link_options(${CPU_LINK_OPT})
103set(CMAKE_ASM_FLAGS "${CPU_LINK_OPT}")
104
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100105set(ARMCLANG_INFO_STR "sizes,totals,unused,veneers,summarysizes")
106if(CMAKE_BUILD_TYPE STREQUAL Debug)
107 # For debug builds, we can add stack information too:
108 set(ARMCLANG_INFO_STR "${ARMCLANG_INFO_STR},stack,summarystack")
109endif()
110
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100111# Warnings to be ignored:
112# L6314W = No section matches pattern
113# L6439W = Multiply defined Global Symbol
114add_link_options(
115 --diag_suppress=L6439W,L6314W
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100116 --info ${ARMCLANG_INFO_STR}
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100117 --strict
118 --callgraph
Kshitij Sisodia7e56d8f2022-04-11 10:34:29 +0100119 --no_exceptions
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100120 --load_addr_map_info
121 --xref
122 "$<$<CONFIG:RELEASE>:--no_debug>")
123
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000124function(configure_semihosting TARGET_NAME SEMIHOSTING)
125 if (${SEMIHOSTING})
126 target_compile_definitions(${TARGET_NAME} PUBLIC USE_SEMIHOSTING)
127 endif()
128endfunction()
129
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100130# Function to add a map file output for the linker to dump diagnostic information to.
131function(add_target_map_file TARGET_NAME MAP_FILE_PATH)
132 target_link_options(${TARGET_NAME} PUBLIC
133 --map --symbols --list=${MAP_FILE_PATH})
134endfunction()
135
136# Function to add linker option to use the chosen linker script (scatter file).
alexander31ae9f02022-02-10 16:15:54 +0000137function(add_linker_script TARGET_NAME SCRIPT_DIR SCRIPT_NAME)
138 set(LINKER_SCRIPT_PATH ${SCRIPT_DIR}/${SCRIPT_NAME}.sct)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100139 if (NOT EXISTS ${LINKER_SCRIPT_PATH})
140 message(FATAL_ERROR "Scatter file not found: ${LINKER_SCRIPT_PATH}")
141 endif()
142 message(STATUS "Using linker script: ${LINKER_SCRIPT_PATH}")
alexander31ae9f02022-02-10 16:15:54 +0000143 target_link_options(${TARGET_NAME} PUBLIC
144 --scatter=${LINKER_SCRIPT_PATH})
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100145endfunction()
146
147# Function to set the command to copy/extract contents from an elf
148# into a binary file.
149function(add_bin_generation_command)
150
151 set(multiValueArgs SECTION_PATTERNS OUTPUT_BIN_NAMES)
152 set(oneValueArgs TARGET_NAME OUTPUT_DIR AXF_PATH)
153 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
154
155 add_custom_command(TARGET ${PARSED_TARGET_NAME}
156 POST_BUILD
157 COMMAND fromelf --bin --output=${PARSED_OUTPUT_DIR}/
158 ${PARSED_AXF_PATH})
159
160endfunction()
161
162# Function to assert the compiler version
163function(enforce_compiler_version)
164 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${MIN_ARM_CLANG_VERSION})
Kshitij Sisodiaee98bd62022-01-17 13:38:54 +0000165 message( FATAL_ERROR "Arm compiler version must be ${MIN_ARM_CLANG_VERSION} or greater." )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100166 endif()
167endfunction()