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