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