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