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