xref: /freebsd/sys/conf/kern.mk (revision 7be9a3b45356747f9fcb6d69a722c1c95f8060bf)
1# $FreeBSD$
2
3#
4# Warning flags for compiling the kernel and components of the kernel:
5#
6CWARNFLAGS?=	-Wall -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.if ${COMPILER_VERSION} >= 100000
29NO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
30.endif
31.if ${COMPILER_VERSION} >= 140000
32NO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
33.endif
34# Several other warnings which might be useful in some cases, but not severe
35# enough to error out the whole kernel build.  Display them anyway, so there is
36# some incentive to fix them eventually.
37CWARNEXTRA?=	-Wno-error=tautological-compare -Wno-error=empty-body \
38		-Wno-error=parentheses-equality -Wno-error=unused-function \
39		-Wno-error=pointer-sign
40CWARNEXTRA+=	-Wno-error=shift-negative-value
41CWARNEXTRA+=	-Wno-address-of-packed-member
42.if ${COMPILER_VERSION} >= 130000
43CWARNFLAGS+=	-Wno-error=unused-but-set-variable
44.endif
45.endif	# clang
46
47.if ${COMPILER_TYPE} == "gcc"
48# Catch-all for all the things that are in our tree, but for which we're
49# not yet ready for this compiler.
50NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
51CWARNEXTRA?=	-Wno-error=address				\
52		-Wno-error=aggressive-loop-optimizations	\
53		-Wno-error=array-bounds				\
54		-Wno-error=attributes				\
55		-Wno-error=cast-qual				\
56		-Wno-error=enum-compare				\
57		-Wno-error=maybe-uninitialized			\
58		-Wno-error=misleading-indentation		\
59		-Wno-error=nonnull-compare			\
60		-Wno-error=overflow				\
61		-Wno-error=sequence-point			\
62		-Wno-error=shift-overflow			\
63		-Wno-error=tautological-compare			\
64		-Wno-unused-but-set-variable
65.if ${COMPILER_VERSION} >= 70100
66CWARNEXTRA+=	-Wno-error=stringop-overflow
67.endif
68.if ${COMPILER_VERSION} >= 70200
69CWARNEXTRA+=	-Wno-error=memset-elt-size
70.endif
71.if ${COMPILER_VERSION} >= 80000
72CWARNEXTRA+=	-Wno-error=packed-not-aligned
73.endif
74.if ${COMPILER_VERSION} >= 90100
75CWARNEXTRA+=	-Wno-address-of-packed-member			\
76		-Wno-error=alloca-larger-than=
77.endif
78.endif	# gcc
79
80# This warning is utter nonsense
81CWARNFLAGS+=	-Wno-format-zero-length
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"
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+=	-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#
132# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
133# point registers within the kernel. However, for kernels supporting hardware
134# float (FPE), we have to include that in the march so we can have limited
135# floating point support in context switching needed for that. This is different
136# than userland where we use a hard-float ABI (lp64d).
137#
138# We also specify the "medium" code model, which generates code suitable for a
139# 2GiB addressing range located at any offset, allowing modules to be located
140# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
141# code model as "medium" and "medany" respectively.
142#
143.if ${MACHINE_CPUARCH} == "riscv"
144CFLAGS+=	-march=rv64imafdc
145CFLAGS+=	-mabi=lp64
146CFLAGS.clang+=	-mcmodel=medium
147CFLAGS.gcc+=	-mcmodel=medany
148INLINE_LIMIT?=	8000
149
150.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
151CFLAGS+=	-mno-relax
152.endif
153.endif
154
155#
156# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
157# operations inside the kernel itself.  These operations are exclusively
158# reserved for user applications.
159#
160# gcc:
161# Setting -mno-mmx implies -mno-3dnow
162# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
163#
164# clang:
165# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
166# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
167# (-mfpmath= is not supported)
168#
169.if ${MACHINE_CPUARCH} == "amd64"
170CFLAGS.clang+=	-mno-aes -mno-avx
171CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
172		-fno-asynchronous-unwind-tables
173INLINE_LIMIT?=	8000
174.endif
175
176#
177# For PowerPC we tell gcc to use floating point emulation.  This avoids using
178# floating point registers for integer operations which it has a tendency to do.
179# Also explicitly disable Altivec instructions inside the kernel.
180#
181.if ${MACHINE_CPUARCH} == "powerpc"
182CFLAGS+=	-mno-altivec -msoft-float
183INLINE_LIMIT?=	15000
184.endif
185
186.if ${MACHINE_ARCH} == "powerpcspe"
187CFLAGS.gcc+=	-mno-spe
188.endif
189
190#
191# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
192# DDB happy. ELFv2, if available, has some other efficiency benefits.
193#
194.if ${MACHINE_ARCH:Mpowerpc64*} != ""
195CFLAGS+=	-mabi=elfv2
196.endif
197
198#
199# GCC 3.0 and above like to do certain optimizations based on the
200# assumption that the program is linked against libc.  Stop this.
201#
202CFLAGS+=	-ffreestanding
203
204#
205# The C standard leaves signed integer overflow behavior undefined.
206# gcc and clang opimizers take advantage of this.  The kernel makes
207# use of signed integer wraparound mechanics so we need the compiler
208# to treat it as a wraparound and not take shortcuts.
209#
210CFLAGS+=	-fwrapv
211
212#
213# GCC SSP support
214#
215.if ${MK_SSP} != "no"
216CFLAGS+=	-fstack-protector
217.endif
218
219#
220# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
221#
222.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
223    ${MK_KERNEL_RETPOLINE} != "no"
224CFLAGS+=	-mretpoline
225.endif
226
227#
228# Initialize stack variables on function entry
229#
230.if ${MK_INIT_ALL_ZERO} == "yes"
231.if ${COMPILER_FEATURES:Minit-all}
232CFLAGS+= -ftrivial-auto-var-init=zero \
233    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
234.else
235.warning InitAll (zeros) requested but not support by compiler
236.endif
237.elif ${MK_INIT_ALL_PATTERN} == "yes"
238.if ${COMPILER_FEATURES:Minit-all}
239CFLAGS+= -ftrivial-auto-var-init=pattern
240.else
241.warning InitAll (pattern) requested but not support by compiler
242.endif
243.endif
244
245CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
246CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
247CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
248
249# Tell bmake not to mistake standard targets for things to be searched for
250# or expect to ever be up-to-date.
251PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
252		beforelinking build build-tools buildfiles buildincludes \
253		checkdpadd clean cleandepend cleandir cleanobj configure \
254		depend distclean distribute exe \
255		html includes install installfiles installincludes \
256		obj objlink objs objwarn \
257		realinstall regress \
258		tags whereobj
259
260.PHONY: ${PHONY_NOTMAIN}
261.NOTMAIN: ${PHONY_NOTMAIN}
262
263CSTD=		c99
264
265.if ${CSTD} == "k&r"
266CFLAGS+=        -traditional
267.elif ${CSTD} == "c89" || ${CSTD} == "c90"
268CFLAGS+=        -std=iso9899:1990
269.elif ${CSTD} == "c94" || ${CSTD} == "c95"
270CFLAGS+=        -std=iso9899:199409
271.elif ${CSTD} == "c99"
272CFLAGS+=        -std=iso9899:1999
273.else # CSTD
274CFLAGS+=        -std=${CSTD}
275.endif # CSTD
276
277# Please keep this if in sync with bsd.sys.mk
278.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
279# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
280.if ${COMPILER_TYPE} == "clang"
281# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
282.if ${COMPILER_VERSION} >= 120000
283CCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
284.else
285CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
286.endif
287.else
288# GCC does not support an absolute path for -fuse-ld so we just print this
289# warning instead and let the user add the required symlinks.
290# However, we can avoid this warning if -B is set appropriately (e.g. for
291# CROSS_TOOLCHAIN=...-gcc).
292.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
293.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
294.endif
295.endif
296.endif
297
298# Set target-specific linker emulation name.
299LD_EMULATION_aarch64=aarch64elf
300LD_EMULATION_amd64=elf_x86_64_fbsd
301LD_EMULATION_arm=armelf_fbsd
302LD_EMULATION_armv6=armelf_fbsd
303LD_EMULATION_armv7=armelf_fbsd
304LD_EMULATION_i386=elf_i386_fbsd
305LD_EMULATION_powerpc= elf32ppc_fbsd
306LD_EMULATION_powerpcspe= elf32ppc_fbsd
307LD_EMULATION_powerpc64= elf64ppc_fbsd
308LD_EMULATION_powerpc64le= elf64lppc_fbsd
309LD_EMULATION_riscv64= elf64lriscv
310LD_EMULATION_riscv64sf= elf64lriscv
311LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
312