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