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