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