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