1# $FreeBSD$
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 -Wredundant-decls\
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.endif # WARNS <= 6
88.if ${WARNS} <= 3
89CWARNFLAGS.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
92CWARNFLAGS.clang+=	-Wno-unused-local-typedef
93.endif
94.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 40000
95CWARNFLAGS.clang+=	-Wno-address-of-packed-member
96.endif
97.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \
98    ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
99CWARNFLAGS.clang+=	-Wno-atomic-alignment
100.endif
101.endif # WARNS <= 3
102.if ${WARNS} <= 2
103CWARNFLAGS.clang+=	-Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
104.endif # WARNS <= 2
105.if ${WARNS} <= 1
106CWARNFLAGS.clang+=	-Wno-parentheses
107.endif # WARNS <= 1
108.if defined(NO_WARRAY_BOUNDS)
109CWARNFLAGS.clang+=	-Wno-array-bounds
110.endif # NO_WARRAY_BOUNDS
111.if defined(NO_WMISLEADING_INDENTATION) && \
112    ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
113     (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100))
114CWARNFLAGS+=		-Wno-misleading-indentation
115.endif # NO_WMISLEADING_INDENTATION
116.endif # WARNS
117
118.if defined(FORMAT_AUDIT)
119WFORMAT=	1
120.endif # FORMAT_AUDIT
121.if defined(WFORMAT)
122.if ${WFORMAT} > 0
123#CWARNFLAGS+=	-Wformat-nonliteral -Wformat-security -Wno-format-extra-args
124CWARNFLAGS+=	-Wformat=2 -Wno-format-extra-args
125.if ${WARNS} <= 3
126CWARNFLAGS.clang+=	-Wno-format-nonliteral
127.endif # WARNS <= 3
128.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
129CWARNFLAGS+=	-Werror
130.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
131.endif # WFORMAT > 0
132.endif # WFORMAT
133.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
134CWARNFLAGS+=	-Wno-format
135.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
136
137# GCC 5.2.0
138.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200
139CWARNFLAGS+=	-Wno-error=address			\
140		-Wno-error=array-bounds			\
141		-Wno-error=attributes			\
142		-Wno-error=bool-compare			\
143		-Wno-error=cast-align			\
144		-Wno-error=clobbered			\
145		-Wno-error=deprecated-declarations	\
146		-Wno-error=enum-compare			\
147		-Wno-error=extra			\
148		-Wno-error=inline			\
149		-Wno-error=logical-not-parentheses	\
150		-Wno-error=strict-aliasing		\
151		-Wno-error=uninitialized		\
152		-Wno-error=unused-but-set-variable	\
153		-Wno-error=unused-function		\
154		-Wno-error=unused-value
155.endif
156
157# GCC 6.1.0
158.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100
159CWARNFLAGS+=	-Wno-error=empty-body			\
160		-Wno-error=nonnull-compare		\
161		-Wno-error=shift-negative-value		\
162		-Wno-error=tautological-compare		\
163		-Wno-error=unused-const-variable
164.endif
165
166# GCC 7.1.0
167.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 70100
168CWARNFLAGS+=	-Wno-error=bool-operation		\
169		-Wno-error=deprecated			\
170		-Wno-error=expansion-to-defined		\
171		-Wno-error=format-overflow		\
172		-Wno-error=format-truncation		\
173		-Wno-error=implicit-fallthrough		\
174		-Wno-error=int-in-bool-context		\
175		-Wno-error=memset-elt-size		\
176		-Wno-error=noexcept-type		\
177		-Wno-error=nonnull			\
178		-Wno-error=pointer-compare		\
179		-Wno-error=stringop-overflow
180.endif
181
182# GCC 8.1.0
183.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100
184CWARNFLAGS+=	-Wno-error=aggressive-loop-optimizations	\
185		-Wno-error=cast-function-type			\
186		-Wno-error=catch-value				\
187		-Wno-error=multistatement-macros		\
188		-Wno-error=restrict				\
189		-Wno-error=sizeof-pointer-memaccess		\
190		-Wno-error=stringop-truncation
191.endif
192
193# How to handle FreeBSD custom printf format specifiers.
194.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
195FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
196.else
197FORMAT_EXTENSIONS=	-fformat-extensions
198.endif
199
200.if defined(IGNORE_PRAGMA)
201CWARNFLAGS+=	-Wno-unknown-pragmas
202.endif # IGNORE_PRAGMA
203
204# This warning is utter nonsense
205CFLAGS+=	-Wno-format-zero-length
206
207# We need this conditional because many places that use it
208# only enable it for some files with CLFAGS.$FILE+=${CLANG_NO_IAS}.
209# unconditionally, and can't easily use the CFLAGS.clang=
210# mechanism.
211.if ${COMPILER_TYPE} == "clang"
212CLANG_NO_IAS=	 -no-integrated-as
213.endif
214CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\
215		 -mllvm -simplifycfg-dup-ret
216.if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700
217CLANG_OPT_SMALL+= -mllvm -enable-gvn=false
218.else
219CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
220.endif
221CFLAGS.clang+=	 -Qunused-arguments
222.if ${MACHINE_CPUARCH} == "sparc64"
223# Don't emit .cfi directives, since we must use GNU as on sparc64, for now.
224CFLAGS.clang+=	 -fno-dwarf2-cfi-asm
225.endif # SPARC64
226# The libc++ headers use c++11 extensions.  These are normally silenced because
227# they are treated as system headers, but we explicitly disable that warning
228# suppression when building the base system to catch bugs in our headers.
229# Eventually we'll want to start building the base system C++ code as C++11,
230# but not yet.
231CXXFLAGS.clang+=	 -Wno-c++11-extensions
232
233.if ${MK_SSP} != "no" && \
234    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
235.if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \
236    (${COMPILER_TYPE} == "gcc" && \
237     (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900))
238# Don't use -Wstack-protector as it breaks world with -Werror.
239SSP_CFLAGS?=	-fstack-protector-strong
240.else
241SSP_CFLAGS?=	-fstack-protector
242.endif
243CFLAGS+=	${SSP_CFLAGS}
244.endif # SSP && !ARM && !MIPS
245
246# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
247# enabled.
248DEBUG_FILES_CFLAGS?= -g
249
250# Allow user-specified additional warning flags, plus compiler and file
251# specific flag overrides, unless we've overridden this...
252.if ${MK_WARNS} != "no"
253CFLAGS+=	${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
254CFLAGS+=	${CWARNFLAGS.${.IMPSRC:T}}
255CXXFLAGS+=	${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
256CXXFLAGS+=	${CXXWARNFLAGS.${.IMPSRC:T}}
257.endif
258
259CFLAGS+=	 ${CFLAGS.${COMPILER_TYPE}}
260CXXFLAGS+=	 ${CXXFLAGS.${COMPILER_TYPE}}
261
262AFLAGS+=	${AFLAGS.${.IMPSRC:T}}
263ACFLAGS+=	${ACFLAGS.${.IMPSRC:T}}
264CFLAGS+=	${CFLAGS.${.IMPSRC:T}}
265CXXFLAGS+=	${CXXFLAGS.${.IMPSRC:T}}
266
267LDFLAGS+=	${LDFLAGS.${LINKER_TYPE}}
268
269.if defined(SRCTOP)
270# Prevent rebuilding during install to support read-only objdirs.
271.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
272CFLAGS+=	ERROR-tried-to-rebuild-during-make-install
273.endif
274.endif
275
276# Tell bmake not to mistake standard targets for things to be searched for
277# or expect to ever be up-to-date.
278PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
279		beforelinking build build-tools buildconfig buildfiles \
280		buildincludes check checkdpadd clean cleandepend cleandir \
281		cleanobj configure depend distclean distribute exe \
282		files html includes install installconfig installdirs \
283		installfiles installincludes lint obj objlink objs objwarn \
284		realinstall tags whereobj
285
286# we don't want ${PROG} to be PHONY
287.PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
288.NOTMAIN: ${PHONY_NOTMAIN:Nall}
289
290.if ${MK_STAGING} != "no"
291.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
292_SKIP_STAGING?= yes
293.endif
294.if ${_SKIP_STAGING:Uno} == "yes"
295staging stage_libs stage_files stage_as stage_links stage_symlinks:
296.else
297# allow targets like beforeinstall to be leveraged
298DESTDIR= ${STAGE_OBJTOP}
299.export DESTDIR
300
301.if target(beforeinstall)
302.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
303staging: beforeinstall
304.endif
305.endif
306
307# normally only libs and includes are staged
308.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
309STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
310
311.if !empty(PROG)
312.if defined(PROGNAME)
313STAGE_AS_SETS+= prog
314STAGE_AS_${PROG}= ${PROGNAME}
315stage_as.prog: ${PROG}
316.else
317STAGE_SETS+= prog
318stage_files.prog: ${PROG}
319STAGE_TARGETS+= stage_files
320.endif
321.endif
322.endif
323
324.if !empty(_LIBS) && !defined(INTERNALLIB)
325.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
326STAGE_SETS+= shlib
327STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
328STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
329stage_files.shlib: ${_LIBS:M*.so.*}
330.endif
331
332.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
333STAGE_AS_SETS+= ldscript
334STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
335stage_as.ldscript: ${SHLIB_LINK:R}.ld
336STAGE_DIR.ldscript = ${STAGE_LIBDIR}
337STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
338NO_SHLIB_LINKS=
339.endif
340
341.if target(stage_files.shlib)
342stage_libs: ${_LIBS}
343.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
344stage_files.shlib: ${SHLIB_NAME}.symbols
345.endif
346.else
347stage_libs: ${_LIBS}
348.endif
349.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
350stage_libs: ${SHLIB_NAME}.symbols
351.endif
352
353.endif
354
355.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
356.if !defined(NO_BEFOREBUILD_INCLUDES)
357stage_includes: buildincludes
358beforebuild: stage_includes
359.endif
360.endif
361
362.for t in stage_libs stage_files stage_as
363.if target($t)
364STAGE_TARGETS+= $t
365.endif
366.endfor
367
368.if !empty(STAGE_AS_SETS)
369STAGE_TARGETS+= stage_as
370.endif
371
372.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
373
374.if !empty(LINKS)
375STAGE_TARGETS+= stage_links
376.if ${MAKE_VERSION} < 20131001
377stage_links.links: ${_LIBS} ${PROG}
378.endif
379STAGE_SETS+= links
380STAGE_LINKS.links= ${LINKS}
381.endif
382
383.if !empty(SYMLINKS)
384STAGE_TARGETS+= stage_symlinks
385STAGE_SETS+= links
386STAGE_SYMLINKS.links= ${SYMLINKS}
387.endif
388
389.endif
390
391.include <meta.stage.mk>
392.endif
393.endif
394
395.if defined(META_TARGETS)
396.for _tgt in ${META_TARGETS}
397.if target(${_tgt})
398${_tgt}: ${META_DEPS}
399.endif
400.endfor
401.endif
402