xref: /freebsd/sys/conf/kern.mk (revision 0bf48626aaa33768078f5872b922b1487b3a9296)
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
41CLANG_NO_IAS= -no-integrated-as
42.endif
43
44.if ${COMPILER_TYPE} == "gcc"
45.if ${COMPILER_VERSION} >= 40800
46# Catch-all for all the things that are in our tree, but for which we're
47# not yet ready for this compiler.
48NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
49CWARNEXTRA?=	-Wno-error=address				\
50		-Wno-error=aggressive-loop-optimizations	\
51		-Wno-error=array-bounds				\
52		-Wno-error=attributes				\
53		-Wno-error=cast-qual				\
54		-Wno-error=enum-compare				\
55		-Wno-error=inline				\
56		-Wno-error=maybe-uninitialized			\
57		-Wno-error=overflow				\
58		-Wno-error=sequence-point			\
59		-Wno-unused-but-set-variable
60.if ${COMPILER_VERSION} >= 60100
61CWARNEXTRA+=	-Wno-error=misleading-indentation		\
62		-Wno-error=nonnull-compare			\
63		-Wno-error=shift-overflow			\
64		-Wno-error=tautological-compare			\
65		-Wno-format-zero-length
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# 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.if ${MACHINE_CPUARCH} == "riscv"
132CFLAGS.gcc+=	-mcmodel=medany -march=rv64imafdc -mabi=lp64
133INLINE_LIMIT?=	8000
134.endif
135
136#
137# For sparc64 we want the medany code model so modules may be located
138# anywhere in the 64-bit address space.  We also tell GCC to use floating
139# point emulation.  This avoids using floating point registers for integer
140# operations which it has a tendency to do.
141#
142.if ${MACHINE_CPUARCH} == "sparc64"
143CFLAGS.clang+=	-mcmodel=large -fno-dwarf2-cfi-asm
144CFLAGS.gcc+=	-mcmodel=medany -msoft-float
145INLINE_LIMIT?=	15000
146.endif
147
148#
149# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
150# operations inside the kernel itself.  These operations are exclusively
151# reserved for user applications.
152#
153# gcc:
154# Setting -mno-mmx implies -mno-3dnow
155# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
156#
157# clang:
158# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
159# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
160# (-mfpmath= is not supported)
161#
162.if ${MACHINE_CPUARCH} == "amd64"
163CFLAGS.clang+=	-mno-aes -mno-avx
164CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
165		-fno-asynchronous-unwind-tables
166INLINE_LIMIT?=	8000
167.endif
168
169#
170# For PowerPC we tell gcc to use floating point emulation.  This avoids using
171# floating point registers for integer operations which it has a tendency to do.
172# Also explicitly disable Altivec instructions inside the kernel.
173#
174.if ${MACHINE_CPUARCH} == "powerpc"
175CFLAGS+=	-mno-altivec -msoft-float
176INLINE_LIMIT?=	15000
177.endif
178
179.if ${MACHINE_ARCH} == "powerpcspe"
180CFLAGS.gcc+=	-mno-spe
181.endif
182
183#
184# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
185# DDB happy. ELFv2, if available, has some other efficiency benefits.
186#
187.if ${MACHINE_ARCH} == "powerpc64"
188.if ${COMPILER_VERSION} >= 40900
189CFLAGS.gcc+=	-mabi=elfv2
190.else
191CFLAGS.gcc+=	-mcall-aixdesc
192.endif
193CFLAGS.clang+=	-mabi=elfv2
194.endif
195
196#
197# For MIPS we also tell gcc to use floating point emulation
198#
199.if ${MACHINE_CPUARCH} == "mips"
200CFLAGS+=	-msoft-float
201INLINE_LIMIT?=	8000
202.endif
203
204#
205# GCC 3.0 and above like to do certain optimizations based on the
206# assumption that the program is linked against libc.  Stop this.
207#
208CFLAGS+=	-ffreestanding
209
210#
211# The C standard leaves signed integer overflow behavior undefined.
212# gcc and clang opimizers take advantage of this.  The kernel makes
213# use of signed integer wraparound mechanics so we need the compiler
214# to treat it as a wraparound and not take shortcuts.
215#
216CFLAGS+=	-fwrapv
217
218#
219# GCC SSP support
220#
221.if ${MK_SSP} != "no" && \
222    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
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# Add -gdwarf-2 when compiling -g. The default starting in clang v3.4
236# and gcc 4.8 is to generate DWARF version 4. However, our tools don't
237# cope well with DWARF 4, so force it to genereate DWARF2, which they
238# understand. Do this unconditionally as it is harmless when not needed,
239# but critical for these newer versions.
240#
241.if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == ""
242CFLAGS+=	-gdwarf-2
243.endif
244
245CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
246CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
247
248# Tell bmake not to mistake standard targets for things to be searched for
249# or expect to ever be up-to-date.
250PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
251		beforelinking build build-tools buildfiles buildincludes \
252		checkdpadd clean cleandepend cleandir cleanobj configure \
253		depend distclean distribute exe \
254		html includes install installfiles installincludes \
255		obj objlink objs objwarn \
256		realinstall regress \
257		tags whereobj
258
259.PHONY: ${PHONY_NOTMAIN}
260.NOTMAIN: ${PHONY_NOTMAIN}
261
262CSTD=		c99
263
264.if ${CSTD} == "k&r"
265CFLAGS+=        -traditional
266.elif ${CSTD} == "c89" || ${CSTD} == "c90"
267CFLAGS+=        -std=iso9899:1990
268.elif ${CSTD} == "c94" || ${CSTD} == "c95"
269CFLAGS+=        -std=iso9899:199409
270.elif ${CSTD} == "c99"
271CFLAGS+=        -std=iso9899:1999
272.else # CSTD
273CFLAGS+=        -std=${CSTD}
274.endif # CSTD
275
276# Set target-specific linker emulation name.
277LD_EMULATION_aarch64=aarch64elf
278LD_EMULATION_amd64=elf_x86_64_fbsd
279LD_EMULATION_arm=armelf_fbsd
280LD_EMULATION_armv6=armelf_fbsd
281LD_EMULATION_armv7=armelf_fbsd
282LD_EMULATION_i386=elf_i386_fbsd
283LD_EMULATION_mips= elf32btsmip_fbsd
284LD_EMULATION_mipshf= elf32btsmip_fbsd
285LD_EMULATION_mips64= elf64btsmip_fbsd
286LD_EMULATION_mips64hf= elf64btsmip_fbsd
287LD_EMULATION_mipsel= elf32ltsmip_fbsd
288LD_EMULATION_mipselhf= elf32ltsmip_fbsd
289LD_EMULATION_mips64el= elf64ltsmip_fbsd
290LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
291LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
292LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd   # I don't think this is a thing that works
293LD_EMULATION_powerpc= elf32ppc_fbsd
294LD_EMULATION_powerpcspe= elf32ppc_fbsd
295LD_EMULATION_powerpc64= elf64ppc_fbsd
296LD_EMULATION_riscv64= elf64lriscv
297LD_EMULATION_sparc64= elf64_sparc_fbsd
298LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
299