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