xref: /freebsd/sys/conf/kern.mk (revision 2a58b312)
1# $FreeBSD$
2
3#
4# Warning flags for compiling the kernel and components of the kernel:
5#
6CWARNFLAGS?=	-Wall -Wstrict-prototypes \
7		-Wmissing-prototypes -Wpointer-arith -Wcast-qual \
8		-Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
9		-Wmissing-include-dirs -fdiagnostics-show-option \
10		-Wno-unknown-pragmas \
11		${CWARNEXTRA}
12#
13# The following flags are next up for working on:
14#	-Wextra
15
16# Disable a few warnings for clang, since there are several places in the
17# kernel where fixing them is more trouble than it is worth, or where there is
18# a false positive.
19.if ${COMPILER_TYPE} == "clang"
20NO_WCONSTANT_CONVERSION=	-Wno-error=constant-conversion
21NO_WSHIFT_COUNT_NEGATIVE=	-Wno-shift-count-negative
22NO_WSHIFT_COUNT_OVERFLOW=	-Wno-shift-count-overflow
23NO_WSELF_ASSIGN=		-Wno-self-assign
24NO_WUNNEEDED_INTERNAL_DECL=	-Wno-error=unneeded-internal-declaration
25NO_WSOMETIMES_UNINITIALIZED=	-Wno-error=sometimes-uninitialized
26NO_WCAST_QUAL=			-Wno-error=cast-qual
27NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
28.if ${COMPILER_VERSION} >= 100000
29NO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
30.endif
31.if ${COMPILER_VERSION} >= 130000
32NO_WUNUSED_BUT_SET_VARIABLE=	-Wno-unused-but-set-variable
33.endif
34.if ${COMPILER_VERSION} >= 140000
35NO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
36.endif
37.if ${COMPILER_VERSION} >= 150000
38NO_WSTRICT_PROTOTYPES=		-Wno-strict-prototypes
39NO_WDEPRECATED_NON_PROTOTYPE=	-Wno-deprecated-non-prototype
40.endif
41# Several other warnings which might be useful in some cases, but not severe
42# enough to error out the whole kernel build.  Display them anyway, so there is
43# some incentive to fix them eventually.
44CWARNEXTRA?=	-Wno-error=tautological-compare -Wno-error=empty-body \
45		-Wno-error=parentheses-equality -Wno-error=unused-function \
46		-Wno-error=pointer-sign
47CWARNEXTRA+=	-Wno-error=shift-negative-value
48CWARNEXTRA+=	-Wno-address-of-packed-member
49.if ${COMPILER_VERSION} >= 150000
50# Clang 15 has much more aggressive diagnostics about inconsistently declared
51# array parameters, K&R prototypes, mismatched prototypes, and unused-but-set
52# variables. Make these non-fatal for the time being.
53CWARNEXTRA+=	-Wno-error=array-parameter
54CWARNEXTRA+=	-Wno-error=deprecated-non-prototype
55CWARNEXTRA+=	-Wno-error=strict-prototypes
56CWARNEXTRA+=	-Wno-error=unused-but-set-variable
57.endif
58.endif	# clang
59
60.if ${COMPILER_TYPE} == "gcc"
61# Catch-all for all the things that are in our tree, but for which we're
62# not yet ready for this compiler.
63NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
64CWARNEXTRA?=	-Wno-error=address				\
65		-Wno-error=aggressive-loop-optimizations	\
66		-Wno-error=array-bounds				\
67		-Wno-error=attributes				\
68		-Wno-error=cast-qual				\
69		-Wno-error=enum-compare				\
70		-Wno-error=maybe-uninitialized			\
71		-Wno-error=misleading-indentation		\
72		-Wno-error=nonnull-compare			\
73		-Wno-error=overflow				\
74		-Wno-error=sequence-point			\
75		-Wno-error=shift-overflow			\
76		-Wno-error=tautological-compare			\
77		-Wno-error=unused-function			\
78		-Wno-unused-but-set-variable
79.if ${COMPILER_VERSION} >= 70100
80CWARNEXTRA+=	-Wno-error=stringop-overflow
81.endif
82.if ${COMPILER_VERSION} >= 70200
83CWARNEXTRA+=	-Wno-error=memset-elt-size
84.endif
85.if ${COMPILER_VERSION} >= 80000
86CWARNEXTRA+=	-Wno-error=packed-not-aligned
87.endif
88.if ${COMPILER_VERSION} >= 90100
89CWARNEXTRA+=	-Wno-address-of-packed-member			\
90		-Wno-error=alloca-larger-than=
91.if ${COMPILER_VERSION} >= 120100
92CWARNEXTRA+=	-Wno-error=nonnull				\
93		-Wno-dangling-pointer				\
94		-Wno-zero-length-bounds
95NO_WINFINITE_RECURSION=	-Wno-infinite-recursion
96NO_WSTRINGOP_OVERREAD=	-Wno-stringop-overread
97.endif
98.endif
99
100# GCC produces false positives for functions that switch on an
101# enum (GCC bug 87950)
102CWARNFLAGS+=	-Wno-return-type
103.endif	# gcc
104
105# This warning is utter nonsense
106CWARNFLAGS+=	-Wno-format-zero-length
107
108# External compilers may not support our format extensions.  Allow them
109# to be disabled.  WARNING: format checking is disabled in this case.
110.if ${MK_FORMAT_EXTENSIONS} == "no"
111FORMAT_EXTENSIONS=	-Wno-format
112.elif ${COMPILER_TYPE} == "clang" || \
113    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
114FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
115.else
116FORMAT_EXTENSIONS=	-fformat-extensions
117.endif
118
119#
120# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
121# and above adds code to the entry and exit point of every function to align the
122# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
123# per function call.  While the 16-byte alignment may benefit micro benchmarks,
124# it is probably an overall loss as it makes the code bigger (less efficient
125# use of code cache tag lines) and uses more stack (less efficient use of data
126# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
127# operations inside the kernel itself.  These operations are exclusively
128# reserved for user applications.
129#
130# gcc:
131# Setting -mno-mmx implies -mno-3dnow
132# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
133#
134# clang:
135# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
136# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
137#
138.if ${MACHINE_CPUARCH} == "i386"
139CFLAGS.gcc+=	-mpreferred-stack-boundary=2
140CFLAGS.clang+=	-mno-aes -mno-avx
141CFLAGS+=	-mno-mmx -mno-sse -msoft-float
142INLINE_LIMIT?=	8000
143.endif
144
145.if ${MACHINE_CPUARCH} == "arm"
146INLINE_LIMIT?=	8000
147.endif
148
149.if ${MACHINE_CPUARCH} == "aarch64"
150# We generally don't want fpu instructions in the kernel.
151CFLAGS += -mgeneral-regs-only
152# Reserve x18 for pcpu data
153CFLAGS += -ffixed-x18
154INLINE_LIMIT?=	8000
155.endif
156
157#
158# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
159# point registers within the kernel. However, for kernels supporting hardware
160# float (FPE), we have to include that in the march so we can have limited
161# floating point support in context switching needed for that. This is different
162# than userland where we use a hard-float ABI (lp64d).
163#
164# We also specify the "medium" code model, which generates code suitable for a
165# 2GiB addressing range located at any offset, allowing modules to be located
166# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
167# code model as "medium" and "medany" respectively.
168#
169.if ${MACHINE_CPUARCH} == "riscv"
170CFLAGS+=	-march=rv64imafdc
171CFLAGS+=	-mabi=lp64
172CFLAGS.clang+=	-mcmodel=medium
173CFLAGS.gcc+=	-mcmodel=medany
174INLINE_LIMIT?=	8000
175
176.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
177CFLAGS+=	-mno-relax
178.endif
179.endif
180
181#
182# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
183# operations inside the kernel itself.  These operations are exclusively
184# reserved for user applications.
185#
186# gcc:
187# Setting -mno-mmx implies -mno-3dnow
188# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
189#
190# clang:
191# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
192# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
193# (-mfpmath= is not supported)
194#
195.if ${MACHINE_CPUARCH} == "amd64"
196CFLAGS.clang+=	-mno-aes -mno-avx
197CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
198		-fno-asynchronous-unwind-tables
199INLINE_LIMIT?=	8000
200.endif
201
202#
203# For PowerPC we tell gcc to use floating point emulation.  This avoids using
204# floating point registers for integer operations which it has a tendency to do.
205# Also explicitly disable Altivec instructions inside the kernel.
206#
207.if ${MACHINE_CPUARCH} == "powerpc"
208CFLAGS+=	-mno-altivec -msoft-float
209INLINE_LIMIT?=	15000
210.endif
211
212.if ${MACHINE_ARCH} == "powerpcspe"
213CFLAGS.gcc+=	-mno-spe
214.endif
215
216#
217# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
218# DDB happy. ELFv2, if available, has some other efficiency benefits.
219#
220.if ${MACHINE_ARCH:Mpowerpc64*} != ""
221CFLAGS+=	-mabi=elfv2
222.endif
223
224#
225# GCC 3.0 and above like to do certain optimizations based on the
226# assumption that the program is linked against libc.  Stop this.
227#
228CFLAGS+=	-ffreestanding
229
230#
231# The C standard leaves signed integer overflow behavior undefined.
232# gcc and clang opimizers take advantage of this.  The kernel makes
233# use of signed integer wraparound mechanics so we need the compiler
234# to treat it as a wraparound and not take shortcuts.
235#
236CFLAGS+=	-fwrapv
237
238#
239# GCC SSP support
240#
241.if ${MK_SSP} != "no"
242CFLAGS+=	-fstack-protector
243.endif
244
245#
246# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
247#
248.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
249    ${MK_KERNEL_RETPOLINE} != "no"
250CFLAGS+=	-mretpoline
251.endif
252
253#
254# Initialize stack variables on function entry
255#
256.if ${MK_INIT_ALL_ZERO} == "yes"
257.if ${COMPILER_FEATURES:Minit-all}
258CFLAGS+= -ftrivial-auto-var-init=zero \
259    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
260.else
261.warning InitAll (zeros) requested but not support by compiler
262.endif
263.elif ${MK_INIT_ALL_PATTERN} == "yes"
264.if ${COMPILER_FEATURES:Minit-all}
265CFLAGS+= -ftrivial-auto-var-init=pattern
266.else
267.warning InitAll (pattern) requested but not support by compiler
268.endif
269.endif
270
271CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
272CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
273CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
274
275# Tell bmake not to mistake standard targets for things to be searched for
276# or expect to ever be up-to-date.
277PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
278		beforelinking build build-tools buildfiles buildincludes \
279		checkdpadd clean cleandepend cleandir cleanobj configure \
280		depend distclean distribute exe \
281		html includes install installfiles installincludes \
282		obj objlink objs objwarn \
283		realinstall regress \
284		tags whereobj
285
286.PHONY: ${PHONY_NOTMAIN}
287.NOTMAIN: ${PHONY_NOTMAIN}
288
289CSTD=		c99
290
291.if ${CSTD} == "k&r"
292CFLAGS+=        -traditional
293.elif ${CSTD} == "c89" || ${CSTD} == "c90"
294CFLAGS+=        -std=iso9899:1990
295.elif ${CSTD} == "c94" || ${CSTD} == "c95"
296CFLAGS+=        -std=iso9899:199409
297.elif ${CSTD} == "c99"
298CFLAGS+=        -std=iso9899:1999
299.else # CSTD
300CFLAGS+=        -std=${CSTD}
301.endif # CSTD
302
303# Please keep this if in sync with bsd.sys.mk
304.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
305# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
306.if ${COMPILER_TYPE} == "clang"
307# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
308.if ${COMPILER_VERSION} >= 120000
309CCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
310.else
311CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
312.endif
313.else
314# GCC does not support an absolute path for -fuse-ld so we just print this
315# warning instead and let the user add the required symlinks.
316# However, we can avoid this warning if -B is set appropriately (e.g. for
317# CROSS_TOOLCHAIN=...-gcc).
318.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
319.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
320.endif
321.endif
322.endif
323
324# Set target-specific linker emulation name.
325LD_EMULATION_aarch64=aarch64elf
326LD_EMULATION_amd64=elf_x86_64_fbsd
327LD_EMULATION_arm=armelf_fbsd
328LD_EMULATION_armv6=armelf_fbsd
329LD_EMULATION_armv7=armelf_fbsd
330LD_EMULATION_i386=elf_i386_fbsd
331LD_EMULATION_powerpc= elf32ppc_fbsd
332LD_EMULATION_powerpcspe= elf32ppc_fbsd
333LD_EMULATION_powerpc64= elf64ppc_fbsd
334LD_EMULATION_powerpc64le= elf64lppc_fbsd
335LD_EMULATION_riscv64= elf64lriscv
336LD_EMULATION_riscv64sf= elf64lriscv
337LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
338