xref: /freebsd/sys/conf/kern.mk (revision 324cdd9320f58837c2fbaa7f6ceb9ea5c33d5b2a)
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
34.if ${COMPILER_VERSION} >= 30700
35CWARNEXTRA+=	-Wno-error-shift-negative-value
36.endif
37.if ${COMPILER_VERSION} >= 40000
38CWARNEXTRA+=	-Wno-address-of-packed-member
39.endif
40.endif
41
42.if ${COMPILER_TYPE} == "gcc"
43.if ${COMPILER_VERSION} >= 40800
44# Catch-all for all the things that are in our tree, but for which we're
45# not yet ready for this compiler.
46NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
47CWARNEXTRA?=	-Wno-error=address				\
48		-Wno-error=aggressive-loop-optimizations	\
49		-Wno-error=array-bounds				\
50		-Wno-error=attributes				\
51		-Wno-error=cast-qual				\
52		-Wno-error=enum-compare				\
53		-Wno-error=inline				\
54		-Wno-error=maybe-uninitialized			\
55		-Wno-error=overflow				\
56		-Wno-error=sequence-point			\
57		-Wno-unused-but-set-variable
58.if ${COMPILER_VERSION} >= 60100
59CWARNEXTRA+=	-Wno-error=misleading-indentation		\
60		-Wno-error=nonnull-compare			\
61		-Wno-error=shift-overflow			\
62		-Wno-error=tautological-compare
63.endif
64.if ${COMPILER_VERSION} >= 70100
65CWARNEXTRA+=	-Wno-error=stringop-overflow
66.endif
67.if ${COMPILER_VERSION} >= 70200
68CWARNEXTRA+=	-Wno-error=memset-elt-size
69.endif
70.if ${COMPILER_VERSION} >= 80000
71CWARNEXTRA+=	-Wno-error=packed-not-aligned
72.endif
73.else
74# For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars.
75CWARNEXTRA?=	-Wno-uninitialized
76# GCC 4.2 doesn't have -Wno-error=cast-qual, so just disable the warning for
77# the few files that are already known to generate cast-qual warnings.
78NO_WCAST_QUAL= -Wno-cast-qual
79NO_WNONNULL=	-Wno-nonnull
80.endif
81.endif
82
83# This warning is utter nonsense
84CWARNFLAGS+=	-Wno-format-zero-length
85
86# External compilers may not support our format extensions.  Allow them
87# to be disabled.  WARNING: format checking is disabled in this case.
88.if ${MK_FORMAT_EXTENSIONS} == "no"
89FORMAT_EXTENSIONS=	-Wno-format
90.elif ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
91FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
92.else
93FORMAT_EXTENSIONS=	-fformat-extensions
94.endif
95
96#
97# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
98# and above adds code to the entry and exit point of every function to align the
99# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
100# per function call.  While the 16-byte alignment may benefit micro benchmarks,
101# it is probably an overall loss as it makes the code bigger (less efficient
102# use of code cache tag lines) and uses more stack (less efficient use of data
103# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
104# operations inside the kernel itself.  These operations are exclusively
105# reserved for user applications.
106#
107# gcc:
108# Setting -mno-mmx implies -mno-3dnow
109# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
110#
111# clang:
112# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
113# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
114#
115.if ${MACHINE_CPUARCH} == "i386"
116CFLAGS.gcc+=	-mno-align-long-strings -mpreferred-stack-boundary=2
117CFLAGS.clang+=	-mno-aes -mno-avx
118CFLAGS+=	-mno-mmx -mno-sse -msoft-float
119INLINE_LIMIT?=	8000
120.endif
121
122.if ${MACHINE_CPUARCH} == "arm"
123INLINE_LIMIT?=	8000
124.endif
125
126.if ${MACHINE_CPUARCH} == "aarch64"
127# We generally don't want fpu instructions in the kernel.
128CFLAGS += -mgeneral-regs-only
129# Reserve x18 for pcpu data
130CFLAGS += -ffixed-x18
131INLINE_LIMIT?=	8000
132.endif
133
134#
135# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
136# point registers within the kernel. We also specify the "medium" code model,
137# which generates code suitable for a 2GiB addressing range located at any
138# offset, allowing modules to be located anywhere in the 64-bit address space.
139# Note that clang and GCC refer to this code model as "medium" and "medany"
140# respectively.
141#
142.if ${MACHINE_CPUARCH} == "riscv"
143CFLAGS+=	-march=rv64imafdc -mabi=lp64
144CFLAGS.clang+=	-mcmodel=medium
145CFLAGS.gcc+=	-mcmodel=medany
146INLINE_LIMIT?=	8000
147.endif
148
149#
150# For sparc64 we want the medany code model so modules may be located
151# anywhere in the 64-bit address space.  We also tell GCC to use floating
152# point emulation.  This avoids using floating point registers for integer
153# operations which it has a tendency to do.
154#
155.if ${MACHINE_CPUARCH} == "sparc64"
156CFLAGS.clang+=	-mcmodel=large -fno-dwarf2-cfi-asm
157CFLAGS.gcc+=	-mcmodel=medany -msoft-float
158INLINE_LIMIT?=	15000
159.endif
160
161#
162# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
163# operations inside the kernel itself.  These operations are exclusively
164# reserved for user applications.
165#
166# gcc:
167# Setting -mno-mmx implies -mno-3dnow
168# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
169#
170# clang:
171# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
172# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
173# (-mfpmath= is not supported)
174#
175.if ${MACHINE_CPUARCH} == "amd64"
176CFLAGS.clang+=	-mno-aes -mno-avx
177CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
178		-fno-asynchronous-unwind-tables
179INLINE_LIMIT?=	8000
180.endif
181
182#
183# For PowerPC we tell gcc to use floating point emulation.  This avoids using
184# floating point registers for integer operations which it has a tendency to do.
185# Also explicitly disable Altivec instructions inside the kernel.
186#
187.if ${MACHINE_CPUARCH} == "powerpc"
188CFLAGS+=	-mno-altivec -msoft-float
189INLINE_LIMIT?=	15000
190.endif
191
192.if ${MACHINE_ARCH} == "powerpcspe"
193CFLAGS.gcc+=	-mno-spe
194.endif
195
196#
197# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
198# DDB happy. ELFv2, if available, has some other efficiency benefits.
199#
200.if ${MACHINE_ARCH} == "powerpc64"
201.if ${COMPILER_VERSION} >= 40900
202CFLAGS.gcc+=	-mabi=elfv2
203.else
204CFLAGS.gcc+=	-mcall-aixdesc
205.endif
206CFLAGS.clang+=	-mabi=elfv2
207.endif
208
209#
210# For MIPS we also tell gcc to use floating point emulation
211#
212.if ${MACHINE_CPUARCH} == "mips"
213CFLAGS+=	-msoft-float
214INLINE_LIMIT?=	8000
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" && \
235    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
236CFLAGS+=	-fstack-protector
237.endif
238
239#
240# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
241#
242.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
243    ${MK_KERNEL_RETPOLINE} != "no"
244CFLAGS+=	-mretpoline
245.endif
246
247#
248# Add -gdwarf-2 when compiling -g. The default starting in clang v3.4
249# and gcc 4.8 is to generate DWARF version 4. However, our tools don't
250# cope well with DWARF 4, so force it to genereate DWARF2, which they
251# understand. Do this unconditionally as it is harmless when not needed,
252# but critical for these newer versions.
253#
254.if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == ""
255CFLAGS+=	-gdwarf-2
256.endif
257
258CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
259CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
260CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
261
262# Tell bmake not to mistake standard targets for things to be searched for
263# or expect to ever be up-to-date.
264PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
265		beforelinking build build-tools buildfiles buildincludes \
266		checkdpadd clean cleandepend cleandir cleanobj configure \
267		depend distclean distribute exe \
268		html includes install installfiles installincludes \
269		obj objlink objs objwarn \
270		realinstall regress \
271		tags whereobj
272
273.PHONY: ${PHONY_NOTMAIN}
274.NOTMAIN: ${PHONY_NOTMAIN}
275
276CSTD=		c99
277
278.if ${CSTD} == "k&r"
279CFLAGS+=        -traditional
280.elif ${CSTD} == "c89" || ${CSTD} == "c90"
281CFLAGS+=        -std=iso9899:1990
282.elif ${CSTD} == "c94" || ${CSTD} == "c95"
283CFLAGS+=        -std=iso9899:199409
284.elif ${CSTD} == "c99"
285CFLAGS+=        -std=iso9899:1999
286.else # CSTD
287CFLAGS+=        -std=${CSTD}
288.endif # CSTD
289
290# Set target-specific linker emulation name.
291LD_EMULATION_aarch64=aarch64elf
292LD_EMULATION_amd64=elf_x86_64_fbsd
293LD_EMULATION_arm=armelf_fbsd
294LD_EMULATION_armv6=armelf_fbsd
295LD_EMULATION_armv7=armelf_fbsd
296LD_EMULATION_i386=elf_i386_fbsd
297LD_EMULATION_mips= elf32btsmip_fbsd
298LD_EMULATION_mipshf= elf32btsmip_fbsd
299LD_EMULATION_mips64= elf64btsmip_fbsd
300LD_EMULATION_mips64hf= elf64btsmip_fbsd
301LD_EMULATION_mipsel= elf32ltsmip_fbsd
302LD_EMULATION_mipselhf= elf32ltsmip_fbsd
303LD_EMULATION_mips64el= elf64ltsmip_fbsd
304LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
305LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
306LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd   # I don't think this is a thing that works
307LD_EMULATION_powerpc= elf32ppc_fbsd
308LD_EMULATION_powerpcspe= elf32ppc_fbsd
309LD_EMULATION_powerpc64= elf64ppc_fbsd
310LD_EMULATION_riscv64= elf64lriscv
311LD_EMULATION_sparc64= elf64_sparc_fbsd
312LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
313