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