1# $FreeBSD$ 2# 3# This file contains common settings used for building FreeBSD 4# sources. 5 6# Enable various levels of compiler warning checks. These may be 7# overridden (e.g. if using a non-gcc compiler) by defining MK_WARNS=no. 8 9# for GCC: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 10# for clang: https://clang.llvm.org/docs/DiagnosticsReference.html 11 12.include <bsd.compiler.mk> 13 14# the default is gnu99 for now 15CSTD?= gnu99 16 17.if ${CSTD} == "c89" || ${CSTD} == "c90" 18CFLAGS+= -std=iso9899:1990 19.elif ${CSTD} == "c94" || ${CSTD} == "c95" 20CFLAGS+= -std=iso9899:199409 21.elif ${CSTD} == "c99" 22CFLAGS+= -std=iso9899:1999 23.else # CSTD 24CFLAGS+= -std=${CSTD} 25.endif # CSTD 26 27.if !empty(CXXSTD) 28CXXFLAGS+= -std=${CXXSTD} 29.endif 30 31# This gives the Makefile we're evaluating at the top-level a chance to set 32# WARNS. If it doesn't do so, we may freely pull a DEFAULTWARNS if it's set 33# and use that. This allows us to default WARNS to 6 for src builds without 34# needing to set the default in various Makefile.inc. 35.if !defined(WARNS) && defined(DEFAULTWARNS) 36WARNS= ${DEFAULTWARNS} 37.endif 38 39# -pedantic is problematic because it also imposes namespace restrictions 40#CFLAGS+= -pedantic 41.if defined(WARNS) 42.if ${WARNS} >= 1 43CWARNFLAGS+= -Wsystem-headers 44.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no" 45CWARNFLAGS+= -Werror 46.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no" 47.endif # WARNS >= 1 48.if ${WARNS} >= 2 49CWARNFLAGS+= -Wall -Wno-format-y2k 50.endif # WARNS >= 2 51.if ${WARNS} >= 3 52CWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\ 53 -Wmissing-prototypes -Wpointer-arith 54.endif # WARNS >= 3 55.if ${WARNS} >= 4 56CWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\ 57 -Wunused-parameter 58.if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE}) 59CWARNFLAGS+= -Wcast-align 60.endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE} 61.endif # WARNS >= 4 62.if ${WARNS} >= 6 63CWARNFLAGS+= -Wchar-subscripts -Wnested-externs \ 64 -Wold-style-definition 65.if !defined(NO_WMISSING_VARIABLE_DECLARATIONS) 66CWARNFLAGS.clang+= -Wmissing-variable-declarations 67.endif 68.if !defined(NO_WTHREAD_SAFETY) 69CWARNFLAGS.clang+= -Wthread-safety 70.endif 71.endif # WARNS >= 6 72.if ${WARNS} >= 2 && ${WARNS} <= 4 73# XXX Delete -Wuninitialized by default for now -- the compiler doesn't 74# XXX always get it right. 75CWARNFLAGS+= -Wno-uninitialized 76.endif # WARNS >=2 && WARNS <= 4 77CWARNFLAGS+= -Wno-pointer-sign 78.if !defined(NO_WDATE_TIME) 79CWARNFLAGS+= -Wdate-time 80.endif # NO_WDATE_TIME 81# Clang has more warnings enabled by default, and when using -Wall, so if WARNS 82# is set to low values, these have to be disabled explicitly. 83.if ${WARNS} <= 6 84CWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int 85CWARNFLAGS.clang+= -Wno-unused-const-variable 86.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 130000 87CWARNFLAGS.clang+= -Wno-error=unused-but-set-variable 88.endif 89.endif # WARNS <= 6 90.if ${WARNS} <= 3 91CWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\ 92 -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion 93CWARNFLAGS.clang+= -Wno-unused-local-typedef 94CWARNFLAGS.clang+= -Wno-address-of-packed-member 95.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100 96CWARNFLAGS.gcc+= -Wno-address-of-packed-member 97.endif 98.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \ 99 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*} 100CWARNFLAGS.clang+= -Wno-atomic-alignment 101.endif 102.endif # WARNS <= 3 103.if ${WARNS} <= 2 104CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter 105.endif # WARNS <= 2 106.if ${WARNS} <= 1 107CWARNFLAGS.clang+= -Wno-parentheses 108.endif # WARNS <= 1 109.if defined(NO_WARRAY_BOUNDS) 110CWARNFLAGS.clang+= -Wno-array-bounds 111.endif # NO_WARRAY_BOUNDS 112.if defined(NO_WMISLEADING_INDENTATION) && \ 113 ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \ 114 ${COMPILER_TYPE} == "gcc") 115CWARNFLAGS+= -Wno-misleading-indentation 116.endif # NO_WMISLEADING_INDENTATION 117.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 140000 118NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical 119.endif 120.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 150000 121NO_WDEPRECATED_NON_PROTOTYPE=-Wno-deprecated-non-prototype 122.endif 123.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 100100 124NO_WZERO_LENGTH_BOUNDS= -Wno-zero-length-bounds 125.endif 126.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 110100 127NO_WARRAY_PARAMETER= -Wno-array-parameter 128.endif 129.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100 130NO_WUSE_AFTER_FREE= -Wno-use-after-free 131NO_WDANGLING_POINTER= -Wno-dangling-pointer 132.endif 133.endif # WARNS 134 135.if defined(FORMAT_AUDIT) 136WFORMAT= 1 137.endif # FORMAT_AUDIT 138.if defined(WFORMAT) 139.if ${WFORMAT} > 0 140#CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args 141CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args 142.if ${WARNS} <= 3 143CWARNFLAGS.clang+= -Wno-format-nonliteral 144.endif # WARNS <= 3 145.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no" 146CWARNFLAGS+= -Werror 147.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no" 148.endif # WFORMAT > 0 149.endif # WFORMAT 150.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE}) 151CWARNFLAGS+= -Wno-format 152.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE} 153 154# GCC 155# We should clean up warnings produced with these flags. 156# They were originally added as a quick hack to enable gcc5/6. 157# The base system requires at least GCC 6.4, but some ports 158# use this file with older compilers. Request an exprun 159# before changing these. 160.if ${COMPILER_TYPE} == "gcc" 161# GCC 5.2.0 162.if ${COMPILER_VERSION} >= 50200 163CWARNFLAGS+= -Wno-error=address \ 164 -Wno-error=array-bounds \ 165 -Wno-error=attributes \ 166 -Wno-error=bool-compare \ 167 -Wno-error=cast-align \ 168 -Wno-error=clobbered \ 169 -Wno-error=deprecated-declarations \ 170 -Wno-error=enum-compare \ 171 -Wno-error=extra \ 172 -Wno-error=logical-not-parentheses \ 173 -Wno-error=strict-aliasing \ 174 -Wno-error=uninitialized \ 175 -Wno-error=unused-but-set-variable \ 176 -Wno-error=unused-function \ 177 -Wno-error=unused-value 178.endif 179 180# GCC 6.1.0 181.if ${COMPILER_VERSION} >= 60100 182CWARNFLAGS+= -Wno-error=empty-body \ 183 -Wno-error=maybe-uninitialized \ 184 -Wno-error=nonnull-compare \ 185 -Wno-error=shift-negative-value \ 186 -Wno-error=tautological-compare \ 187 -Wno-error=unused-const-variable 188.endif 189 190# GCC 7.1.0 191.if ${COMPILER_VERSION} >= 70100 192CWARNFLAGS+= -Wno-error=bool-operation \ 193 -Wno-error=deprecated \ 194 -Wno-error=expansion-to-defined \ 195 -Wno-error=format-overflow \ 196 -Wno-error=format-truncation \ 197 -Wno-error=implicit-fallthrough \ 198 -Wno-error=int-in-bool-context \ 199 -Wno-error=memset-elt-size \ 200 -Wno-error=noexcept-type \ 201 -Wno-error=nonnull \ 202 -Wno-error=pointer-compare \ 203 -Wno-error=stringop-overflow 204.endif 205 206# GCC 8.1.0 207.if ${COMPILER_VERSION} >= 80100 208CWARNFLAGS+= -Wno-error=aggressive-loop-optimizations \ 209 -Wno-error=cast-function-type \ 210 -Wno-error=catch-value \ 211 -Wno-error=multistatement-macros \ 212 -Wno-error=restrict \ 213 -Wno-error=sizeof-pointer-memaccess \ 214 -Wno-error=stringop-truncation 215.endif 216 217# GCC 9.2.0 218.if ${COMPILER_VERSION} >= 90200 219.if ${MACHINE_ARCH} == "i386" 220CWARNFLAGS+= -Wno-error=overflow 221.endif 222.endif 223 224# GCC 12.1.0 225.if ${COMPILER_VERSION} >= 120100 226# These warnings are raised by headers in libc++ so are disabled 227# globally for all C++ 228CXXWARNFLAGS+= -Wno-literal-suffix \ 229 -Wno-error=unknown-pragmas 230.endif 231 232# GCC produces false positives for functions that switch on an 233# enum (GCC bug 87950) 234CWARNFLAGS+= -Wno-return-type 235 236# GCC's own arm_neon.h triggers various warnings 237.if ${MACHINE_CPUARCH} == "aarch64" 238CWARNFLAGS+= -Wno-system-headers 239.endif 240.endif # gcc 241 242# How to handle FreeBSD custom printf format specifiers. 243.if ${COMPILER_TYPE} == "clang" || \ 244 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100) 245FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 246.else 247FORMAT_EXTENSIONS= -fformat-extensions 248.endif 249 250.if defined(IGNORE_PRAGMA) 251CWARNFLAGS+= -Wno-unknown-pragmas 252.endif # IGNORE_PRAGMA 253 254# This warning is utter nonsense 255CFLAGS+= -Wno-format-zero-length 256 257.if ${COMPILER_TYPE} == "clang" 258# The headers provided by clang are incompatible with the FreeBSD headers. 259# If the version of clang is not one that has been patched to omit the 260# incompatible headers, we need to compile with -nobuiltininc and add the 261# resource dir to the end of the search paths. This ensures that headers such as 262# immintrin.h are still found but stddef.h, etc. are picked up from FreeBSD. 263# 264# XXX: This is a hack to support complete external installs of clang while 265# we work to synchronize our decleration guards with those in the clang tree. 266.if ${MK_CLANG_BOOTSTRAP:Uno} == "no" && \ 267 ${COMPILER_RESOURCE_DIR} != "unknown" && !defined(BOOTSTRAPPING) 268CFLAGS+=-nobuiltininc -idirafter ${COMPILER_RESOURCE_DIR}/include 269.endif 270.endif 271 272CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3 273.if ${COMPILER_VERSION} < 130000 274CLANG_OPT_SMALL+= -mllvm -simplifycfg-dup-ret 275.endif 276CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false 277CFLAGS.clang+= -Qunused-arguments 278# The libc++ headers use c++11 extensions. These are normally silenced because 279# they are treated as system headers, but we explicitly disable that warning 280# suppression when building the base system to catch bugs in our headers. 281# Eventually we'll want to start building the base system C++ code as C++11, 282# but not yet. 283CXXFLAGS.clang+= -Wno-c++11-extensions 284 285.if ${MK_SSP} != "no" 286# Don't use -Wstack-protector as it breaks world with -Werror. 287SSP_CFLAGS?= -fstack-protector-strong 288CFLAGS+= ${SSP_CFLAGS} 289.endif # SSP 290 291# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is 292# enabled. 293DEBUG_FILES_CFLAGS?= -g -gz=zlib 294 295# Allow user-specified additional warning flags, plus compiler and file 296# specific flag overrides, unless we've overridden this... 297.if ${MK_WARNS} != "no" 298CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}} 299CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}} 300CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}} 301CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}} 302.endif 303 304CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} 305CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}} 306 307AFLAGS+= ${AFLAGS.${.IMPSRC:T}} 308AFLAGS+= ${AFLAGS.${.TARGET:T}} 309ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}} 310ACFLAGS+= ${ACFLAGS.${.TARGET:T}} 311CFLAGS+= ${CFLAGS.${.IMPSRC:T}} 312CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}} 313 314LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}} 315 316# Only allow .TARGET when not using PROGS as it has the same syntax 317# per PROG which is ambiguous with this syntax. This is only needed 318# for PROG_VARS vars. 319# 320# Some directories (currently just clang) also need to disable this since 321# CFLAGS.${COMPILER_TYPE}, CFLAGS.${.IMPSRC:T} and CFLAGS.${.TARGET:T} all live 322# in the same namespace, meaning that, for example, GCC builds of clang pick up 323# CFLAGS.clang via CFLAGS.${.TARGET:T} and thus try to pass Clang-specific 324# flags. Ideally the different sources of CFLAGS would be namespaced to avoid 325# collisions. 326.if !defined(_RECURSING_PROGS) && !defined(NO_TARGET_FLAGS) 327.if ${MK_WARNS} != "no" 328CFLAGS+= ${CWARNFLAGS.${.TARGET:T}} 329.endif 330CFLAGS+= ${CFLAGS.${.TARGET:T}} 331CXXFLAGS+= ${CXXFLAGS.${.TARGET:T}} 332LDFLAGS+= ${LDFLAGS.${.TARGET:T}} 333LDADD+= ${LDADD.${.TARGET:T}} 334LIBADD+= ${LIBADD.${.TARGET:T}} 335.endif 336 337.if defined(SRCTOP) 338# Prevent rebuilding during install to support read-only objdirs. 339.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta) 340CFLAGS+= ERROR-tried-to-rebuild-during-make-install 341.endif 342.endif 343 344# Please keep this if in sync with kern.mk 345.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld") 346# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld". 347.if ${COMPILER_TYPE} == "clang" 348# Note: Clang does not like relative paths for ld so we map ld.lld -> lld. 349.if ${COMPILER_VERSION} >= 120000 350LDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W} 351.else 352LDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W} 353.endif 354.elif ${COMPILER_TYPE} == "gcc" 355# GCC does not support an absolute path for -fuse-ld so we just print this 356# warning instead and let the user add the required symlinks. 357# However, we can avoid this warning if -B is set appropriately (e.g. for 358# CROSS_TOOLCHAIN=...-gcc). 359.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/}) 360.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported 361.endif 362.endif 363.endif 364 365# Tell bmake not to mistake standard targets for things to be searched for 366# or expect to ever be up-to-date. 367PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \ 368 beforelinking build build-tools buildconfig buildfiles \ 369 buildincludes check checkdpadd clean cleandepend cleandir \ 370 cleanobj configure depend distclean distribute exe \ 371 files html includes install installconfig installdirs \ 372 installfiles installincludes lint obj objlink objs objwarn \ 373 realinstall tags whereobj 374 375# we don't want ${PROG} to be PHONY 376.PHONY: ${PHONY_NOTMAIN:N${PROG:U}} 377.NOTMAIN: ${PHONY_NOTMAIN:Nall} 378 379.if ${MK_STAGING} != "no" 380.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean)) 381_SKIP_STAGING?= yes 382.endif 383.if ${_SKIP_STAGING:Uno} == "yes" 384staging stage_libs stage_files stage_as stage_links stage_symlinks: 385.else 386# allow targets like beforeinstall to be leveraged 387DESTDIR= ${STAGE_OBJTOP} 388.export DESTDIR 389 390.if target(beforeinstall) 391.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 392staging: beforeinstall 393.endif 394.endif 395 396# normally only libs and includes are staged 397.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG) 398STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR} 399 400.if !empty(PROG) 401.if defined(PROGNAME) 402STAGE_AS_SETS+= prog 403STAGE_AS_${PROG}= ${PROGNAME} 404stage_as.prog: ${PROG} 405.else 406STAGE_SETS+= prog 407stage_files.prog: ${PROG} 408STAGE_TARGETS+= stage_files 409.endif 410.endif 411.endif 412 413.if !empty(_LIBS) && !defined(INTERNALLIB) 414.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != "" 415STAGE_SETS+= shlib 416STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR} 417STAGE_FILES.shlib+= ${_LIBS:M*.so.*} 418stage_files.shlib: ${_LIBS:M*.so.*} 419.endif 420 421.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld) 422STAGE_AS_SETS+= ldscript 423STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld 424stage_as.ldscript: ${SHLIB_LINK:R}.ld 425STAGE_DIR.ldscript = ${STAGE_LIBDIR} 426STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK} 427NO_SHLIB_LINKS= 428.endif 429 430.if target(stage_files.shlib) 431stage_libs: ${_LIBS} 432.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 433stage_files.shlib: ${SHLIB_NAME}.symbols 434.endif 435.else 436stage_libs: ${_LIBS} 437.endif 438.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 439stage_libs: ${SHLIB_NAME}.symbols 440.endif 441 442.endif 443 444.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes) 445.if !defined(NO_BEFOREBUILD_INCLUDES) 446stage_includes: buildincludes 447beforebuild: stage_includes 448.endif 449.endif 450 451.for t in stage_libs stage_files stage_as 452.if target($t) 453STAGE_TARGETS+= $t 454.endif 455.endfor 456 457.if !empty(STAGE_AS_SETS) 458STAGE_TARGETS+= stage_as 459.endif 460 461.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 462 463.if !empty(LINKS) 464STAGE_TARGETS+= stage_links 465.if ${MAKE_VERSION} < 20131001 466stage_links.links: ${_LIBS} ${PROG} 467.endif 468STAGE_SETS+= links 469STAGE_LINKS.links= ${LINKS} 470.endif 471 472.if !empty(SYMLINKS) 473STAGE_TARGETS+= stage_symlinks 474STAGE_SETS+= links 475STAGE_SYMLINKS.links= ${SYMLINKS} 476.endif 477 478.endif 479 480.include <meta.stage.mk> 481.endif 482.endif 483 484.if defined(META_TARGETS) 485.for _tgt in ${META_TARGETS} 486.if target(${_tgt}) 487${_tgt}: ${META_DEPS} 488.endif 489.endfor 490.endif 491