blob: 7e565dd20da88b2d0748c177d07448c1132cd26f [file] [log] [blame]
Julian Hall07679f22020-11-23 17:45:16 +01001#-------------------------------------------------------------------------------
Gyorgy Szing748838e2022-03-05 04:17:34 +00002# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
Julian Hall07679f22020-11-23 17:45:16 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
Gyorgy Szing34aaf212022-10-20 07:26:23 +02007
8# Since we append to default compilation flags stop multiple inclusion to avoid
9# flags being added multiple times.
10include_guard(GLOBAL)
11
Gyorgy Szingd80f8562021-02-11 19:31:43 +010012if(NOT CROSS_COMPILE AND NOT DEFINED ENV{CROSS_COMPILE})
Gyorgy Szing748838e2022-03-05 04:17:34 +000013 set(CROSS_COMPILE "aarch64-linux-gnu-;aarch64-none-linux-gnu-" CACHE STRING "List of GCC prefix triplets to use.")
Gyorgy Szingd80f8562021-02-11 19:31:43 +010014endif()
Julian Hall07679f22020-11-23 17:45:16 +010015
16set(CMAKE_CROSSCOMPILING True)
17set(CMAKE_SYSTEM_NAME Linux)
18set(CMAKE_SYSTEM_PROCESSOR arm)
19
Gyorgy Szing34aaf212022-10-20 07:26:23 +020020set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
Balint Dobszay550ce872022-12-15 15:28:40 +010021set(TS_MANDATORY_AARCH_FLAGS "-mstrict-align -march=armv8-a+crc -DARM64=1" CACHE STRING "Compiler flags configuring architecture specific ")
Gabor Toth983264f2024-01-23 09:16:24 +010022set(TS_WARNING_FLAGS "-Wall -Werror" CACHE STRING "Compiler flags affecting generating warning messages.")
Gyorgy Szing34aaf212022-10-20 07:26:23 +020023set(TS_MANDATORY_LINKER_FLAGS "" CACHE STRING "Linker flags needed for correct builds.")
Julian Hall07679f22020-11-23 17:45:16 +010024
Gabor Toth350452a2024-06-19 12:35:08 +020025# branch-protection enables bti/pac while compile force-bti tells the linker to
26# warn if some object files lack the .note.gnu.property section with the BTI
27# flag, and to turn on the BTI flag in the output anyway.
28set(BRANCH_PROTECTION unset CACHE STRING "Enable branch protection")
29set_property(CACHE BRANCH_PROTECTION PROPERTY STRINGS unset 0 1 2 3 4)
Gabor Tothb446a1e2024-05-10 10:42:20 +020030
Gabor Toth350452a2024-06-19 12:35:08 +020031if(BRANCH_PROTECTION STREQUAL "0")
32 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=none")
33elseif(BRANCH_PROTECTION STREQUAL "1")
34 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=standard")
35 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti")
36 add_compile_definitions("BTI_ENABLED")
37elseif(BRANCH_PROTECTION STREQUAL "2")
38 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret")
39elseif(BRANCH_PROTECTION STREQUAL "3")
40 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret+leaf")
41elseif(BRANCH_PROTECTION STREQUAL "4")
Gabor Tothb446a1e2024-05-10 10:42:20 +020042 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=bti")
43 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti")
Gabor Toth350452a2024-06-19 12:35:08 +020044 add_compile_definitions("BTI_ENABLED")
Gabor Tothb446a1e2024-05-10 10:42:20 +020045endif()
46
Gyorgy Szing34aaf212022-10-20 07:26:23 +020047# Set flags affecting all build types
Gabor Toth22d04d62024-06-25 12:42:34 +020048string(APPEND CMAKE_C_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
49string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
50string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${TS_MANDATORY_LINKER_FLAGS}")
51if(DEFINED TS_ROOT)
52 # Flags not to be used with external components.
53 string(APPEND CMAKE_C_FLAGS_INIT " ${TS_WARNING_FLAGS}")
54 string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_WARNING_FLAGS}")
55endif()
Gyorgy Szing34aaf212022-10-20 07:26:23 +020056
57# Set flags affecting all build types supporting debugging.
Gyorgy Szing1ff7d792024-09-18 13:40:05 +020058foreach(_b_type IN ITEMS DEBUG RELWITHDEBINFO MINSIZWITHDEBINFO DEBUGCOVERAGE)
Gyorgy Szing34aaf212022-10-20 07:26:23 +020059 string(APPEND CMAKE_C_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}")
60 string(APPEND CMAKE_CXX_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}")
61endforeach()
62
63# Build type specific flags
64string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " -O0")
65string(APPEND CMAKE_C_FLAGS_MINSIZEREL_INIT " -Os")
66string(APPEND CMAKE_C_FLAGS_MINSIZWITHDEBINFO_INIT " -Os")
67string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " -O2")
68string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO_INIT " -O2")
Gyorgy Szing1ff7d792024-09-18 13:40:05 +020069string(APPEND CMAKE_C_FLAGS_DEBUGCOVERAGE_INIT " -O0 -coverage")
Gyorgy Szing34aaf212022-10-20 07:26:23 +020070string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " -O0")
71string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL_INIT " -Os")
72string(APPEND CMAKE_CXX_FLAGS_MINSIZWITHDEBINFO_INIT " -Os")
73string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " -O2")
74string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT " -O2")
Gyorgy Szing1ff7d792024-09-18 13:40:05 +020075string(APPEND CMAKE_CXX_FLAGS_DEBUGCOVERAGE_INIT " -O0 -coverage")
Julian Hall6e02acf2022-02-22 16:25:03 +000076
Julian Hall07679f22020-11-23 17:45:16 +010077include($ENV{TS_ROOT}/tools/cmake/compiler/GCC.cmake REQUIRED)
Julian Hall0051ed12021-07-22 13:59:24 +010078include($ENV{TS_ROOT}/tools/cmake/compiler/config_iface.cmake REQUIRED)