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