xref: /freebsd/sys/conf/kern.mk (revision c08cbc64dca550d895175d5cf9dc60d00a041415)
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 -Winline -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-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-unneeded-internal-declaration
25NO_WSOMETIMES_UNINITIALIZED=	-Wno-error-sometimes-uninitialized
26NO_WCAST_QUAL=			-Wno-cast-qual
27# Several other warnings which might be useful in some cases, but not severe
28# enough to error out the whole kernel build.  Display them anyway, so there is
29# some incentive to fix them eventually.
30CWARNEXTRA?=	-Wno-error-tautological-compare -Wno-error-empty-body \
31		-Wno-error-parentheses-equality -Wno-error-unused-function \
32		-Wno-error-pointer-sign
33.if ${COMPILER_VERSION} >= 30700
34CWARNEXTRA+=	-Wno-error-shift-negative-value
35.endif
36
37CLANG_NO_IAS= -no-integrated-as
38.if ${COMPILER_VERSION} < 30500
39# XXX: clang < 3.5 integrated-as doesn't grok .codeNN directives
40CLANG_NO_IAS34= -no-integrated-as
41.endif
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.
48CWARNEXTRA?=	-Wno-error=inline -Wno-error=enum-compare -Wno-error=unused-but-set-variable \
49		-Wno-error=aggressive-loop-optimizations -Wno-error=maybe-uninitialized \
50		-Wno-error=array-bounds -Wno-error=address \
51		-Wno-error=cast-qual -Wno-error=sequence-point -Wno-error=attributes \
52		-Wno-error=strict-overflow -Wno-error=overflow
53.if ${COMPILER_VERSION} >= 60100
54CWARNEXTRA+=	-Wno-error=nonnull-compare -Wno-error=shift-overflow
55.endif
56.else
57# For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars.
58CWARNEXTRA?=	-Wno-uninitialized
59.endif
60.endif
61
62# External compilers may not support our format extensions.  Allow them
63# to be disabled.  WARNING: format checking is disabled in this case.
64.if ${MK_FORMAT_EXTENSIONS} == "no"
65FORMAT_EXTENSIONS=	-Wno-format
66.elif ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
67FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
68.else
69FORMAT_EXTENSIONS=	-fformat-extensions
70.endif
71
72#
73# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
74# and above adds code to the entry and exit point of every function to align the
75# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
76# per function call.  While the 16-byte alignment may benefit micro benchmarks,
77# it is probably an overall loss as it makes the code bigger (less efficient
78# use of code cache tag lines) and uses more stack (less efficient use of data
79# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
80# operations inside the kernel itself.  These operations are exclusively
81# reserved for user applications.
82#
83# gcc:
84# Setting -mno-mmx implies -mno-3dnow
85# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
86#
87# clang:
88# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
89# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
90#
91.if ${MACHINE_CPUARCH} == "i386"
92CFLAGS.gcc+=	-mno-align-long-strings -mpreferred-stack-boundary=2
93CFLAGS.clang+=	-mno-aes -mno-avx
94CFLAGS+=	-mno-mmx -mno-sse -msoft-float
95INLINE_LIMIT?=	8000
96.endif
97
98.if ${MACHINE_CPUARCH} == "arm"
99INLINE_LIMIT?=	8000
100.endif
101
102.if ${MACHINE_CPUARCH} == "aarch64"
103# We generally don't want fpu instructions in the kernel.
104CFLAGS += -mgeneral-regs-only
105# Reserve x18 for pcpu data
106CFLAGS += -ffixed-x18
107.endif
108
109.if ${MACHINE_CPUARCH} == "riscv"
110CFLAGS.gcc+=	-mcmodel=medany
111INLINE_LIMIT?=	8000
112.endif
113
114#
115# For sparc64 we want the medany code model so modules may be located
116# anywhere in the 64-bit address space.  We also tell GCC to use floating
117# point emulation.  This avoids using floating point registers for integer
118# operations which it has a tendency to do.
119#
120.if ${MACHINE_CPUARCH} == "sparc64"
121CFLAGS.clang+=	-mcmodel=large -fno-dwarf2-cfi-asm
122CFLAGS.gcc+=	-mcmodel=medany -msoft-float
123INLINE_LIMIT?=	15000
124.endif
125
126#
127# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
128# operations inside the kernel itself.  These operations are exclusively
129# reserved for user applications.
130#
131# gcc:
132# Setting -mno-mmx implies -mno-3dnow
133# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
134#
135# clang:
136# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
137# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
138# (-mfpmath= is not supported)
139#
140.if ${MACHINE_CPUARCH} == "amd64"
141CFLAGS.clang+=	-mno-aes -mno-avx
142CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
143		-fno-asynchronous-unwind-tables
144INLINE_LIMIT?=	8000
145.endif
146
147#
148# For PowerPC we tell gcc to use floating point emulation.  This avoids using
149# floating point registers for integer operations which it has a tendency to do.
150# Also explicitly disable Altivec instructions inside the kernel.
151#
152.if ${MACHINE_CPUARCH} == "powerpc"
153CFLAGS+=	-mno-altivec
154CFLAGS.clang+=	-mllvm -disable-ppc-float-in-variadic=true
155CFLAGS.gcc+=	-msoft-float
156INLINE_LIMIT?=	15000
157.endif
158
159#
160# Use dot symbols on powerpc64 to make ddb happy
161#
162.if ${MACHINE_ARCH} == "powerpc64"
163CFLAGS.gcc+=	-mcall-aixdesc
164.endif
165
166#
167# For MIPS we also tell gcc to use floating point emulation
168#
169.if ${MACHINE_CPUARCH} == "mips"
170CFLAGS+=	-msoft-float
171INLINE_LIMIT?=	8000
172.endif
173
174#
175# GCC 3.0 and above like to do certain optimizations based on the
176# assumption that the program is linked against libc.  Stop this.
177#
178CFLAGS+=	-ffreestanding
179
180#
181# The C standard leaves signed integer overflow behavior undefined.
182# gcc and clang opimizers take advantage of this.  The kernel makes
183# use of signed integer wraparound mechanics so we need the compiler
184# to treat it as a wraparound and not take shortcuts.
185#
186CFLAGS+=	-fwrapv
187
188#
189# GCC SSP support
190#
191.if ${MK_SSP} != "no" && \
192    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
193CFLAGS+=	-fstack-protector
194.endif
195
196#
197# Add -gdwarf-2 when compiling -g. The default starting in clang v3.4
198# and gcc 4.8 is to generate DWARF version 4. However, our tools don't
199# cope well with DWARF 4, so force it to genereate DWARF2, which they
200# understand. Do this unconditionally as it is harmless when not needed,
201# but critical for these newer versions.
202#
203.if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == ""
204CFLAGS+=	-gdwarf-2
205.endif
206
207CFLAGS+= ${CWARNFLAGS} ${CWARNFLAGS.${.IMPSRC:T}}
208CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
209
210# Tell bmake not to mistake standard targets for things to be searched for
211# or expect to ever be up-to-date.
212PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
213		beforelinking build build-tools buildfiles buildincludes \
214		checkdpadd clean cleandepend cleandir cleanobj configure \
215		depend distclean distribute exe \
216		html includes install installfiles installincludes lint \
217		obj objlink objs objwarn \
218		realinstall regress \
219		tags whereobj
220
221.PHONY: ${PHONY_NOTMAIN}
222.NOTMAIN: ${PHONY_NOTMAIN}
223
224CSTD=		c99
225
226.if ${CSTD} == "k&r"
227CFLAGS+=        -traditional
228.elif ${CSTD} == "c89" || ${CSTD} == "c90"
229CFLAGS+=        -std=iso9899:1990
230.elif ${CSTD} == "c94" || ${CSTD} == "c95"
231CFLAGS+=        -std=iso9899:199409
232.elif ${CSTD} == "c99"
233CFLAGS+=        -std=iso9899:1999
234.else # CSTD
235CFLAGS+=        -std=${CSTD}
236.endif # CSTD
237