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