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} == "clang" && ${COMPILER_VERSION} >= 70000 && \
98 ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
99 CWARNFLAGS.clang+= -Wno-atomic-alignment
103 CWARNFLAGS.clang+= -Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
106 CWARNFLAGS.clang+= -Wno-parentheses
108 .if defined(NO_WARRAY_BOUNDS)
109 CWARNFLAGS.clang+= -Wno-array-bounds
110 .endif # NO_WARRAY_BOUNDS
113 .if defined(FORMAT_AUDIT)
115 .endif # FORMAT_AUDIT
118 #CWARNFLAGS+= -Wformat-nonliteral -Wformat-security -Wno-format-extra-args
119 CWARNFLAGS+= -Wformat=2 -Wno-format-extra-args
121 CWARNFLAGS.clang+= -Wno-format-nonliteral
123 .if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
125 .endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
128 .if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
129 CWARNFLAGS+= -Wno-format
130 .endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
133 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200
134 CWARNFLAGS+= -Wno-error=address \
135 -Wno-error=array-bounds \
136 -Wno-error=attributes \
137 -Wno-error=bool-compare \
138 -Wno-error=cast-align \
139 -Wno-error=clobbered \
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_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100
153 CWARNFLAGS+= -Wno-error=misleading-indentation \
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_TYPE} == "gcc" && ${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_TYPE} == "gcc" && ${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
187 # How to handle FreeBSD custom printf format specifiers.
188 .if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
189 FORMAT_EXTENSIONS= -D__printf__=__freebsd_kprintf__
191 FORMAT_EXTENSIONS= -fformat-extensions
194 .if defined(IGNORE_PRAGMA)
195 CWARNFLAGS+= -Wno-unknown-pragmas
196 .endif # IGNORE_PRAGMA
198 # We need this conditional because many places that use it
199 # only enable it for some files with CLFAGS.$FILE+=${CLANG_NO_IAS}.
200 # unconditionally, and can't easily use the CFLAGS.clang=
202 .if ${COMPILER_TYPE} == "clang"
203 CLANG_NO_IAS= -no-integrated-as
205 CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\
206 -mllvm -simplifycfg-dup-ret
207 .if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700
208 CLANG_OPT_SMALL+= -mllvm -enable-gvn=false
210 CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
212 CFLAGS.clang+= -Qunused-arguments
213 .if ${MACHINE_CPUARCH} == "sparc64"
214 # Don't emit .cfi directives, since we must use GNU as on sparc64, for now.
215 CFLAGS.clang+= -fno-dwarf2-cfi-asm
217 # The libc++ headers use c++11 extensions. These are normally silenced because
218 # they are treated as system headers, but we explicitly disable that warning
219 # suppression when building the base system to catch bugs in our headers.
220 # Eventually we'll want to start building the base system C++ code as C++11,
222 CXXFLAGS.clang+= -Wno-c++11-extensions
224 .if ${MK_SSP} != "no" && \
225 ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
226 .if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \
227 (${COMPILER_TYPE} == "gcc" && \
228 (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900))
229 # Don't use -Wstack-protector as it breaks world with -Werror.
230 SSP_CFLAGS?= -fstack-protector-strong
232 SSP_CFLAGS?= -fstack-protector
234 CFLAGS+= ${SSP_CFLAGS}
235 .endif # SSP && !ARM && !MIPS
237 # Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
239 DEBUG_FILES_CFLAGS?= -g
241 # Allow user-specified additional warning flags, plus compiler and file
242 # specific flag overrides, unless we've overridden this...
243 .if ${MK_WARNS} != "no"
244 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
245 CFLAGS+= ${CWARNFLAGS.${.IMPSRC:T}}
246 CXXFLAGS+= ${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
247 CXXFLAGS+= ${CXXWARNFLAGS.${.IMPSRC:T}}
250 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}}
251 CXXFLAGS+= ${CXXFLAGS.${COMPILER_TYPE}}
253 AFLAGS+= ${AFLAGS.${.IMPSRC:T}}
254 ACFLAGS+= ${ACFLAGS.${.IMPSRC:T}}
255 CFLAGS+= ${CFLAGS.${.IMPSRC:T}}
256 CXXFLAGS+= ${CXXFLAGS.${.IMPSRC:T}}
258 LDFLAGS+= ${LDFLAGS.${LINKER_TYPE}}
261 # Prevent rebuilding during install to support read-only objdirs.
262 .if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
263 CFLAGS+= ERROR-tried-to-rebuild-during-make-install
267 # Tell bmake not to mistake standard targets for things to be searched for
268 # or expect to ever be up-to-date.
269 PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
270 beforelinking build build-tools buildconfig buildfiles \
271 buildincludes check checkdpadd clean cleandepend cleandir \
272 cleanobj configure depend distclean distribute exe \
273 files html includes install installconfig installdirs \
274 installfiles installincludes lint obj objlink objs objwarn \
275 realinstall tags whereobj
277 # we don't want ${PROG} to be PHONY
278 .PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
279 .NOTMAIN: ${PHONY_NOTMAIN:Nall}
281 .if ${MK_STAGING} != "no"
282 .if defined(_SKIP_BUILD) || (!make(all) && !make(clean*))
285 .if ${_SKIP_STAGING:Uno} == "yes"
286 staging stage_libs stage_files stage_as stage_links stage_symlinks:
288 # allow targets like beforeinstall to be leveraged
289 DESTDIR= ${STAGE_OBJTOP}
292 .if target(beforeinstall)
293 .if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
294 staging: beforeinstall
298 # normally only libs and includes are staged
299 .if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
300 STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
303 .if defined(PROGNAME)
305 STAGE_AS_${PROG}= ${PROGNAME}
306 stage_as.prog: ${PROG}
309 stage_files.prog: ${PROG}
310 STAGE_TARGETS+= stage_files
315 .if !empty(_LIBS) && !defined(INTERNALLIB)
316 .if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
318 STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
319 STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
320 stage_files.shlib: ${_LIBS:M*.so.*}
323 .if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
324 STAGE_AS_SETS+= ldscript
325 STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
326 stage_as.ldscript: ${SHLIB_LINK:R}.ld
327 STAGE_DIR.ldscript = ${STAGE_LIBDIR}
328 STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
332 .if target(stage_files.shlib)
334 .if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
335 stage_files.shlib: ${SHLIB_NAME}.symbols
340 .if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
341 stage_libs: ${SHLIB_NAME}.symbols
346 .if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
347 .if !defined(NO_BEFOREBUILD_INCLUDES)
348 stage_includes: buildincludes
349 beforebuild: stage_includes
353 .for t in stage_libs stage_files stage_as
359 .if !empty(STAGE_AS_SETS)
360 STAGE_TARGETS+= stage_as
363 .if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
366 STAGE_TARGETS+= stage_links
367 .if ${MAKE_VERSION} < 20131001
368 stage_links.links: ${_LIBS} ${PROG}
371 STAGE_LINKS.links= ${LINKS}
375 STAGE_TARGETS+= stage_symlinks
377 STAGE_SYMLINKS.links= ${SYMLINKS}
382 .include <meta.stage.mk>
386 .if defined(META_TARGETS)
387 .for _tgt in ${META_TARGETS}
389 ${_tgt}: ${META_DEPS}