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