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