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