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