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
39 NO_WDEPRECATED_NON_PROTOTYPE= -Wno-deprecated-non-prototype
41 # Several other warnings which might be useful in some cases, but not severe
42 # enough to error out the whole kernel build. Display them anyway, so there is
43 # some incentive to fix them eventually.
44 CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \
45 -Wno-error=parentheses-equality -Wno-error=unused-function \
46 -Wno-error=pointer-sign
47 CWARNEXTRA+= -Wno-error=shift-negative-value
48 CWARNEXTRA+= -Wno-address-of-packed-member
49 .if ${COMPILER_VERSION} >= 150000
50 # Clang 15 has much more aggressive diagnostics about K&R prototypes,
51 # mismatched prototypes, and unused-but-set variables. Make these
52 # non-fatal for the time being.
53 CWARNEXTRA+= -Wno-error=deprecated-non-prototype
54 CWARNEXTRA+= -Wno-error=strict-prototypes
55 CWARNEXTRA+= -Wno-error=unused-but-set-variable
59 .if ${COMPILER_TYPE} == "gcc"
60 # Catch-all for all the things that are in our tree, but for which we're
61 # not yet ready for this compiler.
62 NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
63 CWARNEXTRA?= -Wno-error=address \
64 -Wno-error=aggressive-loop-optimizations \
65 -Wno-error=array-bounds \
66 -Wno-error=attributes \
67 -Wno-error=cast-qual \
68 -Wno-error=enum-compare \
69 -Wno-error=maybe-uninitialized \
70 -Wno-error=misleading-indentation \
71 -Wno-error=nonnull-compare \
73 -Wno-error=sequence-point \
74 -Wno-error=shift-overflow \
75 -Wno-error=tautological-compare \
76 -Wno-error=unused-function
77 .if ${COMPILER_VERSION} >= 70100
78 CWARNEXTRA+= -Wno-error=stringop-overflow
80 .if ${COMPILER_VERSION} >= 70200
81 CWARNEXTRA+= -Wno-error=memset-elt-size
83 .if ${COMPILER_VERSION} >= 80000
84 CWARNEXTRA+= -Wno-error=packed-not-aligned
86 .if ${COMPILER_VERSION} >= 90100
87 CWARNEXTRA+= -Wno-address-of-packed-member \
88 -Wno-error=alloca-larger-than=
89 .if ${COMPILER_VERSION} >= 120100
90 CWARNEXTRA+= -Wno-error=nonnull \
91 -Wno-dangling-pointer \
92 -Wno-zero-length-bounds
93 NO_WINFINITE_RECURSION= -Wno-infinite-recursion
94 NO_WSTRINGOP_OVERREAD= -Wno-stringop-overread
98 # GCC produces false positives for functions that switch on an
99 # enum (GCC bug 87950)
100 CWARNFLAGS+= -Wno-return-type
103 # This warning is utter nonsense
104 CWARNFLAGS+= -Wno-format-zero-length
106 # External compilers may not support our format extensions. Allow them
107 # to be disabled. WARNING: format checking is disabled in this case.
108 .if ${MK_FORMAT_EXTENSIONS} == "no"
109 FORMAT_EXTENSIONS= -Wno-format
110 .elif ${COMPILER_TYPE} == "clang" || \
111 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
112 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
114 FORMAT_EXTENSIONS= -fformat-extensions
118 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
119 # and above adds code to the entry and exit point of every function to align the
120 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
121 # per function call. While the 16-byte alignment may benefit micro benchmarks,
122 # it is probably an overall loss as it makes the code bigger (less efficient
123 # use of code cache tag lines) and uses more stack (less efficient use of data
124 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
125 # operations inside the kernel itself. These operations are exclusively
126 # reserved for user applications.
129 # Setting -mno-mmx implies -mno-3dnow
130 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
133 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
134 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
136 .if ${MACHINE_CPUARCH} == "i386"
137 CFLAGS.gcc+= -mpreferred-stack-boundary=2
138 CFLAGS.clang+= -mno-aes -mno-avx
139 CFLAGS+= -mno-mmx -mno-sse -msoft-float
143 .if ${MACHINE_CPUARCH} == "arm"
147 .if ${MACHINE_CPUARCH} == "aarch64"
148 # We generally don't want fpu instructions in the kernel.
149 CFLAGS += -mgeneral-regs-only
150 # Reserve x18 for pcpu data
151 CFLAGS += -ffixed-x18
156 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
157 # point registers within the kernel. However, for kernels supporting hardware
158 # float (FPE), we have to include that in the march so we can have limited
159 # floating point support in context switching needed for that. This is different
160 # than userland where we use a hard-float ABI (lp64d).
162 # We also specify the "medium" code model, which generates code suitable for a
163 # 2GiB addressing range located at any offset, allowing modules to be located
164 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
165 # code model as "medium" and "medany" respectively.
167 .if ${MACHINE_CPUARCH} == "riscv"
168 CFLAGS+= -march=rv64imafdc
170 CFLAGS.clang+= -mcmodel=medium
171 CFLAGS.gcc+= -mcmodel=medany
174 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
180 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
181 # operations inside the kernel itself. These operations are exclusively
182 # reserved for user applications.
185 # Setting -mno-mmx implies -mno-3dnow
186 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
189 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
190 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
191 # (-mfpmath= is not supported)
193 .if ${MACHINE_CPUARCH} == "amd64"
194 CFLAGS.clang+= -mno-aes -mno-avx
195 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
196 -fno-asynchronous-unwind-tables
201 # For PowerPC we tell gcc to use floating point emulation. This avoids using
202 # floating point registers for integer operations which it has a tendency to do.
203 # Also explicitly disable Altivec instructions inside the kernel.
205 .if ${MACHINE_CPUARCH} == "powerpc"
206 CFLAGS+= -mno-altivec -msoft-float
210 .if ${MACHINE_ARCH} == "powerpcspe"
211 CFLAGS.gcc+= -mno-spe
215 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
216 # DDB happy. ELFv2, if available, has some other efficiency benefits.
218 .if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
219 ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
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 CFLAGS+= -fstack-protector
245 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
247 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
248 ${MK_KERNEL_RETPOLINE} != "no"
253 # Initialize stack variables on function entry
255 .if ${MK_INIT_ALL_ZERO} == "yes"
256 .if ${COMPILER_FEATURES:Minit-all}
257 CFLAGS+= -ftrivial-auto-var-init=zero \
258 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
260 .warning InitAll (zeros) requested but not support by compiler
262 .elif ${MK_INIT_ALL_PATTERN} == "yes"
263 .if ${COMPILER_FEATURES:Minit-all}
264 CFLAGS+= -ftrivial-auto-var-init=pattern
266 .warning InitAll (pattern) requested but not support by compiler
270 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
271 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
272 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
274 # Tell bmake not to mistake standard targets for things to be searched for
275 # or expect to ever be up-to-date.
276 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
277 beforelinking build build-tools buildfiles buildincludes \
278 checkdpadd clean cleandepend cleandir cleanobj configure \
279 depend distclean distribute exe \
280 html includes install installfiles installincludes \
281 obj objlink objs objwarn \
282 realinstall regress \
285 .PHONY: ${PHONY_NOTMAIN}
286 .NOTMAIN: ${PHONY_NOTMAIN}
291 CFLAGS+= -traditional
292 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
293 CFLAGS+= -std=iso9899:1990
294 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
295 CFLAGS+= -std=iso9899:199409
296 .elif ${CSTD} == "c99"
297 CFLAGS+= -std=iso9899:1999
299 CFLAGS+= -std=${CSTD}
302 # Please keep this if in sync with bsd.sys.mk
303 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
304 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
305 .if ${COMPILER_TYPE} == "clang"
306 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
307 .if ${COMPILER_VERSION} >= 120000
308 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
310 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
313 # GCC does not support an absolute path for -fuse-ld so we just print this
314 # warning instead and let the user add the required symlinks.
315 # However, we can avoid this warning if -B is set appropriately (e.g. for
316 # CROSS_TOOLCHAIN=...-gcc).
317 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
318 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
323 # Set target-specific linker emulation name.
324 LD_EMULATION_aarch64=aarch64elf
325 LD_EMULATION_amd64=elf_x86_64_fbsd
326 LD_EMULATION_arm=armelf_fbsd
327 LD_EMULATION_armv6=armelf_fbsd
328 LD_EMULATION_armv7=armelf_fbsd
329 LD_EMULATION_i386=elf_i386_fbsd
330 LD_EMULATION_powerpc= elf32ppc_fbsd
331 LD_EMULATION_powerpcspe= elf32ppc_fbsd
332 LD_EMULATION_powerpc64= elf64ppc_fbsd
333 LD_EMULATION_powerpc64le= elf64lppc_fbsd
334 LD_EMULATION_riscv64= elf64lriscv
335 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}