1# $FreeBSD$ 2 3# 4# Warning flags for compiling the kernel and components of the kernel: 5# 6CWARNFLAGS?= -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 \ 11 ${CWARNEXTRA} 12# 13# The following flags are next up for working on: 14# -Wextra 15 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 18# a false positive. 19.if ${COMPILER_TYPE} == "clang" 20NO_WCONSTANT_CONVERSION= -Wno-error=constant-conversion 21NO_WSHIFT_COUNT_NEGATIVE= -Wno-shift-count-negative 22NO_WSHIFT_COUNT_OVERFLOW= -Wno-shift-count-overflow 23NO_WSELF_ASSIGN= -Wno-self-assign 24NO_WUNNEEDED_INTERNAL_DECL= -Wno-error=unneeded-internal-declaration 25NO_WSOMETIMES_UNINITIALIZED= -Wno-error=sometimes-uninitialized 26NO_WCAST_QUAL= -Wno-error=cast-qual 27NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare 28.if ${COMPILER_VERSION} >= 100000 29NO_WMISLEADING_INDENTATION= -Wno-misleading-indentation 30.endif 31# Several other warnings which might be useful in some cases, but not severe 32# enough to error out the whole kernel build. Display them anyway, so there is 33# some incentive to fix them eventually. 34CWARNEXTRA?= -Wno-error=tautological-compare -Wno-error=empty-body \ 35 -Wno-error=parentheses-equality -Wno-error=unused-function \ 36 -Wno-error=pointer-sign 37CWARNEXTRA+= -Wno-error=shift-negative-value 38CWARNEXTRA+= -Wno-address-of-packed-member 39.if ${COMPILER_VERSION} >= 130000 40CWARNFLAGS+= -Wno-error=unused-but-set-variable 41.endif 42.endif # clang 43 44.if ${COMPILER_TYPE} == "gcc" 45# Catch-all for all the things that are in our tree, but for which we're 46# not yet ready for this compiler. 47NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable 48CWARNEXTRA?= -Wno-error=address \ 49 -Wno-error=aggressive-loop-optimizations \ 50 -Wno-error=array-bounds \ 51 -Wno-error=attributes \ 52 -Wno-error=cast-qual \ 53 -Wno-error=enum-compare \ 54 -Wno-error=maybe-uninitialized \ 55 -Wno-error=misleading-indentation \ 56 -Wno-error=nonnull-compare \ 57 -Wno-error=overflow \ 58 -Wno-error=sequence-point \ 59 -Wno-error=shift-overflow \ 60 -Wno-error=tautological-compare \ 61 -Wno-unused-but-set-variable 62.if ${COMPILER_VERSION} >= 70100 63CWARNEXTRA+= -Wno-error=stringop-overflow 64.endif 65.if ${COMPILER_VERSION} >= 70200 66CWARNEXTRA+= -Wno-error=memset-elt-size 67.endif 68.if ${COMPILER_VERSION} >= 80000 69CWARNEXTRA+= -Wno-error=packed-not-aligned 70.endif 71.if ${COMPILER_VERSION} >= 90100 72CWARNEXTRA+= -Wno-address-of-packed-member \ 73 -Wno-error=alloca-larger-than= 74.endif 75.endif # gcc 76 77# This warning is utter nonsense 78CWARNFLAGS+= -Wno-format-zero-length 79 80# External compilers may not support our format extensions. Allow them 81# to be disabled. WARNING: format checking is disabled in this case. 82.if ${MK_FORMAT_EXTENSIONS} == "no" 83FORMAT_EXTENSIONS= -Wno-format 84.elif ${COMPILER_TYPE} == "clang" 85FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 86.else 87FORMAT_EXTENSIONS= -fformat-extensions 88.endif 89 90# 91# On i386, do not align the stack to 16-byte boundaries. Otherwise GCC 2.95 92# and above adds code to the entry and exit point of every function to align the 93# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack 94# per function call. While the 16-byte alignment may benefit micro benchmarks, 95# it is probably an overall loss as it makes the code bigger (less efficient 96# use of code cache tag lines) and uses more stack (less efficient use of data 97# cache tag lines). Explicitly prohibit the use of FPU, SSE and other SIMD 98# operations inside the kernel itself. These operations are exclusively 99# reserved for user applications. 100# 101# gcc: 102# Setting -mno-mmx implies -mno-3dnow 103# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3 104# 105# clang: 106# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 107# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 108# 109.if ${MACHINE_CPUARCH} == "i386" 110CFLAGS.gcc+= -mpreferred-stack-boundary=2 111CFLAGS.clang+= -mno-aes -mno-avx 112CFLAGS+= -mno-mmx -mno-sse -msoft-float 113INLINE_LIMIT?= 8000 114.endif 115 116.if ${MACHINE_CPUARCH} == "arm" 117INLINE_LIMIT?= 8000 118.endif 119 120.if ${MACHINE_CPUARCH} == "aarch64" 121# We generally don't want fpu instructions in the kernel. 122CFLAGS += -mgeneral-regs-only 123# Reserve x18 for pcpu data 124CFLAGS += -ffixed-x18 125INLINE_LIMIT?= 8000 126.endif 127 128# 129# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating 130# point registers within the kernel. However, for kernels supporting hardware 131# float (FPE), we have to include that in the march so we can have limited 132# floating point support in context switching needed for that. This is different 133# than userland where we use a hard-float ABI (lp64d). 134# 135# We also specify the "medium" code model, which generates code suitable for a 136# 2GiB addressing range located at any offset, allowing modules to be located 137# anywhere in the 64-bit address space. Note that clang and GCC refer to this 138# code model as "medium" and "medany" respectively. 139# 140.if ${MACHINE_CPUARCH} == "riscv" 141CFLAGS+= -march=rv64imafdc 142CFLAGS+= -mabi=lp64 143CFLAGS.clang+= -mcmodel=medium 144CFLAGS.gcc+= -mcmodel=medany 145INLINE_LIMIT?= 8000 146 147.if ${LINKER_FEATURES:Mriscv-relaxations} == "" 148CFLAGS+= -mno-relax 149.endif 150.endif 151 152# 153# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD 154# operations inside the kernel itself. These operations are exclusively 155# reserved for user applications. 156# 157# gcc: 158# Setting -mno-mmx implies -mno-3dnow 159# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387 160# 161# clang: 162# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 163# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 164# (-mfpmath= is not supported) 165# 166.if ${MACHINE_CPUARCH} == "amd64" 167CFLAGS.clang+= -mno-aes -mno-avx 168CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \ 169 -fno-asynchronous-unwind-tables 170INLINE_LIMIT?= 8000 171.endif 172 173# 174# For PowerPC we tell gcc to use floating point emulation. This avoids using 175# floating point registers for integer operations which it has a tendency to do. 176# Also explicitly disable Altivec instructions inside the kernel. 177# 178.if ${MACHINE_CPUARCH} == "powerpc" 179CFLAGS+= -mno-altivec -msoft-float 180INLINE_LIMIT?= 15000 181.endif 182 183.if ${MACHINE_ARCH} == "powerpcspe" 184CFLAGS.gcc+= -mno-spe 185.endif 186 187# 188# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make 189# DDB happy. ELFv2, if available, has some other efficiency benefits. 190# 191.if ${MACHINE_ARCH:Mpowerpc64*} != "" 192CFLAGS+= -mabi=elfv2 193.endif 194 195# 196# GCC 3.0 and above like to do certain optimizations based on the 197# assumption that the program is linked against libc. Stop this. 198# 199CFLAGS+= -ffreestanding 200 201# 202# The C standard leaves signed integer overflow behavior undefined. 203# gcc and clang opimizers take advantage of this. The kernel makes 204# use of signed integer wraparound mechanics so we need the compiler 205# to treat it as a wraparound and not take shortcuts. 206# 207CFLAGS+= -fwrapv 208 209# 210# GCC SSP support 211# 212.if ${MK_SSP} != "no" 213CFLAGS+= -fstack-protector 214.endif 215 216# 217# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715) 218# 219.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \ 220 ${MK_KERNEL_RETPOLINE} != "no" 221CFLAGS+= -mretpoline 222.endif 223 224# 225# Initialize stack variables on function entry 226# 227.if ${MK_INIT_ALL_ZERO} == "yes" 228.if ${COMPILER_FEATURES:Minit-all} 229CFLAGS+= -ftrivial-auto-var-init=zero \ 230 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 231.else 232.warning InitAll (zeros) requested but not support by compiler 233.endif 234.elif ${MK_INIT_ALL_PATTERN} == "yes" 235.if ${COMPILER_FEATURES:Minit-all} 236CFLAGS+= -ftrivial-auto-var-init=pattern 237.else 238.warning InitAll (pattern) requested but not support by compiler 239.endif 240.endif 241 242CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}} 243CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}} 244CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}} 245 246# Tell bmake not to mistake standard targets for things to be searched for 247# or expect to ever be up-to-date. 248PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \ 249 beforelinking build build-tools buildfiles buildincludes \ 250 checkdpadd clean cleandepend cleandir cleanobj configure \ 251 depend distclean distribute exe \ 252 html includes install installfiles installincludes \ 253 obj objlink objs objwarn \ 254 realinstall regress \ 255 tags whereobj 256 257.PHONY: ${PHONY_NOTMAIN} 258.NOTMAIN: ${PHONY_NOTMAIN} 259 260CSTD= c99 261 262.if ${CSTD} == "k&r" 263CFLAGS+= -traditional 264.elif ${CSTD} == "c89" || ${CSTD} == "c90" 265CFLAGS+= -std=iso9899:1990 266.elif ${CSTD} == "c94" || ${CSTD} == "c95" 267CFLAGS+= -std=iso9899:199409 268.elif ${CSTD} == "c99" 269CFLAGS+= -std=iso9899:1999 270.else # CSTD 271CFLAGS+= -std=${CSTD} 272.endif # CSTD 273 274# Please keep this if in sync with bsd.sys.mk 275.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld") 276# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld". 277.if ${COMPILER_TYPE} == "clang" 278# Note: Clang does not like relative paths for ld so we map ld.lld -> lld. 279.if ${COMPILER_VERSION} >= 120000 280CCLDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W} 281.else 282CCLDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W} 283.endif 284.else 285# GCC does not support an absolute path for -fuse-ld so we just print this 286# warning instead and let the user add the required symlinks. 287# However, we can avoid this warning if -B is set appropriately (e.g. for 288# CROSS_TOOLCHAIN=...-gcc). 289.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/}) 290.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported 291.endif 292.endif 293.endif 294 295# Set target-specific linker emulation name. 296LD_EMULATION_aarch64=aarch64elf 297LD_EMULATION_amd64=elf_x86_64_fbsd 298LD_EMULATION_arm=armelf_fbsd 299LD_EMULATION_armv6=armelf_fbsd 300LD_EMULATION_armv7=armelf_fbsd 301LD_EMULATION_i386=elf_i386_fbsd 302LD_EMULATION_powerpc= elf32ppc_fbsd 303LD_EMULATION_powerpcspe= elf32ppc_fbsd 304LD_EMULATION_powerpc64= elf64ppc_fbsd 305LD_EMULATION_powerpc64le= elf64lppc_fbsd 306LD_EMULATION_riscv64= elf64lriscv 307LD_EMULATION_riscv64sf= elf64lriscv 308LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}} 309