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