xref: /freebsd/share/mk/bsd.sys.mk (revision 0bd5d367989b3d2de0e8d8ceaa2e31d3f0d96536)
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.endif # WARNS
112
113.if defined(FORMAT_AUDIT)
114WFORMAT=	1
115.endif # FORMAT_AUDIT
116.if defined(WFORMAT)
117.if ${WFORMAT} > 0
118#CWARNFLAGS+=	-Wformat-nonliteral -Wformat-security -Wno-format-extra-args
119CWARNFLAGS+=	-Wformat=2 -Wno-format-extra-args
120.if ${WARNS} <= 3
121CWARNFLAGS.clang+=	-Wno-format-nonliteral
122.endif # WARNS <= 3
123.if !defined(NO_WERROR) && !defined(NO_WERROR.${COMPILER_TYPE})
124CWARNFLAGS+=	-Werror
125.endif # !NO_WERROR && !NO_WERROR.${COMPILER_TYPE}
126.endif # WFORMAT > 0
127.endif # WFORMAT
128.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
129CWARNFLAGS+=	-Wno-format
130.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
131
132# GCC 5.2.0
133.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200
134CWARNFLAGS+=	-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			\
141		-Wno-error=extra			\
142		-Wno-error=inline			\
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
149.endif
150
151# GCC 6.1.0
152.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60100
153CWARNFLAGS+=	-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
158.endif
159
160# GCC 7.1.0
161.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 70100
162CWARNFLAGS+=	-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		\
171		-Wno-error=nonnull			\
172		-Wno-error=pointer-compare		\
173		-Wno-error=stringop-overflow
174.endif
175
176# GCC 8.1.0
177.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100
178CWARNFLAGS+=	-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
185.endif
186
187# How to handle FreeBSD custom printf format specifiers.
188.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
189FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
190.else
191FORMAT_EXTENSIONS=	-fformat-extensions
192.endif
193
194.if defined(IGNORE_PRAGMA)
195CWARNFLAGS+=	-Wno-unknown-pragmas
196.endif # IGNORE_PRAGMA
197
198# This warning is utter nonsense
199CFLAGS+=	-Wno-format-zero-length
200
201CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3\
202		 -mllvm -simplifycfg-dup-ret
203.if ${COMPILER_VERSION} >= 30500 && ${COMPILER_VERSION} < 30700
204CLANG_OPT_SMALL+= -mllvm -enable-gvn=false
205.else
206CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
207.endif
208CFLAGS.clang+=	 -Qunused-arguments
209.if ${MACHINE_CPUARCH} == "sparc64"
210# Don't emit .cfi directives, since we must use GNU as on sparc64, for now.
211CFLAGS.clang+=	 -fno-dwarf2-cfi-asm
212.endif # SPARC64
213# The libc++ headers use c++11 extensions.  These are normally silenced because
214# they are treated as system headers, but we explicitly disable that warning
215# suppression when building the base system to catch bugs in our headers.
216# Eventually we'll want to start building the base system C++ code as C++11,
217# but not yet.
218CXXFLAGS.clang+=	 -Wno-c++11-extensions
219
220.if ${MK_SSP} != "no" && \
221    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
222.if (${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30500) || \
223    (${COMPILER_TYPE} == "gcc" && \
224     (${COMPILER_VERSION} == 40201 || ${COMPILER_VERSION} >= 40900))
225# Don't use -Wstack-protector as it breaks world with -Werror.
226SSP_CFLAGS?=	-fstack-protector-strong
227.else
228SSP_CFLAGS?=	-fstack-protector
229.endif
230CFLAGS+=	${SSP_CFLAGS}
231.endif # SSP && !ARM && !MIPS
232
233# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
234# enabled.
235DEBUG_FILES_CFLAGS?= -g
236
237# Allow user-specified additional warning flags, plus compiler and file
238# specific flag overrides, unless we've overridden this...
239.if ${MK_WARNS} != "no"
240CFLAGS+=	${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
241CFLAGS+=	${CWARNFLAGS.${.IMPSRC:T}}
242CXXFLAGS+=	${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
243CXXFLAGS+=	${CXXWARNFLAGS.${.IMPSRC:T}}
244.endif
245
246CFLAGS+=	 ${CFLAGS.${COMPILER_TYPE}}
247CXXFLAGS+=	 ${CXXFLAGS.${COMPILER_TYPE}}
248
249AFLAGS+=	${AFLAGS.${.IMPSRC:T}}
250AFLAGS+=	${AFLAGS.${.TARGET:T}}
251ACFLAGS+=	${ACFLAGS.${.IMPSRC:T}}
252ACFLAGS+=	${ACFLAGS.${.TARGET:T}}
253CFLAGS+=	${CFLAGS.${.IMPSRC:T}}
254CXXFLAGS+=	${CXXFLAGS.${.IMPSRC:T}}
255
256LDFLAGS+=	${LDFLAGS.${LINKER_TYPE}}
257
258# Only allow .TARGET when not using PROGS as it has the same syntax
259# per PROG which is ambiguous with this syntax. This is only needed
260# for PROG_VARS vars.
261.if !defined(_RECURSING_PROGS)
262.if ${MK_WARNS} != "no"
263CFLAGS+=	${CWARNFLAGS.${.TARGET:T}}
264.endif
265CFLAGS+=	${CFLAGS.${.TARGET:T}}
266CXXFLAGS+=	${CXXFLAGS.${.TARGET:T}}
267LDFLAGS+=	${LDFLAGS.${.TARGET:T}}
268LDADD+=		${LDADD.${.TARGET:T}}
269LIBADD+=	${LIBADD.${.TARGET:T}}
270.endif
271
272.if defined(SRCTOP)
273# Prevent rebuilding during install to support read-only objdirs.
274.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
275CFLAGS+=	ERROR-tried-to-rebuild-during-make-install
276.endif
277.endif
278
279# Tell bmake not to mistake standard targets for things to be searched for
280# or expect to ever be up-to-date.
281PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
282		beforelinking build build-tools buildconfig buildfiles \
283		buildincludes check checkdpadd clean cleandepend cleandir \
284		cleanobj configure depend distclean distribute exe \
285		files html includes install installconfig installdirs \
286		installfiles installincludes lint obj objlink objs objwarn \
287		realinstall tags whereobj
288
289# we don't want ${PROG} to be PHONY
290.PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
291.NOTMAIN: ${PHONY_NOTMAIN:Nall}
292
293.if ${MK_STAGING} != "no"
294.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
295_SKIP_STAGING?= yes
296.endif
297.if ${_SKIP_STAGING:Uno} == "yes"
298staging stage_libs stage_files stage_as stage_links stage_symlinks:
299.else
300# allow targets like beforeinstall to be leveraged
301DESTDIR= ${STAGE_OBJTOP}
302.export DESTDIR
303
304.if target(beforeinstall)
305.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
306staging: beforeinstall
307.endif
308.endif
309
310# normally only libs and includes are staged
311.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
312STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
313
314.if !empty(PROG)
315.if defined(PROGNAME)
316STAGE_AS_SETS+= prog
317STAGE_AS_${PROG}= ${PROGNAME}
318stage_as.prog: ${PROG}
319.else
320STAGE_SETS+= prog
321stage_files.prog: ${PROG}
322STAGE_TARGETS+= stage_files
323.endif
324.endif
325.endif
326
327.if !empty(_LIBS) && !defined(INTERNALLIB)
328.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
329STAGE_SETS+= shlib
330STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
331STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
332stage_files.shlib: ${_LIBS:M*.so.*}
333.endif
334
335.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
336STAGE_AS_SETS+= ldscript
337STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
338stage_as.ldscript: ${SHLIB_LINK:R}.ld
339STAGE_DIR.ldscript = ${STAGE_LIBDIR}
340STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
341NO_SHLIB_LINKS=
342.endif
343
344.if target(stage_files.shlib)
345stage_libs: ${_LIBS}
346.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
347stage_files.shlib: ${SHLIB_NAME}.symbols
348.endif
349.else
350stage_libs: ${_LIBS}
351.endif
352.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
353stage_libs: ${SHLIB_NAME}.symbols
354.endif
355
356.endif
357
358.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
359.if !defined(NO_BEFOREBUILD_INCLUDES)
360stage_includes: buildincludes
361beforebuild: stage_includes
362.endif
363.endif
364
365.for t in stage_libs stage_files stage_as
366.if target($t)
367STAGE_TARGETS+= $t
368.endif
369.endfor
370
371.if !empty(STAGE_AS_SETS)
372STAGE_TARGETS+= stage_as
373.endif
374
375.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
376
377.if !empty(LINKS)
378STAGE_TARGETS+= stage_links
379.if ${MAKE_VERSION} < 20131001
380stage_links.links: ${_LIBS} ${PROG}
381.endif
382STAGE_SETS+= links
383STAGE_LINKS.links= ${LINKS}
384.endif
385
386.if !empty(SYMLINKS)
387STAGE_TARGETS+= stage_symlinks
388STAGE_SETS+= links
389STAGE_SYMLINKS.links= ${SYMLINKS}
390.endif
391
392.endif
393
394.include <meta.stage.mk>
395.endif
396.endif
397
398.if defined(META_TARGETS)
399.for _tgt in ${META_TARGETS}
400.if target(${_tgt})
401${_tgt}: ${META_DEPS}
402.endif
403.endfor
404.endif
405