Boyan Karatotev | 3df79ae | 2025-04-09 14:26:17 +0100 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2025, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | |
| 7 | GCC_V_OUTPUT := $(if $($(ARCH)-cc),$(shell $($(ARCH)-cc) -v 2>&1)) |
| 8 | PIE_FOUND := $(findstring --enable-default-pie,${GCC_V_OUTPUT}) |
| 9 | |
| 10 | ################################################################################ |
| 11 | # Compiler Configuration based on ARCH_MAJOR and ARCH_MINOR flags |
| 12 | ################################################################################ |
| 13 | ifeq (${ARM_ARCH_MAJOR},7) |
| 14 | target32-directive = -target arm-none-eabi |
| 15 | # Will set march-directive from platform configuration |
| 16 | else |
| 17 | target32-directive = -target armv8a-none-eabi |
| 18 | endif #(ARM_ARCH_MAJOR) |
| 19 | |
| 20 | ifneq ($(filter %-clang,$($(ARCH)-cc-id)),) |
| 21 | ifeq ($($(ARCH)-cc-id),arm-clang) |
| 22 | TF_CFLAGS_aarch32 := -target arm-arm-none-eabi |
| 23 | TF_CFLAGS_aarch64 := -target aarch64-arm-none-eabi |
| 24 | else |
| 25 | TF_CFLAGS_aarch32 = $(target32-directive) |
| 26 | TF_CFLAGS_aarch64 := -target aarch64-elf |
| 27 | endif |
| 28 | endif #(clang) |
| 29 | |
| 30 | # Process Debug flag |
| 31 | ifneq (${DEBUG}, 0) |
| 32 | TF_CFLAGS += -g -gdwarf-4 |
Boyan Karatotev | 3df79ae | 2025-04-09 14:26:17 +0100 | [diff] [blame] | 33 | endif #(Debug) |
| 34 | |
| 35 | ifeq (${AARCH32_INSTRUCTION_SET},A32) |
| 36 | TF_CFLAGS_aarch32 += -marm |
| 37 | else ifeq (${AARCH32_INSTRUCTION_SET},T32) |
| 38 | TF_CFLAGS_aarch32 += -mthumb |
| 39 | endif #(AARCH32_INSTRUCTION_SET) |
| 40 | |
| 41 | TF_CFLAGS_aarch32 += -mno-unaligned-access |
| 42 | TF_CFLAGS_aarch64 += -mgeneral-regs-only -mstrict-align |
| 43 | |
| 44 | ############################################################################## |
| 45 | # WARNINGS Configuration |
| 46 | ############################################################################### |
| 47 | # General warnings |
| 48 | WARNINGS := -Wall -Wmissing-include-dirs -Wunused \ |
| 49 | -Wdisabled-optimization -Wvla -Wshadow \ |
| 50 | -Wredundant-decls |
| 51 | # stricter warnings |
| 52 | WARNINGS += -Wextra -Wno-trigraphs |
| 53 | # too verbose for generic build |
| 54 | WARNINGS += -Wno-missing-field-initializers \ |
| 55 | -Wno-type-limits -Wno-sign-compare \ |
| 56 | # on clang this flag gets reset if -Wextra is set after it. No difference on gcc |
| 57 | WARNINGS += -Wno-unused-parameter |
| 58 | |
| 59 | # Additional warnings |
| 60 | # Level 1 - infrequent warnings we should have none of |
| 61 | # full -Wextra |
| 62 | WARNING1 += -Wsign-compare |
| 63 | WARNING1 += -Wtype-limits |
| 64 | WARNING1 += -Wmissing-field-initializers |
| 65 | |
| 66 | # Level 2 - problematic warnings that we want |
| 67 | # zlib, compiler-rt, coreboot, and mbdedtls blow up with these |
| 68 | # TODO: disable just for them and move into default build |
| 69 | WARNING2 += -Wold-style-definition |
| 70 | WARNING2 += -Wmissing-prototypes |
| 71 | WARNING2 += -Wmissing-format-attribute |
| 72 | # TF-A aims to comply with this eventually. Effort too large at present |
| 73 | WARNING2 += -Wundef |
| 74 | # currently very involved and many platforms set this off |
| 75 | WARNING2 += -Wunused-const-variable=2 |
| 76 | |
| 77 | # Level 3 - very pedantic, frequently ignored |
| 78 | WARNING3 := -Wbad-function-cast |
| 79 | WARNING3 += -Waggregate-return |
| 80 | WARNING3 += -Wnested-externs |
| 81 | WARNING3 += -Wcast-align |
| 82 | WARNING3 += -Wcast-qual |
| 83 | WARNING3 += -Wconversion |
| 84 | WARNING3 += -Wpacked |
| 85 | WARNING3 += -Wpointer-arith |
| 86 | WARNING3 += -Wswitch-default |
| 87 | |
| 88 | ifeq (${W},1) |
| 89 | WARNINGS += $(WARNING1) |
| 90 | else ifeq (${W},2) |
| 91 | WARNINGS += $(WARNING1) $(WARNING2) |
| 92 | else ifeq (${W},3) |
| 93 | WARNINGS += $(WARNING1) $(WARNING2) $(WARNING3) |
| 94 | endif #(W) |
| 95 | |
| 96 | ifneq (${E},0) |
| 97 | ERRORS := -Werror |
| 98 | endif #(E) |
| 99 | |
| 100 | # Compiler specific warnings |
| 101 | ifeq ($(filter %-clang,$($(ARCH)-cc-id)),) |
| 102 | # not using clang |
| 103 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523 |
| 104 | TF_CFLAGS_MIN_PAGE_SIZE := $(call cc_option, --param=min-pagesize=0) |
| 105 | TF_CFLAGS += $(TF_CFLAGS_MIN_PAGE_SIZE) |
| 106 | |
| 107 | ifeq ($(HARDEN_SLS), 1) |
| 108 | TF_CFLAGS_MHARDEN_SLS := $(call cc_option, -mharden-sls=all) |
| 109 | TF_CFLAGS_aarch64 += $(TF_CFLAGS_MHARDEN_SLS) |
| 110 | endif |
| 111 | |
| 112 | WARNINGS += -Wunused-but-set-variable -Wmaybe-uninitialized \ |
| 113 | -Wpacked-bitfield-compat -Wshift-overflow=2 \ |
| 114 | -Wlogical-op |
| 115 | |
| 116 | else |
| 117 | # using clang |
| 118 | WARNINGS += -Wshift-overflow -Wshift-sign-overflow \ |
| 119 | -Wlogical-op-parentheses |
| 120 | endif #(Clang Warning) |
| 121 | |
| 122 | CPPFLAGS = ${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc \ |
| 123 | $(ERRORS) $(WARNINGS) |
| 124 | |
| 125 | |
| 126 | TF_CFLAGS += -ffunction-sections -fdata-sections \ |
| 127 | -ffreestanding -fno-common \ |
| 128 | -Os -std=gnu99 |
| 129 | |
| 130 | ifneq (${BP_OPTION},none) |
| 131 | TF_CFLAGS_aarch64 += -mbranch-protection=${BP_OPTION} |
| 132 | endif #(BP_OPTION) |
| 133 | |
| 134 | ifeq (${SANITIZE_UB},on) |
| 135 | TF_CFLAGS += -fsanitize=undefined -fno-sanitize-recover |
| 136 | endif #(${SANITIZE_UB},on) |
| 137 | |
| 138 | ifeq (${SANITIZE_UB},trap) |
| 139 | TF_CFLAGS += -fsanitize=undefined -fno-sanitize-recover \ |
| 140 | -fsanitize-undefined-trap-on-error |
| 141 | endif #(${SANITIZE_UB},trap) |
| 142 | |
| 143 | ifeq ($($(ARCH)-cc-id),gnu-gcc) |
| 144 | # Enable LTO only for aarch64 |
| 145 | LTO_CFLAGS = $(if $(filter-out 0,$(ENABLE_LTO)),-flto) |
| 146 | endif #(gnu-gcc) |
| 147 | |
| 148 | ifeq (${ERROR_DEPRECATED},0) |
| 149 | # Check if deprecated declarations and cpp warnings should be treated as error or not. |
| 150 | ifneq ($(filter %-clang,$($(ARCH)-cc-id)),) |
| 151 | CPPFLAGS += -Wno-error=deprecated-declarations |
| 152 | else |
| 153 | CPPFLAGS += -Wno-error=deprecated-declarations -Wno-error=cpp |
| 154 | endif |
| 155 | endif #(!ERROR_DEPRECATED) |
| 156 | |
| 157 | ################################################################################ |
| 158 | # Platform specific Makefile might provide us ARCH_MAJOR/MINOR use that to come |
| 159 | # up with appropriate march values for compiler. |
| 160 | ################################################################################ |
| 161 | include ${MAKE_HELPERS_DIRECTORY}march.mk |
| 162 | ifeq (${ARM_ARCH_MAJOR},7) |
| 163 | include make_helpers/armv7-a-cpus.mk |
| 164 | endif |
| 165 | |
| 166 | TF_CFLAGS += $(march-directive) |
Boyan Karatotev | 3df79ae | 2025-04-09 14:26:17 +0100 | [diff] [blame] | 167 | |
| 168 | ifneq ($(PIE_FOUND),) |
| 169 | TF_CFLAGS += -fno-PIE |
| 170 | endif |
| 171 | |
| 172 | TF_CFLAGS += $(CPPFLAGS) $(TF_CFLAGS_$(ARCH)) |
Boyan Karatotev | 6bb9f05 | 2025-04-25 13:25:50 +0100 | [diff] [blame] | 173 | TF_CFLAGS += $(CFLAGS) |
Boyan Karatotev | 7416eb2 | 2025-04-11 14:47:20 +0100 | [diff] [blame] | 174 | ASFLAGS += -Wa,--fatal-warnings |
Boyan Karatotev | 3df79ae | 2025-04-09 14:26:17 +0100 | [diff] [blame] | 175 | TF_LDFLAGS += -z noexecstack |
| 176 | |
| 177 | # LD = armlink |
| 178 | ifeq ($($(ARCH)-ld-id),arm-link) |
| 179 | TF_LDFLAGS += --diag_error=warning --lto_level=O1 |
| 180 | TF_LDFLAGS += --remove --info=unused,unusedsymbols |
| 181 | TF_LDFLAGS += $(TF_LDFLAGS_$(ARCH)) |
| 182 | |
| 183 | # LD = gcc (used when GCC LTO is enabled) |
| 184 | else ifeq ($($(ARCH)-ld-id),gnu-gcc) |
| 185 | # Pass ld options with Wl or Xlinker switches |
| 186 | TF_LDFLAGS += $(call ld_option,-Xlinker --no-warn-rwx-segments) |
| 187 | TF_LDFLAGS += -Wl,--fatal-warnings -O1 |
| 188 | TF_LDFLAGS += -Wl,--gc-sections |
| 189 | |
| 190 | TF_LDFLAGS += -Wl,-z,common-page-size=4096 #Configure page size constants |
| 191 | TF_LDFLAGS += -Wl,-z,max-page-size=4096 |
| 192 | TF_LDFLAGS += -Wl,--build-id=none |
| 193 | |
| 194 | ifeq ($(ENABLE_LTO),1) |
| 195 | TF_LDFLAGS += -flto -fuse-linker-plugin |
| 196 | TF_LDFLAGS += -flto-partition=one |
| 197 | endif #(ENABLE_LTO) |
| 198 | |
| 199 | # GCC automatically adds fix-cortex-a53-843419 flag when used to link |
| 200 | # which breaks some builds, so disable if errata fix is not explicitly enabled |
| 201 | ifeq (${ARCH},aarch64) |
| 202 | ifneq (${ERRATA_A53_843419},1) |
| 203 | TF_LDFLAGS += -mno-fix-cortex-a53-843419 |
| 204 | endif |
| 205 | endif |
| 206 | TF_LDFLAGS += -nostdlib |
| 207 | TF_LDFLAGS += $(subst --,-Xlinker --,$(TF_LDFLAGS_$(ARCH))) |
| 208 | |
| 209 | # LD = gcc-ld (ld) or llvm-ld (ld.lld) or other |
| 210 | else |
| 211 | # With ld.bfd version 2.39 and newer new warnings are added. Skip those since we |
| 212 | # are not loaded by a elf loader. |
| 213 | TF_LDFLAGS += $(call ld_option, --no-warn-rwx-segments) |
| 214 | TF_LDFLAGS += -O1 |
| 215 | TF_LDFLAGS += --gc-sections |
| 216 | |
| 217 | TF_LDFLAGS += -z common-page-size=4096 # Configure page size constants |
| 218 | TF_LDFLAGS += -z max-page-size=4096 |
| 219 | TF_LDFLAGS += --build-id=none |
| 220 | |
| 221 | # ld.lld doesn't recognize the errata flags, |
| 222 | # therefore don't add those in that case. |
| 223 | # ld.lld reports section type mismatch warnings, |
| 224 | # therefore don't add --fatal-warnings to it. |
| 225 | ifneq ($($(ARCH)-ld-id),llvm-lld) |
| 226 | TF_LDFLAGS += $(TF_LDFLAGS_$(ARCH)) --fatal-warnings |
| 227 | endif |
| 228 | |
| 229 | endif #(LD = armlink) |
| 230 | |
| 231 | ifneq ($(PIE_FOUND),) |
| 232 | ifeq ($($(ARCH)-ld-id),gnu-gcc) |
| 233 | TF_LDFLAGS += -no-pie |
| 234 | endif |
| 235 | endif #(PIE_FOUND) |
| 236 | |
| 237 | ifeq ($($(ARCH)-ld-id),gnu-gcc) |
| 238 | PIE_LDFLAGS += -Wl,-pie -Wl,--no-dynamic-linker |
| 239 | else |
| 240 | PIE_LDFLAGS += -pie --no-dynamic-linker |
| 241 | endif |
| 242 | |
| 243 | ifeq ($(ENABLE_PIE),1) |
| 244 | ifeq ($(RESET_TO_BL2),1) |
| 245 | ifneq ($(BL2_IN_XIP_MEM),1) |
| 246 | BL2_CPPFLAGS += -fpie |
| 247 | BL2_CFLAGS += -fpie |
| 248 | BL2_LDFLAGS += $(PIE_LDFLAGS) |
| 249 | endif #(BL2_IN_XIP_MEM) |
| 250 | endif #(RESET_TO_BL2) |
| 251 | BL31_CPPFLAGS += -fpie |
| 252 | BL31_CFLAGS += -fpie |
| 253 | BL31_LDFLAGS += $(PIE_LDFLAGS) |
| 254 | |
| 255 | BL32_CPPFLAGS += -fpie |
| 256 | BL32_CFLAGS += -fpie |
| 257 | BL32_LDFLAGS += $(PIE_LDFLAGS) |
| 258 | endif #(ENABLE_PIE) |
| 259 | |
| 260 | BL1_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} |
| 261 | BL31_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} |
| 262 | BL32_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} |
| 263 | |
| 264 | BL1_CPPFLAGS += -DIMAGE_AT_EL3 |
| 265 | ifeq ($(RESET_TO_BL2),1) |
| 266 | BL2_CPPFLAGS += -DIMAGE_AT_EL3 |
| 267 | else |
| 268 | BL2_CPPFLAGS += -DIMAGE_AT_EL1 |
| 269 | endif #(RESET_TO_BL2) |
| 270 | |
| 271 | ifeq (${ARCH},aarch64) |
| 272 | BL2U_CPPFLAGS += -DIMAGE_AT_EL1 |
| 273 | BL31_CPPFLAGS += -DIMAGE_AT_EL3 |
| 274 | BL32_CPPFLAGS += -DIMAGE_AT_EL1 |
| 275 | else |
| 276 | BL32_CPPFLAGS += -DIMAGE_AT_EL3 |
| 277 | endif |
| 278 | |
| 279 | ifeq (${SPD},spmd) |
| 280 | ifeq ($(findstring optee_sp,$(ARM_SPMC_MANIFEST_DTS)),optee_sp) |
| 281 | DTC_CPPFLAGS += -DOPTEE_SP_FW_CONFIG |
| 282 | endif |
| 283 | |
| 284 | ifeq ($(findstring trusty_sp,$(ARM_SPMC_MANIFEST_DTS)),trusty_sp) |
| 285 | DTC_CPPFLAGS += -DTRUSTY_SP_FW_CONFIG |
| 286 | endif |
| 287 | |
| 288 | ifeq ($(TS_SP_FW_CONFIG),1) |
| 289 | DTC_CPPFLAGS += -DTS_SP_FW_CONFIG |
| 290 | endif |
| 291 | |
| 292 | ifneq ($(ARM_BL2_SP_LIST_DTS),) |
| 293 | DTC_CPPFLAGS += -DARM_BL2_SP_LIST_DTS=$(ARM_BL2_SP_LIST_DTS) |
| 294 | endif |
| 295 | endif |
| 296 | |
| 297 | |
| 298 | DTC_FLAGS += -I dts -O dtb |
Boyan Karatotev | 1e8b535 | 2025-04-29 09:32:12 +0100 | [diff] [blame] | 299 | DTC_CPPFLAGS += -Ifdts -undef |
Boyan Karatotev | 3df79ae | 2025-04-09 14:26:17 +0100 | [diff] [blame] | 300 | |