4 # Warning flags for compiling the kernel and components of the kernel:
6 CWARNFLAGS?= -Wall -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 .if ${COMPILER_VERSION} >= 130000
32 NO_WUNUSED_BUT_SET_VARIABLE= -Wno-unused-but-set-variable
34 .if ${COMPILER_VERSION} >= 140000
35 NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical
37 .if ${COMPILER_VERSION} >= 150000
38 NO_WSTRICT_PROTOTYPES= -Wno-strict-prototypes
40 # Several other warnings which might be useful in some cases, but not severe
41 # enough to error out the whole kernel build. Display them anyway, so there is
42 # some incentive to fix them eventually.
43 CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \
44 -Wno-error=parentheses-equality -Wno-error=unused-function \
45 -Wno-error=pointer-sign
46 CWARNEXTRA+= -Wno-error=shift-negative-value
47 CWARNEXTRA+= -Wno-address-of-packed-member
48 .if ${COMPILER_VERSION} >= 130000
49 CWARNFLAGS+= -Wno-error=unused-but-set-variable
51 .if ${COMPILER_VERSION} >= 150000
52 # Clang 15 has much more aggressive diagnostics about inconsistently declared
53 # array parameters, K&R prototypes, mismatched prototypes, and unused-but-set
54 # variables. Make these non-fatal for the time being.
55 CWARNEXTRA+= -Wno-error=array-parameter
56 CWARNEXTRA+= -Wno-error=deprecated-non-prototype
57 CWARNEXTRA+= -Wno-error=strict-prototypes
58 CWARNEXTRA+= -Wno-error=unused-but-set-variable
59 NO_WDEPRECATED_NON_PROTOTYPE=-Wno-deprecated-non-prototype
63 .if ${COMPILER_TYPE} == "gcc"
64 # Catch-all for all the things that are in our tree, but for which we're
65 # not yet ready for this compiler.
66 NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
67 CWARNEXTRA?= -Wno-error=address \
68 -Wno-error=aggressive-loop-optimizations \
69 -Wno-error=array-bounds \
70 -Wno-error=attributes \
71 -Wno-error=cast-qual \
72 -Wno-error=enum-compare \
73 -Wno-error=maybe-uninitialized \
74 -Wno-error=misleading-indentation \
75 -Wno-error=nonnull-compare \
77 -Wno-error=sequence-point \
78 -Wno-error=shift-overflow \
79 -Wno-error=tautological-compare \
80 -Wno-unused-but-set-variable
81 .if ${COMPILER_VERSION} >= 70100
82 CWARNEXTRA+= -Wno-error=stringop-overflow
84 .if ${COMPILER_VERSION} >= 70200
85 CWARNEXTRA+= -Wno-error=memset-elt-size
87 .if ${COMPILER_VERSION} >= 80000
88 CWARNEXTRA+= -Wno-error=packed-not-aligned
90 .if ${COMPILER_VERSION} >= 90100
91 CWARNEXTRA+= -Wno-address-of-packed-member \
92 -Wno-error=alloca-larger-than=
93 .if ${COMPILER_VERSION} >= 120100
94 CWARNEXTRA+= -Wno-error=nonnull \
95 -Wno-dangling-pointer \
96 -Wno-zero-length-bounds
97 NO_WINFINITE_RECURSION= -Wno-infinite-recursion
98 NO_WSTRINGOP_OVERREAD= -Wno-stringop-overread
102 # GCC produces false positives for functions that switch on an
103 # enum (GCC bug 87950)
104 CWARNFLAGS+= -Wno-return-type
107 # This warning is utter nonsense
108 CWARNFLAGS+= -Wno-format-zero-length
110 # External compilers may not support our format extensions. Allow them
111 # to be disabled. WARNING: format checking is disabled in this case.
112 .if ${MK_FORMAT_EXTENSIONS} == "no"
113 FORMAT_EXTENSIONS= -Wno-format
114 .elif ${COMPILER_TYPE} == "clang" || \
115 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
116 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
118 FORMAT_EXTENSIONS= -fformat-extensions
122 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
123 # and above adds code to the entry and exit point of every function to align the
124 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
125 # per function call. While the 16-byte alignment may benefit micro benchmarks,
126 # it is probably an overall loss as it makes the code bigger (less efficient
127 # use of code cache tag lines) and uses more stack (less efficient use of data
128 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
129 # operations inside the kernel itself. These operations are exclusively
130 # reserved for user applications.
133 # Setting -mno-mmx implies -mno-3dnow
134 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
137 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
138 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
140 .if ${MACHINE_CPUARCH} == "i386"
141 CFLAGS.gcc+= -mpreferred-stack-boundary=2
142 CFLAGS.clang+= -mno-aes -mno-avx
143 CFLAGS+= -mno-mmx -mno-sse -msoft-float
147 .if ${MACHINE_CPUARCH} == "arm"
151 .if ${MACHINE_CPUARCH} == "aarch64"
152 # We generally don't want fpu instructions in the kernel.
153 CFLAGS += -mgeneral-regs-only
154 # Reserve x18 for pcpu data
155 CFLAGS += -ffixed-x18
160 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
161 # point registers within the kernel. However, for kernels supporting hardware
162 # float (FPE), we have to include that in the march so we can have limited
163 # floating point support in context switching needed for that. This is different
164 # than userland where we use a hard-float ABI (lp64d).
166 # We also specify the "medium" code model, which generates code suitable for a
167 # 2GiB addressing range located at any offset, allowing modules to be located
168 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
169 # code model as "medium" and "medany" respectively.
171 .if ${MACHINE_CPUARCH} == "riscv"
172 CFLAGS+= -march=rv64imafdc
174 CFLAGS.clang+= -mcmodel=medium
175 CFLAGS.gcc+= -mcmodel=medany
178 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
184 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
185 # operations inside the kernel itself. These operations are exclusively
186 # reserved for user applications.
189 # Setting -mno-mmx implies -mno-3dnow
190 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
193 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
194 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
195 # (-mfpmath= is not supported)
197 .if ${MACHINE_CPUARCH} == "amd64"
198 CFLAGS.clang+= -mno-aes -mno-avx
199 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
200 -fno-asynchronous-unwind-tables
205 # For PowerPC we tell gcc to use floating point emulation. This avoids using
206 # floating point registers for integer operations which it has a tendency to do.
207 # Also explicitly disable Altivec instructions inside the kernel.
209 .if ${MACHINE_CPUARCH} == "powerpc"
210 CFLAGS+= -mno-altivec -msoft-float
214 .if ${MACHINE_ARCH} == "powerpcspe"
215 CFLAGS.gcc+= -mno-spe
219 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
220 # DDB happy. ELFv2, if available, has some other efficiency benefits.
222 .if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
223 ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
228 # For MIPS we also tell gcc to use floating point emulation
230 .if ${MACHINE_CPUARCH} == "mips"
231 CFLAGS+= -msoft-float
236 # GCC 3.0 and above like to do certain optimizations based on the
237 # assumption that the program is linked against libc. Stop this.
239 CFLAGS+= -ffreestanding
242 # The C standard leaves signed integer overflow behavior undefined.
243 # gcc and clang opimizers take advantage of this. The kernel makes
244 # use of signed integer wraparound mechanics so we need the compiler
245 # to treat it as a wraparound and not take shortcuts.
252 .if ${MK_SSP} != "no"
253 CFLAGS+= -fstack-protector
257 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
259 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
260 ${MK_KERNEL_RETPOLINE} != "no"
265 # Initialize stack variables on function entry
267 .if ${MK_INIT_ALL_ZERO} == "yes"
268 .if ${COMPILER_FEATURES:Minit-all}
269 CFLAGS+= -ftrivial-auto-var-init=zero
270 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
271 CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
274 .warning InitAll (zeros) requested but not supported by compiler
276 .elif ${MK_INIT_ALL_PATTERN} == "yes"
277 .if ${COMPILER_FEATURES:Minit-all}
278 CFLAGS+= -ftrivial-auto-var-init=pattern
280 .warning InitAll (pattern) requested but not support by compiler
284 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
285 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
286 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
288 # Tell bmake not to mistake standard targets for things to be searched for
289 # or expect to ever be up-to-date.
290 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
291 beforelinking build build-tools buildfiles buildincludes \
292 checkdpadd clean cleandepend cleandir cleanobj configure \
293 depend distclean distribute exe \
294 html includes install installfiles installincludes \
295 obj objlink objs objwarn \
296 realinstall regress \
299 .PHONY: ${PHONY_NOTMAIN}
300 .NOTMAIN: ${PHONY_NOTMAIN}
305 CFLAGS+= -traditional
306 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
307 CFLAGS+= -std=iso9899:1990
308 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
309 CFLAGS+= -std=iso9899:199409
310 .elif ${CSTD} == "c99"
311 CFLAGS+= -std=iso9899:1999
313 CFLAGS+= -std=${CSTD}
316 # Please keep this if in sync with bsd.sys.mk
317 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
318 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
319 .if ${COMPILER_TYPE} == "clang"
320 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
321 .if ${COMPILER_VERSION} >= 120000
322 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
324 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
327 # GCC does not support an absolute path for -fuse-ld so we just print this
328 # warning instead and let the user add the required symlinks.
329 # However, we can avoid this warning if -B is set appropriately (e.g. for
330 # CROSS_TOOLCHAIN=...-gcc).
331 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
332 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
337 # Set target-specific linker emulation name.
338 LD_EMULATION_aarch64=aarch64elf
339 LD_EMULATION_amd64=elf_x86_64_fbsd
340 LD_EMULATION_arm=armelf_fbsd
341 LD_EMULATION_armv6=armelf_fbsd
342 LD_EMULATION_armv7=armelf_fbsd
343 LD_EMULATION_i386=elf_i386_fbsd
344 LD_EMULATION_mips= elf32btsmip_fbsd
345 LD_EMULATION_mipshf= elf32btsmip_fbsd
346 LD_EMULATION_mips64= elf64btsmip_fbsd
347 LD_EMULATION_mips64hf= elf64btsmip_fbsd
348 LD_EMULATION_mipsel= elf32ltsmip_fbsd
349 LD_EMULATION_mipselhf= elf32ltsmip_fbsd
350 LD_EMULATION_mips64el= elf64ltsmip_fbsd
351 LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
352 LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
353 LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works
354 LD_EMULATION_powerpc= elf32ppc_fbsd
355 LD_EMULATION_powerpcspe= elf32ppc_fbsd
356 LD_EMULATION_powerpc64= elf64ppc_fbsd
357 LD_EMULATION_powerpc64le= elf64lppc_fbsd
358 LD_EMULATION_riscv64= elf64lriscv
359 LD_EMULATION_riscv64sf= elf64lriscv
360 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}