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