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