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