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