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-error-shift-count-negative 22NO_WSHIFT_COUNT_OVERFLOW= -Wno-error-shift-count-overflow 23NO_WSELF_ASSIGN= -Wno-error-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 27# Several other warnings which might be useful in some cases, but not severe 28# enough to error out the whole kernel build. Display them anyway, so there is 29# some incentive to fix them eventually. 30CWARNEXTRA?= -Wno-error-tautological-compare -Wno-error-empty-body \ 31 -Wno-error-parentheses-equality -Wno-error-unused-function \ 32 -Wno-error-pointer-sign 33.if ${COMPILER_VERSION} >= 30700 34CWARNEXTRA+= -Wno-error-shift-negative-value 35.endif 36.if ${COMPILER_VERSION} >= 40000 37CWARNEXTRA+= -Wno-error-address-of-packed-member 38.endif 39 40CLANG_NO_IAS= -no-integrated-as 41.if ${COMPILER_VERSION} < 30500 42# XXX: clang < 3.5 integrated-as doesn't grok .codeNN directives 43CLANG_NO_IAS34= -no-integrated-as 44.endif 45.endif 46 47.if ${COMPILER_TYPE} == "gcc" 48.if ${COMPILER_VERSION} >= 40800 49# Catch-all for all the things that are in our tree, but for which we're 50# not yet ready for this compiler. 51CWARNEXTRA?= -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=inline \ 58 -Wno-error=maybe-uninitialized \ 59 -Wno-error=overflow \ 60 -Wno-error=sequence-point \ 61 -Wno-error=unused-but-set-variable 62.if ${COMPILER_VERSION} >= 60100 63CWARNEXTRA+= -Wno-error=misleading-indentation \ 64 -Wno-error=nonnull-compare \ 65 -Wno-error=shift-overflow \ 66 -Wno-error=tautological-compare 67.endif 68.if ${COMPILER_VERSION} >= 80000 69CWARNEXTRA+= -Wno-error=packed-not-aligned 70.endif 71.else 72# For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars. 73CWARNEXTRA?= -Wno-uninitialized 74# GCC 4.2 doesn't have -Wno-error=cast-qual, so just disable the warning for 75# the few files that are already known to generate cast-qual warnings. 76NO_WCAST_QUAL= -Wno-cast-qual 77.endif 78.endif 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" && ${COMPILER_VERSION} >= 30600 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+= -mno-align-long-strings -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.if ${MACHINE_CPUARCH} == "riscv" 129CFLAGS.gcc+= -mcmodel=medany -march=rv64imafdc -mabi=lp64 130INLINE_LIMIT?= 8000 131.endif 132 133# 134# For sparc64 we want the medany code model so modules may be located 135# anywhere in the 64-bit address space. We also tell GCC to use floating 136# point emulation. This avoids using floating point registers for integer 137# operations which it has a tendency to do. 138# 139.if ${MACHINE_CPUARCH} == "sparc64" 140CFLAGS.clang+= -mcmodel=large -fno-dwarf2-cfi-asm 141CFLAGS.gcc+= -mcmodel=medany -msoft-float 142INLINE_LIMIT?= 15000 143.endif 144 145# 146# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD 147# operations inside the kernel itself. These operations are exclusively 148# reserved for user applications. 149# 150# gcc: 151# Setting -mno-mmx implies -mno-3dnow 152# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387 153# 154# clang: 155# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa 156# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42 157# (-mfpmath= is not supported) 158# 159.if ${MACHINE_CPUARCH} == "amd64" 160CFLAGS.clang+= -mno-aes -mno-avx 161CFLAGS+= -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \ 162 -fno-asynchronous-unwind-tables 163INLINE_LIMIT?= 8000 164.endif 165 166# 167# For PowerPC we tell gcc to use floating point emulation. This avoids using 168# floating point registers for integer operations which it has a tendency to do. 169# Also explicitly disable Altivec instructions inside the kernel. 170# 171.if ${MACHINE_CPUARCH} == "powerpc" 172CFLAGS+= -mno-altivec -msoft-float 173INLINE_LIMIT?= 15000 174.endif 175 176.if ${MACHINE_ARCH} == "powerpcspe" 177CFLAGS.gcc+= -mno-spe 178.endif 179 180# 181# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make 182# DDB happy. ELFv2, if available, has some other efficiency benefits. 183# 184.if ${MACHINE_ARCH} == "powerpc64" 185.if ${COMPILER_VERSION} >= 40900 186CFLAGS.gcc+= -mabi=elfv2 187.else 188CFLAGS.gcc+= -mcall-aixdesc 189.endif 190CFLAGS.clang+= -mabi=elfv2 191.endif 192 193# 194# For MIPS we also tell gcc to use floating point emulation 195# 196.if ${MACHINE_CPUARCH} == "mips" 197CFLAGS+= -msoft-float 198INLINE_LIMIT?= 8000 199.endif 200 201# 202# GCC 3.0 and above like to do certain optimizations based on the 203# assumption that the program is linked against libc. Stop this. 204# 205CFLAGS+= -ffreestanding 206 207# 208# The C standard leaves signed integer overflow behavior undefined. 209# gcc and clang opimizers take advantage of this. The kernel makes 210# use of signed integer wraparound mechanics so we need the compiler 211# to treat it as a wraparound and not take shortcuts. 212# 213CFLAGS+= -fwrapv 214 215# 216# GCC SSP support 217# 218.if ${MK_SSP} != "no" && \ 219 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips" 220CFLAGS+= -fstack-protector 221.endif 222 223# 224# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715) 225# 226.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \ 227 ${MK_KERNEL_RETPOLINE} != "no" 228CFLAGS+= -mretpoline 229.endif 230 231# 232# Add -gdwarf-2 when compiling -g. The default starting in clang v3.4 233# and gcc 4.8 is to generate DWARF version 4. However, our tools don't 234# cope well with DWARF 4, so force it to genereate DWARF2, which they 235# understand. Do this unconditionally as it is harmless when not needed, 236# but critical for these newer versions. 237# 238.if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == "" 239CFLAGS+= -gdwarf-2 240.endif 241 242CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}} 243CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}} 244 245# Tell bmake not to mistake standard targets for things to be searched for 246# or expect to ever be up-to-date. 247PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \ 248 beforelinking build build-tools buildfiles buildincludes \ 249 checkdpadd clean cleandepend cleandir cleanobj configure \ 250 depend distclean distribute exe \ 251 html includes install installfiles installincludes \ 252 obj objlink objs objwarn \ 253 realinstall regress \ 254 tags whereobj 255 256.PHONY: ${PHONY_NOTMAIN} 257.NOTMAIN: ${PHONY_NOTMAIN} 258 259CSTD= c99 260 261.if ${CSTD} == "k&r" 262CFLAGS+= -traditional 263.elif ${CSTD} == "c89" || ${CSTD} == "c90" 264CFLAGS+= -std=iso9899:1990 265.elif ${CSTD} == "c94" || ${CSTD} == "c95" 266CFLAGS+= -std=iso9899:199409 267.elif ${CSTD} == "c99" 268CFLAGS+= -std=iso9899:1999 269.else # CSTD 270CFLAGS+= -std=${CSTD} 271.endif # CSTD 272 273# Set target-specific linker emulation name. 274LD_EMULATION_aarch64=aarch64elf 275LD_EMULATION_amd64=elf_x86_64_fbsd 276LD_EMULATION_arm=armelf_fbsd 277LD_EMULATION_armeb=armelfb_fbsd 278LD_EMULATION_armv6=armelf_fbsd 279LD_EMULATION_armv7=armelf_fbsd 280LD_EMULATION_i386=elf_i386_fbsd 281LD_EMULATION_mips= elf32btsmip_fbsd 282LD_EMULATION_mipshf= elf32btsmip_fbsd 283LD_EMULATION_mips64= elf64btsmip_fbsd 284LD_EMULATION_mips64hf= elf64btsmip_fbsd 285LD_EMULATION_mipsel= elf32ltsmip_fbsd 286LD_EMULATION_mipselhf= elf32ltsmip_fbsd 287LD_EMULATION_mips64el= elf64ltsmip_fbsd 288LD_EMULATION_mips64elhf= elf64ltsmip_fbsd 289LD_EMULATION_mipsn32= elf32btsmipn32_fbsd 290LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd # I don't think this is a thing that works 291LD_EMULATION_powerpc= elf32ppc_fbsd 292LD_EMULATION_powerpcspe= elf32ppc_fbsd 293LD_EMULATION_powerpc64= elf64ppc_fbsd 294LD_EMULATION_riscv64= elf64lriscv 295LD_EMULATION_sparc64= elf64_sparc_fbsd 296LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}} 297