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 .if ${COMPILER_VERSION} >= 140000
32 NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical
34 .if ${COMPILER_VERSION} >= 150000
35 NO_WSTRICT_PROTOTYPES= -Wno-strict-prototypes
37 # Several other warnings which might be useful in some cases, but not severe
38 # enough to error out the whole kernel build. Display them anyway, so there is
39 # some incentive to fix them eventually.
40 CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \
41 -Wno-error=parentheses-equality -Wno-error=unused-function \
42 -Wno-error=pointer-sign
43 CWARNEXTRA+= -Wno-error=shift-negative-value
44 CWARNEXTRA+= -Wno-address-of-packed-member
45 .if ${COMPILER_VERSION} >= 130000
46 CWARNFLAGS+= -Wno-error=unused-but-set-variable
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-unused-but-set-variable
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=
82 # GCC produces false positives for functions that switch on an
83 # enum (GCC bug 87950)
84 CWARNFLAGS+= -Wno-return-type
87 # This warning is utter nonsense
88 CWARNFLAGS+= -Wno-format-zero-length
90 # External compilers may not support our format extensions. Allow them
91 # to be disabled. WARNING: format checking is disabled in this case.
92 .if ${MK_FORMAT_EXTENSIONS} == "no"
93 FORMAT_EXTENSIONS= -Wno-format
94 .elif ${COMPILER_TYPE} == "clang"
95 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
97 FORMAT_EXTENSIONS= -fformat-extensions
101 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
102 # and above adds code to the entry and exit point of every function to align the
103 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
104 # per function call. While the 16-byte alignment may benefit micro benchmarks,
105 # it is probably an overall loss as it makes the code bigger (less efficient
106 # use of code cache tag lines) and uses more stack (less efficient use of data
107 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
108 # operations inside the kernel itself. These operations are exclusively
109 # reserved for user applications.
112 # Setting -mno-mmx implies -mno-3dnow
113 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
116 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
117 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
119 .if ${MACHINE_CPUARCH} == "i386"
120 CFLAGS.gcc+= -mpreferred-stack-boundary=2
121 CFLAGS.clang+= -mno-aes -mno-avx
122 CFLAGS+= -mno-mmx -mno-sse -msoft-float
126 .if ${MACHINE_CPUARCH} == "arm"
130 .if ${MACHINE_CPUARCH} == "aarch64"
131 # We generally don't want fpu instructions in the kernel.
132 CFLAGS += -mgeneral-regs-only
133 # Reserve x18 for pcpu data
134 CFLAGS += -ffixed-x18
139 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
140 # point registers within the kernel. However, for kernels supporting hardware
141 # float (FPE), we have to include that in the march so we can have limited
142 # floating point support in context switching needed for that. This is different
143 # than userland where we use a hard-float ABI (lp64d).
145 # We also specify the "medium" code model, which generates code suitable for a
146 # 2GiB addressing range located at any offset, allowing modules to be located
147 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
148 # code model as "medium" and "medany" respectively.
150 .if ${MACHINE_CPUARCH} == "riscv"
151 CFLAGS+= -march=rv64imafdc
153 CFLAGS.clang+= -mcmodel=medium
154 CFLAGS.gcc+= -mcmodel=medany
157 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
163 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
164 # operations inside the kernel itself. These operations are exclusively
165 # reserved for user applications.
168 # Setting -mno-mmx implies -mno-3dnow
169 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
172 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
173 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
174 # (-mfpmath= is not supported)
176 .if ${MACHINE_CPUARCH} == "amd64"
177 CFLAGS.clang+= -mno-aes -mno-avx
178 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
179 -fno-asynchronous-unwind-tables
184 # For PowerPC we tell gcc to use floating point emulation. This avoids using
185 # floating point registers for integer operations which it has a tendency to do.
186 # Also explicitly disable Altivec instructions inside the kernel.
188 .if ${MACHINE_CPUARCH} == "powerpc"
189 CFLAGS+= -mno-altivec -msoft-float
193 .if ${MACHINE_ARCH} == "powerpcspe"
194 CFLAGS.gcc+= -mno-spe
198 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
199 # DDB happy. ELFv2, if available, has some other efficiency benefits.
201 .if ${MACHINE_ARCH:Mpowerpc64*} != ""
206 # For MIPS we also tell gcc to use floating point emulation
208 .if ${MACHINE_CPUARCH} == "mips"
209 CFLAGS+= -msoft-float
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 ${MK_INIT_ALL_ZERO} == "yes"
246 .if ${COMPILER_FEATURES:Minit-all}
247 CFLAGS+= -ftrivial-auto-var-init=zero \
248 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
250 .warning InitAll (zeros) requested but not support by compiler
252 .elif ${MK_INIT_ALL_PATTERN} == "yes"
253 .if ${COMPILER_FEATURES:Minit-all}
254 CFLAGS+= -ftrivial-auto-var-init=pattern
256 .warning InitAll (pattern) requested but not support by compiler
260 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
261 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
262 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
264 # Tell bmake not to mistake standard targets for things to be searched for
265 # or expect to ever be up-to-date.
266 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
267 beforelinking build build-tools buildfiles buildincludes \
268 checkdpadd clean cleandepend cleandir cleanobj configure \
269 depend distclean distribute exe \
270 html includes install installfiles installincludes \
271 obj objlink objs objwarn \
272 realinstall regress \
275 .PHONY: ${PHONY_NOTMAIN}
276 .NOTMAIN: ${PHONY_NOTMAIN}
281 CFLAGS+= -traditional
282 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
283 CFLAGS+= -std=iso9899:1990
284 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
285 CFLAGS+= -std=iso9899:199409
286 .elif ${CSTD} == "c99"
287 CFLAGS+= -std=iso9899:1999
289 CFLAGS+= -std=${CSTD}
292 # Please keep this if in sync with bsd.sys.mk
293 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
294 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
295 .if ${COMPILER_TYPE} == "clang"
296 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
297 .if ${COMPILER_VERSION} >= 120000
298 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
300 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
303 # GCC does not support an absolute path for -fuse-ld so we just print this
304 # warning instead and let the user add the required symlinks.
305 # However, we can avoid this warning if -B is set appropriately (e.g. for
306 # CROSS_TOOLCHAIN=...-gcc).
307 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
308 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
313 # Set target-specific linker emulation name.
314 LD_EMULATION_aarch64=aarch64elf
315 LD_EMULATION_amd64=elf_x86_64_fbsd
316 LD_EMULATION_arm=armelf_fbsd
317 LD_EMULATION_armv6=armelf_fbsd
318 LD_EMULATION_armv7=armelf_fbsd
319 LD_EMULATION_i386=elf_i386_fbsd
320 LD_EMULATION_mips= elf32btsmip_fbsd
321 LD_EMULATION_mipshf= elf32btsmip_fbsd
322 LD_EMULATION_mips64= elf64btsmip_fbsd
323 LD_EMULATION_mips64hf= elf64btsmip_fbsd
324 LD_EMULATION_mipsel= elf32ltsmip_fbsd
325 LD_EMULATION_mipselhf= elf32ltsmip_fbsd
326 LD_EMULATION_mips64el= elf64ltsmip_fbsd
327 LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
328 LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
329 LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works
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_riscv64sf= elf64lriscv
336 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}