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 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
13 .include <bsd.compiler.mk>
15 # the default is gnu99 for now
18 .if ${CSTD} == "c89" || ${CSTD} == "c90"
19 CFLAGS+= -std=iso9899:1990
20 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
21 CFLAGS+= -std=iso9899:199409
22 .elif ${CSTD} == "c99"
23 CFLAGS+= -std=iso9899:1999
29 CXXFLAGS+= -std=${CXXSTD}
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.
37 .if ${COMPILER_VERSION} < 50000
41 # -pedantic is problematic because it also imposes namespace restrictions
45 CWARNFLAGS+= -Wsystem-headers
46 .if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
48 .endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
51 CWARNFLAGS+= -Wall -Wno-format-y2k
54 CWARNFLAGS+= -W -Wno-unused-parameter -Wstrict-prototypes\
55 -Wmissing-prototypes -Wpointer-arith
58 CWARNFLAGS+= -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\
60 .if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE})
61 CWARNFLAGS+= -Wcast-align
62 .endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE}
65 CWARNFLAGS+= -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls\
66 -Wold-style-definition
67 .if !defined(NO_WMISSING_VARIABLE_DECLARATIONS)
68 CWARNFLAGS.clang+= -Wmissing-variable-declarations
70 .if !defined(NO_WTHREAD_SAFETY)
71 CWARNFLAGS.clang+= -Wthread-safety
74 .if ${WARNS} >= 2 && ${WARNS} <= 4
75 # XXX Delete -Wuninitialized by default for now -- the compiler doesn't
76 # XXX always get it right.
77 CWARNFLAGS+= -Wno-uninitialized
78 .endif # WARNS >=2 && WARNS <= 4
79 CWARNFLAGS+= -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.
83 CWARNFLAGS.clang+= -Wno-empty-body -Wno-string-plus-int
84 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30400
85 CWARNFLAGS.clang+= -Wno-unused-const-variable
89 CWARNFLAGS.clang+= -Wno-tautological-compare -Wno-unused-value\
90 -Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion
91 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
92 CWARNFLAGS.clang+= -Wno-unused-local-typedef
94 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 40000
95 CWARNFLAGS.clang+= -Wno-address-of-packed-member
97 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100
98 CWARNFLAGS.gcc+= -Wno-address-of-packed-member
100 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \
101 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
102 CWARNFLAGS.clang+= -Wno-atomic-alignment
106 CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
109 CWARNFLAGS.clang+= -Wno-parentheses
111 .if defined(NO_WARRAY_BOUNDS)
112 CWARNFLAGS.clang+= -Wno-array-bounds
113 .endif # NO_WARRAY_BOUNDS
114 .if defined(NO_WMISLEADING_INDENTATION) && \
115 ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
116 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100))
117 CWARNFLAGS+= -Wno-misleading-indentation
118 .endif # NO_WMISLEADING_INDENTATION
121 .if defined(FORMAT_AUDIT)
123 .endif # FORMAT_AUDIT
126 #CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args
127 CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args
129 CWARNFLAGS.clang+= -Wno-format-nonliteral
131 .if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
133 .endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
136 .if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
137 CWARNFLAGS+= -Wno-format
138 .endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
141 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200
142 CWARNFLAGS+= -Wno-error=address \
143 -Wno-error=array-bounds \
144 -Wno-error=attributes \
145 -Wno-error=bool-compare \
146 -Wno-error=cast-align \
147 -Wno-error=clobbered \
148 -Wno-error=deprecated-declarations \
149 -Wno-error=enum-compare \
152 -Wno-error=logical-not-parentheses \
153 -Wno-error=strict-aliasing \
154 -Wno-error=uninitialized \
155 -Wno-error=unused-but-set-variable \
156 -Wno-error=unused-function \
157 -Wno-error=unused-value
161 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100
162 CWARNFLAGS+= -Wno-error=nonnull-compare \
163 -Wno-error=shift-negative-value \
164 -Wno-error=tautological-compare \
165 -Wno-error=unused-const-variable
169 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 70100
170 CWARNFLAGS+= -Wno-error=bool-operation \
171 -Wno-error=deprecated \
172 -Wno-error=expansion-to-defined \
173 -Wno-error=format-overflow \
174 -Wno-error=format-truncation \
175 -Wno-error=implicit-fallthrough \
176 -Wno-error=int-in-bool-context \
177 -Wno-error=memset-elt-size \
178 -Wno-error=noexcept-type \
180 -Wno-error=pointer-compare \
181 -Wno-error=stringop-overflow
185 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100
186 CWARNFLAGS+= -Wno-error=aggressive-loop-optimizations \
187 -Wno-error=cast-function-type \
188 -Wno-error=catch-value \
189 -Wno-error=multistatement-macros \
190 -Wno-error=restrict \
191 -Wno-error=sizeof-pointer-memaccess \
192 -Wno-error=stringop-truncation
195 # How to handle FreeBSD custom printf format specifiers.
196 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
197 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
199 FORMAT_EXTENSIONS= -fformat-extensions
202 .if defined(IGNORE_PRAGMA)
203 CWARNFLAGS+= -Wno-unknown-pragmas
204 .endif # IGNORE_PRAGMA
206 # This warning is utter nonsense
207 CFLAGS+= -Wno-format-zero-length
209 CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\
210 -mllvm -simplifycfg-dup-ret
211 .if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700
212 CLANG_OPT_SMALL+= -mllvm -enable-gvn=false
214 CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
216 CFLAGS.clang+= -Qunused-arguments
217 .if ${MACHINE_CPUARCH} == "sparc64"
218 # Don't emit .cfi directives, since we must use GNU as on sparc64, for now.
219 CFLAGS.clang+= -fno-dwarf2-cfi-asm
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 .if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \
231 (${COMPILER_TYPE} == "gcc" && \
232 (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900))
233 # Don't use -Wstack-protector as it breaks world with -Werror.
234 SSP_CFLAGS?= -fstack-protector-strong
236 SSP_CFLAGS?= -fstack-protector
238 CFLAGS+= ${SSP_CFLAGS}
239 .endif # SSP && !ARM && !MIPS
241 # Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
243 DEBUG_FILES_CFLAGS?= -g
245 # Allow user-specified additional warning flags, plus compiler and file
246 # specific flag overrides, unless we've overridden this...
247 .if ${MK_WARNS} != "no"
248 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
249 CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}}
250 CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
251 CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}}
254 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}}
255 CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}}
257 AFLAGS+= ${AFLAGS.${.IMPSRC:T}}
258 AFLAGS+= ${AFLAGS.${.TARGET:T}}
259 ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}}
260 ACFLAGS+= ${ACFLAGS.${.TARGET:T}}
261 CFLAGS+= ${CFLAGS.${.IMPSRC:T}}
262 CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}}
264 LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}}
266 # Only allow .TARGET when not using PROGS as it has the same syntax
267 # per PROG which is ambiguous with this syntax. This is only needed
268 # for PROG_VARS vars.
269 .if !defined(_RECURSING_PROGS)
270 .if ${MK_WARNS} != "no"
271 CFLAGS+= ${CWARNFLAGS.${.TARGET:T}}
273 CFLAGS+= ${CFLAGS.${.TARGET:T}}
274 CXXFLAGS+= ${CXXFLAGS.${.TARGET:T}}
275 LDFLAGS+= ${LDFLAGS.${.TARGET:T}}
276 LDADD+= ${LDADD.${.TARGET:T}}
277 LIBADD+= ${LIBADD.${.TARGET:T}}
281 # Prevent rebuilding during install to support read-only objdirs.
282 .if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
283 CFLAGS+= ERROR-tried-to-rebuild-during-make-install
287 # Tell bmake not to mistake standard targets for things to be searched for
288 # or expect to ever be up-to-date.
289 PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
290 beforelinking build build-tools buildconfig buildfiles \
291 buildincludes check checkdpadd clean cleandepend cleandir \
292 cleanobj configure depend distclean distribute exe \
293 files html includes install installconfig installdirs \
294 installfiles installincludes lint obj objlink objs objwarn \
295 realinstall tags whereobj
297 # we don't want ${PROG} to be PHONY
298 .PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
299 .NOTMAIN: ${PHONY_NOTMAIN:Nall}
301 .if ${MK_STAGING} != "no"
302 .if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
305 .if ${_SKIP_STAGING:Uno} == "yes"
306 staging stage_libs stage_files stage_as stage_links stage_symlinks:
308 # allow targets like beforeinstall to be leveraged
309 DESTDIR= ${STAGE_OBJTOP}
312 .if target(beforeinstall)
313 .if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
314 staging: beforeinstall
318 # normally only libs and includes are staged
319 .if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
320 STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
323 .if defined(PROGNAME)
325 STAGE_AS_${PROG}= ${PROGNAME}
326 stage_as.prog: ${PROG}
329 stage_files.prog: ${PROG}
330 STAGE_TARGETS+= stage_files
335 .if !empty(_LIBS) && !defined(INTERNALLIB)
336 .if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
338 STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
339 STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
340 stage_files.shlib: ${_LIBS:M*.so.*}
343 .if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
344 STAGE_AS_SETS+= ldscript
345 STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
346 stage_as.ldscript: ${SHLIB_LINK:R}.ld
347 STAGE_DIR.ldscript = ${STAGE_LIBDIR}
348 STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
352 .if target(stage_files.shlib)
354 .if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
355 stage_files.shlib: ${SHLIB_NAME}.symbols
360 .if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
361 stage_libs: ${SHLIB_NAME}.symbols
366 .if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
367 .if !defined(NO_BEFOREBUILD_INCLUDES)
368 stage_includes: buildincludes
369 beforebuild: stage_includes
373 .for t in stage_libs stage_files stage_as
379 .if !empty(STAGE_AS_SETS)
380 STAGE_TARGETS+= stage_as
383 .if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
386 STAGE_TARGETS+= stage_links
387 .if ${MAKE_VERSION} < 20131001
388 stage_links.links: ${_LIBS} ${PROG}
391 STAGE_LINKS.links= ${LINKS}
395 STAGE_TARGETS+= stage_symlinks
397 STAGE_SYMLINKS.links= ${SYMLINKS}
402 .include <meta.stage.mk>
406 .if defined(META_TARGETS)
407 .for _tgt in ${META_TARGETS}
409 ${_tgt}: ${META_DEPS}