xref: /freebsd/share/mk/bsd.sys.mk (revision ca4eddea97c54a567c25e0fcf4641a6885f33bdf)
1d510ecb4SKris Kennaway#
2d510ecb4SKris Kennaway# This file contains common settings used for building FreeBSD
3d510ecb4SKris Kennaway# sources.
4d510ecb4SKris Kennaway
5d510ecb4SKris Kennaway# Enable various levels of compiler warning checks.  These may be
6ab72ce6bSWarner Losh# overridden (e.g. if using a non-gcc compiler) by defining MK_WARNS=no.
7d510ecb4SKris Kennaway
8fac6dee9SEric van Gyzen# for GCC:   https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
95d1ee539SEitan Adler# for clang: https://clang.llvm.org/docs/DiagnosticsReference.html
10b0b7c216SDavid E. O'Brien
119215d178SBrooks Davis.include <bsd.compiler.mk>
129215d178SBrooks Davis
13*ca4eddeaSMinsoo ChooCSTD?=		gnu17
14300d03a8SRoman Divacky
155d1ee539SEitan Adler.if ${CSTD} == "c89" || ${CSTD} == "c90"
16513f04caSDag-Erling SmørgravCFLAGS+=	-std=iso9899:1990
17513f04caSDag-Erling Smørgrav.elif ${CSTD} == "c94" || ${CSTD} == "c95"
18513f04caSDag-Erling SmørgravCFLAGS+=	-std=iso9899:199409
19513f04caSDag-Erling Smørgrav.elif ${CSTD} == "c99"
20513f04caSDag-Erling SmørgravCFLAGS+=	-std=iso9899:1999
213b05e58bSDimitry Andric.else # CSTD
22513f04caSDag-Erling SmørgravCFLAGS+=	-std=${CSTD}
233b05e58bSDimitry Andric.endif # CSTD
24e8067928SEnji Cooper
25db933d76SEnji Cooper.if !empty(CXXSTD)
26e8067928SEnji CooperCXXFLAGS+=	-std=${CXXSTD}
27db933d76SEnji Cooper.endif
28e8067928SEnji Cooper
29fe815331SKyle Evans# This gives the Makefile we're evaluating at the top-level a chance to set
30fe815331SKyle Evans# WARNS.  If it doesn't do so, we may freely pull a DEFAULTWARNS if it's set
31fe815331SKyle Evans# and use that.  This allows us to default WARNS to 6 for src builds without
32fe815331SKyle Evans# needing to set the default in various Makefile.inc.
33fe815331SKyle Evans.if !defined(WARNS) && defined(DEFAULTWARNS)
34fe815331SKyle EvansWARNS=	${DEFAULTWARNS}
35fe815331SKyle Evans.endif
36fe815331SKyle Evans
37513f04caSDag-Erling Smørgrav# -pedantic is problematic because it also imposes namespace restrictions
38513f04caSDag-Erling Smørgrav#CFLAGS+=	-pedantic
39d510ecb4SKris Kennaway.if defined(WARNS)
40626cba20SDavid E. O'Brien.if ${WARNS} >= 1
4112db4047SEd MasteCWARNFLAGS+=	-Wsystem-headers
427fa2f2a6SAlex Richardson.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
4380451781SRuslan ErmilovCWARNFLAGS+=	-Werror
447fa2f2a6SAlex Richardson.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
453b05e58bSDimitry Andric.endif # WARNS >= 1
46626cba20SDavid E. O'Brien.if ${WARNS} >= 2
4780451781SRuslan ErmilovCWARNFLAGS+=	-Wall -Wno-format-y2k
483b05e58bSDimitry Andric.endif # WARNS >= 2
49626cba20SDavid E. O'Brien.if ${WARNS} >= 3
50526f57bfSDimitry AndricCWARNFLAGS+=	-W -Wno-unused-parameter
513dad1c66SJohn Baldwin.if ${COMPILER_TYPE} == "clang"
52526f57bfSDimitry AndricCWARNFLAGS+=	-Wstrict-prototypes
53526f57bfSDimitry Andric.endif
54526f57bfSDimitry AndricCWARNFLAGS+=	-Wmissing-prototypes -Wpointer-arith
553b05e58bSDimitry Andric.endif # WARNS >= 3
56626cba20SDavid E. O'Brien.if ${WARNS} >= 4
573b05e58bSDimitry AndricCWARNFLAGS+=	-Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow\
583b05e58bSDimitry Andric		-Wunused-parameter
592ded1ea2SWarner Losh.if !defined(NO_WCAST_ALIGN) && !defined(NO_WCAST_ALIGN.${COMPILER_TYPE})
60c9a900edSMarcel MoolenaarCWARNFLAGS+=	-Wcast-align
612ded1ea2SWarner Losh.endif # !NO_WCAST_ALIGN !NO_WCAST_ALIGN.${COMPILER_TYPE}
623b05e58bSDimitry Andric.endif # WARNS >= 4
63626cba20SDavid E. O'Brien.if ${WARNS} >= 6
64c227269eSJohn BaldwinCWARNFLAGS+=	-Wchar-subscripts -Wnested-externs \
653b05e58bSDimitry Andric		-Wold-style-definition
6641603b83SWarner Losh.if !defined(NO_WMISSING_VARIABLE_DECLARATIONS)
67a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wmissing-variable-declarations
687e659f94SEd Schouten.endif
6913b7412dSEd Schouten.if !defined(NO_WTHREAD_SAFETY)
7013b7412dSEd SchoutenCWARNFLAGS.clang+=	-Wthread-safety
7113b7412dSEd Schouten.endif
723b05e58bSDimitry Andric.endif # WARNS >= 6
73626cba20SDavid E. O'Brien.if ${WARNS} >= 2 && ${WARNS} <= 4
743ad920feSDavid Malone# XXX Delete -Wuninitialized by default for now -- the compiler doesn't
753ad920feSDavid Malone# XXX always get it right.
7680451781SRuslan ErmilovCWARNFLAGS+=	-Wno-uninitialized
773b05e58bSDimitry Andric.endif # WARNS >=2 && WARNS <= 4
78d9e2d1a3SAlexander KabaevCWARNFLAGS+=	-Wno-pointer-sign
79a8f92d24SEd Maste.if !defined(NO_WDATE_TIME)
80a8f92d24SEd MasteCWARNFLAGS+=	-Wdate-time
81a8f92d24SEd Maste.endif # NO_WDATE_TIME
82e2325c4aSDimitry Andric# Clang has more warnings enabled by default, and when using -Wall, so if WARNS
83e2325c4aSDimitry Andric# is set to low values, these have to be disabled explicitly.
84dff0c46cSDimitry Andric.if ${WARNS} <= 6
8599929852SWarner LoshCWARNFLAGS.clang+=	-Wno-empty-body -Wno-string-plus-int
8699929852SWarner LoshCWARNFLAGS.clang+=	-Wno-unused-const-variable
87526f57bfSDimitry Andric.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 150000
88526f57bfSDimitry AndricCWARNFLAGS.clang+=	-Wno-error=unused-but-set-parameter
89526f57bfSDimitry Andric.endif
90d5750775SDimitry Andric.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 190000
91d5750775SDimitry Andric# Similar to gcc >= 8.1 -Wno-error=cast-function-type below
92d5750775SDimitry AndricCWARNFLAGS.clang+=	-Wno-error=cast-function-type-mismatch
93d5750775SDimitry Andric.endif
94dff0c46cSDimitry Andric.endif # WARNS <= 6
95e2325c4aSDimitry Andric.if ${WARNS} <= 3
96a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wno-tautological-compare -Wno-unused-value\
97e33625bfSDimitry Andric		-Wno-parentheses-equality -Wno-unused-function -Wno-enum-conversion
98e004ed02SDimitry AndricCWARNFLAGS.clang+=	-Wno-unused-local-typedef
99e6e7c786SDimitry AndricCWARNFLAGS.clang+=	-Wno-address-of-packed-member
100fc41af14SRyan Libby.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 90100
101fc41af14SRyan LibbyCWARNFLAGS.gcc+=	-Wno-address-of-packed-member
102fc41af14SRyan Libby.endif
1033b05e58bSDimitry Andric.endif # WARNS <= 3
104e2325c4aSDimitry Andric.if ${WARNS} <= 2
105a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wno-switch -Wno-switch-enum -Wno-knr-promoted-parameter
1063b05e58bSDimitry Andric.endif # WARNS <= 2
107e2325c4aSDimitry Andric.if ${WARNS} <= 1
108a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wno-parentheses
1093b05e58bSDimitry Andric.endif # WARNS <= 1
110c9009f9fSDimitry Andric.if defined(NO_WARRAY_BOUNDS)
111a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wno-array-bounds
1123b05e58bSDimitry Andric.endif # NO_WARRAY_BOUNDS
113533a4a62SDimitry Andric.if defined(NO_WMISLEADING_INDENTATION) && \
114533a4a62SDimitry Andric    ((${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 100000) || \
115fac6dee9SEric van Gyzen      ${COMPILER_TYPE} == "gcc")
116533a4a62SDimitry AndricCWARNFLAGS+=		-Wno-misleading-indentation
117533a4a62SDimitry Andric.endif # NO_WMISLEADING_INDENTATION
118ad79b018SJohn Baldwin.if ${COMPILER_VERSION} >= 130000
119ad79b018SJohn BaldwinNO_WUNUSED_BUT_SET_VARIABLE=	-Wno-unused-but-set-variable
120ad79b018SJohn Baldwin.endif
1215f2aca83SDimitry Andric.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 140000
1225f2aca83SDimitry AndricNO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
1235f2aca83SDimitry Andric.endif
1240ecb6161SWarner Losh.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 150000
125cd800d3cSJohn BaldwinNO_WARRAY_PARAMETER=	-Wno-array-parameter
126d04c8671SWarner LoshNO_WSTRICT_PROTOTYPES=	-Wno-strict-prototypes
1270ecb6161SWarner LoshNO_WDEPRECATED_NON_PROTOTYPE=-Wno-deprecated-non-prototype
1280ecb6161SWarner Losh.endif
129ad79b018SJohn Baldwin.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 50200
130ad79b018SJohn BaldwinNO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
131ad79b018SJohn Baldwin.endif
1329134d926SJohn Baldwin.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 100100
1339134d926SJohn BaldwinNO_WZERO_LENGTH_BOUNDS=	-Wno-zero-length-bounds
1349134d926SJohn Baldwin.endif
135409dae26SJohn Baldwin.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 110100
136409dae26SJohn BaldwinNO_WARRAY_PARAMETER=	-Wno-array-parameter
137409dae26SJohn Baldwin.endif
13867b07512SJohn Baldwin.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100
13967b07512SJohn BaldwinNO_WUSE_AFTER_FREE=	-Wno-use-after-free
140ad96424eSJohn BaldwinNO_WDANGLING_POINTER=	-Wno-dangling-pointer
14167b07512SJohn Baldwin.endif
1423b05e58bSDimitry Andric.endif # WARNS
143d510ecb4SKris Kennaway
144d510ecb4SKris Kennaway.if defined(FORMAT_AUDIT)
145d510ecb4SKris KennawayWFORMAT=	1
1463b05e58bSDimitry Andric.endif # FORMAT_AUDIT
147d510ecb4SKris Kennaway.if defined(WFORMAT)
148d510ecb4SKris Kennaway.if ${WFORMAT} > 0
14980451781SRuslan Ermilov#CWARNFLAGS+=	-Wformat-nonliteral -Wformat-security -Wno-format-extra-args
15080451781SRuslan ErmilovCWARNFLAGS+=	-Wformat=2 -Wno-format-extra-args
1514e8b87afSSteve Kiernan.if ${WARNS:U0} <= 3
152a18673d4SWarner LoshCWARNFLAGS.clang+=	-Wno-format-nonliteral
153dff0c46cSDimitry Andric.endif # WARNS <= 3
1547fa2f2a6SAlex Richardson.if ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
15580451781SRuslan ErmilovCWARNFLAGS+=	-Werror
1567fa2f2a6SAlex Richardson.endif # ${MK_WERROR} != "no" && ${MK_WERROR.${COMPILER_TYPE}:Uyes} != "no"
1573b05e58bSDimitry Andric.endif # WFORMAT > 0
1583b05e58bSDimitry Andric.endif # WFORMAT
1592ded1ea2SWarner Losh.if defined(NO_WFORMAT) || defined(NO_WFORMAT.${COMPILER_TYPE})
160a6c0f1f2SPawel Jakub DawidekCWARNFLAGS+=	-Wno-format
1612ded1ea2SWarner Losh.endif # NO_WFORMAT || NO_WFORMAT.${COMPILER_TYPE}
162d510ecb4SKris Kennaway
163fac6dee9SEric van Gyzen# GCC
164fac6dee9SEric van Gyzen# We should clean up warnings produced with these flags.
165fac6dee9SEric van Gyzen# They were originally added as a quick hack to enable gcc5/6.
166824214daSEric van Gyzen# The base system requires at least GCC 6.4, but some ports
167824214daSEric van Gyzen# use this file with older compilers.  Request an exprun
168824214daSEric van Gyzen# before changing these.
169fac6dee9SEric van Gyzen.if ${COMPILER_TYPE} == "gcc"
170824214daSEric van Gyzen# GCC 5.2.0
171824214daSEric van Gyzen.if ${COMPILER_VERSION} >= 50200
1728500b15fSRuslan BukinCWARNFLAGS+=	-Wno-error=address			\
1738500b15fSRuslan Bukin		-Wno-error=array-bounds			\
1748500b15fSRuslan Bukin		-Wno-error=attributes			\
1758500b15fSRuslan Bukin		-Wno-error=bool-compare			\
1768500b15fSRuslan Bukin		-Wno-error=cast-align			\
1778500b15fSRuslan Bukin		-Wno-error=clobbered			\
17823e2b4d9SDimitry Andric		-Wno-error=deprecated-declarations	\
1798500b15fSRuslan Bukin		-Wno-error=enum-compare			\
1808500b15fSRuslan Bukin		-Wno-error=extra			\
1818500b15fSRuslan Bukin		-Wno-error=logical-not-parentheses	\
1828500b15fSRuslan Bukin		-Wno-error=strict-aliasing		\
1838500b15fSRuslan Bukin		-Wno-error=uninitialized		\
1848500b15fSRuslan Bukin		-Wno-error=unused-function		\
1858500b15fSRuslan Bukin		-Wno-error=unused-value
186824214daSEric van Gyzen.endif
187824214daSEric van Gyzen
188824214daSEric van Gyzen# GCC 6.1.0
189824214daSEric van Gyzen.if ${COMPILER_VERSION} >= 60100
19073fd212cSJohn BaldwinCWARNFLAGS+=	-Wno-error=empty-body			\
19173fd212cSJohn Baldwin		-Wno-error=maybe-uninitialized		\
192a02fb762SJohn Baldwin		-Wno-error=nonnull-compare		\
193824214daSEric van Gyzen		-Wno-error=shift-negative-value		\
194824214daSEric van Gyzen		-Wno-error=tautological-compare		\
195824214daSEric van Gyzen		-Wno-error=unused-const-variable
196824214daSEric van Gyzen.endif
197b6f02827SRuslan Bukin
1981ec31f2cSRuslan Bukin# GCC 7.1.0
199fac6dee9SEric van Gyzen.if ${COMPILER_VERSION} >= 70100
200a8db1fa1SRuslan BukinCWARNFLAGS+=	-Wno-error=bool-operation		\
201a8db1fa1SRuslan Bukin		-Wno-error=deprecated			\
202a8db1fa1SRuslan Bukin		-Wno-error=expansion-to-defined		\
203a8db1fa1SRuslan Bukin		-Wno-error=format-overflow		\
2041ec31f2cSRuslan Bukin		-Wno-error=format-truncation		\
2051ec31f2cSRuslan Bukin		-Wno-error=implicit-fallthrough		\
2061ec31f2cSRuslan Bukin		-Wno-error=int-in-bool-context		\
2071ec31f2cSRuslan Bukin		-Wno-error=memset-elt-size		\
20827c2ac11SRuslan Bukin		-Wno-error=noexcept-type		\
209a8db1fa1SRuslan Bukin		-Wno-error=nonnull			\
210a8db1fa1SRuslan Bukin		-Wno-error=pointer-compare		\
211a8db1fa1SRuslan Bukin		-Wno-error=stringop-overflow
2121ec31f2cSRuslan Bukin.endif
2131ec31f2cSRuslan Bukin
214f41c578fSRuslan Bukin# GCC 8.1.0
215fac6dee9SEric van Gyzen.if ${COMPILER_VERSION} >= 80100
216f41c578fSRuslan BukinCWARNFLAGS+=	-Wno-error=aggressive-loop-optimizations	\
217f41c578fSRuslan Bukin		-Wno-error=cast-function-type			\
218c26e80e1SRuslan Bukin		-Wno-error=catch-value				\
219f41c578fSRuslan Bukin		-Wno-error=multistatement-macros		\
220f41c578fSRuslan Bukin		-Wno-error=restrict				\
221f41c578fSRuslan Bukin		-Wno-error=sizeof-pointer-memaccess		\
222f41c578fSRuslan Bukin		-Wno-error=stringop-truncation
223a3a88aa1SDimitry AndricCXXWARNFLAGS+=	-Wno-error=class-memaccess
224f41c578fSRuslan Bukin.endif
225776b260aSJohn Baldwin
22693d6fa53SJohn Baldwin# GCC 9.2.0
22793d6fa53SJohn Baldwin.if ${COMPILER_VERSION} >= 90200
22893d6fa53SJohn Baldwin.if ${MACHINE_ARCH} == "i386"
22993d6fa53SJohn BaldwinCWARNFLAGS+=	-Wno-error=overflow
23093d6fa53SJohn Baldwin.endif
23193d6fa53SJohn Baldwin.endif
23293d6fa53SJohn Baldwin
233442295c1SJohn Baldwin# GCC 12.1.0
234442295c1SJohn Baldwin.if ${COMPILER_VERSION} >= 120100
235442295c1SJohn Baldwin# These warnings are raised by headers in libc++ so are disabled
236442295c1SJohn Baldwin# globally for all C++
237442295c1SJohn BaldwinCXXWARNFLAGS+=	-Wno-literal-suffix 			\
238442295c1SJohn Baldwin		-Wno-error=unknown-pragmas
239442295c1SJohn Baldwin.endif
240442295c1SJohn Baldwin
24128fc837bSJohn Baldwin# GCC 13.1.0
24228fc837bSJohn Baldwin.if ${COMPILER_VERSION} >= 130100
24328fc837bSJohn Baldwin# These warnings are raised by headers in libc++ so are disabled
24428fc837bSJohn Baldwin# globally for all C++
24528fc837bSJohn BaldwinCXXWARNFLAGS+=	-Wno-dangling-reference
24628fc837bSJohn Baldwin.endif
24728fc837bSJohn Baldwin
2482f6a8424SJohn Baldwin# GCC produces false positives for functions that switch on an
2492f6a8424SJohn Baldwin# enum (GCC bug 87950)
2502f6a8424SJohn BaldwinCWARNFLAGS+=	-Wno-return-type
2512f6a8424SJohn Baldwin
252776b260aSJohn Baldwin# GCC's own arm_neon.h triggers various warnings
2532a6803deSAndrew Turner.if ${MACHINE_CPUARCH} == "aarch64"
254776b260aSJohn BaldwinCWARNFLAGS+=	-Wno-system-headers
255776b260aSJohn Baldwin.endif
256fac6dee9SEric van Gyzen.endif	# gcc
257f41c578fSRuslan Bukin
258accc510aSDimitry Andric# How to handle FreeBSD custom printf format specifiers.
2595115511eSJohn Baldwin.if ${COMPILER_TYPE} == "clang" || \
2605115511eSJohn Baldwin    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
261accc510aSDimitry AndricFORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
262accc510aSDimitry Andric.else
263accc510aSDimitry AndricFORMAT_EXTENSIONS=	-fformat-extensions
264accc510aSDimitry Andric.endif
265accc510aSDimitry Andric
266f4c93e2cSJohn Birrell.if defined(IGNORE_PRAGMA)
267f4c93e2cSJohn BirrellCWARNFLAGS+=	-Wno-unknown-pragmas
2683b05e58bSDimitry Andric.endif # IGNORE_PRAGMA
269f4c93e2cSJohn Birrell
270be3ab187SConrad Meyer# This warning is utter nonsense
27147fec6f3SConrad MeyerCFLAGS+=	-Wno-format-zero-length
272be3ab187SConrad Meyer
273f382bac4SBrooks Davis.if ${COMPILER_TYPE} == "clang"
274f382bac4SBrooks Davis# The headers provided by clang are incompatible with the FreeBSD headers.
275f382bac4SBrooks Davis# If the version of clang is not one that has been patched to omit the
276f382bac4SBrooks Davis# incompatible headers, we need to compile with -nobuiltininc and add the
277f382bac4SBrooks Davis# resource dir to the end of the search paths. This ensures that headers such as
278f382bac4SBrooks Davis# immintrin.h are still found but stddef.h, etc. are picked up from FreeBSD.
279f382bac4SBrooks Davis#
280f382bac4SBrooks Davis# XXX: This is a hack to support complete external installs of clang while
281f382bac4SBrooks Davis# we work to synchronize our decleration guards with those in the clang tree.
282cddd13a4SKyle Evans.if ${MK_CLANG_BOOTSTRAP:Uno} == "no" && \
283cddd13a4SKyle Evans    ${COMPILER_RESOURCE_DIR} != "unknown" && !defined(BOOTSTRAPPING)
284f382bac4SBrooks DavisCFLAGS+=-nobuiltininc -idirafter ${COMPILER_RESOURCE_DIR}/include
285f382bac4SBrooks Davis.endif
286f382bac4SBrooks Davis.endif
287f382bac4SBrooks Davis
28822b8ab15SDimitry AndricCLANG_OPT_SMALL= -mstack-alignment=8 -mllvm -inline-threshold=3
28922b8ab15SDimitry Andric.if ${COMPILER_VERSION} < 130000
29022b8ab15SDimitry AndricCLANG_OPT_SMALL+= -mllvm -simplifycfg-dup-ret
29122b8ab15SDimitry Andric.endif
292ab0116f5SDimitry AndricCLANG_OPT_SMALL+= -mllvm -enable-load-pre=false
293a18673d4SWarner LoshCFLAGS.clang+=	 -Qunused-arguments
2942879baf2SDimitry Andric# The libc++ headers use c++11 extensions.  These are normally silenced because
2952879baf2SDimitry Andric# they are treated as system headers, but we explicitly disable that warning
2962879baf2SDimitry Andric# suppression when building the base system to catch bugs in our headers.
2972879baf2SDimitry Andric# Eventually we'll want to start building the base system C++ code as C++11,
2982879baf2SDimitry Andric# but not yet.
299a18673d4SWarner LoshCXXFLAGS.clang+=	 -Wno-c++11-extensions
30007b202a8SDimitry Andric
3019bfd3b40SKyle Evans# XXX This should be defaulted to 2 when WITH_SSP is in use after further
3029bfd3b40SKyle Evans# testing and soak time.
3039bfd3b40SKyle EvansFORTIFY_SOURCE?=	0
3041b8db4b4SMitchell Horne.if ${MK_SSP} != "no"
305042df2e2SRuslan Ermilov# Don't use -Wstack-protector as it breaks world with -Werror.
3061c2ae923SAlexander Leidinger.if ${COMPILER_FEATURES:Mstackclash}
307f934e629SAlexander LeidingerSSP_CFLAGS?=	-fstack-protector-strong -fstack-clash-protection
3081c2ae923SAlexander Leidinger.else
3091c2ae923SAlexander LeidingerSSP_CFLAGS?=	-fstack-protector-strong
3101c2ae923SAlexander Leidinger.endif
311a52bef9cSEnji CooperCFLAGS+=	${SSP_CFLAGS}
3121b8db4b4SMitchell Horne.endif # SSP
3139bfd3b40SKyle Evans.if ${FORTIFY_SOURCE} > 0
3149bfd3b40SKyle EvansCFLAGS+=	-D_FORTIFY_SOURCE=${FORTIFY_SOURCE}
3159bfd3b40SKyle EvansCXXFLAGS+=	-D_FORTIFY_SOURCE=${FORTIFY_SOURCE}
3169bfd3b40SKyle Evans.endif
317042df2e2SRuslan Ermilov
318de6feefdSJohn Baldwin# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
319de6feefdSJohn Baldwin# enabled.
32047363e99SEd MasteDEBUG_FILES_CFLAGS?= -g -gz=zlib
321de6feefdSJohn Baldwin
322ff7ea788SSteven Hartland# Allow user-specified additional warning flags, plus compiler and file
323f324fafcSEitan Adler# specific flag overrides, unless we've overridden this...
324ab72ce6bSWarner Losh.if ${MK_WARNS} != "no"
3259346408dSRuslan BukinCFLAGS+=	${CWARNFLAGS:M*} ${CWARNFLAGS.${COMPILER_TYPE}}
326ff7ea788SSteven HartlandCFLAGS+=	${CWARNFLAGS.${.IMPSRC:T}}
3276b184f62SAlan SomersCXXFLAGS+=	${CXXWARNFLAGS:M*} ${CXXWARNFLAGS.${COMPILER_TYPE}}
3286b184f62SAlan SomersCXXFLAGS+=	${CXXWARNFLAGS.${.IMPSRC:T}}
32941603b83SWarner Losh.endif
33041603b83SWarner Losh
33141603b83SWarner LoshCFLAGS+=	 ${CFLAGS.${COMPILER_TYPE}}
33241603b83SWarner LoshCXXFLAGS+=	 ${CXXFLAGS.${COMPILER_TYPE}}
3330815243cSMarcel Moolenaar
334b38eb9eaSBryan DreweryAFLAGS+=	${AFLAGS.${.IMPSRC:T}}
33548da57b4SBryan DreweryAFLAGS+=	${AFLAGS.${.TARGET:T}}
33659f6130dSBryan DreweryACFLAGS+=	${ACFLAGS.${.IMPSRC:T}}
33748da57b4SBryan DreweryACFLAGS+=	${ACFLAGS.${.TARGET:T}}
33859f6130dSBryan DreweryCFLAGS+=	${CFLAGS.${.IMPSRC:T}}
33959f6130dSBryan DreweryCXXFLAGS+=	${CXXFLAGS.${.IMPSRC:T}}
34059f6130dSBryan Drewery
341b25a46f7SEd MasteLDFLAGS+=	${LDFLAGS.${LINKER_TYPE}}
3428e0c3739SBryan Drewery
3438e0c3739SBryan Drewery# Only allow .TARGET when not using PROGS as it has the same syntax
3448e0c3739SBryan Drewery# per PROG which is ambiguous with this syntax. This is only needed
3458e0c3739SBryan Drewery# for PROG_VARS vars.
346c8edd054SJessica Clarke#
347c8edd054SJessica Clarke# Some directories (currently just clang) also need to disable this since
348c8edd054SJessica Clarke# CFLAGS.${COMPILER_TYPE}, CFLAGS.${.IMPSRC:T} and CFLAGS.${.TARGET:T} all live
349c8edd054SJessica Clarke# in the same namespace, meaning that, for example, GCC builds of clang pick up
350c8edd054SJessica Clarke# CFLAGS.clang via CFLAGS.${.TARGET:T} and thus try to pass Clang-specific
351c8edd054SJessica Clarke# flags. Ideally the different sources of CFLAGS would be namespaced to avoid
352c8edd054SJessica Clarke# collisions.
353c8edd054SJessica Clarke.if !defined(_RECURSING_PROGS) && !defined(NO_TARGET_FLAGS)
3548e0c3739SBryan Drewery.if ${MK_WARNS} != "no"
3558e0c3739SBryan DreweryCFLAGS+=	${CWARNFLAGS.${.TARGET:T}}
3568e0c3739SBryan Drewery.endif
3578e0c3739SBryan DreweryCFLAGS+=	${CFLAGS.${.TARGET:T}}
3588e0c3739SBryan DreweryCXXFLAGS+=	${CXXFLAGS.${.TARGET:T}}
35948da57b4SBryan DreweryLDFLAGS+=	${LDFLAGS.${.TARGET:T}}
36048da57b4SBryan DreweryLDADD+=		${LDADD.${.TARGET:T}}
36148da57b4SBryan DreweryLIBADD+=	${LIBADD.${.TARGET:T}}
3628e0c3739SBryan Drewery.endif
363b25a46f7SEd Maste
36429fbb807SBryan Drewery.if defined(SRCTOP)
36529fbb807SBryan Drewery# Prevent rebuilding during install to support read-only objdirs.
366df5b485aSBryan Drewery.if ${.TARGETS:M*install*} == ${.TARGETS} && empty(.MAKE.MODE:Mmeta)
36729fbb807SBryan DreweryCFLAGS+=	ERROR-tried-to-rebuild-during-make-install
36829fbb807SBryan Drewery.endif
36929fbb807SBryan Drewery.endif
37029fbb807SBryan Drewery
3712b6ee34cSAlex Richardson# Please keep this if in sync with kern.mk
3722b6ee34cSAlex Richardson.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
3732b6ee34cSAlex Richardson# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
3742b6ee34cSAlex Richardson.if ${COMPILER_TYPE} == "clang"
37578380908SBrooks Davis# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
37617b8b8fbSBrooks Davis.if ${COMPILER_VERSION} >= 120000
37717b8b8fbSBrooks DavisLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
37817b8b8fbSBrooks Davis.else
3792d6bee8fSAlex RichardsonLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
38017b8b8fbSBrooks Davis.endif
3814143e4fbSEd Maste.elif ${COMPILER_TYPE} == "gcc"
3822b6ee34cSAlex Richardson# GCC does not support an absolute path for -fuse-ld so we just print this
3832b6ee34cSAlex Richardson# warning instead and let the user add the required symlinks.
384172a624fSAlex Richardson# However, we can avoid this warning if -B is set appropriately (e.g. for
385172a624fSAlex Richardson# CROSS_TOOLCHAIN=...-gcc).
386172a624fSAlex Richardson.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
3872b6ee34cSAlex Richardson.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
3882b6ee34cSAlex Richardson.endif
3892b6ee34cSAlex Richardson.endif
390172a624fSAlex Richardson.endif
3912b6ee34cSAlex Richardson
3920815243cSMarcel Moolenaar# Tell bmake not to mistake standard targets for things to be searched for
3930815243cSMarcel Moolenaar# or expect to ever be up-to-date.
394aae63957SBryan DreweryPHONY_NOTMAIN = analyze afterdepend afterinstall all beforedepend beforeinstall \
39508cf86feSBryan Drewery		beforelinking build build-tools buildconfig buildfiles \
39671b7fa12SEnji Cooper		buildincludes check checkdpadd clean cleandepend cleandir \
3974e47b95cSBryan Drewery		cleanobj configure depend distclean distribute exe \
398c6552846SBryan Drewery		files html includes install installconfig installdirs \
399c6552846SBryan Drewery		installfiles installincludes lint obj objlink objs objwarn \
4004e47b95cSBryan Drewery		realinstall tags whereobj
4010815243cSMarcel Moolenaar
40272484446SSimon J. Gerraty# we don't want ${PROG} to be PHONY
40372484446SSimon J. Gerraty.PHONY: ${PHONY_NOTMAIN:N${PROG:U}}
404f187ca8cSSimon J. Gerraty.NOTMAIN: ${PHONY_NOTMAIN:Nall}
40557bf5f75SSimon J. Gerraty
40657bf5f75SSimon J. Gerraty.if ${MK_STAGING} != "no"
4073b53f994SBryan Drewery.if defined(_SKIP_BUILD) || (!make(all) && !make(clean*) && !make(*clean))
40872484446SSimon J. Gerraty_SKIP_STAGING?= yes
40972484446SSimon J. Gerraty.endif
41072484446SSimon J. Gerraty.if ${_SKIP_STAGING:Uno} == "yes"
4118fe4beeeSSimon J. Gerratystaging stage_libs stage_files stage_as stage_links stage_symlinks:
41257bf5f75SSimon J. Gerraty.else
41357bf5f75SSimon J. Gerraty# allow targets like beforeinstall to be leveraged
41457bf5f75SSimon J. GerratyDESTDIR= ${STAGE_OBJTOP}
4154d7ae5efSBryan Drewery.export DESTDIR
41657bf5f75SSimon J. Gerraty
417fa3afecfSBryan Drewery.if target(beforeinstall)
4181da13532SBryan Drewery.if !empty(_LIBS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
4198fe4beeeSSimon J. Gerratystaging: beforeinstall
42057bf5f75SSimon J. Gerraty.endif
42157bf5f75SSimon J. Gerraty.endif
42257bf5f75SSimon J. Gerraty
42357bf5f75SSimon J. Gerraty# normally only libs and includes are staged
4241da13532SBryan Drewery.if ${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG)
42557bf5f75SSimon J. GerratySTAGE_DIR.prog= ${STAGE_OBJTOP}${BINDIR}
42657bf5f75SSimon J. Gerraty
4273cf3d039SBryan Drewery.if !empty(PROG)
42857bf5f75SSimon J. Gerraty.if defined(PROGNAME)
42957bf5f75SSimon J. GerratySTAGE_AS_SETS+= prog
43057bf5f75SSimon J. GerratySTAGE_AS_${PROG}= ${PROGNAME}
43157bf5f75SSimon J. Gerratystage_as.prog: ${PROG}
43257bf5f75SSimon J. Gerraty.else
43357bf5f75SSimon J. GerratySTAGE_SETS+= prog
43457bf5f75SSimon J. Gerratystage_files.prog: ${PROG}
435eef72759SSimon J. GerratySTAGE_TARGETS+= stage_files
43657bf5f75SSimon J. Gerraty.endif
43757bf5f75SSimon J. Gerraty.endif
43857bf5f75SSimon J. Gerraty.endif
43957bf5f75SSimon J. Gerraty
44057bf5f75SSimon J. Gerraty.if !empty(_LIBS) && !defined(INTERNALLIB)
441f187ca8cSSimon J. Gerraty.if defined(SHLIBDIR) && ${SHLIBDIR} != ${LIBDIR} && ${_LIBS:Uno:M*.so.*} != ""
442f187ca8cSSimon J. GerratySTAGE_SETS+= shlib
443f187ca8cSSimon J. GerratySTAGE_DIR.shlib= ${STAGE_OBJTOP}${SHLIBDIR}
444f187ca8cSSimon J. GerratySTAGE_FILES.shlib+= ${_LIBS:M*.so.*}
445f187ca8cSSimon J. Gerratystage_files.shlib: ${_LIBS:M*.so.*}
446f187ca8cSSimon J. Gerraty.endif
447f187ca8cSSimon J. Gerraty
448f187ca8cSSimon J. Gerraty.if defined(SHLIB_LINK) && commands(${SHLIB_LINK:R}.ld)
449f187ca8cSSimon J. GerratySTAGE_AS_SETS+= ldscript
450f187ca8cSSimon J. GerratySTAGE_AS.ldscript+= ${SHLIB_LINK:R}.ld
451f187ca8cSSimon J. Gerratystage_as.ldscript: ${SHLIB_LINK:R}.ld
452f187ca8cSSimon J. GerratySTAGE_DIR.ldscript = ${STAGE_LIBDIR}
453f187ca8cSSimon J. GerratySTAGE_AS_${SHLIB_LINK:R}.ld:= ${SHLIB_LINK}
454f187ca8cSSimon J. GerratyNO_SHLIB_LINKS=
455f187ca8cSSimon J. Gerraty.endif
456f187ca8cSSimon J. Gerraty
4575ef0969eSSimon J. Gerraty.if defined(STATIC_LDSCRIPT) && target(lib${LIB}.ald)
4585ef0969eSSimon J. GerratySTAGE_AS_SETS+= ald
4595ef0969eSSimon J. GerratySTAGE_DIR.ald = ${STAGE_LIBDIR}
4605ef0969eSSimon J. GerratySTAGE_AS.ald+= lib${LIB}.ald
4615ef0969eSSimon J. GerratySTAGE_AS_lib${LIB}.ald = lib${LIB}.a
4625ef0969eSSimon J. Gerratystage_as.ald: lib${LIB}.ald
4635ef0969eSSimon J. Gerraty.endif
4645ef0969eSSimon J. Gerraty
465f187ca8cSSimon J. Gerraty.if target(stage_files.shlib)
46657bf5f75SSimon J. Gerratystage_libs: ${_LIBS}
467f187ca8cSSimon J. Gerraty.if defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
468f187ca8cSSimon J. Gerratystage_files.shlib: ${SHLIB_NAME}.symbols
469f187ca8cSSimon J. Gerraty.endif
470f187ca8cSSimon J. Gerraty.else
471f187ca8cSSimon J. Gerratystage_libs: ${_LIBS}
472f187ca8cSSimon J. Gerraty.endif
47372484446SSimon J. Gerraty.if defined(SHLIB_NAME) && defined(DEBUG_FLAGS) && target(${SHLIB_NAME}.symbols)
47472484446SSimon J. Gerratystage_libs: ${SHLIB_NAME}.symbols
47572484446SSimon J. Gerraty.endif
476f187ca8cSSimon J. Gerraty
47757bf5f75SSimon J. Gerraty.endif
47857bf5f75SSimon J. Gerraty
47957bf5f75SSimon J. Gerraty.if !empty(INCS) || !empty(INCSGROUPS) && target(buildincludes)
480f187ca8cSSimon J. Gerraty.if !defined(NO_BEFOREBUILD_INCLUDES)
481f187ca8cSSimon J. Gerratystage_includes: buildincludes
482f187ca8cSSimon J. Gerratybeforebuild: stage_includes
483f187ca8cSSimon J. Gerraty.endif
48457bf5f75SSimon J. Gerraty.endif
48557bf5f75SSimon J. Gerraty
48692054a53SSimon J. Gerraty.for t in stage_libs stage_files stage_as
48792054a53SSimon J. Gerraty.if target($t)
488eef72759SSimon J. GerratySTAGE_TARGETS+= $t
48992054a53SSimon J. Gerraty.endif
49092054a53SSimon J. Gerraty.endfor
49192054a53SSimon J. Gerraty
49257bf5f75SSimon J. Gerraty.if !empty(STAGE_AS_SETS)
493eef72759SSimon J. GerratySTAGE_TARGETS+= stage_as
49457bf5f75SSimon J. Gerraty.endif
49557bf5f75SSimon J. Gerraty
496a6589ab7SSimon J. Gerraty.if !empty(STAGE_TARGETS) || (${MK_STAGING_PROG} != "no" && !defined(INTERNALPROG))
49757bf5f75SSimon J. Gerraty
49857bf5f75SSimon J. Gerraty.if !empty(LINKS)
499eef72759SSimon J. GerratySTAGE_TARGETS+= stage_links
50072484446SSimon J. Gerraty.if ${MAKE_VERSION} < 20131001
50172484446SSimon J. Gerratystage_links.links: ${_LIBS} ${PROG}
50272484446SSimon J. Gerraty.endif
50357bf5f75SSimon J. GerratySTAGE_SETS+= links
50457bf5f75SSimon J. GerratySTAGE_LINKS.links= ${LINKS}
50557bf5f75SSimon J. Gerraty.endif
50657bf5f75SSimon J. Gerraty
507c04d716bSBryan Drewery.if !empty(SYMLINKS)
508eef72759SSimon J. GerratySTAGE_TARGETS+= stage_symlinks
50957bf5f75SSimon J. GerratySTAGE_SETS+= links
51057bf5f75SSimon J. GerratySTAGE_SYMLINKS.links= ${SYMLINKS}
51157bf5f75SSimon J. Gerraty.endif
51257bf5f75SSimon J. Gerraty
51357bf5f75SSimon J. Gerraty.endif
51457bf5f75SSimon J. Gerraty
51557bf5f75SSimon J. Gerraty.include <meta.stage.mk>
51657bf5f75SSimon J. Gerraty.endif
51757bf5f75SSimon J. Gerraty.endif
51857bf5f75SSimon J. Gerraty
51994086ceaSBryan Drewery.if defined(META_TARGETS)
52094086ceaSBryan Drewery.for _tgt in ${META_TARGETS}
52194086ceaSBryan Drewery.if target(${_tgt})
52294086ceaSBryan Drewery${_tgt}: ${META_DEPS}
52394086ceaSBryan Drewery.endif
52494086ceaSBryan Drewery.endfor
52594086ceaSBryan Drewery.endif
526b45ab4b0SSimon J. Gerraty
527b45ab4b0SSimon J. Gerraty# we are generally the last makefile read
528b45ab4b0SSimon J. GerratyCFLAGS+= ${CFLAGS_LAST}
529b45ab4b0SSimon J. GerratyCXXFLAGS+= ${CXXFLAGS_LAST}
530b45ab4b0SSimon J. GerratyLDFLAGS+= ${LDFLAGS_LAST}
531