blob: d41ecec5cb1e813feae9a4755ef805085f32ae73 [file] [log] [blame]
Imre Kis2cfb2b42021-12-15 19:15:42 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Gyorgy Szing34aaf212022-10-20 07:26:23 +02008# Since we append to default compilation flags stop multiple inclusion to avoid
9# flags being added multiple times.
10include_guard(GLOBAL)
11
Imre Kis2cfb2b42021-12-15 19:15:42 +010012#GNUARM v8 and v9 compilers use a different triplet.
13if(NOT CROSS_COMPILE AND NOT DEFINED ENV{CROSS_COMPILE})
14 set(CROSS_COMPILE "aarch64-elf-;aarch64-none-elf-;aarch64-linux-gnu-;aarch64-none-linux-gnu-" CACHE STRING "List of GCC prefix triplets to use.")
15endif()
16
17set(CMAKE_CROSSCOMPILING True)
18set(CMAKE_SYSTEM_NAME Generic)
19set(CMAKE_SYSTEM_PROCESSOR arm)
20set(CMAKE_POSITION_INDEPENDENT_CODE True)
21
Gyorgy Szing34aaf212022-10-20 07:26:23 +020022set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
Balint Dobszay550ce872022-12-15 15:28:40 +010023set(TS_MANDATORY_AARCH_FLAGS "-fpie -mstrict-align -march=armv8-a+crc" CACHE STRING "Compiler flags configuring architecture specific ")
Gyorgy Szing34aaf212022-10-20 07:26:23 +020024set(TS_WARNING_FLAGS "-Wall" CACHE STRING "Compiler flags affecting generating warning messages.")
25set(TS_MANDATORY_LINKER_FLAGS "-Wl,-pie -Wl,--no-dynamic-linker -Wl,--sort-section=alignment -zmax-page-size=4096" CACHE STRING "Linker flags needed for correct builds.")
26
Gabor Toth350452a2024-06-19 12:35:08 +020027# branch-protection enables bti/pac while compile force-bti tells the linker to
28# warn if some object files lack the .note.gnu.property section with the BTI
29# flag, and to turn on the BTI flag in the output anyway.
30set(BRANCH_PROTECTION unset CACHE STRING "Enable branch protection")
31set_property(CACHE BRANCH_PROTECTION PROPERTY STRINGS unset 0 1 2 3 4)
Gabor Tothb446a1e2024-05-10 10:42:20 +020032
Gabor Toth350452a2024-06-19 12:35:08 +020033if(BRANCH_PROTECTION STREQUAL "0")
34 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=none")
35elseif(BRANCH_PROTECTION STREQUAL "1")
36 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=standard")
37 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti")
38 add_compile_definitions("BTI_ENABLED")
39elseif(BRANCH_PROTECTION STREQUAL "2")
40 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret")
41elseif(BRANCH_PROTECTION STREQUAL "3")
42 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret+leaf")
43elseif(BRANCH_PROTECTION STREQUAL "4")
Gabor Tothb446a1e2024-05-10 10:42:20 +020044 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=bti")
45 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti")
Gabor Tothc9d54f62024-06-06 14:09:14 +020046 add_compile_definitions("BTI_ENABLED")
Gabor Tothb446a1e2024-05-10 10:42:20 +020047endif()
48
Gyorgy Szing34aaf212022-10-20 07:26:23 +020049# Set flags affecting all build types
Gabor Toth22d04d62024-06-25 12:42:34 +020050string(APPEND CMAKE_C_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
51string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
52string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${TS_MANDATORY_LINKER_FLAGS}")
53if(DEFINED TS_ROOT)
54 # Flags not to be used with external components.
55 string(APPEND CMAKE_C_FLAGS_INIT " ${TS_WARNING_FLAGS}")
56 string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_WARNING_FLAGS}")
57endif()
Gyorgy Szing34aaf212022-10-20 07:26:23 +020058
59# Set flags affecting all build types supporting debugging.
60foreach(_b_type IN ITEMS DEBUG RELWITHDEBINFO MINSIZWITHDEBINFO)
61 string(APPEND CMAKE_C_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}")
62 string(APPEND CMAKE_CXX_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}")
63endforeach()
64
65# Build type specific flags
66string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " -O0")
67string(APPEND CMAKE_C_FLAGS_MINSIZEREL_INIT " -Os")
68string(APPEND CMAKE_C_FLAGS_MINSIZWITHDEBINFO_INIT " -Os")
69string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " -O2")
70string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO_INIT " -O2")
71string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " -O0")
72string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL_INIT " -Os")
73string(APPEND CMAKE_CXX_FLAGS_MINSIZWITHDEBINFO_INIT " -Os")
74string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " -O2")
75string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT " -O2")
76
Imre Kis2cfb2b42021-12-15 19:15:42 +010077include($ENV{TS_ROOT}/tools/cmake/compiler/GCC.cmake REQUIRED)
78include($ENV{TS_ROOT}/tools/cmake/compiler/config_iface.cmake REQUIRED)