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