176861Skris# $FreeBSD: stable/11/share/mk/bsd.sys.mk 360658 2020-05-05 12:59:04Z dim $ 276861Skris# 376861Skris# This file contains common settings used for building FreeBSD 476861Skris# sources. 576861Skris 676861Skris# Enable various levels of compiler warning checks. These may be 7265830Simp# overridden (e.g. if using a non-gcc compiler) by defining MK_WARNS=no. 876861Skris 9233052Sdim# for GCC: http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Warning-Options.html 1096316Sobrien 11240468Sbrooks.include <bsd.compiler.mk> 12240468Sbrooks 13189801Srdivacky# the default is gnu99 for now 14233052SdimCSTD?= gnu99 15189801Srdivacky 16220863Sdim.if ${CSTD} == "k&r" 17233052SdimCFLAGS+= -traditional 18220863Sdim.elif ${CSTD} == "c89" || ${CSTD} == "c90" 19233052SdimCFLAGS+= -std=iso9899:1990 20220863Sdim.elif ${CSTD} == "c94" || ${CSTD} == "c95" 21233052SdimCFLAGS+= -std=iso9899:199409 22220863Sdim.elif ${CSTD} == "c99" 23233052SdimCFLAGS+= -std=iso9899:1999 24233052Sdim.else # CSTD 25233052SdimCFLAGS+= -std=${CSTD} 26233052Sdim.endif # CSTD 27352023Simp# 28352023Simp# Turn off -Werror for gcc 4.2.1. The compiler is on the glide path out of the 29352023Simp# system, and any warnings specific to it are no longer relevant as there are 30352023Simp# too many false positives. 31352023Simp# 32352023Simp.if ${COMPILER_VERSION} < 50000 33352023SimpNO_WERROR.gcc= yes 34352023Simp.endif 35352023Simp 36161214Sdes# -pedantic is problematic because it also imposes namespace restrictions 37233052Sdim#CFLAGS+= -pedantic 38233052Sdim.if defined(WARNS) 39233052Sdim.if ${WARNS} >= 1 40233052SdimCWARNFLAGS+= -Wsystem-headers 41264932Simp.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE}) 42233052SdimCWARNFLAGS+= -Werror 43264932Simp.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE} 44233052Sdim.endif # WARNS >= 1 45233052Sdim.if ${WARNS} >= 2 46233052SdimCWARNFLAGS+= -Wall -Wno-format-y2k 47233052Sdim.endif # WARNS >= 2 48233052Sdim.if ${WARNS} >= 3 49233052SdimCWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\ 50233052Sdim -Wmissing-prototypes -Wpointer-arith 51233052Sdim.endif # WARNS >= 3 52233052Sdim.if ${WARNS} >= 4 53233052SdimCWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\ 54233052Sdim -Wunused-parameter 55264932Simp.if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE}) 56233052SdimCWARNFLAGS+= -Wcast-align 57264932Simp.endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE} 58233052Sdim.endif # WARNS >= 4 5994332Sobrien# BDECFLAGS 60233052Sdim.if ${WARNS} >= 6 61233052SdimCWARNFLAGS+= -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls\ 62233052Sdim -Wold-style-definition 63265401Simp.if !defined(NO_WMISSING_VARIABLE_DECLARATIONS) 64265400SimpCWARNFLAGS.clang+= -Wmissing-variable-declarations 65249657Sed.endif 66270951Sed.if !defined(NO_WTHREAD_SAFETY) 67270951SedCWARNFLAGS.clang+= -Wthread-safety 68270951Sed.endif 69233052Sdim.endif # WARNS >= 6 70233052Sdim.if ${WARNS} >= 2 && ${WARNS} <= 4 7188936Sdwmalone# XXX Delete -Wuninitialized by default for now -- the compiler doesn't 7288936Sdwmalone# XXX always get it right. 73233052SdimCWARNFLAGS+= -Wno-uninitialized 74233052Sdim.endif # WARNS >=2 && WARNS <= 4 75233052SdimCWARNFLAGS+= -Wno-pointer-sign 76228546Sdim# Clang has more warnings enabled by default, and when using -Wall, so if WARNS 77228546Sdim# is set to low values, these have to be disabled explicitly. 78234353Sdim.if ${WARNS} <= 6 79265829SimpCWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int 80280031Sdim.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30400 81265829SimpCWARNFLAGS.clang+= -Wno-unused-const-variable 82265829Simp.endif 83234353Sdim.endif # WARNS <= 6 84233052Sdim.if ${WARNS} <= 3 85265400SimpCWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\ 86259083Sdim -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion 87280031Sdim.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600 88280031SdimCWARNFLAGS.clang+= -Wno-unused-local-typedef 89280031Sdim.endif 90316423Sdim.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 40000 91316423SdimCWARNFLAGS.clang+= -Wno-address-of-packed-member 92316423Sdim.endif 93344213Sdim.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \ 94344213Sdim ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*} 95344213SdimCWARNFLAGS.clang+= -Wno-atomic-alignment 96344213Sdim.endif 97233052Sdim.endif # WARNS <= 3 98233052Sdim.if ${WARNS} <= 2 99265400SimpCWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter 100233052Sdim.endif # WARNS <= 2 101233052Sdim.if ${WARNS} <= 1 102265400SimpCWARNFLAGS.clang+= -Wno-parentheses 103233052Sdim.endif # WARNS <= 1 104233052Sdim.if defined(NO_WARRAY_BOUNDS) 105265400SimpCWARNFLAGS.clang+= -Wno-array-bounds 106233052Sdim.endif # NO_WARRAY_BOUNDS 107357521Sdim.if defined(NO_WMISLEADING_INDENTATION) && \ 108357521Sdim ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \ 109357521Sdim (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100)) 110357521SdimCWARNFLAGS+= -Wno-misleading-indentation 111357521Sdim.endif # NO_WMISLEADING_INDENTATION 112233052Sdim.endif # WARNS 11376861Skris 114233052Sdim.if defined(FORMAT_AUDIT) 115233052SdimWFORMAT= 1 116233052Sdim.endif # FORMAT_AUDIT 117233052Sdim.if defined(WFORMAT) 118233052Sdim.if ${WFORMAT} > 0 119233052Sdim#CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args 120233052SdimCWARNFLAGS+= -Wformat=2 -Wno-format-extra-args 121234353Sdim.if ${WARNS} <= 3 122265400SimpCWARNFLAGS.clang+= -Wno-format-nonliteral 123234353Sdim.endif # WARNS <= 3 124264932Simp.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE}) 125233052SdimCWARNFLAGS+= -Werror 126264932Simp.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE} 127233052Sdim.endif # WFORMAT > 0 128233052Sdim.endif # WFORMAT 129264932Simp.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE}) 130233052SdimCWARNFLAGS+= -Wno-format 131264932Simp.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE} 13276861Skris 133292124Sbr# GCC 5.2.0 134292124Sbr.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200 135323164SrlibbyCWARNFLAGS+= -Wno-error=address \ 136323164Srlibby -Wno-error=array-bounds \ 137323164Srlibby -Wno-error=attributes \ 138323164Srlibby -Wno-error=bool-compare \ 139323164Srlibby -Wno-error=cast-align \ 140323164Srlibby -Wno-error=clobbered \ 141360658Sdim -Wno-error=deprecated-declarations \ 142323164Srlibby -Wno-error=enum-compare \ 143323164Srlibby -Wno-error=extra \ 144323164Srlibby -Wno-error=inline \ 145323164Srlibby -Wno-error=logical-not-parentheses \ 146323164Srlibby -Wno-error=strict-aliasing \ 147323164Srlibby -Wno-error=uninitialized \ 148323164Srlibby -Wno-error=unused-but-set-variable \ 149323164Srlibby -Wno-error=unused-function \ 150323164Srlibby -Wno-error=unused-value 151292124Sbr.endif 152292124Sbr 153323164Srlibby# GCC 6.1.0 154323164Srlibby.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100 155357521SdimCWARNFLAGS+= -Wno-error=nonnull-compare \ 156323164Srlibby -Wno-error=shift-negative-value \ 157323164Srlibby -Wno-error=tautological-compare \ 158323164Srlibby -Wno-error=unused-const-variable 159323164Srlibby.endif 160323164Srlibby 161280031Sdim# How to handle FreeBSD custom printf format specifiers. 162280031Sdim.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600 163280031SdimFORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 164280031Sdim.else 165280031SdimFORMAT_EXTENSIONS= -fformat-extensions 166280031Sdim.endif 167280031Sdim 168163971Sjb.if defined(IGNORE_PRAGMA) 169233052SdimCWARNFLAGS+= -Wno-unknown-pragmas 170233052Sdim.endif # IGNORE_PRAGMA 171163971Sjb 172276497Simp# We need this conditional because many places that use it 173276497Simp# only enable it for some files with CLFAGS.$FILE+=${CLANG_NO_IAS}. 174276497Simp# unconditionally, and can't easily use the CFLAGS.clang= 175276497Simp# mechanism. 176276497Simp.if ${COMPILER_TYPE} == "clang" 177233052SdimCLANG_NO_IAS= -no-integrated-as 178276497Simp.endif 179234353SdimCLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\ 180280328Sdim -mllvm -simplifycfg-dup-ret 181288943Sdim.if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700 182280328SdimCLANG_OPT_SMALL+= -mllvm -enable-gvn=false 183280784Sdim.else 184280784SdimCLANG_OPT_SMALL+= -mllvm -enable-load-pre=false 185279018Simp.endif 186265400SimpCFLAGS.clang+= -Qunused-arguments 187262613Sdim.if ${MACHINE_CPUARCH} == "sparc64" 188262613Sdim# Don't emit .cfi directives, since we must use GNU as on sparc64, for now. 189265400SimpCFLAGS.clang+= -fno-dwarf2-cfi-asm 190262613Sdim.endif # SPARC64 191262310Sdim# The libc++ headers use c++11 extensions. These are normally silenced because 192262310Sdim# they are treated as system headers, but we explicitly disable that warning 193262310Sdim# suppression when building the base system to catch bugs in our headers. 194262310Sdim# Eventually we'll want to start building the base system C++ code as C++11, 195262310Sdim# but not yet. 196265400SimpCXXFLAGS.clang+= -Wno-c++11-extensions 197232263Sdim 198268351Smarcel.if ${MK_SSP} != "no" && \ 199220863Sdim ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips" 200289465Sngie.if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \ 201289465Sngie (${COMPILER_TYPE} == "gcc" && \ 202289481Sngie (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900)) 203180012Sru# Don't use -Wstack-protector as it breaks world with -Werror. 204288669SpfgSSP_CFLAGS?= -fstack-protector-strong 205289481Sngie.else 206289481SngieSSP_CFLAGS?= -fstack-protector 207289481Sngie.endif 208233052SdimCFLAGS+= ${SSP_CFLAGS} 209268351Smarcel.endif # SSP && !ARM && !MIPS 210180012Sru 211293905Ssmh# Allow user-specified additional warning flags, plus compiler and file 212293905Ssmh# specific flag overrides, unless we've overriden this... 213265830Simp.if ${MK_WARNS} != "no" 214323164SrlibbyCFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}} 215293905SsmhCFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}} 216360658SdimCXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}} 217360658SdimCXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}} 218265401Simp.endif 219241298Smarcel 220265401SimpCFLAGS+= ${CFLAGS.${COMPILER_TYPE}} 221265401SimpCXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}} 222265401Simp 223302090SbdreweryAFLAGS+= ${AFLAGS.${.IMPSRC:T}} 224297283SbdreweryACFLAGS+= ${ACFLAGS.${.IMPSRC:T}} 225297283SbdreweryCFLAGS+= ${CFLAGS.${.IMPSRC:T}} 226297283SbdreweryCXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}} 227297283Sbdrewery 228298219Sbdrewery.if defined(SRCTOP) 229298219Sbdrewery# Prevent rebuilding during install to support read-only objdirs. 230311196Sbdrewery.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta) 231298219SbdreweryCFLAGS+= ERROR-tried-to-rebuild-during-make-install 232298219Sbdrewery.endif 233298219Sbdrewery.endif 234298219Sbdrewery 235241298Smarcel# Tell bmake not to mistake standard targets for things to be searched for 236241298Smarcel# or expect to ever be up-to-date. 237296124SbdreweryPHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \ 238289335Sbdrewery beforelinking build build-tools buildconfig buildfiles \ 239295380Sngie buildincludes check checkdpadd clean cleandepend cleandir \ 240296117Sbdrewery cleanobj configure depend distclean distribute exe \ 241289335Sbdrewery files html includes install installconfig installfiles \ 242296117Sbdrewery installincludes lint obj objlink objs objwarn \ 243296117Sbdrewery realinstall tags whereobj 244241298Smarcel 245284345Ssjg# we don't want ${PROG} to be PHONY 246284345Ssjg.PHONY: ${PHONY_NOTMAIN:N${PROG:U}} 247284345Ssjg.NOTMAIN: ${PHONY_NOTMAIN:Nall} 248284345Ssjg 249284345Ssjg.if ${MK_STAGING} != "no" 250359715Sbdrewery.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean)) 251284345Ssjg_SKIP_STAGING?= yes 252284345Ssjg.endif 253284345Ssjg.if ${_SKIP_STAGING:Uno} == "yes" 254284345Ssjgstaging stage_libs stage_files stage_as stage_links stage_symlinks: 255284345Ssjg.else 256284345Ssjg# allow targets like beforeinstall to be leveraged 257284345SsjgDESTDIR= ${STAGE_OBJTOP} 258296694Sbdrewery.export DESTDIR 259284345Ssjg 260296693Sbdrewery.if target(beforeinstall) 261291087Sbdrewery.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 262284345Ssjgstaging: beforeinstall 263284345Ssjg.endif 264284345Ssjg.endif 265284345Ssjg 266284345Ssjg# normally only libs and includes are staged 267291087Sbdrewery.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG) 268284345SsjgSTAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR} 269284345Ssjg 270296555Sbdrewery.if !empty(PROG) 271284345Ssjg.if defined(PROGNAME) 272284345SsjgSTAGE_AS_SETS+= prog 273284345SsjgSTAGE_AS_${PROG}= ${PROGNAME} 274284345Ssjgstage_as.prog: ${PROG} 275284345Ssjg.else 276284345SsjgSTAGE_SETS+= prog 277284345Ssjgstage_files.prog: ${PROG} 278288964SsjgSTAGE_TARGETS+= stage_files 279284345Ssjg.endif 280284345Ssjg.endif 281284345Ssjg.endif 282284345Ssjg 283284345Ssjg.if !empty(_LIBS) && !defined(INTERNALLIB) 284284345Ssjg.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != "" 285284345SsjgSTAGE_SETS+= shlib 286284345SsjgSTAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR} 287284345SsjgSTAGE_FILES.shlib+= ${_LIBS:M*.so.*} 288284345Ssjgstage_files.shlib: ${_LIBS:M*.so.*} 289284345Ssjg.endif 290284345Ssjg 291284345Ssjg.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld) 292284345SsjgSTAGE_AS_SETS+= ldscript 293284345SsjgSTAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld 294284345Ssjgstage_as.ldscript: ${SHLIB_LINK:R}.ld 295284345SsjgSTAGE_DIR.ldscript = ${STAGE_LIBDIR} 296284345SsjgSTAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK} 297284345SsjgNO_SHLIB_LINKS= 298284345Ssjg.endif 299284345Ssjg 300284345Ssjg.if target(stage_files.shlib) 301284345Ssjgstage_libs: ${_LIBS} 302284345Ssjg.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 303284345Ssjgstage_files.shlib: ${SHLIB_NAME}.symbols 304284345Ssjg.endif 305284345Ssjg.else 306284345Ssjgstage_libs: ${_LIBS} 307284345Ssjg.endif 308284345Ssjg.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 309284345Ssjgstage_libs: ${SHLIB_NAME}.symbols 310284345Ssjg.endif 311284345Ssjg 312284345Ssjg.endif 313284345Ssjg 314284345Ssjg.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes) 315284345Ssjg.if !defined(NO_BEFOREBUILD_INCLUDES) 316284345Ssjgstage_includes: buildincludes 317284345Ssjgbeforebuild: stage_includes 318284345Ssjg.endif 319284345Ssjg.endif 320284345Ssjg 321284345Ssjg.for t in stage_libs stage_files stage_as 322284345Ssjg.if target($t) 323288964SsjgSTAGE_TARGETS+= $t 324284345Ssjg.endif 325284345Ssjg.endfor 326284345Ssjg 327284345Ssjg.if !empty(STAGE_AS_SETS) 328288964SsjgSTAGE_TARGETS+= stage_as 329284345Ssjg.endif 330284345Ssjg 331291087Sbdrewery.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 332284345Ssjg 333284345Ssjg.if !empty(LINKS) 334288964SsjgSTAGE_TARGETS+= stage_links 335284345Ssjg.if ${MAKE_VERSION} < 20131001 336284345Ssjgstage_links.links: ${_LIBS} ${PROG} 337284345Ssjg.endif 338284345SsjgSTAGE_SETS+= links 339284345SsjgSTAGE_LINKS.links= ${LINKS} 340284345Ssjg.endif 341284345Ssjg 342290801Sbdrewery.if !empty(SYMLINKS) 343288964SsjgSTAGE_TARGETS+= stage_symlinks 344284345SsjgSTAGE_SETS+= links 345284345SsjgSTAGE_SYMLINKS.links= ${SYMLINKS} 346284345Ssjg.endif 347284345Ssjg 348284345Ssjg.endif 349284345Ssjg 350284345Ssjg.include <meta.stage.mk> 351284345Ssjg.endif 352284345Ssjg.endif 353284345Ssjg 354297994Sbdrewery.if defined(META_TARGETS) 355297994Sbdrewery.for _tgt in ${META_TARGETS} 356297994Sbdrewery.if target(${_tgt}) 357297994Sbdrewery${_tgt}: ${META_DEPS} 358297994Sbdrewery.endif 359297994Sbdrewery.endfor 360297994Sbdrewery.endif 361