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