1# $FreeBSD: stable/12/share/mk/bsd.sys.mk 372989 2023-03-22 22:30:10Z jhb $ 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 4.2.1 GCC: http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Warning-Options.html 10# for current GCC: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 11# for clang: https://clang.llvm.org/docs/DiagnosticsReference.html 12 13.include <bsd.compiler.mk> 14 15# the default is gnu99 for now 16CSTD?= gnu99 17 18.if ${CSTD} == "c89" || ${CSTD} == "c90" 19CFLAGS+= -std=iso9899:1990 20.elif ${CSTD} == "c94" || ${CSTD} == "c95" 21CFLAGS+= -std=iso9899:199409 22.elif ${CSTD} == "c99" 23CFLAGS+= -std=iso9899:1999 24.else # CSTD 25CFLAGS+= -std=${CSTD} 26.endif # CSTD 27 28.if !empty(CXXSTD) 29CXXFLAGS+= -std=${CXXSTD} 30.endif 31 32# 33# Turn off -Werror for gcc 4.2.1. The compiler is on the glide path out of the 34# system, and any warnings specific to it are no longer relevant as there are 35# too many false positives. 36# 37.if ${COMPILER_VERSION} < 50000 38NO_WERROR.gcc= yes 39.endif 40 41# -pedantic is problematic because it also imposes namespace restrictions 42#CFLAGS+= -pedantic 43.if defined(WARNS) 44.if ${WARNS} >= 1 45CWARNFLAGS+= -Wsystem-headers 46.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE}) 47CWARNFLAGS+= -Werror 48.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE} 49.endif # WARNS >= 1 50.if ${WARNS} >= 2 51CWARNFLAGS+= -Wall -Wno-format-y2k 52.endif # WARNS >= 2 53.if ${WARNS} >= 3 54CWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\ 55 -Wmissing-prototypes -Wpointer-arith 56.endif # WARNS >= 3 57.if ${WARNS} >= 4 58CWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\ 59 -Wunused-parameter 60.if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE}) 61CWARNFLAGS+= -Wcast-align 62.endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE} 63.endif # WARNS >= 4 64.if ${WARNS} >= 6 65CWARNFLAGS+= -Wchar-subscripts -Winline -Wnested-externs \ 66 -Wold-style-definition 67.if !defined(NO_WMISSING_VARIABLE_DECLARATIONS) 68CWARNFLAGS.clang+= -Wmissing-variable-declarations 69.endif 70.if !defined(NO_WTHREAD_SAFETY) 71CWARNFLAGS.clang+= -Wthread-safety 72.endif 73.endif # WARNS >= 6 74.if ${WARNS} >= 2 && ${WARNS} <= 4 75# XXX Delete -Wuninitialized by default for now -- the compiler doesn't 76# XXX always get it right. 77CWARNFLAGS+= -Wno-uninitialized 78.endif # WARNS >=2 && WARNS <= 4 79CWARNFLAGS+= -Wno-pointer-sign 80# Clang has more warnings enabled by default, and when using -Wall, so if WARNS 81# is set to low values, these have to be disabled explicitly. 82.if ${WARNS} <= 6 83CWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int 84.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30400 85CWARNFLAGS.clang+= -Wno-unused-const-variable 86.endif 87.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 130000 88CWARNFLAGS.clang+= -Wno-error=unused-but-set-variable 89.endif 90.endif # WARNS <= 6 91.if ${WARNS} <= 3 92CWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\ 93 -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion 94.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600 95CWARNFLAGS.clang+= -Wno-unused-local-typedef 96.endif 97.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 40000 98CWARNFLAGS.clang+= -Wno-address-of-packed-member 99.endif 100.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100 101CWARNFLAGS.gcc+= -Wno-address-of-packed-member 102.endif 103.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \ 104 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*} 105CWARNFLAGS.clang+= -Wno-atomic-alignment 106.endif 107.endif # WARNS <= 3 108.if ${WARNS} <= 2 109CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter 110.endif # WARNS <= 2 111.if ${WARNS} <= 1 112CWARNFLAGS.clang+= -Wno-parentheses 113.endif # WARNS <= 1 114.if defined(NO_WARRAY_BOUNDS) 115CWARNFLAGS.clang+= -Wno-array-bounds 116.endif # NO_WARRAY_BOUNDS 117.if defined(NO_WMISLEADING_INDENTATION) && \ 118 ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \ 119 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100)) 120CWARNFLAGS+= -Wno-misleading-indentation 121.endif # NO_WMISLEADING_INDENTATION 122.endif # WARNS 123 124.if defined(FORMAT_AUDIT) 125WFORMAT= 1 126.endif # FORMAT_AUDIT 127.if defined(WFORMAT) 128.if ${WFORMAT} > 0 129#CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args 130CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args 131.if ${WARNS} <= 3 132CWARNFLAGS.clang+= -Wno-format-nonliteral 133.endif # WARNS <= 3 134.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE}) 135CWARNFLAGS+= -Werror 136.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE} 137.endif # WFORMAT > 0 138.endif # WFORMAT 139.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE}) 140CWARNFLAGS+= -Wno-format 141.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE} 142 143# GCC 5.2.0 144.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200 145CWARNFLAGS+= -Wno-error=address \ 146 -Wno-error=array-bounds \ 147 -Wno-error=attributes \ 148 -Wno-error=bool-compare \ 149 -Wno-error=cast-align \ 150 -Wno-error=clobbered \ 151 -Wno-error=deprecated-declarations \ 152 -Wno-error=enum-compare \ 153 -Wno-error=extra \ 154 -Wno-error=inline \ 155 -Wno-error=logical-not-parentheses \ 156 -Wno-error=strict-aliasing \ 157 -Wno-error=uninitialized \ 158 -Wno-error=unused-but-set-variable \ 159 -Wno-error=unused-function \ 160 -Wno-error=unused-value 161.endif 162 163# GCC 6.1.0 164.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100 165CWARNFLAGS+= -Wno-error=empty-body \ 166 -Wno-error=maybe-uninitialized \ 167 -Wno-error=nonnull-compare \ 168 -Wno-error=shift-negative-value \ 169 -Wno-error=tautological-compare \ 170 -Wno-error=unused-const-variable 171.endif 172 173# GCC 7.1.0 174.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 70100 175CWARNFLAGS+= -Wno-error=bool-operation \ 176 -Wno-error=deprecated \ 177 -Wno-error=expansion-to-defined \ 178 -Wno-error=format-overflow \ 179 -Wno-error=format-truncation \ 180 -Wno-error=implicit-fallthrough \ 181 -Wno-error=int-in-bool-context \ 182 -Wno-error=memset-elt-size \ 183 -Wno-error=noexcept-type \ 184 -Wno-error=nonnull \ 185 -Wno-error=pointer-compare \ 186 -Wno-error=stringop-overflow 187.endif 188 189# GCC 8.1.0 190.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100 191CWARNFLAGS+= -Wno-error=aggressive-loop-optimizations \ 192 -Wno-error=cast-function-type \ 193 -Wno-error=catch-value \ 194 -Wno-error=multistatement-macros \ 195 -Wno-error=restrict \ 196 -Wno-error=sizeof-pointer-memaccess \ 197 -Wno-error=stringop-truncation 198.endif 199 200.if ${COMPILER_TYPE} == "gcc" 201# GCC produces false positives for functions that switch on an 202# enum (GCC bug 87950) 203CWARNFLAGS+= -Wno-return-type 204.endif 205 206# How to handle FreeBSD custom printf format specifiers. 207.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600 208FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__ 209.else 210FORMAT_EXTENSIONS= -fformat-extensions 211.endif 212 213.if defined(IGNORE_PRAGMA) 214CWARNFLAGS+= -Wno-unknown-pragmas 215.endif # IGNORE_PRAGMA 216 217# This warning is utter nonsense 218CFLAGS+= -Wno-format-zero-length 219 220CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3 221.if ${COMPILER_VERSION} < 130000 222CLANG_OPT_SMALL+= -mllvm -simplifycfg-dup-ret 223.endif 224.if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700 225CLANG_OPT_SMALL+= -mllvm -enable-gvn=false 226.else 227CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false 228.endif 229CFLAGS.clang+= -Qunused-arguments 230.if ${MACHINE_CPUARCH} == "sparc64" 231# Don't emit .cfi directives, since we must use GNU as on sparc64, for now. 232CFLAGS.clang+= -fno-dwarf2-cfi-asm 233.endif # SPARC64 234# The libc++ headers use c++11 extensions. These are normally silenced because 235# they are treated as system headers, but we explicitly disable that warning 236# suppression when building the base system to catch bugs in our headers. 237# Eventually we'll want to start building the base system C++ code as C++11, 238# but not yet. 239CXXFLAGS.clang+= -Wno-c++11-extensions 240 241.if ${MK_SSP} != "no" && \ 242 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips" 243.if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \ 244 (${COMPILER_TYPE} == "gcc" && \ 245 (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900)) 246# Don't use -Wstack-protector as it breaks world with -Werror. 247SSP_CFLAGS?= -fstack-protector-strong 248.else 249SSP_CFLAGS?= -fstack-protector 250.endif 251CFLAGS+= ${SSP_CFLAGS} 252.endif # SSP && !ARM && !MIPS 253 254# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is 255# enabled. 256DEBUG_FILES_CFLAGS?= -g 257 258# Allow user-specified additional warning flags, plus compiler and file 259# specific flag overrides, unless we've overridden this... 260.if ${MK_WARNS} != "no" 261CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}} 262CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}} 263CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}} 264CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}} 265.endif 266 267CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} 268CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}} 269 270AFLAGS+= ${AFLAGS.${.IMPSRC:T}} 271AFLAGS+= ${AFLAGS.${.TARGET:T}} 272ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}} 273ACFLAGS+= ${ACFLAGS.${.TARGET:T}} 274CFLAGS+= ${CFLAGS.${.IMPSRC:T}} 275CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}} 276 277LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}} 278 279# Only allow .TARGET when not using PROGS as it has the same syntax 280# per PROG which is ambiguous with this syntax. This is only needed 281# for PROG_VARS vars. 282# 283# Some directories (currently just clang) also need to disable this since 284# CFLAGS.${COMPILER_TYPE}, CFLAGS.${.IMPSRC:T} and CFLAGS.${.TARGET:T} all live 285# in the same namespace, meaning that, for example, GCC builds of clang pick up 286# CFLAGS.clang via CFLAGS.${.TARGET:T} and thus try to pass Clang-specific 287# flags. Ideally the different sources of CFLAGS would be namespaced to avoid 288# collisions. 289.if !defined(_RECURSING_PROGS) && !defined(NO_TARGET_FLAGS) 290.if ${MK_WARNS} != "no" 291CFLAGS+= ${CWARNFLAGS.${.TARGET:T}} 292.endif 293CFLAGS+= ${CFLAGS.${.TARGET:T}} 294CXXFLAGS+= ${CXXFLAGS.${.TARGET:T}} 295LDFLAGS+= ${LDFLAGS.${.TARGET:T}} 296LDADD+= ${LDADD.${.TARGET:T}} 297LIBADD+= ${LIBADD.${.TARGET:T}} 298.endif 299 300.if defined(SRCTOP) 301# Prevent rebuilding during install to support read-only objdirs. 302.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta) 303CFLAGS+= ERROR-tried-to-rebuild-during-make-install 304.endif 305.endif 306 307# Tell bmake not to mistake standard targets for things to be searched for 308# or expect to ever be up-to-date. 309PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \ 310 beforelinking build build-tools buildconfig buildfiles \ 311 buildincludes check checkdpadd clean cleandepend cleandir \ 312 cleanobj configure depend distclean distribute exe \ 313 files html includes install installconfig installdirs \ 314 installfiles installincludes lint obj objlink objs objwarn \ 315 realinstall tags whereobj 316 317# we don't want ${PROG} to be PHONY 318.PHONY: ${PHONY_NOTMAIN:N${PROG:U}} 319.NOTMAIN: ${PHONY_NOTMAIN:Nall} 320 321.if ${MK_STAGING} != "no" 322.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean)) 323_SKIP_STAGING?= yes 324.endif 325.if ${_SKIP_STAGING:Uno} == "yes" 326staging stage_libs stage_files stage_as stage_links stage_symlinks: 327.else 328# allow targets like beforeinstall to be leveraged 329DESTDIR= ${STAGE_OBJTOP} 330.export DESTDIR 331 332.if target(beforeinstall) 333.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 334staging: beforeinstall 335.endif 336.endif 337 338# normally only libs and includes are staged 339.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG) 340STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR} 341 342.if !empty(PROG) 343.if defined(PROGNAME) 344STAGE_AS_SETS+= prog 345STAGE_AS_${PROG}= ${PROGNAME} 346stage_as.prog: ${PROG} 347.else 348STAGE_SETS+= prog 349stage_files.prog: ${PROG} 350STAGE_TARGETS+= stage_files 351.endif 352.endif 353.endif 354 355.if !empty(_LIBS) && !defined(INTERNALLIB) 356.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != "" 357STAGE_SETS+= shlib 358STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR} 359STAGE_FILES.shlib+= ${_LIBS:M*.so.*} 360stage_files.shlib: ${_LIBS:M*.so.*} 361.endif 362 363.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld) 364STAGE_AS_SETS+= ldscript 365STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld 366stage_as.ldscript: ${SHLIB_LINK:R}.ld 367STAGE_DIR.ldscript = ${STAGE_LIBDIR} 368STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK} 369NO_SHLIB_LINKS= 370.endif 371 372.if target(stage_files.shlib) 373stage_libs: ${_LIBS} 374.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 375stage_files.shlib: ${SHLIB_NAME}.symbols 376.endif 377.else 378stage_libs: ${_LIBS} 379.endif 380.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols) 381stage_libs: ${SHLIB_NAME}.symbols 382.endif 383 384.endif 385 386.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes) 387.if !defined(NO_BEFOREBUILD_INCLUDES) 388stage_includes: buildincludes 389beforebuild: stage_includes 390.endif 391.endif 392 393.for t in stage_libs stage_files stage_as 394.if target($t) 395STAGE_TARGETS+= $t 396.endif 397.endfor 398 399.if !empty(STAGE_AS_SETS) 400STAGE_TARGETS+= stage_as 401.endif 402 403.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)) 404 405.if !empty(LINKS) 406STAGE_TARGETS+= stage_links 407.if ${MAKE_VERSION} < 20131001 408stage_links.links: ${_LIBS} ${PROG} 409.endif 410STAGE_SETS+= links 411STAGE_LINKS.links= ${LINKS} 412.endif 413 414.if !empty(SYMLINKS) 415STAGE_TARGETS+= stage_symlinks 416STAGE_SETS+= links 417STAGE_SYMLINKS.links= ${SYMLINKS} 418.endif 419 420.endif 421 422.include <meta.stage.mk> 423.endif 424.endif 425 426.if defined(META_TARGETS) 427.for _tgt in ${META_TARGETS} 428.if target(${_tgt}) 429${_tgt}: ${META_DEPS} 430.endif 431.endfor 432.endif 433