4 # Warning flags for compiling the kernel and components of the kernel:
6 CWARNFLAGS?= -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 \
13 # The following flags are next up for working on:
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
19 .if ${COMPILER_TYPE} == "clang"
20 NO_WCONSTANT_CONVERSION= -Wno-error=constant-conversion
21 NO_WSHIFT_COUNT_NEGATIVE= -Wno-shift-count-negative
22 NO_WSHIFT_COUNT_OVERFLOW= -Wno-shift-count-overflow
23 NO_WSELF_ASSIGN= -Wno-self-assign
24 NO_WUNNEEDED_INTERNAL_DECL= -Wno-error=unneeded-internal-declaration
25 NO_WSOMETIMES_UNINITIALIZED= -Wno-error=sometimes-uninitialized
26 NO_WCAST_QUAL= -Wno-error=cast-qual
27 NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
28 .if ${COMPILER_VERSION} >= 100000
29 NO_WMISLEADING_INDENTATION= -Wno-misleading-indentation
31 # Several other warnings which might be useful in some cases, but not severe
32 # enough to error out the whole kernel build. Display them anyway, so there is
33 # some incentive to fix them eventually.
34 CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \
35 -Wno-error=parentheses-equality -Wno-error=unused-function \
36 -Wno-error=pointer-sign
37 .if ${COMPILER_VERSION} >= 30700
38 CWARNEXTRA+= -Wno-error=shift-negative-value
40 .if ${COMPILER_VERSION} >= 40000
41 CWARNEXTRA+= -Wno-address-of-packed-member
43 .if ${COMPILER_VERSION} >= 130000
44 CWARNFLAGS+= -Wno-error=unused-but-set-variable
48 .if ${COMPILER_TYPE} == "gcc"
49 .if ${COMPILER_VERSION} >= 40800
50 # Catch-all for all the things that are in our tree, but for which we're
51 # not yet ready for this compiler.
52 NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
53 CWARNEXTRA?= -Wno-error=address \
54 -Wno-error=aggressive-loop-optimizations \
55 -Wno-error=array-bounds \
56 -Wno-error=attributes \
57 -Wno-error=cast-qual \
58 -Wno-error=enum-compare \
60 -Wno-error=maybe-uninitialized \
62 -Wno-error=sequence-point \
63 -Wno-error=unused-but-set-variable
64 .if ${COMPILER_VERSION} >= 60100
65 CWARNEXTRA+= -Wno-error=misleading-indentation \
66 -Wno-error=nonnull-compare \
67 -Wno-error=shift-overflow \
68 -Wno-error=tautological-compare
70 .if ${COMPILER_VERSION} >= 70200
71 CWARNEXTRA+= -Wno-error=memset-elt-size
73 .if ${COMPILER_VERSION} >= 80000
74 CWARNEXTRA+= -Wno-error=packed-not-aligned
77 # For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars.
78 CWARNEXTRA?= -Wno-uninitialized
79 # GCC 4.2 doesn't have -Wno-error=cast-qual, so just disable the warning for
80 # the few files that are already known to generate cast-qual warnings.
81 NO_WCAST_QUAL= -Wno-cast-qual
85 # This warning is utter nonsense
86 CWARNFLAGS+= -Wno-format-zero-length
88 # External compilers may not support our format extensions. Allow them
89 # to be disabled. WARNING: format checking is disabled in this case.
90 .if ${MK_FORMAT_EXTENSIONS} == "no"
91 FORMAT_EXTENSIONS= -Wno-format
92 .elif ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
93 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
95 FORMAT_EXTENSIONS= -fformat-extensions
99 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
100 # and above adds code to the entry and exit point of every function to align the
101 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
102 # per function call. While the 16-byte alignment may benefit micro benchmarks,
103 # it is probably an overall loss as it makes the code bigger (less efficient
104 # use of code cache tag lines) and uses more stack (less efficient use of data
105 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
106 # operations inside the kernel itself. These operations are exclusively
107 # reserved for user applications.
110 # Setting -mno-mmx implies -mno-3dnow
111 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
114 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
115 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
117 .if ${MACHINE_CPUARCH} == "i386"
118 CFLAGS.gcc+= -mno-align-long-strings -mpreferred-stack-boundary=2
119 CFLAGS.clang+= -mno-aes -mno-avx
120 CFLAGS+= -mno-mmx -mno-sse -msoft-float
124 .if ${MACHINE_CPUARCH} == "arm"
128 .if ${MACHINE_CPUARCH} == "aarch64"
129 # We generally don't want fpu instructions in the kernel.
130 CFLAGS += -mgeneral-regs-only
131 # Reserve x18 for pcpu data
132 CFLAGS += -ffixed-x18
137 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
138 # point registers within the kernel. We also specify the "medium" code model,
139 # which generates code suitable for a 2GiB addressing range located at any
140 # offset, allowing modules to be located anywhere in the 64-bit address space.
141 # Note that clang and GCC refer to this code model as "medium" and "medany"
144 .if ${MACHINE_CPUARCH} == "riscv"
145 CFLAGS+= -march=rv64imafdc -mabi=lp64
146 CFLAGS.clang+= -mcmodel=medium
147 CFLAGS.gcc+= -mcmodel=medany
150 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
156 # For sparc64 we want the medany code model so modules may be located
157 # anywhere in the 64-bit address space. We also tell GCC to use floating
158 # point emulation. This avoids using floating point registers for integer
159 # operations which it has a tendency to do.
161 .if ${MACHINE_CPUARCH} == "sparc64"
162 CFLAGS.clang+= -mcmodel=large -fno-dwarf2-cfi-asm
163 CFLAGS.gcc+= -mcmodel=medany -msoft-float
168 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
169 # operations inside the kernel itself. These operations are exclusively
170 # reserved for user applications.
173 # Setting -mno-mmx implies -mno-3dnow
174 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
177 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
178 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
179 # (-mfpmath= is not supported)
181 .if ${MACHINE_CPUARCH} == "amd64"
182 CFLAGS.clang+= -mno-aes -mno-avx
183 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
184 -fno-asynchronous-unwind-tables
189 # For PowerPC we tell gcc to use floating point emulation. This avoids using
190 # floating point registers for integer operations which it has a tendency to do.
191 # Also explicitly disable Altivec instructions inside the kernel.
193 .if ${MACHINE_CPUARCH} == "powerpc"
194 CFLAGS+= -mno-altivec -msoft-float
198 .if ${MACHINE_ARCH} == "powerpcspe"
199 CFLAGS.gcc+= -mno-spe
203 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
204 # DDB happy. ELFv2, if available, has some other efficiency benefits.
206 .if ${MACHINE_ARCH} == "powerpc64"
207 .if ${COMPILER_VERSION} >= 40900
208 CFLAGS.gcc+= -mabi=elfv2
210 CFLAGS.gcc+= -mcall-aixdesc
212 CFLAGS.clang+= -mabi=elfv2
216 # For MIPS we also tell gcc to use floating point emulation
218 .if ${MACHINE_CPUARCH} == "mips"
219 CFLAGS+= -msoft-float
224 # GCC 3.0 and above like to do certain optimizations based on the
225 # assumption that the program is linked against libc. Stop this.
227 CFLAGS+= -ffreestanding
230 # The C standard leaves signed integer overflow behavior undefined.
231 # gcc and clang opimizers take advantage of this. The kernel makes
232 # use of signed integer wraparound mechanics so we need the compiler
233 # to treat it as a wraparound and not take shortcuts.
240 .if ${MK_SSP} != "no" && \
241 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
242 CFLAGS+= -fstack-protector
246 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
248 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
249 ${MK_KERNEL_RETPOLINE} != "no"
254 # Add -gdwarf-2 when compiling -g. The default starting in clang v3.4
255 # and gcc 4.8 is to generate DWARF version 4. However, our tools don't
256 # cope well with DWARF 4, so force it to genereate DWARF2, which they
257 # understand. Do this unconditionally as it is harmless when not needed,
258 # but critical for these newer versions.
260 .if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == ""
264 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
265 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
267 # Tell bmake not to mistake standard targets for things to be searched for
268 # or expect to ever be up-to-date.
269 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
270 beforelinking build build-tools buildfiles buildincludes \
271 checkdpadd clean cleandepend cleandir cleanobj configure \
272 depend distclean distribute exe \
273 html includes install installfiles installincludes \
274 obj objlink objs objwarn \
275 realinstall regress \
278 .PHONY: ${PHONY_NOTMAIN}
279 .NOTMAIN: ${PHONY_NOTMAIN}
284 CFLAGS+= -traditional
285 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
286 CFLAGS+= -std=iso9899:1990
287 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
288 CFLAGS+= -std=iso9899:199409
289 .elif ${CSTD} == "c99"
290 CFLAGS+= -std=iso9899:1999
292 CFLAGS+= -std=${CSTD}
295 # Set target-specific linker emulation name.
296 LD_EMULATION_aarch64=aarch64elf
297 LD_EMULATION_amd64=elf_x86_64_fbsd
298 LD_EMULATION_arm=armelf_fbsd
299 LD_EMULATION_armv6=armelf_fbsd
300 LD_EMULATION_armv7=armelf_fbsd
301 LD_EMULATION_i386=elf_i386_fbsd
302 LD_EMULATION_mips= elf32btsmip_fbsd
303 LD_EMULATION_mipshf= elf32btsmip_fbsd
304 LD_EMULATION_mips64= elf64btsmip_fbsd
305 LD_EMULATION_mips64hf= elf64btsmip_fbsd
306 LD_EMULATION_mipsel= elf32ltsmip_fbsd
307 LD_EMULATION_mipselhf= elf32ltsmip_fbsd
308 LD_EMULATION_mips64el= elf64ltsmip_fbsd
309 LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
310 LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
311 LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works
312 LD_EMULATION_powerpc= elf32ppc_fbsd
313 LD_EMULATION_powerpcspe= elf32ppc_fbsd
314 LD_EMULATION_powerpc64= elf64ppc_fbsd
315 LD_EMULATION_riscv64= elf64lriscv
316 LD_EMULATION_sparc64= elf64_sparc_fbsd
317 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}