4 # Warning flags for compiling the kernel and components of the kernel:
6 CWARNFLAGS?= -Wall -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} >= 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 # 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
47 .if ${COMPILER_TYPE} == "gcc"
48 # Catch-all for all the things that are in our tree, but for which we're
49 # not yet ready for this compiler.
50 NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
51 CWARNEXTRA?= -Wno-error=address \
52 -Wno-error=aggressive-loop-optimizations \
53 -Wno-error=array-bounds \
54 -Wno-error=attributes \
55 -Wno-error=cast-qual \
56 -Wno-error=enum-compare \
57 -Wno-error=maybe-uninitialized \
58 -Wno-error=misleading-indentation \
59 -Wno-error=nonnull-compare \
61 -Wno-error=sequence-point \
62 -Wno-error=shift-overflow \
63 -Wno-error=tautological-compare \
64 -Wno-unused-but-set-variable
65 .if ${COMPILER_VERSION} >= 70100
66 CWARNEXTRA+= -Wno-error=stringop-overflow
68 .if ${COMPILER_VERSION} >= 70200
69 CWARNEXTRA+= -Wno-error=memset-elt-size
71 .if ${COMPILER_VERSION} >= 80000
72 CWARNEXTRA+= -Wno-error=packed-not-aligned
74 .if ${COMPILER_VERSION} >= 90100
75 CWARNEXTRA+= -Wno-address-of-packed-member \
76 -Wno-error=alloca-larger-than=
79 # GCC produces false positives for functions that switch on an
80 # enum (GCC bug 87950)
81 CWARNFLAGS+= -Wno-return-type
84 # This warning is utter nonsense
85 CWARNFLAGS+= -Wno-format-zero-length
87 # External compilers may not support our format extensions. Allow them
88 # to be disabled. WARNING: format checking is disabled in this case.
89 .if ${MK_FORMAT_EXTENSIONS} == "no"
90 FORMAT_EXTENSIONS= -Wno-format
91 .elif ${COMPILER_TYPE} == "clang"
92 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
94 FORMAT_EXTENSIONS= -fformat-extensions
98 # On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95
99 # and above adds code to the entry and exit point of every function to align the
100 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
101 # per function call. While the 16-byte alignment may benefit micro benchmarks,
102 # it is probably an overall loss as it makes the code bigger (less efficient
103 # use of code cache tag lines) and uses more stack (less efficient use of data
104 # cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD
105 # operations inside the kernel itself. These operations are exclusively
106 # reserved for user applications.
109 # Setting -mno-mmx implies -mno-3dnow
110 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
113 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
114 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
116 .if ${MACHINE_CPUARCH} == "i386"
117 CFLAGS.gcc+= -mpreferred-stack-boundary=2
118 CFLAGS.clang+= -mno-aes -mno-avx
119 CFLAGS+= -mno-mmx -mno-sse -msoft-float
123 .if ${MACHINE_CPUARCH} == "arm"
127 .if ${MACHINE_CPUARCH} == "aarch64"
128 # We generally don't want fpu instructions in the kernel.
129 CFLAGS += -mgeneral-regs-only
130 # Reserve x18 for pcpu data
131 CFLAGS += -ffixed-x18
136 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
137 # point registers within the kernel. However, for kernels supporting hardware
138 # float (FPE), we have to include that in the march so we can have limited
139 # floating point support in context switching needed for that. This is different
140 # than userland where we use a hard-float ABI (lp64d).
142 # We also specify the "medium" code model, which generates code suitable for a
143 # 2GiB addressing range located at any offset, allowing modules to be located
144 # anywhere in the 64-bit address space. Note that clang and GCC refer to this
145 # code model as "medium" and "medany" respectively.
147 .if ${MACHINE_CPUARCH} == "riscv"
148 CFLAGS+= -march=rv64imafdc
150 CFLAGS.clang+= -mcmodel=medium
151 CFLAGS.gcc+= -mcmodel=medany
154 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
160 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
161 # operations inside the kernel itself. These operations are exclusively
162 # reserved for user applications.
165 # Setting -mno-mmx implies -mno-3dnow
166 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
169 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
170 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
171 # (-mfpmath= is not supported)
173 .if ${MACHINE_CPUARCH} == "amd64"
174 CFLAGS.clang+= -mno-aes -mno-avx
175 CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
176 -fno-asynchronous-unwind-tables
181 # For PowerPC we tell gcc to use floating point emulation. This avoids using
182 # floating point registers for integer operations which it has a tendency to do.
183 # Also explicitly disable Altivec instructions inside the kernel.
185 .if ${MACHINE_CPUARCH} == "powerpc"
186 CFLAGS+= -mno-altivec -msoft-float
190 .if ${MACHINE_ARCH} == "powerpcspe"
191 CFLAGS.gcc+= -mno-spe
195 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
196 # DDB happy. ELFv2, if available, has some other efficiency benefits.
198 .if ${MACHINE_ARCH:Mpowerpc64*} != ""
203 # GCC 3.0 and above like to do certain optimizations based on the
204 # assumption that the program is linked against libc. Stop this.
206 CFLAGS+= -ffreestanding
209 # The C standard leaves signed integer overflow behavior undefined.
210 # gcc and clang opimizers take advantage of this. The kernel makes
211 # use of signed integer wraparound mechanics so we need the compiler
212 # to treat it as a wraparound and not take shortcuts.
219 .if ${MK_SSP} != "no"
220 CFLAGS+= -fstack-protector
224 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
226 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
227 ${MK_KERNEL_RETPOLINE} != "no"
232 # Initialize stack variables on function entry
234 .if ${MK_INIT_ALL_ZERO} == "yes"
235 .if ${COMPILER_FEATURES:Minit-all}
236 CFLAGS+= -ftrivial-auto-var-init=zero \
237 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
239 .warning InitAll (zeros) requested but not support by compiler
241 .elif ${MK_INIT_ALL_PATTERN} == "yes"
242 .if ${COMPILER_FEATURES:Minit-all}
243 CFLAGS+= -ftrivial-auto-var-init=pattern
245 .warning InitAll (pattern) requested but not support by compiler
249 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
250 CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
251 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
253 # Tell bmake not to mistake standard targets for things to be searched for
254 # or expect to ever be up-to-date.
255 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
256 beforelinking build build-tools buildfiles buildincludes \
257 checkdpadd clean cleandepend cleandir cleanobj configure \
258 depend distclean distribute exe \
259 html includes install installfiles installincludes \
260 obj objlink objs objwarn \
261 realinstall regress \
264 .PHONY: ${PHONY_NOTMAIN}
265 .NOTMAIN: ${PHONY_NOTMAIN}
270 CFLAGS+= -traditional
271 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
272 CFLAGS+= -std=iso9899:1990
273 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
274 CFLAGS+= -std=iso9899:199409
275 .elif ${CSTD} == "c99"
276 CFLAGS+= -std=iso9899:1999
278 CFLAGS+= -std=${CSTD}
281 # Please keep this if in sync with bsd.sys.mk
282 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
283 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
284 .if ${COMPILER_TYPE} == "clang"
285 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
286 .if ${COMPILER_VERSION} >= 120000
287 CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
289 CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
292 # GCC does not support an absolute path for -fuse-ld so we just print this
293 # warning instead and let the user add the required symlinks.
294 # However, we can avoid this warning if -B is set appropriately (e.g. for
295 # CROSS_TOOLCHAIN=...-gcc).
296 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
297 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
302 # Set target-specific linker emulation name.
303 LD_EMULATION_aarch64=aarch64elf
304 LD_EMULATION_amd64=elf_x86_64_fbsd
305 LD_EMULATION_arm=armelf_fbsd
306 LD_EMULATION_armv6=armelf_fbsd
307 LD_EMULATION_armv7=armelf_fbsd
308 LD_EMULATION_i386=elf_i386_fbsd
309 LD_EMULATION_powerpc= elf32ppc_fbsd
310 LD_EMULATION_powerpcspe= elf32ppc_fbsd
311 LD_EMULATION_powerpc64= elf64ppc_fbsd
312 LD_EMULATION_powerpc64le= elf64lppc_fbsd
313 LD_EMULATION_riscv64= elf64lriscv
314 LD_EMULATION_riscv64sf= elf64lriscv
315 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}