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