3 # Warning flags for compiling the kernel and components of the kernel:
5 CWARNFLAGS?= -Wall -Wstrict-prototypes \
6 -Wmissing-prototypes -Wpointer-arith -Wcast-qual \
7 -Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
8 -Wmissing-include-dirs -fdiagnostics-show-option \
12 # The following flags are next up for working on:
15 # Disable a few warnings for clang, since there are several places in the
16 # kernel where fixing them is more trouble than it is worth, or where there is
18 .if ${COMPILER_TYPE} == "clang"
19 NO_WCONSTANT_CONVERSION= -Wno-error=constant-conversion
20 NO_WSHIFT_COUNT_NEGATIVE= -Wno-shift-count-negative
21 NO_WSHIFT_COUNT_OVERFLOW= -Wno-shift-count-overflow
22 NO_WSELF_ASSIGN= -Wno-self-assign
23 NO_WUNNEEDED_INTERNAL_DECL= -Wno-error=unneeded-internal-declaration
24 NO_WSOMETIMES_UNINITIALIZED= -Wno-error=sometimes-uninitialized
25 NO_WCAST_QUAL= -Wno-error=cast-qual
26 NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
27 .if ${COMPILER_VERSION} >= 100000
28 NO_WMISLEADING_INDENTATION= -Wno-misleading-indentation
30 .if ${COMPILER_VERSION} >= 130000
31 NO_WUNUSED_BUT_SET_VARIABLE= -Wno-unused-but-set-variable
33 .if ${COMPILER_VERSION} >= 140000
34 NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical
36 .if ${COMPILER_VERSION} >= 150000
37 NO_WSTRICT_PROTOTYPES= -Wno-strict-prototypes
38 NO_WDEPRECATED_NON_PROTOTYPE= -Wno-deprecated-non-prototype
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
50 .if ${COMPILER_TYPE} == "gcc"
51 # Catch-all for all the things that are in our tree, but for which we're
52 # not yet ready for this compiler.
53 NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
54 CWARNEXTRA?= -Wno-error=address \
55 -Wno-error=aggressive-loop-optimizations \
56 -Wno-error=array-bounds \
57 -Wno-error=attributes \
58 -Wno-error=cast-qual \
59 -Wno-error=enum-compare \
60 -Wno-error=maybe-uninitialized \
61 -Wno-error=misleading-indentation \
62 -Wno-error=nonnull-compare \
64 -Wno-error=sequence-point \
65 -Wno-error=shift-overflow \
66 -Wno-error=tautological-compare \
67 -Wno-error=unused-function
68 .if ${COMPILER_VERSION} >= 70100
69 CWARNEXTRA+= -Wno-error=stringop-overflow
71 .if ${COMPILER_VERSION} >= 70200
72 CWARNEXTRA+= -Wno-error=memset-elt-size
74 .if ${COMPILER_VERSION} >= 80000
75 CWARNEXTRA+= -Wno-error=packed-not-aligned
77 .if ${COMPILER_VERSION} >= 90100
78 CWARNEXTRA+= -Wno-address-of-packed-member \
79 -Wno-error=alloca-larger-than=
80 .if ${COMPILER_VERSION} >= 120100
81 CWARNEXTRA+= -Wno-error=nonnull \
82 -Wno-dangling-pointer \
83 -Wno-zero-length-bounds
84 NO_WINFINITE_RECURSION= -Wno-infinite-recursion
85 NO_WSTRINGOP_OVERREAD= -Wno-stringop-overread
89 # GCC produces false positives for functions that switch on an
90 # enum (GCC bug 87950)
91 CWARNFLAGS+= -Wno-return-type
94 # This warning is utter nonsense
95 CWARNFLAGS+= -Wno-format-zero-length
97 # External compilers may not support our format extensions. Allow them
98 # to be disabled. WARNING: format checking is disabled in this case.
99 .if ${MK_FORMAT_EXTENSIONS} == "no"
100 FORMAT_EXTENSIONS= -Wno-format
101 .elif ${COMPILER_TYPE} == "clang" || \
102 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
103 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
105 FORMAT_EXTENSIONS= -fformat-extensions
109 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
110 # and above adds code to the entry and exit point of every function to align the
111 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
112 # per function call. While the 16-byte alignment may benefit micro benchmarks,
113 # it is probably an overall loss as it makes the code bigger (less efficient
114 # use of code cache tag lines) and uses more stack (less efficient use of data
115 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
116 # operations inside the kernel itself. These operations are exclusively
117 # reserved for user applications.
120 # Setting -mno-mmx implies -mno-3dnow
121 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
124 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
125 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
127 .if ${MACHINE_CPUARCH} == "i386"
128 CFLAGS.gcc+= -mpreferred-stack-boundary=2
129 CFLAGS.clang+= -mno-aes -mno-avx
130 CFLAGS+= -mno-mmx -mno-sse -msoft-float
134 .if ${MACHINE_CPUARCH} == "arm"
138 .if ${MACHINE_CPUARCH} == "aarch64"
139 # We generally don't want fpu instructions in the kernel.
140 CFLAGS += -mgeneral-regs-only
141 # Reserve x18 for pcpu data
142 CFLAGS += -ffixed-x18
147 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
148 # point registers within the kernel. However, we include the F and D extensions
149 # in -march so we can have limited floating point support in context switching
150 # code. This is different than userland where we use a hard-float ABI (lp64d).
152 # We also specify the "medium" code model, which generates code suitable for a
153 # 2GiB addressing range located at any offset, allowing modules to be located
154 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
155 # code model as "medium" and "medany" respectively.
157 .if ${MACHINE_CPUARCH} == "riscv"
158 CFLAGS+= -march=rv64imafdc
160 CFLAGS.clang+= -mcmodel=medium
161 CFLAGS.gcc+= -mcmodel=medany
164 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
170 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
171 # operations inside the kernel itself. These operations are exclusively
172 # reserved for user applications.
175 # Setting -mno-mmx implies -mno-3dnow
176 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
179 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
180 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
181 # (-mfpmath= is not supported)
183 .if ${MACHINE_CPUARCH} == "amd64"
184 CFLAGS.clang+= -mno-aes -mno-avx
185 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
186 -fno-asynchronous-unwind-tables
191 # For PowerPC we tell gcc to use floating point emulation. This avoids using
192 # floating point registers for integer operations which it has a tendency to do.
193 # Also explicitly disable Altivec instructions inside the kernel.
195 .if ${MACHINE_CPUARCH} == "powerpc"
196 CFLAGS+= -mno-altivec -msoft-float
200 .if ${MACHINE_ARCH} == "powerpcspe"
201 CFLAGS.gcc+= -mno-spe
205 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
206 # DDB happy. ELFv2, if available, has some other efficiency benefits.
208 .if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
209 ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
214 # GCC 3.0 and above like to do certain optimizations based on the
215 # assumption that the program is linked against libc. Stop this.
217 CFLAGS+= -ffreestanding
220 # The C standard leaves signed integer overflow behavior undefined.
221 # gcc and clang opimizers take advantage of this. The kernel makes
222 # use of signed integer wraparound mechanics so we need the compiler
223 # to treat it as a wraparound and not take shortcuts.
230 .if ${MK_SSP} != "no"
231 CFLAGS+= -fstack-protector
235 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
237 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
238 ${MK_KERNEL_RETPOLINE} != "no"
243 # Initialize stack variables on function entry
245 .if ${OPT_INIT_ALL} != "none"
246 .if ${COMPILER_FEATURES:Minit-all}
247 CFLAGS+= -ftrivial-auto-var-init=${OPT_INIT_ALL}
248 .if ${OPT_INIT_ALL} == "zero" && ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
249 CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
252 .warning INIT_ALL (${OPT_INIT_ALL}) requested but not supported by compiler
256 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
257 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
258 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
260 # Tell bmake not to mistake standard targets for things to be searched for
261 # or expect to ever be up-to-date.
262 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
263 beforelinking build build-tools buildfiles buildincludes \
264 checkdpadd clean cleandepend cleandir cleanobj configure \
265 depend distclean distribute exe \
266 html includes install installfiles installincludes \
267 obj objlink objs objwarn \
268 realinstall regress \
271 .PHONY: ${PHONY_NOTMAIN}
272 .NOTMAIN: ${PHONY_NOTMAIN}
277 CFLAGS+= -traditional
278 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
279 CFLAGS+= -std=iso9899:1990
280 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
281 CFLAGS+= -std=iso9899:199409
282 .elif ${CSTD} == "c99"
283 CFLAGS+= -std=iso9899:1999
285 CFLAGS+= -std=${CSTD}
288 # Please keep this if in sync with bsd.sys.mk
289 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
290 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
291 .if ${COMPILER_TYPE} == "clang"
292 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
293 .if ${COMPILER_VERSION} >= 120000
294 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
296 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
299 # GCC does not support an absolute path for -fuse-ld so we just print this
300 # warning instead and let the user add the required symlinks.
301 # However, we can avoid this warning if -B is set appropriately (e.g. for
302 # CROSS_TOOLCHAIN=...-gcc).
303 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
304 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
309 # Set target-specific linker emulation name.
310 LD_EMULATION_aarch64=aarch64elf
311 LD_EMULATION_amd64=elf_x86_64_fbsd
312 LD_EMULATION_arm=armelf_fbsd
313 LD_EMULATION_armv6=armelf_fbsd
314 LD_EMULATION_armv7=armelf_fbsd
315 LD_EMULATION_i386=elf_i386_fbsd
316 LD_EMULATION_powerpc= elf32ppc_fbsd
317 LD_EMULATION_powerpcspe= elf32ppc_fbsd
318 LD_EMULATION_powerpc64= elf64ppc_fbsd
319 LD_EMULATION_powerpc64le= elf64lppc_fbsd
320 LD_EMULATION_riscv64= elf64lriscv
321 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}