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