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 # 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.
31 CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \
32 -Wno-error=parentheses-equality -Wno-error=unused-function \
33 -Wno-error=pointer-sign
34 CWARNEXTRA+= -Wno-error=shift-negative-value
35 CWARNEXTRA+= -Wno-address-of-packed-member
36 .if ${COMPILER_VERSION} >= 100000
37 NO_WMISLEADING_INDENTATION= -Wno-misleading-indentation
41 .if ${COMPILER_TYPE} == "gcc"
42 # Catch-all for all the things that are in our tree, but for which we're
43 # not yet ready for this compiler.
44 NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
45 CWARNEXTRA?= -Wno-error=address \
46 -Wno-error=aggressive-loop-optimizations \
47 -Wno-error=array-bounds \
48 -Wno-error=attributes \
49 -Wno-error=cast-qual \
50 -Wno-error=enum-compare \
51 -Wno-error=maybe-uninitialized \
52 -Wno-error=misleading-indentation \
53 -Wno-error=nonnull-compare \
55 -Wno-error=sequence-point \
56 -Wno-error=shift-overflow \
57 -Wno-error=tautological-compare \
58 -Wno-unused-but-set-variable
59 .if ${COMPILER_VERSION} >= 70100
60 CWARNEXTRA+= -Wno-error=stringop-overflow
62 .if ${COMPILER_VERSION} >= 70200
63 CWARNEXTRA+= -Wno-error=memset-elt-size
65 .if ${COMPILER_VERSION} >= 80000
66 CWARNEXTRA+= -Wno-error=packed-not-aligned
68 .if ${COMPILER_VERSION} >= 90100
69 CWARNEXTRA+= -Wno-address-of-packed-member
73 # This warning is utter nonsense
74 CWARNFLAGS+= -Wno-format-zero-length
76 # External compilers may not support our format extensions. Allow them
77 # to be disabled. WARNING: format checking is disabled in this case.
78 .if ${MK_FORMAT_EXTENSIONS} == "no"
79 FORMAT_EXTENSIONS= -Wno-format
80 .elif ${COMPILER_TYPE} == "clang"
81 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
83 FORMAT_EXTENSIONS= -fformat-extensions
87 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
88 # and above adds code to the entry and exit point of every function to align the
89 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
90 # per function call. While the 16-byte alignment may benefit micro benchmarks,
91 # it is probably an overall loss as it makes the code bigger (less efficient
92 # use of code cache tag lines) and uses more stack (less efficient use of data
93 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
94 # operations inside the kernel itself. These operations are exclusively
95 # reserved for user applications.
98 # Setting -mno-mmx implies -mno-3dnow
99 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
102 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
103 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
105 .if ${MACHINE_CPUARCH} == "i386"
106 CFLAGS.gcc+= -mpreferred-stack-boundary=2
107 CFLAGS.clang+= -mno-aes -mno-avx
108 CFLAGS+= -mno-mmx -mno-sse -msoft-float
112 .if ${MACHINE_CPUARCH} == "arm"
116 .if ${MACHINE_CPUARCH} == "aarch64"
117 # We generally don't want fpu instructions in the kernel.
118 CFLAGS += -mgeneral-regs-only
119 # Reserve x18 for pcpu data
120 CFLAGS += -ffixed-x18
125 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
126 # point registers within the kernel. However, for kernels supporting hardware
127 # float (FPE), we have to include that in the march so we can have limited
128 # floating point support in context switching needed for that. This is different
129 # than userland where we use a hard-float ABI (lp64d).
131 # We also specify the "medium" code model, which generates code suitable for a
132 # 2GiB addressing range located at any offset, allowing modules to be located
133 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
134 # code model as "medium" and "medany" respectively.
136 .if ${MACHINE_CPUARCH} == "riscv"
137 CFLAGS+= -march=rv64imafdc
139 CFLAGS.clang+= -mcmodel=medium
140 CFLAGS.gcc+= -mcmodel=medany
143 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
149 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
150 # operations inside the kernel itself. These operations are exclusively
151 # reserved for user applications.
154 # Setting -mno-mmx implies -mno-3dnow
155 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
158 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
159 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
160 # (-mfpmath= is not supported)
162 .if ${MACHINE_CPUARCH} == "amd64"
163 CFLAGS.clang+= -mno-aes -mno-avx
164 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
165 -fno-asynchronous-unwind-tables
170 # For PowerPC we tell gcc to use floating point emulation. This avoids using
171 # floating point registers for integer operations which it has a tendency to do.
172 # Also explicitly disable Altivec instructions inside the kernel.
174 .if ${MACHINE_CPUARCH} == "powerpc"
175 CFLAGS+= -mno-altivec -msoft-float
179 .if ${MACHINE_ARCH} == "powerpcspe"
180 CFLAGS.gcc+= -mno-spe
184 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
185 # DDB happy. ELFv2, if available, has some other efficiency benefits.
187 .if ${MACHINE_ARCH:Mpowerpc64*} != ""
192 # For MIPS we also tell gcc to use floating point emulation
194 .if ${MACHINE_CPUARCH} == "mips"
195 CFLAGS+= -msoft-float
200 # GCC 3.0 and above like to do certain optimizations based on the
201 # assumption that the program is linked against libc. Stop this.
203 CFLAGS+= -ffreestanding
206 # The C standard leaves signed integer overflow behavior undefined.
207 # gcc and clang opimizers take advantage of this. The kernel makes
208 # use of signed integer wraparound mechanics so we need the compiler
209 # to treat it as a wraparound and not take shortcuts.
216 .if ${MK_SSP} != "no" && \
217 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
218 CFLAGS+= -fstack-protector
222 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
224 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
225 ${MK_KERNEL_RETPOLINE} != "no"
230 # Initialize stack variables on function entry
232 .if ${MK_INIT_ALL_ZERO} == "yes"
233 .if ${COMPILER_FEATURES:Minit-all}
234 CFLAGS+= -ftrivial-auto-var-init=zero \
235 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
237 .warning InitAll (zeros) requested but not support by compiler
239 .elif ${MK_INIT_ALL_PATTERN} == "yes"
240 .if ${COMPILER_FEATURES:Minit-all}
241 CFLAGS+= -ftrivial-auto-var-init=pattern
243 .warning InitAll (pattern) requested but not support by compiler
247 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
248 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
249 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
251 # Tell bmake not to mistake standard targets for things to be searched for
252 # or expect to ever be up-to-date.
253 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
254 beforelinking build build-tools buildfiles buildincludes \
255 checkdpadd clean cleandepend cleandir cleanobj configure \
256 depend distclean distribute exe \
257 html includes install installfiles installincludes \
258 obj objlink objs objwarn \
259 realinstall regress \
262 .PHONY: ${PHONY_NOTMAIN}
263 .NOTMAIN: ${PHONY_NOTMAIN}
268 CFLAGS+= -traditional
269 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
270 CFLAGS+= -std=iso9899:1990
271 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
272 CFLAGS+= -std=iso9899:199409
273 .elif ${CSTD} == "c99"
274 CFLAGS+= -std=iso9899:1999
276 CFLAGS+= -std=${CSTD}
279 # Please keep this if in sync with bsd.sys.mk
280 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
281 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
282 .if ${COMPILER_TYPE} == "clang"
283 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
284 .if ${COMPILER_VERSION} >= 120000
285 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
287 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
290 # GCC does not support an absolute path for -fuse-ld so we just print this
291 # warning instead and let the user add the required symlinks.
292 # However, we can avoid this warning if -B is set appropriately (e.g. for
293 # CROSS_TOOLCHAIN=...-gcc).
294 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
295 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
300 # Set target-specific linker emulation name.
301 LD_EMULATION_aarch64=aarch64elf
302 LD_EMULATION_amd64=elf_x86_64_fbsd
303 LD_EMULATION_arm=armelf_fbsd
304 LD_EMULATION_armv6=armelf_fbsd
305 LD_EMULATION_armv7=armelf_fbsd
306 LD_EMULATION_i386=elf_i386_fbsd
307 LD_EMULATION_mips= elf32btsmip_fbsd
308 LD_EMULATION_mipshf= elf32btsmip_fbsd
309 LD_EMULATION_mips64= elf64btsmip_fbsd
310 LD_EMULATION_mips64hf= elf64btsmip_fbsd
311 LD_EMULATION_mipsel= elf32ltsmip_fbsd
312 LD_EMULATION_mipselhf= elf32ltsmip_fbsd
313 LD_EMULATION_mips64el= elf64ltsmip_fbsd
314 LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
315 LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
316 LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works
317 LD_EMULATION_powerpc= elf32ppc_fbsd
318 LD_EMULATION_powerpcspe= elf32ppc_fbsd
319 LD_EMULATION_powerpc64= elf64ppc_fbsd
320 LD_EMULATION_powerpc64le= elf64lppc_fbsd
321 LD_EMULATION_riscv64= elf64lriscv
322 LD_EMULATION_riscv64sf= elf64lriscv
323 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}