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