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