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