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