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