1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8# Since we append to default compilation flags stop multiple inclusion to avoid
9# flags being added multiple times.
10include_guard(GLOBAL)
11
12#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
22set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
23set(TS_MANDATORY_AARCH_FLAGS "-fpie -mstrict-align -march=armv8-a+crc" CACHE STRING "Compiler flags configuring architecture specific ")
24set(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
27# 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)
32
33if(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")
44	set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=bti")
45	set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti")
46	add_compile_definitions("BTI_ENABLED")
47endif()
48
49# Set flags affecting all build types
50string(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()
58
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
77include($ENV{TS_ROOT}/tools/cmake/compiler/GCC.cmake REQUIRED)
78include($ENV{TS_ROOT}/tools/cmake/compiler/config_iface.cmake REQUIRED)
79