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