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 # -pedantic is problematic because it also imposes namespace restrictions
35 CWARNFLAGS+= -Wsystem-headers
36 .if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
38 .endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
41 CWARNFLAGS+= -Wall -Wno-format-y2k
44 CWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\
45 -Wmissing-prototypes -Wpointer-arith
48 CWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\
50 .if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE})
51 CWARNFLAGS+= -Wcast-align
52 .endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE}
55 CWARNFLAGS+= -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls\
56 -Wold-style-definition
57 .if !defined(NO_WMISSING_VARIABLE_DECLARATIONS)
58 CWARNFLAGS.clang+= -Wmissing-variable-declarations
60 .if !defined(NO_WTHREAD_SAFETY)
61 CWARNFLAGS.clang+= -Wthread-safety
64 .if ${WARNS} >= 2 && ${WARNS} <= 4
65 # XXX Delete -Wuninitialized by default for now -- the compiler doesn't
66 # XXX always get it right.
67 CWARNFLAGS+= -Wno-uninitialized
68 .endif # WARNS >=2 && WARNS <= 4
69 CWARNFLAGS+= -Wno-pointer-sign
70 # Clang has more warnings enabled by default, and when using -Wall, so if WARNS
71 # is set to low values, these have to be disabled explicitly.
73 CWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int
74 CWARNFLAGS.clang+= -Wno-unused-const-variable
77 CWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\
78 -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion
79 CWARNFLAGS.clang+= -Wno-unused-local-typedef
80 CWARNFLAGS.clang+= -Wno-address-of-packed-member
81 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100
82 CWARNFLAGS.gcc+= -Wno-address-of-packed-member
84 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \
85 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
86 CWARNFLAGS.clang+= -Wno-atomic-alignment
90 CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
93 CWARNFLAGS.clang+= -Wno-parentheses
95 .if defined(NO_WARRAY_BOUNDS)
96 CWARNFLAGS.clang+= -Wno-array-bounds
97 .endif # NO_WARRAY_BOUNDS
98 .if defined(NO_WMISLEADING_INDENTATION) && \
99 ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
100 ${COMPILER_TYPE} == "gcc")
101 CWARNFLAGS+= -Wno-misleading-indentation
102 .endif # NO_WMISLEADING_INDENTATION
105 .if defined(FORMAT_AUDIT)
107 .endif # FORMAT_AUDIT
110 #CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args
111 CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args
113 CWARNFLAGS.clang+= -Wno-format-nonliteral
115 .if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
117 .endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
120 .if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
121 CWARNFLAGS+= -Wno-format
122 .endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
125 # We should clean up warnings produced with these flags.
126 # They were originally added as a quick hack to enable gcc5/6.
127 # The base system requires at least GCC 6.4, but some ports
128 # use this file with older compilers. Request an exprun
129 # before changing these.
130 .if ${COMPILER_TYPE} == "gcc"
132 .if ${COMPILER_VERSION} >= 50200
133 CWARNFLAGS+= -Wno-error=address \
134 -Wno-error=array-bounds \
135 -Wno-error=attributes \
136 -Wno-error=bool-compare \
137 -Wno-error=cast-align \
138 -Wno-error=clobbered \
139 -Wno-error=deprecated-declarations \
140 -Wno-error=enum-compare \
143 -Wno-error=logical-not-parentheses \
144 -Wno-error=strict-aliasing \
145 -Wno-error=uninitialized \
146 -Wno-error=unused-but-set-variable \
147 -Wno-error=unused-function \
148 -Wno-error=unused-value
152 .if ${COMPILER_VERSION} >= 60100
153 CWARNFLAGS+= -Wno-error=maybe-uninitialized \
154 -Wno-error=nonnull-compare \
155 -Wno-error=shift-negative-value \
156 -Wno-error=tautological-compare \
157 -Wno-error=unused-const-variable
161 .if ${COMPILER_VERSION} >= 70100
162 CWARNFLAGS+= -Wno-error=bool-operation \
163 -Wno-error=deprecated \
164 -Wno-error=expansion-to-defined \
165 -Wno-error=format-overflow \
166 -Wno-error=format-truncation \
167 -Wno-error=implicit-fallthrough \
168 -Wno-error=int-in-bool-context \
169 -Wno-error=memset-elt-size \
170 -Wno-error=noexcept-type \
172 -Wno-error=pointer-compare \
173 -Wno-error=stringop-overflow
177 .if ${COMPILER_VERSION} >= 80100
178 CWARNFLAGS+= -Wno-error=aggressive-loop-optimizations \
179 -Wno-error=cast-function-type \
180 -Wno-error=catch-value \
181 -Wno-error=multistatement-macros \
182 -Wno-error=restrict \
183 -Wno-error=sizeof-pointer-memaccess \
184 -Wno-error=stringop-truncation
188 # How to handle FreeBSD custom printf format specifiers.
189 .if ${COMPILER_TYPE} == "clang"
190 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
192 FORMAT_EXTENSIONS= -fformat-extensions
195 .if defined(IGNORE_PRAGMA)
196 CWARNFLAGS+= -Wno-unknown-pragmas
197 .endif # IGNORE_PRAGMA
199 # This warning is utter nonsense
200 CFLAGS+= -Wno-format-zero-length
202 .if ${COMPILER_TYPE} == "clang"
203 # The headers provided by clang are incompatible with the FreeBSD headers.
204 # If the version of clang is not one that has been patched to omit the
205 # incompatible headers, we need to compile with -nobuiltininc and add the
206 # resource dir to the end of the search paths. This ensures that headers such as
207 # immintrin.h are still found but stddef.h, etc. are picked up from FreeBSD.
209 # XXX: This is a hack to support complete external installs of clang while
210 # we work to synchronize our decleration guards with those in the clang tree.
211 .if ${MK_CLANG_BOOTSTRAP:Uno} == "no" && \
212 ${COMPILER_RESOURCE_DIR} != "unknown" && !defined(BOOTSTRAPPING)
213 CFLAGS+=-nobuiltininc -idirafter ${COMPILER_RESOURCE_DIR}/include
217 CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\
218 -mllvm -simplifycfg-dup-ret
219 CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
220 CFLAGS.clang+= -Qunused-arguments
221 # The libc++ headers use c++11 extensions. These are normally silenced because
222 # they are treated as system headers, but we explicitly disable that warning
223 # suppression when building the base system to catch bugs in our headers.
224 # Eventually we'll want to start building the base system C++ code as C++11,
226 CXXFLAGS.clang+= -Wno-c++11-extensions
228 .if ${MK_SSP} != "no" && \
229 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
230 # Don't use -Wstack-protector as it breaks world with -Werror.
231 SSP_CFLAGS?= -fstack-protector-strong
232 CFLAGS+= ${SSP_CFLAGS}
233 .endif # SSP && !ARM && !MIPS
235 # Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
237 DEBUG_FILES_CFLAGS?= -g
239 # Allow user-specified additional warning flags, plus compiler and file
240 # specific flag overrides, unless we've overridden this...
241 .if ${MK_WARNS} != "no"
242 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
243 CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}}
244 CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
245 CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}}
248 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}}
249 CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}}
251 AFLAGS+= ${AFLAGS.${.IMPSRC:T}}
252 AFLAGS+= ${AFLAGS.${.TARGET:T}}
253 ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}}
254 ACFLAGS+= ${ACFLAGS.${.TARGET:T}}
255 CFLAGS+= ${CFLAGS.${.IMPSRC:T}}
256 CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}}
258 LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}}
260 # Only allow .TARGET when not using PROGS as it has the same syntax
261 # per PROG which is ambiguous with this syntax. This is only needed
262 # for PROG_VARS vars.
263 .if !defined(_RECURSING_PROGS)
264 .if ${MK_WARNS} != "no"
265 CFLAGS+= ${CWARNFLAGS.${.TARGET:T}}
267 CFLAGS+= ${CFLAGS.${.TARGET:T}}
268 CXXFLAGS+= ${CXXFLAGS.${.TARGET:T}}
269 LDFLAGS+= ${LDFLAGS.${.TARGET:T}}
270 LDADD+= ${LDADD.${.TARGET:T}}
271 LIBADD+= ${LIBADD.${.TARGET:T}}
275 # Prevent rebuilding during install to support read-only objdirs.
276 .if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
277 CFLAGS+= ERROR-tried-to-rebuild-during-make-install
281 # Tell bmake not to mistake standard targets for things to be searched for
282 # or expect to ever be up-to-date.
283 PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
284 beforelinking build build-tools buildconfig buildfiles \
285 buildincludes check checkdpadd clean cleandepend cleandir \
286 cleanobj configure depend distclean distribute exe \
287 files html includes install installconfig installdirs \
288 installfiles installincludes lint obj objlink objs objwarn \
289 realinstall tags whereobj
291 # we don't want ${PROG} to be PHONY
292 .PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
293 .NOTMAIN: ${PHONY_NOTMAIN:Nall}
295 .if ${MK_STAGING} != "no"
296 .if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
299 .if ${_SKIP_STAGING:Uno} == "yes"
300 staging stage_libs stage_files stage_as stage_links stage_symlinks:
302 # allow targets like beforeinstall to be leveraged
303 DESTDIR= ${STAGE_OBJTOP}
306 .if target(beforeinstall)
307 .if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
308 staging: beforeinstall
312 # normally only libs and includes are staged
313 .if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
314 STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
317 .if defined(PROGNAME)
319 STAGE_AS_${PROG}= ${PROGNAME}
320 stage_as.prog: ${PROG}
323 stage_files.prog: ${PROG}
324 STAGE_TARGETS+= stage_files
329 .if !empty(_LIBS) && !defined(INTERNALLIB)
330 .if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
332 STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
333 STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
334 stage_files.shlib: ${_LIBS:M*.so.*}
337 .if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
338 STAGE_AS_SETS+= ldscript
339 STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
340 stage_as.ldscript: ${SHLIB_LINK:R}.ld
341 STAGE_DIR.ldscript = ${STAGE_LIBDIR}
342 STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
346 .if target(stage_files.shlib)
348 .if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
349 stage_files.shlib: ${SHLIB_NAME}.symbols
354 .if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
355 stage_libs: ${SHLIB_NAME}.symbols
360 .if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
361 .if !defined(NO_BEFOREBUILD_INCLUDES)
362 stage_includes: buildincludes
363 beforebuild: stage_includes
367 .for t in stage_libs stage_files stage_as
373 .if !empty(STAGE_AS_SETS)
374 STAGE_TARGETS+= stage_as
377 .if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
380 STAGE_TARGETS+= stage_links
381 .if ${MAKE_VERSION} < 20131001
382 stage_links.links: ${_LIBS} ${PROG}
385 STAGE_LINKS.links= ${LINKS}
389 STAGE_TARGETS+= stage_symlinks
391 STAGE_SYMLINKS.links= ${SYMLINKS}
396 .include <meta.stage.mk>
400 .if defined(META_TARGETS)
401 .for _tgt in ${META_TARGETS}
403 ${_tgt}: ${META_DEPS}