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