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 "-fpic -mstrict-align -march=armv8-a+crc" CACHE STRING "Compiler flags configuring architecture specific ") 24set(TS_WARNING_FLAGS "-Wall -Werror" CACHE STRING "Compiler flags affecting generating warning messages.") 25set(TS_MANDATORY_LINKER_FLAGS "-pie -Wl,--as-needed -Wl,--sort-section=alignment -zmax-page-size=4096" 26 CACHE STRING "Linker flags needed for correct builds.") 27 28# branch-protection enables bti/pac while compile force-bti tells the linker to 29# warn if some object files lack the .note.gnu.property section with the BTI 30# flag, and to turn on the BTI flag in the output anyway. 31set(BRANCH_PROTECTION unset CACHE STRING "Enable branch protection") 32set_property(CACHE BRANCH_PROTECTION PROPERTY STRINGS unset 0 1 2 3 4) 33 34if(BRANCH_PROTECTION STREQUAL "0") 35 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=none") 36elseif(BRANCH_PROTECTION STREQUAL "1") 37 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=standard") 38 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti") 39 add_compile_definitions("BTI_ENABLED") 40elseif(BRANCH_PROTECTION STREQUAL "2") 41 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret") 42elseif(BRANCH_PROTECTION STREQUAL "3") 43 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=pac-ret+leaf") 44elseif(BRANCH_PROTECTION STREQUAL "4") 45 set(TS_MANDATORY_AARCH_FLAGS "${TS_MANDATORY_AARCH_FLAGS} -mbranch-protection=bti") 46 set(TS_MANDATORY_LINKER_FLAGS "${TS_MANDATORY_LINKER_FLAGS} -zforce-bti") 47 add_compile_definitions("BTI_ENABLED") 48endif() 49 50# Set flags affecting all build types 51string(APPEND CMAKE_C_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}") 52string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}") 53string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${TS_MANDATORY_LINKER_FLAGS}") 54if(DEFINED TS_ROOT) 55 # Flags not to be used with external components. 56 string(APPEND CMAKE_C_FLAGS_INIT " ${TS_WARNING_FLAGS}") 57 string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_WARNING_FLAGS}") 58endif() 59 60# Set flags affecting all build types supporting debugging. 61foreach(_b_type IN ITEMS DEBUG RELWITHDEBINFO MINSIZWITHDEBINFO) 62 string(APPEND CMAKE_C_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}") 63 string(APPEND CMAKE_CXX_FLAGS_${_b_type}_INIT " ${TS_DEBUG_INFO_FLAGS}") 64endforeach() 65 66# Build type specific flags 67string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " -O0") 68string(APPEND CMAKE_C_FLAGS_MINSIZEREL_INIT " -Os") 69string(APPEND CMAKE_C_FLAGS_MINSIZWITHDEBINFO_INIT " -Os") 70string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " -O2") 71string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO_INIT " -O2") 72string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " -O0") 73string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL_INIT " -Os") 74string(APPEND CMAKE_CXX_FLAGS_MINSIZWITHDEBINFO_INIT " -Os") 75string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " -O2") 76string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT " -O2") 77 78include($ENV{TS_ROOT}/tools/cmake/compiler/GCC.cmake REQUIRED) 79include($ENV{TS_ROOT}/tools/cmake/compiler/config_iface.cmake REQUIRED) 80