3 # This file contains common settings used for building FreeBSD
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.
9 # for GCC: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
10 # for clang: https://clang.llvm.org/docs/DiagnosticsReference.html
12 .include <bsd.compiler.mk>
14 # the default is gnu99 for now
17 .if ${CSTD} == "c89" || ${CSTD} == "c90"
18 CFLAGS+= -std=iso9899:1990
19 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
20 CFLAGS+= -std=iso9899:199409
21 .elif ${CSTD} == "c99"
22 CFLAGS+= -std=iso9899:1999
28 CXXFLAGS+= -std=${CXXSTD}
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)
36 WARNS= ${DEFAULTWARNS}
39 # -pedantic is problematic because it also imposes namespace restrictions
43 CWARNFLAGS+= -Wsystem-headers
44 .if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
46 .endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
49 CWARNFLAGS+= -Wall -Wno-format-y2k
52 CWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\
53 -Wmissing-prototypes -Wpointer-arith
56 CWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\
58 .if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE})
59 CWARNFLAGS+= -Wcast-align
60 .endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE}
63 CWARNFLAGS+= -Wchar-subscripts -Wnested-externs \
64 -Wold-style-definition
65 .if !defined(NO_WMISSING_VARIABLE_DECLARATIONS)
66 CWARNFLAGS.clang+= -Wmissing-variable-declarations
68 .if !defined(NO_WTHREAD_SAFETY)
69 CWARNFLAGS.clang+= -Wthread-safety
72 .if ${WARNS} >= 2 && ${WARNS} <= 4
73 # XXX Delete -Wuninitialized by default for now -- the compiler doesn't
74 # XXX always get it right.
75 CWARNFLAGS+= -Wno-uninitialized
76 .endif # WARNS >=2 && WARNS <= 4
77 CWARNFLAGS+= -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.
81 CWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int
82 CWARNFLAGS.clang+= -Wno-unused-const-variable
83 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 130000
84 CWARNFLAGS.clang+= -Wno-error=unused-but-set-variable
88 CWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\
89 -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion
90 CWARNFLAGS.clang+= -Wno-unused-local-typedef
91 CWARNFLAGS.clang+= -Wno-address-of-packed-member
92 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100
93 CWARNFLAGS.gcc+= -Wno-address-of-packed-member
95 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \
96 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
97 CWARNFLAGS.clang+= -Wno-atomic-alignment
101 CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
104 CWARNFLAGS.clang+= -Wno-parentheses
106 .if defined(NO_WARRAY_BOUNDS)
107 CWARNFLAGS.clang+= -Wno-array-bounds
108 .endif # NO_WARRAY_BOUNDS
109 .if defined(NO_WMISLEADING_INDENTATION) && \
110 ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
111 ${COMPILER_TYPE} == "gcc")
112 CWARNFLAGS+= -Wno-misleading-indentation
113 .endif # NO_WMISLEADING_INDENTATION
114 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 140000
115 NO_WBITWISE_INSTEAD_OF_LOGICAL= -Wno-bitwise-instead-of-logical
119 .if defined(FORMAT_AUDIT)
121 .endif # FORMAT_AUDIT
124 #CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args
125 CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args
127 CWARNFLAGS.clang+= -Wno-format-nonliteral
129 .if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
131 .endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
134 .if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
135 CWARNFLAGS+= -Wno-format
136 .endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
139 # We should clean up warnings produced with these flags.
140 # They were originally added as a quick hack to enable gcc5/6.
141 # The base system requires at least GCC 6.4, but some ports
142 # use this file with older compilers. Request an exprun
143 # before changing these.
144 .if ${COMPILER_TYPE} == "gcc"
146 .if ${COMPILER_VERSION} >= 50200
147 CWARNFLAGS+= -Wno-error=address \
148 -Wno-error=array-bounds \
149 -Wno-error=attributes \
150 -Wno-error=bool-compare \
151 -Wno-error=cast-align \
152 -Wno-error=clobbered \
153 -Wno-error=deprecated-declarations \
154 -Wno-error=enum-compare \
156 -Wno-error=logical-not-parentheses \
157 -Wno-error=strict-aliasing \
158 -Wno-error=uninitialized \
159 -Wno-error=unused-but-set-variable \
160 -Wno-error=unused-function \
161 -Wno-error=unused-value
165 .if ${COMPILER_VERSION} >= 60100
166 CWARNFLAGS+= -Wno-error=empty-body \
167 -Wno-error=maybe-uninitialized \
168 -Wno-error=nonnull-compare \
169 -Wno-error=shift-negative-value \
170 -Wno-error=tautological-compare \
171 -Wno-error=unused-const-variable
175 .if ${COMPILER_VERSION} >= 70100
176 CWARNFLAGS+= -Wno-error=bool-operation \
177 -Wno-error=deprecated \
178 -Wno-error=expansion-to-defined \
179 -Wno-error=format-overflow \
180 -Wno-error=format-truncation \
181 -Wno-error=implicit-fallthrough \
182 -Wno-error=int-in-bool-context \
183 -Wno-error=memset-elt-size \
184 -Wno-error=noexcept-type \
186 -Wno-error=pointer-compare \
187 -Wno-error=stringop-overflow
191 .if ${COMPILER_VERSION} >= 80100
192 CWARNFLAGS+= -Wno-error=aggressive-loop-optimizations \
193 -Wno-error=cast-function-type \
194 -Wno-error=catch-value \
195 -Wno-error=multistatement-macros \
196 -Wno-error=restrict \
197 -Wno-error=sizeof-pointer-memaccess \
198 -Wno-error=stringop-truncation
202 .if ${COMPILER_VERSION} >= 90200
203 .if ${MACHINE_ARCH} == "i386"
204 CWARNFLAGS+= -Wno-error=overflow
208 # GCC produces false positives for functions that switch on an
209 # enum (GCC bug 87950)
210 CWARNFLAGS+= -Wno-return-type
212 # GCC's own arm_neon.h triggers various warnings
213 .if ${MACHINE_CPUARCH} == "aarch64"
214 CWARNFLAGS+= -Wno-system-headers
218 # How to handle FreeBSD custom printf format specifiers.
219 .if ${COMPILER_TYPE} == "clang"
220 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
222 FORMAT_EXTENSIONS= -fformat-extensions
225 .if defined(IGNORE_PRAGMA)
226 CWARNFLAGS+= -Wno-unknown-pragmas
227 .endif # IGNORE_PRAGMA
229 # This warning is utter nonsense
230 CFLAGS+= -Wno-format-zero-length
232 .if ${COMPILER_TYPE} == "clang"
233 # The headers provided by clang are incompatible with the FreeBSD headers.
234 # If the version of clang is not one that has been patched to omit the
235 # incompatible headers, we need to compile with -nobuiltininc and add the
236 # resource dir to the end of the search paths. This ensures that headers such as
237 # immintrin.h are still found but stddef.h, etc. are picked up from FreeBSD.
239 # XXX: This is a hack to support complete external installs of clang while
240 # we work to synchronize our decleration guards with those in the clang tree.
241 .if ${MK_CLANG_BOOTSTRAP:Uno} == "no" && \
242 ${COMPILER_RESOURCE_DIR} != "unknown" && !defined(BOOTSTRAPPING)
243 CFLAGS+=-nobuiltininc -idirafter ${COMPILER_RESOURCE_DIR}/include
247 CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3
248 .if ${COMPILER_VERSION} < 130000
249 CLANG_OPT_SMALL+= -mllvm -simplifycfg-dup-ret
251 CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
252 CFLAGS.clang+= -Qunused-arguments
253 # The libc++ headers use c++11 extensions. These are normally silenced because
254 # they are treated as system headers, but we explicitly disable that warning
255 # suppression when building the base system to catch bugs in our headers.
256 # Eventually we'll want to start building the base system C++ code as C++11,
258 CXXFLAGS.clang+= -Wno-c++11-extensions
260 .if ${MK_SSP} != "no"
261 # Don't use -Wstack-protector as it breaks world with -Werror.
262 SSP_CFLAGS?= -fstack-protector-strong
263 CFLAGS+= ${SSP_CFLAGS}
266 # Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
268 DEBUG_FILES_CFLAGS?= -g -gz=zlib
270 # Allow user-specified additional warning flags, plus compiler and file
271 # specific flag overrides, unless we've overridden this...
272 .if ${MK_WARNS} != "no"
273 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
274 CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}}
275 CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
276 CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}}
279 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}}
280 CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}}
282 AFLAGS+= ${AFLAGS.${.IMPSRC:T}}
283 AFLAGS+= ${AFLAGS.${.TARGET:T}}
284 ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}}
285 ACFLAGS+= ${ACFLAGS.${.TARGET:T}}
286 CFLAGS+= ${CFLAGS.${.IMPSRC:T}}
287 CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}}
289 LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}}
291 # Only allow .TARGET when not using PROGS as it has the same syntax
292 # per PROG which is ambiguous with this syntax. This is only needed
293 # for PROG_VARS vars.
295 # Some directories (currently just clang) also need to disable this since
296 # CFLAGS.${COMPILER_TYPE}, CFLAGS.${.IMPSRC:T} and CFLAGS.${.TARGET:T} all live
297 # in the same namespace, meaning that, for example, GCC builds of clang pick up
298 # CFLAGS.clang via CFLAGS.${.TARGET:T} and thus try to pass Clang-specific
299 # flags. Ideally the different sources of CFLAGS would be namespaced to avoid
301 .if !defined(_RECURSING_PROGS) && !defined(NO_TARGET_FLAGS)
302 .if ${MK_WARNS} != "no"
303 CFLAGS+= ${CWARNFLAGS.${.TARGET:T}}
305 CFLAGS+= ${CFLAGS.${.TARGET:T}}
306 CXXFLAGS+= ${CXXFLAGS.${.TARGET:T}}
307 LDFLAGS+= ${LDFLAGS.${.TARGET:T}}
308 LDADD+= ${LDADD.${.TARGET:T}}
309 LIBADD+= ${LIBADD.${.TARGET:T}}
313 # Prevent rebuilding during install to support read-only objdirs.
314 .if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
315 CFLAGS+= ERROR-tried-to-rebuild-during-make-install
319 # Please keep this if in sync with kern.mk
320 .if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
321 # Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
322 .if ${COMPILER_TYPE} == "clang"
323 # Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
324 .if ${COMPILER_VERSION} >= 120000
325 LDFLAGS+= --ld-path=${LD:[1]:S/^ld.//1W}
327 LDFLAGS+= -fuse-ld=${LD:[1]:S/^ld.//1W}
329 .elif ${COMPILER_TYPE} == "gcc"
330 # GCC does not support an absolute path for -fuse-ld so we just print this
331 # warning instead and let the user add the required symlinks.
332 # However, we can avoid this warning if -B is set appropriately (e.g. for
333 # CROSS_TOOLCHAIN=...-gcc).
334 .if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
335 .warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
340 # Tell bmake not to mistake standard targets for things to be searched for
341 # or expect to ever be up-to-date.
342 PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
343 beforelinking build build-tools buildconfig buildfiles \
344 buildincludes check checkdpadd clean cleandepend cleandir \
345 cleanobj configure depend distclean distribute exe \
346 files html includes install installconfig installdirs \
347 installfiles installincludes lint obj objlink objs objwarn \
348 realinstall tags whereobj
350 # we don't want ${PROG} to be PHONY
351 .PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
352 .NOTMAIN: ${PHONY_NOTMAIN:Nall}
354 .if ${MK_STAGING} != "no"
355 .if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
358 .if ${_SKIP_STAGING:Uno} == "yes"
359 staging stage_libs stage_files stage_as stage_links stage_symlinks:
361 # allow targets like beforeinstall to be leveraged
362 DESTDIR= ${STAGE_OBJTOP}
365 .if target(beforeinstall)
366 .if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
367 staging: beforeinstall
371 # normally only libs and includes are staged
372 .if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
373 STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
376 .if defined(PROGNAME)
378 STAGE_AS_${PROG}= ${PROGNAME}
379 stage_as.prog: ${PROG}
382 stage_files.prog: ${PROG}
383 STAGE_TARGETS+= stage_files
388 .if !empty(_LIBS) && !defined(INTERNALLIB)
389 .if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
391 STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
392 STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
393 stage_files.shlib: ${_LIBS:M*.so.*}
396 .if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
397 STAGE_AS_SETS+= ldscript
398 STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
399 stage_as.ldscript: ${SHLIB_LINK:R}.ld
400 STAGE_DIR.ldscript = ${STAGE_LIBDIR}
401 STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
405 .if target(stage_files.shlib)
407 .if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
408 stage_files.shlib: ${SHLIB_NAME}.symbols
413 .if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
414 stage_libs: ${SHLIB_NAME}.symbols
419 .if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
420 .if !defined(NO_BEFOREBUILD_INCLUDES)
421 stage_includes: buildincludes
422 beforebuild: stage_includes
426 .for t in stage_libs stage_files stage_as
432 .if !empty(STAGE_AS_SETS)
433 STAGE_TARGETS+= stage_as
436 .if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
439 STAGE_TARGETS+= stage_links
440 .if ${MAKE_VERSION} < 20131001
441 stage_links.links: ${_LIBS} ${PROG}
444 STAGE_LINKS.links= ${LINKS}
448 STAGE_TARGETS+= stage_symlinks
450 STAGE_SYMLINKS.links= ${SYMLINKS}
455 .include <meta.stage.mk>
459 .if defined(META_TARGETS)
460 .for _tgt in ${META_TARGETS}
462 ${_tgt}: ${META_DEPS}