xref: /freebsd/share/mk/bsd.sys.mk (revision 7543a9c0280a0f4262489671936a6e03b9b2c563)
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:   https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
10# for clang: https://clang.llvm.org/docs/DiagnosticsReference.html
11
12.include <bsd.compiler.mk>
13
14# the default is gnu99 for now
15CSTD?=		gnu99
16
17.if ${CSTD} == "c89" || ${CSTD} == "c90"
18CFLAGS+=	-std=iso9899:1990
19.elif ${CSTD} == "c94" || ${CSTD} == "c95"
20CFLAGS+=	-std=iso9899:199409
21.elif ${CSTD} == "c99"
22CFLAGS+=	-std=iso9899:1999
23.else # CSTD
24CFLAGS+=	-std=${CSTD}
25.endif # CSTD
26
27.if !empty(CXXSTD)
28CXXFLAGS+=	-std=${CXXSTD}
29.endif
30
31# This gives the Makefile we're evaluating at the top-level a chance to set
32# WARNS.  If it doesn't do so, we may freely pull a DEFAULTWARNS if it's set
33# and use that.  This allows us to default WARNS to 6 for src builds without
34# needing to set the default in various Makefile.inc.
35.if !defined(WARNS) && defined(DEFAULTWARNS)
36WARNS=	${DEFAULTWARNS}
37.endif
38
39# -pedantic is problematic because it also imposes namespace restrictions
40#CFLAGS+=	-pedantic
41.if defined(WARNS)
42.if ${WARNS} >= 1
43CWARNFLAGS+=	-Wsystem-headers
44.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
45CWARNFLAGS+=	-Werror
46.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
47.endif # WARNS >= 1
48.if ${WARNS} >= 2
49CWARNFLAGS+=	-Wall -Wno-format-y2k
50.endif # WARNS >= 2
51.if ${WARNS} >= 3
52CWARNFLAGS+=	-W -Wno-unused-parameter -Wstrict-prototypes\
53		-Wmissing-prototypes -Wpointer-arith
54.endif # WARNS >= 3
55.if ${WARNS} >= 4
56CWARNFLAGS+=	-Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\
57		-Wunused-parameter
58.if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE})
59CWARNFLAGS+=	-Wcast-align
60.endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE}
61.endif # WARNS >= 4
62.if ${WARNS} >= 6
63CWARNFLAGS+=	-Wchar-subscripts -Wnested-externs \
64		-Wold-style-definition
65.if !defined(NO_WMISSING_VARIABLE_DECLARATIONS)
66CWARNFLAGS.clang+=	-Wmissing-variable-declarations
67.endif
68.if !defined(NO_WTHREAD_SAFETY)
69CWARNFLAGS.clang+=	-Wthread-safety
70.endif
71.endif # WARNS >= 6
72.if ${WARNS} >= 2 && ${WARNS} <= 4
73# XXX Delete -Wuninitialized by default for now -- the compiler doesn't
74# XXX always get it right.
75CWARNFLAGS+=	-Wno-uninitialized
76.endif # WARNS >=2 && WARNS <= 4
77CWARNFLAGS+=	-Wno-pointer-sign
78# Clang has more warnings enabled by default, and when using -Wall, so if WARNS
79# is set to low values, these have to be disabled explicitly.
80.if ${WARNS} <= 6
81CWARNFLAGS.clang+=	-Wno-empty-body -Wno-string-plus-int
82CWARNFLAGS.clang+=	-Wno-unused-const-variable
83.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 130000
84CWARNFLAGS.clang+=	-Wno-error=unused-but-set-variable
85.endif
86.endif # WARNS <= 6
87.if ${WARNS} <= 3
88CWARNFLAGS.clang+=	-Wno-tautological-compare -Wno-unused-value\
89		-Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion
90CWARNFLAGS.clang+=	-Wno-unused-local-typedef
91CWARNFLAGS.clang+=	-Wno-address-of-packed-member
92.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100
93CWARNFLAGS.gcc+=	-Wno-address-of-packed-member
94.endif
95.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 70000 && \
96    ${MACHINE_CPUARCH} == "arm" && !${MACHINE_ARCH:Marmv[67]*}
97CWARNFLAGS.clang+=	-Wno-atomic-alignment
98.endif
99.endif # WARNS <= 3
100.if ${WARNS} <= 2
101CWARNFLAGS.clang+=	-Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
102.endif # WARNS <= 2
103.if ${WARNS} <= 1
104CWARNFLAGS.clang+=	-Wno-parentheses
105.endif # WARNS <= 1
106.if defined(NO_WARRAY_BOUNDS)
107CWARNFLAGS.clang+=	-Wno-array-bounds
108.endif # NO_WARRAY_BOUNDS
109.if defined(NO_WMISLEADING_INDENTATION) && \
110    ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
111      ${COMPILER_TYPE} == "gcc")
112CWARNFLAGS+=		-Wno-misleading-indentation
113.endif # NO_WMISLEADING_INDENTATION
114.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 140000
115NO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
116.endif
117.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 110100
118NO_WARRAY_PARAMETER=	-Wno-array-parameter
119.endif
120.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100
121NO_WUSE_AFTER_FREE=	-Wno-use-after-free
122.endif
123.endif # WARNS
124
125.if defined(FORMAT_AUDIT)
126WFORMAT=	1
127.endif # FORMAT_AUDIT
128.if defined(WFORMAT)
129.if ${WFORMAT} > 0
130#CWARNFLAGS+=	-Wformat-nonliteral -Wformat-security -Wno-format-extra-args
131CWARNFLAGS+=	-Wformat=2 -Wno-format-extra-args
132.if ${WARNS} <= 3
133CWARNFLAGS.clang+=	-Wno-format-nonliteral
134.endif # WARNS <= 3
135.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
136CWARNFLAGS+=	-Werror
137.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
138.endif # WFORMAT > 0
139.endif # WFORMAT
140.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
141CWARNFLAGS+=	-Wno-format
142.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
143
144# GCC
145# We should clean up warnings produced with these flags.
146# They were originally added as a quick hack to enable gcc5/6.
147# The base system requires at least GCC 6.4, but some ports
148# use this file with older compilers.  Request an exprun
149# before changing these.
150.if ${COMPILER_TYPE} == "gcc"
151# GCC 5.2.0
152.if ${COMPILER_VERSION} >= 50200
153CWARNFLAGS+=	-Wno-error=address			\
154		-Wno-error=array-bounds			\
155		-Wno-error=attributes			\
156		-Wno-error=bool-compare			\
157		-Wno-error=cast-align			\
158		-Wno-error=clobbered			\
159		-Wno-error=deprecated-declarations	\
160		-Wno-error=enum-compare			\
161		-Wno-error=extra			\
162		-Wno-error=logical-not-parentheses	\
163		-Wno-error=strict-aliasing		\
164		-Wno-error=uninitialized		\
165		-Wno-error=unused-but-set-variable	\
166		-Wno-error=unused-function		\
167		-Wno-error=unused-value
168.endif
169
170# GCC 6.1.0
171.if ${COMPILER_VERSION} >= 60100
172CWARNFLAGS+=	-Wno-error=empty-body			\
173		-Wno-error=maybe-uninitialized		\
174		-Wno-error=nonnull-compare		\
175		-Wno-error=shift-negative-value		\
176		-Wno-error=tautological-compare		\
177		-Wno-error=unused-const-variable
178.endif
179
180# GCC 7.1.0
181.if ${COMPILER_VERSION} >= 70100
182CWARNFLAGS+=	-Wno-error=bool-operation		\
183		-Wno-error=deprecated			\
184		-Wno-error=expansion-to-defined		\
185		-Wno-error=format-overflow		\
186		-Wno-error=format-truncation		\
187		-Wno-error=implicit-fallthrough		\
188		-Wno-error=int-in-bool-context		\
189		-Wno-error=memset-elt-size		\
190		-Wno-error=noexcept-type		\
191		-Wno-error=nonnull			\
192		-Wno-error=pointer-compare		\
193		-Wno-error=stringop-overflow
194.endif
195
196# GCC 8.1.0
197.if ${COMPILER_VERSION} >= 80100
198CWARNFLAGS+=	-Wno-error=aggressive-loop-optimizations	\
199		-Wno-error=cast-function-type			\
200		-Wno-error=catch-value				\
201		-Wno-error=multistatement-macros		\
202		-Wno-error=restrict				\
203		-Wno-error=sizeof-pointer-memaccess		\
204		-Wno-error=stringop-truncation
205.endif
206
207# GCC 9.2.0
208.if ${COMPILER_VERSION} >= 90200
209.if ${MACHINE_ARCH} == "i386"
210CWARNFLAGS+=	-Wno-error=overflow
211.endif
212.endif
213
214# GCC produces false positives for functions that switch on an
215# enum (GCC bug 87950)
216CWARNFLAGS+=	-Wno-return-type
217
218# GCC's own arm_neon.h triggers various warnings
219.if ${MACHINE_CPUARCH} == "aarch64"
220CWARNFLAGS+=	-Wno-system-headers
221.endif
222.endif	# gcc
223
224# How to handle FreeBSD custom printf format specifiers.
225.if ${COMPILER_TYPE} == "clang" || \
226    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
227FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
228.else
229FORMAT_EXTENSIONS=	-fformat-extensions
230.endif
231
232.if defined(IGNORE_PRAGMA)
233CWARNFLAGS+=	-Wno-unknown-pragmas
234.endif # IGNORE_PRAGMA
235
236# This warning is utter nonsense
237CFLAGS+=	-Wno-format-zero-length
238
239.if ${COMPILER_TYPE} == "clang"
240# The headers provided by clang are incompatible with the FreeBSD headers.
241# If the version of clang is not one that has been patched to omit the
242# incompatible headers, we need to compile with -nobuiltininc and add the
243# resource dir to the end of the search paths. This ensures that headers such as
244# immintrin.h are still found but stddef.h, etc. are picked up from FreeBSD.
245#
246# XXX: This is a hack to support complete external installs of clang while
247# we work to synchronize our decleration guards with those in the clang tree.
248.if ${MK_CLANG_BOOTSTRAP:Uno} == "no" && \
249    ${COMPILER_RESOURCE_DIR} != "unknown" && !defined(BOOTSTRAPPING)
250CFLAGS+=-nobuiltininc -idirafter ${COMPILER_RESOURCE_DIR}/include
251.endif
252.endif
253
254CLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3
255.if ${COMPILER_VERSION} < 130000
256CLANG_OPT_SMALL+= -mllvm -simplifycfg-dup-ret
257.endif
258CLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
259CFLAGS.clang+=	 -Qunused-arguments
260# The libc++ headers use c++11 extensions.  These are normally silenced because
261# they are treated as system headers, but we explicitly disable that warning
262# suppression when building the base system to catch bugs in our headers.
263# Eventually we'll want to start building the base system C++ code as C++11,
264# but not yet.
265CXXFLAGS.clang+=	 -Wno-c++11-extensions
266
267.if ${MK_SSP} != "no"
268# Don't use -Wstack-protector as it breaks world with -Werror.
269SSP_CFLAGS?=	-fstack-protector-strong
270CFLAGS+=	${SSP_CFLAGS}
271.endif # SSP
272
273# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
274# enabled.
275DEBUG_FILES_CFLAGS?= -g -gz=zlib
276
277# Allow user-specified additional warning flags, plus compiler and file
278# specific flag overrides, unless we've overridden this...
279.if ${MK_WARNS} != "no"
280CFLAGS+=	${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
281CFLAGS+=	${CWARNFLAGS.${.IMPSRC:T}}
282CXXFLAGS+=	${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
283CXXFLAGS+=	${CXXWARNFLAGS.${.IMPSRC:T}}
284.endif
285
286CFLAGS+=	 ${CFLAGS.${COMPILER_TYPE}}
287CXXFLAGS+=	 ${CXXFLAGS.${COMPILER_TYPE}}
288
289AFLAGS+=	${AFLAGS.${.IMPSRC:T}}
290AFLAGS+=	${AFLAGS.${.TARGET:T}}
291ACFLAGS+=	${ACFLAGS.${.IMPSRC:T}}
292ACFLAGS+=	${ACFLAGS.${.TARGET:T}}
293CFLAGS+=	${CFLAGS.${.IMPSRC:T}}
294CXXFLAGS+=	${CXXFLAGS.${.IMPSRC:T}}
295
296LDFLAGS+=	${LDFLAGS.${LINKER_TYPE}}
297
298# Only allow .TARGET when not using PROGS as it has the same syntax
299# per PROG which is ambiguous with this syntax. This is only needed
300# for PROG_VARS vars.
301#
302# Some directories (currently just clang) also need to disable this since
303# CFLAGS.${COMPILER_TYPE}, CFLAGS.${.IMPSRC:T} and CFLAGS.${.TARGET:T} all live
304# in the same namespace, meaning that, for example, GCC builds of clang pick up
305# CFLAGS.clang via CFLAGS.${.TARGET:T} and thus try to pass Clang-specific
306# flags. Ideally the different sources of CFLAGS would be namespaced to avoid
307# collisions.
308.if !defined(_RECURSING_PROGS) && !defined(NO_TARGET_FLAGS)
309.if ${MK_WARNS} != "no"
310CFLAGS+=	${CWARNFLAGS.${.TARGET:T}}
311.endif
312CFLAGS+=	${CFLAGS.${.TARGET:T}}
313CXXFLAGS+=	${CXXFLAGS.${.TARGET:T}}
314LDFLAGS+=	${LDFLAGS.${.TARGET:T}}
315LDADD+=		${LDADD.${.TARGET:T}}
316LIBADD+=	${LIBADD.${.TARGET:T}}
317.endif
318
319.if defined(SRCTOP)
320# Prevent rebuilding during install to support read-only objdirs.
321.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
322CFLAGS+=	ERROR-tried-to-rebuild-during-make-install
323.endif
324.endif
325
326# Please keep this if in sync with kern.mk
327.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
328# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
329.if ${COMPILER_TYPE} == "clang"
330# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
331.if ${COMPILER_VERSION} >= 120000
332LDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
333.else
334LDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
335.endif
336.elif ${COMPILER_TYPE} == "gcc"
337# GCC does not support an absolute path for -fuse-ld so we just print this
338# warning instead and let the user add the required symlinks.
339# However, we can avoid this warning if -B is set appropriately (e.g. for
340# CROSS_TOOLCHAIN=...-gcc).
341.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
342.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
343.endif
344.endif
345.endif
346
347# Tell bmake not to mistake standard targets for things to be searched for
348# or expect to ever be up-to-date.
349PHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
350		beforelinking build build-tools buildconfig buildfiles \
351		buildincludes check checkdpadd clean cleandepend cleandir \
352		cleanobj configure depend distclean distribute exe \
353		files html includes install installconfig installdirs \
354		installfiles installincludes lint obj objlink objs objwarn \
355		realinstall tags whereobj
356
357# we don't want ${PROG} to be PHONY
358.PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
359.NOTMAIN: ${PHONY_NOTMAIN:Nall}
360
361.if ${MK_STAGING} != "no"
362.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
363_SKIP_STAGING?= yes
364.endif
365.if ${_SKIP_STAGING:Uno} == "yes"
366staging stage_libs stage_files stage_as stage_links stage_symlinks:
367.else
368# allow targets like beforeinstall to be leveraged
369DESTDIR= ${STAGE_OBJTOP}
370.export DESTDIR
371
372.if target(beforeinstall)
373.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
374staging: beforeinstall
375.endif
376.endif
377
378# normally only libs and includes are staged
379.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
380STAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
381
382.if !empty(PROG)
383.if defined(PROGNAME)
384STAGE_AS_SETS+= prog
385STAGE_AS_${PROG}= ${PROGNAME}
386stage_as.prog: ${PROG}
387.else
388STAGE_SETS+= prog
389stage_files.prog: ${PROG}
390STAGE_TARGETS+= stage_files
391.endif
392.endif
393.endif
394
395.if !empty(_LIBS) && !defined(INTERNALLIB)
396.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
397STAGE_SETS+= shlib
398STAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
399STAGE_FILES.shlib+= ${_LIBS:M*.so.*}
400stage_files.shlib: ${_LIBS:M*.so.*}
401.endif
402
403.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
404STAGE_AS_SETS+= ldscript
405STAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
406stage_as.ldscript: ${SHLIB_LINK:R}.ld
407STAGE_DIR.ldscript = ${STAGE_LIBDIR}
408STAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
409NO_SHLIB_LINKS=
410.endif
411
412.if target(stage_files.shlib)
413stage_libs: ${_LIBS}
414.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
415stage_files.shlib: ${SHLIB_NAME}.symbols
416.endif
417.else
418stage_libs: ${_LIBS}
419.endif
420.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
421stage_libs: ${SHLIB_NAME}.symbols
422.endif
423
424.endif
425
426.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
427.if !defined(NO_BEFOREBUILD_INCLUDES)
428stage_includes: buildincludes
429beforebuild: stage_includes
430.endif
431.endif
432
433.for t in stage_libs stage_files stage_as
434.if target($t)
435STAGE_TARGETS+= $t
436.endif
437.endfor
438
439.if !empty(STAGE_AS_SETS)
440STAGE_TARGETS+= stage_as
441.endif
442
443.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
444
445.if !empty(LINKS)
446STAGE_TARGETS+= stage_links
447.if ${MAKE_VERSION} < 20131001
448stage_links.links: ${_LIBS} ${PROG}
449.endif
450STAGE_SETS+= links
451STAGE_LINKS.links= ${LINKS}
452.endif
453
454.if !empty(SYMLINKS)
455STAGE_TARGETS+= stage_symlinks
456STAGE_SETS+= links
457STAGE_SYMLINKS.links= ${SYMLINKS}
458.endif
459
460.endif
461
462.include <meta.stage.mk>
463.endif
464.endif
465
466.if defined(META_TARGETS)
467.for _tgt in ${META_TARGETS}
468.if target(${_tgt})
469${_tgt}: ${META_DEPS}
470.endif
471.endfor
472.endif
473