xref: /freebsd/sys/conf/kern.mk (revision 090e9752d7291db0c251a5576892e3bbbaea2479)
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, for kernels supporting hardware
157# float (FPE), we have to include that in the march so we can have limited
158# floating point support in context switching needed for that. This is different
159# than userland where we use a hard-float ABI (lp64d).
160#
161# We also specify the "medium" code model, which generates code suitable for a
162# 2GiB addressing range located at any offset, allowing modules to be located
163# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
164# code model as "medium" and "medany" respectively.
165#
166.if ${MACHINE_CPUARCH} == "riscv"
167CFLAGS+=	-march=rv64imafdc
168CFLAGS+=	-mabi=lp64
169CFLAGS.clang+=	-mcmodel=medium
170CFLAGS.gcc+=	-mcmodel=medany
171INLINE_LIMIT?=	8000
172
173.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
174CFLAGS+=	-mno-relax
175.endif
176.endif
177
178#
179# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
180# operations inside the kernel itself.  These operations are exclusively
181# reserved for user applications.
182#
183# gcc:
184# Setting -mno-mmx implies -mno-3dnow
185# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
186#
187# clang:
188# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
189# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
190# (-mfpmath= is not supported)
191#
192.if ${MACHINE_CPUARCH} == "amd64"
193CFLAGS.clang+=	-mno-aes -mno-avx
194CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
195		-fno-asynchronous-unwind-tables
196INLINE_LIMIT?=	8000
197.endif
198
199#
200# For PowerPC we tell gcc to use floating point emulation.  This avoids using
201# floating point registers for integer operations which it has a tendency to do.
202# Also explicitly disable Altivec instructions inside the kernel.
203#
204.if ${MACHINE_CPUARCH} == "powerpc"
205CFLAGS+=	-mno-altivec -msoft-float
206INLINE_LIMIT?=	15000
207.endif
208
209.if ${MACHINE_ARCH} == "powerpcspe"
210CFLAGS.gcc+=	-mno-spe
211.endif
212
213#
214# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
215# DDB happy. ELFv2, if available, has some other efficiency benefits.
216#
217.if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
218    ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
219CFLAGS+=	-mabi=elfv2
220.endif
221
222#
223# GCC 3.0 and above like to do certain optimizations based on the
224# assumption that the program is linked against libc.  Stop this.
225#
226CFLAGS+=	-ffreestanding
227
228#
229# The C standard leaves signed integer overflow behavior undefined.
230# gcc and clang opimizers take advantage of this.  The kernel makes
231# use of signed integer wraparound mechanics so we need the compiler
232# to treat it as a wraparound and not take shortcuts.
233#
234CFLAGS+=	-fwrapv
235
236#
237# GCC SSP support
238#
239.if ${MK_SSP} != "no"
240CFLAGS+=	-fstack-protector
241.endif
242
243#
244# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
245#
246.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
247    ${MK_KERNEL_RETPOLINE} != "no"
248CFLAGS+=	-mretpoline
249.endif
250
251#
252# Initialize stack variables on function entry
253#
254.if ${MK_INIT_ALL_ZERO} == "yes"
255.if ${COMPILER_FEATURES:Minit-all}
256CFLAGS+= -ftrivial-auto-var-init=zero \
257    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
258.else
259.warning InitAll (zeros) requested but not support by compiler
260.endif
261.elif ${MK_INIT_ALL_PATTERN} == "yes"
262.if ${COMPILER_FEATURES:Minit-all}
263CFLAGS+= -ftrivial-auto-var-init=pattern
264.else
265.warning InitAll (pattern) requested but not support by compiler
266.endif
267.endif
268
269CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
270CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
271CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
272
273# Tell bmake not to mistake standard targets for things to be searched for
274# or expect to ever be up-to-date.
275PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
276		beforelinking build build-tools buildfiles buildincludes \
277		checkdpadd clean cleandepend cleandir cleanobj configure \
278		depend distclean distribute exe \
279		html includes install installfiles installincludes \
280		obj objlink objs objwarn \
281		realinstall regress \
282		tags whereobj
283
284.PHONY: ${PHONY_NOTMAIN}
285.NOTMAIN: ${PHONY_NOTMAIN}
286
287CSTD=		c99
288
289.if ${CSTD} == "k&r"
290CFLAGS+=        -traditional
291.elif ${CSTD} == "c89" || ${CSTD} == "c90"
292CFLAGS+=        -std=iso9899:1990
293.elif ${CSTD} == "c94" || ${CSTD} == "c95"
294CFLAGS+=        -std=iso9899:199409
295.elif ${CSTD} == "c99"
296CFLAGS+=        -std=iso9899:1999
297.else # CSTD
298CFLAGS+=        -std=${CSTD}
299.endif # CSTD
300
301# Please keep this if in sync with bsd.sys.mk
302.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
303# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
304.if ${COMPILER_TYPE} == "clang"
305# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
306.if ${COMPILER_VERSION} >= 120000
307CCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
308.else
309CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
310.endif
311.else
312# GCC does not support an absolute path for -fuse-ld so we just print this
313# warning instead and let the user add the required symlinks.
314# However, we can avoid this warning if -B is set appropriately (e.g. for
315# CROSS_TOOLCHAIN=...-gcc).
316.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
317.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
318.endif
319.endif
320.endif
321
322# Set target-specific linker emulation name.
323LD_EMULATION_aarch64=aarch64elf
324LD_EMULATION_amd64=elf_x86_64_fbsd
325LD_EMULATION_arm=armelf_fbsd
326LD_EMULATION_armv6=armelf_fbsd
327LD_EMULATION_armv7=armelf_fbsd
328LD_EMULATION_i386=elf_i386_fbsd
329LD_EMULATION_powerpc= elf32ppc_fbsd
330LD_EMULATION_powerpcspe= elf32ppc_fbsd
331LD_EMULATION_powerpc64= elf64ppc_fbsd
332LD_EMULATION_powerpc64le= elf64lppc_fbsd
333LD_EMULATION_riscv64= elf64lriscv
334LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
335