]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - Makefile.inc1
Add mkimg to the list of bootstrap tools
[FreeBSD/FreeBSD.git] / Makefile.inc1
1 #
2 # $FreeBSD$
3 #
4 # Make command line options:
5 #       -DNO_CLEANDIR run ${MAKE} clean, instead of ${MAKE} cleandir
6 #       -DNO_CLEAN do not clean at all
7 #       -DDB_FROM_SRC use the user/group databases in src/etc instead of
8 #           the system database when installing.
9 #       -DNO_SHARE do not go into share subdir
10 #       -DKERNFAST define NO_KERNEL{CONFIG,CLEAN,OBJ}
11 #       -DNO_KERNELCONFIG do not run config in ${MAKE} buildkernel
12 #       -DNO_KERNELCLEAN do not run ${MAKE} clean in ${MAKE} buildkernel
13 #       -DNO_KERNELOBJ do not run ${MAKE} obj in ${MAKE} buildkernel
14 #       -DNO_PORTSUPDATE do not update ports in ${MAKE} update
15 #       -DNO_ROOT install without using root privilege
16 #       -DNO_DOCUPDATE do not update doc in ${MAKE} update
17 #       -DWITHOUT_CTF do not run the DTrace CTF conversion tools on built objects
18 #       LOCAL_DIRS="list of dirs" to add additional dirs to the SUBDIR list
19 #       LOCAL_ITOOLS="list of tools" to add additional tools to the ITOOLS list
20 #       LOCAL_LIB_DIRS="list of dirs" to add additional dirs to libraries target
21 #       LOCAL_MTREE="list of mtree files" to process to allow local directories
22 #           to be created before files are installed
23 #       LOCAL_LEGACY_DIRS="list of dirs" to add additional dirs to the legacy
24 #           target
25 #       LOCAL_BSTOOL_DIRS="list of dirs" to add additional dirs to the
26 #           bootstrap-tools target
27 #       LOCAL_TOOL_DIRS="list of dirs" to add additional dirs to the build-tools
28 #           target
29 #       LOCAL_XTOOL_DIRS="list of dirs" to add additional dirs to the
30 #           cross-tools target
31 #       METALOG="path to metadata log" to write permission and ownership
32 #           when NO_ROOT is set.  (default: ${DESTDIR}/${DISTDIR}/METALOG,
33 #           check /etc/make.conf for DISTDIR)
34 #       TARGET="machine" to crossbuild world for a different machine type
35 #       TARGET_ARCH= may be required when a TARGET supports multiple endians
36 #       BUILDENV_SHELL= shell to launch for the buildenv target (def:${SHELL})
37 #       WORLD_FLAGS= additional flags to pass to make(1) during buildworld
38 #       KERNEL_FLAGS= additional flags to pass to make(1) during buildkernel
39 #       SUBDIR_OVERRIDE="list of dirs" to build rather than everything.
40 #           All libraries and includes, and some build tools will still build.
41
42 #
43 # The intended user-driven targets are:
44 # buildworld  - rebuild *everything*, including glue to help do upgrades
45 # installworld- install everything built by "buildworld"
46 # checkworld  - run test suite on installed world
47 # doxygen     - build API documentation of the kernel
48 # update      - convenient way to update your source tree (eg: svn/svnup)
49 #
50 # Standard targets (not defined here) are documented in the makefiles in
51 # /usr/share/mk.  These include:
52 #               obj depend all install clean cleandepend cleanobj
53
54 .if !defined(TARGET) || !defined(TARGET_ARCH)
55 .error "Both TARGET and TARGET_ARCH must be defined."
56 .endif
57
58 .if make(showconfig) || make(test-system-*)
59 _MKSHOWCONFIG=  t
60 .endif
61
62 SRCDIR?=        ${.CURDIR}
63 LOCALBASE?=     /usr/local
64
65 .include "share/mk/src.tools.mk"
66
67 # Cross toolchain changes must be in effect before bsd.compiler.mk
68 # so that gets the right CC, and pass CROSS_TOOLCHAIN to submakes.
69 .if defined(CROSS_TOOLCHAIN)
70 .if exists(${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk)
71 .include "${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk"
72 .elif exists(/usr/share/toolchains/${CROSS_TOOLCHAIN}.mk)
73 .include "/usr/share/toolchains/${CROSS_TOOLCHAIN}.mk"
74 .elif exists(${CROSS_TOOLCHAIN})
75 .include "${CROSS_TOOLCHAIN}"
76 .else
77 .error CROSS_TOOLCHAIN ${CROSS_TOOLCHAIN} not found
78 .endif
79 CROSSENV+=CROSS_TOOLCHAIN="${CROSS_TOOLCHAIN}"
80 .endif
81 .if defined(CROSS_TOOLCHAIN_PREFIX)
82 CROSS_COMPILER_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
83 .endif
84
85 XCOMPILERS=     CC CXX CPP
86 .for COMPILER in ${XCOMPILERS}
87 .if defined(CROSS_COMPILER_PREFIX)
88 X${COMPILER}?=  ${CROSS_COMPILER_PREFIX}${${COMPILER}}
89 .else
90 X${COMPILER}?=  ${${COMPILER}}
91 .endif
92 .endfor
93 # If a full path to an external cross compiler is given, don't build
94 # a cross compiler.
95 .if ${XCC:N${CCACHE_BIN}:M/*}
96 MK_CLANG_BOOTSTRAP=     no
97 .endif
98
99 # Pull in compiler metadata from buildworld/toolchain if possible to avoid
100 # running CC from bsd.compiler.mk.
101 .if make(installworld) || make(install) || make(distributeworld) || \
102     make(stageworld)
103 .-include "${OBJTOP}/toolchain-metadata.mk"
104 .if !defined(_LOADED_TOOLCHAIN_METADATA)
105 .error A build is required first.  You may have the wrong MAKEOBJDIRPREFIX set.
106 .endif
107 .endif
108
109 # Pull in COMPILER_TYPE and COMPILER_FREEBSD_VERSION early. Pull it from the
110 # tree to be friendlier to foreign OS builds. It's safe to do so unconditionally
111 # here since we will always have the right make, unlike in src/Makefile
112 # Don't include bsd.linker.mk yet until XBINUTILS is handled (after src.opts.mk)
113 _NO_INCLUDE_LINKERMK=   t
114 # We also want the X_COMPILER* variables if we are using an external toolchain.
115 _WANT_TOOLCHAIN_CROSS_VARS=     t
116 .include "share/mk/bsd.compiler.mk"
117 .undef _NO_INCLUDE_LINKERMK
118 .undef _WANT_TOOLCHAIN_CROSS_VARS
119 # src.opts.mk depends on COMPILER_FEATURES
120 .include "share/mk/src.opts.mk"
121
122 .if ${TARGET} == ${MACHINE}
123 TARGET_CPUTYPE?=${CPUTYPE}
124 .else
125 TARGET_CPUTYPE?=
126 .endif
127 .if !empty(TARGET_CPUTYPE)
128 _TARGET_CPUTYPE=${TARGET_CPUTYPE}
129 .else
130 _TARGET_CPUTYPE=dummy
131 .endif
132 .if ${TARGET} == "arm"
133 .if ${TARGET_ARCH:Marmv[67]*} != "" && ${TARGET_CPUTYPE:M*soft*} == ""
134 TARGET_ABI=     gnueabihf
135 .else
136 TARGET_ABI=     gnueabi
137 .endif
138 .endif
139 MACHINE_ABI?=   unknown
140 MACHINE_TRIPLE?=${MACHINE_ARCH:S/amd64/x86_64/:C/[hs]f$//:S/mipsn32/mips64/}-${MACHINE_ABI}-freebsd13.0
141 TARGET_ABI?=    unknown
142 TARGET_TRIPLE?= ${TARGET_ARCH:S/amd64/x86_64/:C/[hs]f$//:S/mipsn32/mips64/}-${TARGET_ABI}-freebsd13.0
143 KNOWN_ARCHES?=  aarch64/arm64 \
144                 amd64 \
145                 armv6/arm \
146                 armv7/arm \
147                 i386 \
148                 mips \
149                 mipsel/mips \
150                 mips64el/mips \
151                 mipsn32el/mips \
152                 mips64/mips \
153                 mipsn32/mips \
154                 mipshf/mips \
155                 mipselhf/mips \
156                 mips64elhf/mips \
157                 mips64hf/mips \
158                 powerpc \
159                 powerpc64/powerpc \
160                 powerpc64le/powerpc \
161                 powerpcspe/powerpc \
162                 riscv64/riscv \
163                 riscv64sf/riscv
164
165 .if ${TARGET} == ${TARGET_ARCH}
166 _t=             ${TARGET}
167 .else
168 _t=             ${TARGET_ARCH}/${TARGET}
169 .endif
170 .for _t in ${_t}
171 .if empty(KNOWN_ARCHES:M${_t})
172 .error Unknown target ${TARGET_ARCH}:${TARGET}.
173 .endif
174 .endfor
175
176 .if ${.MAKE.OS} != "FreeBSD"
177 CROSSBUILD_HOST=${.MAKE.OS}
178 .if ${.MAKE.OS} != "Linux" && ${.MAKE.OS} != "Darwin"
179 .warning "Unsupported crossbuild system: ${.MAKE.OS}. Build will probably fail!"
180 .endif
181 # We need to force NO_ROOT/DB_FROM_SRC builds when building on other operating
182 # systems since the BSD.foo.dist specs contain users and groups that do not
183 # exist by default on a Linux/MacOS system.
184 NO_ROOT:=       1
185 DB_FROM_SRC:=   1
186 .export NO_ROOT
187 .endif
188
189 # If all targets are disabled for system llvm then don't expect it to work
190 # for cross-builds.
191 .if !defined(TOOLS_PREFIX) && ${MK_LLVM_TARGET_ALL} == "no" && \
192     ${MACHINE} != ${TARGET} && ${MACHINE_ARCH} != ${TARGET_ARCH} && \
193     !make(showconfig)
194 MK_SYSTEM_COMPILER=     no
195 MK_SYSTEM_LINKER=       no
196 .endif
197
198 # Handle external binutils.
199 .if defined(CROSS_TOOLCHAIN_PREFIX)
200 CROSS_BINUTILS_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
201 .endif
202 XBINUTILS=      AS AR LD NM OBJCOPY RANLIB SIZE STRINGS STRIPBIN
203 .for BINUTIL in ${XBINUTILS}
204 .if defined(CROSS_BINUTILS_PREFIX) && \
205     exists(${CROSS_BINUTILS_PREFIX}/${${BINUTIL}})
206 X${BINUTIL}?=   ${CROSS_BINUTILS_PREFIX:C,/*$,,}/${${BINUTIL}}
207 .else
208 X${BINUTIL}?=   ${${BINUTIL}}
209 .endif
210 .endfor
211
212 # If a full path to an external linker is given, don't build lld.
213 .if ${XLD:M/*}
214 MK_LLD_BOOTSTRAP=       no
215 .endif
216
217 # We also want the X_LINKER* variables if we are using an external toolchain.
218 _WANT_TOOLCHAIN_CROSS_VARS=     t
219 .include "share/mk/bsd.linker.mk"
220 .undef _WANT_TOOLCHAIN_CROSS_VARS
221
222 # Begin WITH_SYSTEM_COMPILER / WITH_SYSTEM_LD
223
224 # WITH_SYSTEM_COMPILER - Pull in needed values and make a decision.
225
226 # Check if there is a local compiler that can satisfy as an external compiler.
227 # Which compiler is expected to be used?
228 .if ${MK_CLANG_BOOTSTRAP} == "yes"
229 WANT_COMPILER_TYPE=     clang
230 .else
231 WANT_COMPILER_TYPE=
232 .endif
233
234 .if !defined(WANT_COMPILER_FREEBSD_VERSION) && !make(showconfig) && \
235     !make(test-system-linker)
236 .if ${WANT_COMPILER_TYPE} == "clang"
237 WANT_COMPILER_FREEBSD_VERSION_FILE= lib/clang/freebsd_cc_version.h
238 WANT_COMPILER_FREEBSD_VERSION!= \
239         awk '$$2 == "FREEBSD_CC_VERSION" {printf("%d\n", $$3)}' \
240         ${SRCDIR}/${WANT_COMPILER_FREEBSD_VERSION_FILE} || echo unknown
241 WANT_COMPILER_VERSION_FILE= lib/clang/include/clang/Basic/Version.inc
242 WANT_COMPILER_VERSION!= \
243         awk '$$2 == "CLANG_VERSION" {split($$3, a, "."); print a[1] * 10000 + a[2] * 100 + a[3]}' \
244         ${SRCDIR}/${WANT_COMPILER_VERSION_FILE} || echo unknown
245 .endif
246 .export WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_VERSION
247 .endif  # !defined(WANT_COMPILER_FREEBSD_VERSION)
248
249 # It needs to be the same revision as we would build for the bootstrap.
250 # If the expected vs CC is different then we can't skip.
251 # GCC cannot be used for cross-arch yet.  For clang we pass -target later if
252 # TARGET_ARCH!=MACHINE_ARCH.
253 .if ${MK_SYSTEM_COMPILER} == "yes" && \
254     defined(WANT_COMPILER_FREEBSD_VERSION) && \
255     ${MK_CLANG_BOOTSTRAP} == "yes" && \
256     !make(xdev*) && \
257     ${X_COMPILER_TYPE} == ${WANT_COMPILER_TYPE} && \
258     (${X_COMPILER_TYPE} == "clang" || ${TARGET_ARCH} == ${MACHINE_ARCH}) && \
259     ${X_COMPILER_VERSION} == ${WANT_COMPILER_VERSION} && \
260     ${X_COMPILER_FREEBSD_VERSION} == ${WANT_COMPILER_FREEBSD_VERSION}
261 # Everything matches, disable the bootstrap compiler.
262 MK_CLANG_BOOTSTRAP=     no
263 USING_SYSTEM_COMPILER=  yes
264 .endif  # ${WANT_COMPILER_TYPE} == ${COMPILER_TYPE}
265
266 # WITH_SYSTEM_LD - Pull in needed values and make a decision.
267
268 # Check if there is a local linker that can satisfy as an external linker.
269 # Which linker is expected to be used?
270 .if ${MK_LLD_BOOTSTRAP} == "yes"
271 WANT_LINKER_TYPE=               lld
272 .else
273 WANT_LINKER_TYPE=
274 .endif
275
276 .if !defined(WANT_LINKER_FREEBSD_VERSION) && !make(showconfig) && \
277     !make(test-system-compiler)
278 .if ${WANT_LINKER_TYPE} == "lld"
279 WANT_LINKER_FREEBSD_VERSION_FILE= lib/clang/include/VCSVersion.inc
280 _WANT_LINKER_FREEBSD_VERSION!= \
281         awk '$$2 == "LLD_REVISION" {gsub(/"/, "", $$3); print $$3}' \
282         ${SRCDIR}/${WANT_LINKER_FREEBSD_VERSION_FILE} || echo unknown
283 WANT_LINKER_FREEBSD_VERSION=${_WANT_LINKER_FREEBSD_VERSION:C/.*-(.*)/\1/}
284 WANT_LINKER_VERSION_FILE= lib/clang/include/lld/Common/Version.inc
285 WANT_LINKER_VERSION!= \
286         awk '$$2 == "LLD_VERSION_STRING" { gsub("\"", "", $$3); split($$3, a, "."); print a[1] * 10000 + a[2] * 100 + a[3]}' \
287         ${SRCDIR}/${WANT_LINKER_VERSION_FILE} || echo unknown
288 .else
289 WANT_LINKER_FREEBSD_VERSION_FILE=
290 WANT_LINKER_FREEBSD_VERSION=
291 .endif
292 .export WANT_LINKER_FREEBSD_VERSION WANT_LINKER_VERSION
293 .endif  # !defined(WANT_LINKER_FREEBSD_VERSION)
294
295 .if ${MK_SYSTEM_LINKER} == "yes" && \
296     defined(WANT_LINKER_FREEBSD_VERSION) && \
297     (${MK_LLD_BOOTSTRAP} == "yes") && \
298     !make(xdev*) && \
299     ${X_LINKER_TYPE} == ${WANT_LINKER_TYPE} && \
300     ${X_LINKER_VERSION} == ${WANT_LINKER_VERSION} && \
301     ${X_LINKER_FREEBSD_VERSION} == ${WANT_LINKER_FREEBSD_VERSION}
302 # Everything matches, disable the bootstrap linker.
303 MK_LLD_BOOTSTRAP=       no
304 USING_SYSTEM_LINKER=    yes
305 .endif  # ${WANT_LINKER_TYPE} == ${LINKER_TYPE}
306
307 # WITH_SYSTEM_COMPILER / WITH_SYSTEM_LINKER - Handle defaults and debug.
308 USING_SYSTEM_COMPILER?= no
309 USING_SYSTEM_LINKER?=   no
310
311 TEST_SYSTEM_COMPILER_VARS= \
312         USING_SYSTEM_COMPILER MK_SYSTEM_COMPILER \
313         MK_CROSS_COMPILER MK_CLANG_BOOTSTRAP \
314         WANT_COMPILER_TYPE WANT_COMPILER_VERSION WANT_COMPILER_VERSION_FILE \
315         WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_FREEBSD_VERSION_FILE \
316         CC COMPILER_TYPE COMPILER_FEATURES COMPILER_VERSION \
317         COMPILER_FREEBSD_VERSION \
318         XCC X_COMPILER_TYPE X_COMPILER_FEATURES X_COMPILER_VERSION \
319         X_COMPILER_FREEBSD_VERSION
320 TEST_SYSTEM_LINKER_VARS= \
321         USING_SYSTEM_LINKER MK_SYSTEM_LINKER \
322         MK_LLD_BOOTSTRAP \
323         WANT_LINKER_TYPE WANT_LINKER_VERSION WANT_LINKER_VERSION_FILE \
324         WANT_LINKER_FREEBSD_VERSION WANT_LINKER_FREEBSD_VERSION_FILE \
325         LD LINKER_TYPE LINKER_FEATURES LINKER_VERSION \
326         LINKER_FREEBSD_VERSION \
327         XLD X_LINKER_TYPE X_LINKER_FEATURES X_LINKER_VERSION \
328         X_LINKER_FREEBSD_VERSION
329
330 .for _t in compiler linker
331 test-system-${_t}: .PHONY
332 .for v in ${TEST_SYSTEM_${_t:tu}_VARS}
333         ${_+_}@printf "%-35s= %s\n" "${v}" "${${v}}"
334 .endfor
335 .endfor
336 .if (make(buildworld) || make(buildkernel) || make(kernel-toolchain) || \
337     make(toolchain) || make(_cross-tools))
338 .if ${USING_SYSTEM_COMPILER} == "yes"
339 .info SYSTEM_COMPILER: Determined that CC=${CC} matches the source tree.  Not bootstrapping a cross-compiler.
340 .elif ${MK_CLANG_BOOTSTRAP} == "yes"
341 .info SYSTEM_COMPILER: libclang will be built for bootstrapping a cross-compiler.
342 .endif
343 .if ${USING_SYSTEM_LINKER} == "yes"
344 .info SYSTEM_LINKER: Determined that LD=${LD} matches the source tree.  Not bootstrapping a cross-linker.
345 .elif ${MK_LLD_BOOTSTRAP} == "yes"
346 .info SYSTEM_LINKER: libclang will be built for bootstrapping a cross-linker.
347 .endif
348 .endif
349
350 # End WITH_SYSTEM_COMPILER / WITH_SYSTEM_LD
351
352 # Store some compiler metadata for use in installworld where we don't
353 # want to invoke CC at all.
354 _TOOLCHAIN_METADATA_VARS=       COMPILER_VERSION \
355                                 COMPILER_TYPE \
356                                 COMPILER_FEATURES \
357                                 COMPILER_FREEBSD_VERSION \
358                                 COMPILER_RESOURCE_DIR \
359                                 LINKER_VERSION \
360                                 LINKER_FEATURES \
361                                 LINKER_TYPE \
362                                 LINKER_FREEBSD_VERSION
363 toolchain-metadata.mk: .PHONY .META
364         @: > ${.TARGET}
365         @echo ".info Using cached toolchain metadata from build at $$(hostname) on $$(date)" \
366             > ${.TARGET}
367         @echo "_LOADED_TOOLCHAIN_METADATA=t" >> ${.TARGET}
368 .for v in ${_TOOLCHAIN_METADATA_VARS}
369         @echo "${v}=${${v}}" >> ${.TARGET}
370         @echo "X_${v}=${X_${v}}" >> ${.TARGET}
371 .endfor
372         @echo ".export ${_TOOLCHAIN_METADATA_VARS}" >> ${.TARGET}
373         @echo ".export ${_TOOLCHAIN_METADATA_VARS:C,^,X_,}" >> ${.TARGET}
374
375
376 # We must do lib/ and libexec/ before bin/ in case of a mid-install error to
377 # keep the users system reasonably usable.  For static->dynamic root upgrades,
378 # we don't want to install a dynamic binary without rtld and the needed
379 # libraries.  More commonly, for dynamic root, we don't want to install a
380 # binary that requires a newer library version that hasn't been installed yet.
381 # This ordering is not a guarantee though.  The only guarantee of a working
382 # system here would require fine-grained ordering of all components based
383 # on their dependencies.
384 .if !empty(SUBDIR_OVERRIDE)
385 SUBDIR= ${SUBDIR_OVERRIDE}
386 .else
387 SUBDIR= lib libexec
388 # Add LOCAL_LIB_DIRS, but only if they will not be picked up as a SUBDIR
389 # of a LOCAL_DIRS directory.  This allows LOCAL_DIRS=foo and
390 # LOCAL_LIB_DIRS=foo/lib to behave as expected.
391 .for _DIR in ${LOCAL_DIRS:M*/} ${LOCAL_DIRS:N*/:S|$|/|}
392 _REDUNDANT_LIB_DIRS+=    ${LOCAL_LIB_DIRS:M${_DIR}*}
393 .endfor
394 .for _DIR in ${LOCAL_LIB_DIRS}
395 .if ${_DIR} == ".WAIT" || (empty(_REDUNDANT_LIB_DIRS:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile))
396 SUBDIR+=        ${_DIR}
397 .endif
398 .endfor
399 .if !defined(NO_ROOT) && (make(installworld) || make(install))
400 # Ensure libraries are installed before progressing.
401 SUBDIR+=.WAIT
402 .endif
403 SUBDIR+=bin
404 .if ${MK_CDDL} != "no"
405 SUBDIR+=cddl
406 .endif
407 SUBDIR+=gnu include
408 .if ${MK_KERBEROS} != "no"
409 SUBDIR+=kerberos5
410 .endif
411 .if ${MK_RESCUE} != "no"
412 SUBDIR+=rescue
413 .endif
414 SUBDIR+=sbin
415 .if ${MK_CRYPT} != "no"
416 SUBDIR+=secure
417 .endif
418 .if !defined(NO_SHARE)
419 SUBDIR+=share
420 .endif
421 .if ${MK_BOOT} != "no"
422 SUBDIR+=stand
423 .endif
424 SUBDIR+=sys usr.bin usr.sbin
425 .if ${MK_TESTS} != "no"
426 SUBDIR+=        tests
427 .endif
428
429 # Local directories are built in parallel with the base system directories.
430 # Users may insert a .WAIT directive at the beginning or elsewhere within
431 # the LOCAL_DIRS and LOCAL_LIB_DIRS lists as needed.
432 .for _DIR in ${LOCAL_DIRS}
433 .if ${_DIR} == ".WAIT" || exists(${.CURDIR}/${_DIR}/Makefile)
434 SUBDIR+=        ${_DIR}
435 .endif
436 .endfor
437
438 # We must do etc/ last as it hooks into building the man whatis file
439 # by calling 'makedb' in share/man.  This is only relevant for
440 # install/distribute so they build the whatis file after every manpage is
441 # installed.
442 .if make(installworld) || make(install)
443 SUBDIR+=.WAIT
444 .endif
445 SUBDIR+=etc
446
447 .endif  # !empty(SUBDIR_OVERRIDE)
448
449 .if defined(NOCLEAN)
450 .warning The src.conf WITHOUT_CLEAN option can now be used instead of NOCLEAN.
451 MK_CLEAN:=      no
452 .endif
453 .if defined(NO_CLEAN)
454 .info The src.conf WITHOUT_CLEAN option can now be used instead of NO_CLEAN.
455 MK_CLEAN:=      no
456 .endif
457 .if defined(NO_CLEANDIR)
458 CLEANDIR=       clean cleandepend
459 .else
460 CLEANDIR=       cleandir
461 .endif
462
463 .if defined(WORLDFAST)
464 MK_CLEAN:=      no
465 NO_OBJWALK=     t
466 .endif
467
468 .if ${MK_META_MODE} == "yes"
469 # If filemon is used then we can rely on the build being incremental-safe.
470 # The .meta files will also track the build command and rebuild should
471 # it change.
472 .if empty(.MAKE.MODE:Mnofilemon)
473 MK_CLEAN:=      no
474 .endif
475 .endif
476 .if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
477 NO_OBJWALK=     t
478 NO_KERNELOBJ=   t
479 .endif
480 .if !defined(NO_OBJWALK)
481 _obj=           obj
482 .endif
483
484 LOCAL_TOOL_DIRS?=
485 PACKAGEDIR?=    ${DESTDIR}/${DISTDIR}
486
487 .if empty(SHELL:M*csh*)
488 BUILDENV_SHELL?=${SHELL}
489 .else
490 BUILDENV_SHELL?=/bin/sh
491 .endif
492
493 .if !defined(_MKSHOWCONFIG)
494 .if !defined(SVN_CMD) || empty(SVN_CMD)
495 . for _P in /usr/bin /usr/local/bin
496 .  for _S in svn svnlite
497 .   if exists(${_P}/${_S})
498 SVN_CMD=   ${_P}/${_S}
499 .   endif
500 .  endfor
501 . endfor
502 .export SVN_CMD
503 .endif
504 SVNFLAGS?=      -r HEAD
505 .if !defined(VCS_REVISION) || empty(VCS_REVISION)
506 .if !defined(SVNVERSION_CMD) || empty(SVNVERSION_CMD)
507 . for _D in ${PATH:S,:, ,g}
508 .  if exists(${_D}/svnversion)
509 SVNVERSION_CMD?=${_D}/svnversion
510 .  endif
511 .  if exists(${_D}/svnliteversion)
512 SVNVERSION_CMD?=${_D}/svnliteversion
513 .  endif
514 . endfor
515 .endif
516 _VCS_REVISION?= $$(eval ${SVNVERSION_CMD} ${SRCDIR})
517 . if !empty(_VCS_REVISION)
518 VCS_REVISION=   $$(echo r${_VCS_REVISION})
519 . endif
520 .export VCS_REVISION
521 .endif
522
523 .if !defined(GIT_CMD) || empty(GIT_CMD)
524 . for _P in /usr/bin /usr/local/bin
525 .  if exists(${_P}/git)
526 GIT_CMD=   ${_P}/git
527 .  endif
528 . endfor
529 .export GIT_CMD
530 .endif
531
532 .if !defined(OSRELDATE)
533 .if exists(/usr/include/osreldate.h)
534 OSRELDATE!=     awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
535                 /usr/include/osreldate.h
536 .else
537 OSRELDATE=      0
538 .endif
539 .export OSRELDATE
540 .endif
541
542 # Set VERSION for CTFMERGE to use via the default CTFFLAGS=-L VERSION.
543 .if !defined(_REVISION)
544 _REVISION!=     ${MAKE} -C ${SRCDIR}/release MK_AUTO_OBJ=no -V REVISION
545 .export _REVISION
546 .endif
547 .if !defined(_BRANCH)
548 _BRANCH!=       ${MAKE} -C ${SRCDIR}/release MK_AUTO_OBJ=no -V BRANCH
549 .export _BRANCH
550 .endif
551 .if !defined(SRCRELDATE)
552 SRCRELDATE!=    awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
553                 ${SRCDIR}/sys/sys/param.h
554 .export SRCRELDATE
555 .endif
556 .if !defined(VERSION)
557 VERSION=        FreeBSD ${_REVISION}-${_BRANCH:C/-p[0-9]+$//} ${TARGET_ARCH} ${SRCRELDATE}
558 .export VERSION
559 .endif
560
561 .if !defined(PKG_VERSION)
562 .if ${_BRANCH:MSTABLE*} || ${_BRANCH:MCURRENT*}
563 TIMENOW=        %Y%m%d%H%M%S
564 EXTRA_REVISION= .s${TIMENOW:gmtime}
565 .elif ${_BRANCH:MALPHA*}
566 EXTRA_REVISION= _${_BRANCH:C/-ALPHA/.a/}
567 .elif ${_BRANCH:MBETA*}
568 EXTRA_REVISION= _${_BRANCH:C/-BETA/.b/}
569 .elif ${_BRANCH:MRC*}
570 EXTRA_REVISION= _${_BRANCH:C/-RC/.r/}
571 .elif ${_BRANCH:MPRERELEASE*}
572 EXTRA_REVISION= _${_BRANCH:C/-PRERELEASE/.p/}
573 .elif ${_BRANCH:M*-p*}
574 EXTRA_REVISION= _${_BRANCH:C/.*-p([0-9]+$)/\1/}
575 .endif
576 PKG_VERSION:=   ${_REVISION}${EXTRA_REVISION}
577 .endif
578 .endif  # !defined(PKG_VERSION)
579
580 .if !defined(PKG_TIMESTAMP)
581 TIMEEPOCHNOW=           %s
582 SOURCE_DATE_EPOCH=      ${TIMEEPOCHNOW:gmtime}
583 .else
584 SOURCE_DATE_EPOCH=      ${PKG_TIMESTAMP}
585 .endif
586
587 PKG_NAME_PREFIX?=       FreeBSD
588 PKG_MAINTAINER?=        re@FreeBSD.org
589 PKG_WWW?=               https://www.FreeBSD.org
590 .export PKG_NAME_PREFIX
591 .export PKG_MAINTAINER
592 .export PKG_WWW
593
594 .if !defined(_MKSHOWCONFIG)
595 _CPUTYPE!=      MAKEFLAGS= CPUTYPE=${_TARGET_CPUTYPE} ${MAKE} -f /dev/null \
596                 -m ${.CURDIR}/share/mk MK_AUTO_OBJ=no -V CPUTYPE
597 .if ${_CPUTYPE} != ${_TARGET_CPUTYPE}
598 .error CPUTYPE global should be set with ?=.
599 .endif
600 .endif
601 .if make(buildworld)
602 BUILD_ARCH!=    uname -p
603 # On some Linux systems uname -p returns "unknown" so skip this check there.
604 # This check only exists to tell people to use TARGET_ARCH instead of
605 # MACHINE_ARCH so skipping it when crossbuilding on non-FreeBSD should be fine.
606 .if ${MACHINE_ARCH} != ${BUILD_ARCH} && ${.MAKE.OS} == "FreeBSD"
607 .error To cross-build, set TARGET_ARCH.
608 .endif
609 .endif
610 WORLDTMP?=      ${OBJTOP}/tmp
611 BPATH=          ${CCACHE_WRAPPER_PATH_PFX}${WORLDTMP}/legacy/usr/sbin:${WORLDTMP}/legacy/usr/bin:${WORLDTMP}/legacy/bin:${WORLDTMP}/legacy/usr/libexec
612 XPATH=          ${WORLDTMP}/bin:${WORLDTMP}/usr/sbin:${WORLDTMP}/usr/bin
613
614 # When building we want to find the cross tools before the host tools in ${BPATH}.
615 # We also need to add UNIVERSE_TOOLCHAIN_PATH so that we can find the shared
616 # toolchain files (clang, lld, etc.) during make universe/tinderbox
617 STRICTTMPPATH=  ${XPATH}:${BPATH}:${UNIVERSE_TOOLCHAIN_PATH}
618 # We should not be using tools from /usr/bin accidentally since this could cause
619 # the build to break on other systems that don't have that tool. For now we
620 # still allow using the old behaviour (inheriting $PATH) if
621 # BUILD_WITH_STRICT_TMPPATH is set to 0 but this will eventually be removed.
622
623 # Currently strict $PATH can cause build failures and does not work yet with
624 # USING_SYSTEM_LINKER/USING_SYSTEM_COMPILER. Once these issues have been
625 # resolved it will be turned on by default.
626 BUILD_WITH_STRICT_TMPPATH?=0
627 .if defined(CROSSBUILD_HOST)
628 # When building on non-FreeBSD we can't rely on the tools in /usr/bin being compatible
629 # with what FreeBSD expects. Therefore we only use tools from STRICTTMPPATH
630 # during the world build stage. We build most tools during the bootstrap-tools
631 # phase but symlink host tools that are known to work instead of building them
632 BUILD_WITH_STRICT_TMPPATH:=1
633 .endif
634 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
635 TMPPATH=        ${STRICTTMPPATH}
636 .else
637 TMPPATH=        ${STRICTTMPPATH}:${PATH}
638 .endif
639
640 #
641 # Avoid running mktemp(1) unless actually needed.
642 # It may not be functional, e.g., due to new ABI
643 # when in the middle of installing over this system.
644 #
645 .if make(distributeworld) || make(installworld) || make(stageworld)
646 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
647 MKTEMP=${WORLDTMP}/legacy/usr/bin/mktemp
648 .if !exists(${MKTEMP})
649 .error "mktemp binary doesn't exist in expected location: ${MKTEMP}"
650 .endif
651 .else
652 MKTEMP=mktemp
653 .endif
654 INSTALLTMP!=    ${MKTEMP} -d -u -t install
655 .endif
656
657 .if make(stagekernel) || make(distributekernel)
658 TAGS+=          kernel
659 PACKAGE=        kernel
660 .endif
661
662 #
663 # Building a world goes through the following stages
664 #
665 # 1. legacy stage [BMAKE]
666 #       This stage is responsible for creating compatibility
667 #       shims that are needed by the bootstrap-tools,
668 #       build-tools and cross-tools stages. These are generally
669 #       APIs that tools from one of those three stages need to
670 #       build that aren't present on the host.
671 # 1. bootstrap-tools stage [BMAKE]
672 #       This stage is responsible for creating programs that
673 #       are needed for backward compatibility reasons. They
674 #       are not built as cross-tools.
675 # 2. build-tools stage [TMAKE]
676 #       This stage is responsible for creating the object
677 #       tree and building any tools that are needed during
678 #       the build process. Some programs are listed during
679 #       this phase because they build binaries to generate
680 #       files needed to build these programs. This stage also
681 #       builds the 'build-tools' target rather than 'all'.
682 # 3. cross-tools stage [XMAKE]
683 #       This stage is responsible for creating any tools that
684 #       are needed for building the system. A cross-compiler is one
685 #       of them. This differs from build tools in two ways:
686 #       1. the 'all' target is built rather than 'build-tools'
687 #       2. these tools are installed into TMPPATH for stage 4.
688 # 4. world stage [WMAKE]
689 #       This stage actually builds the world.
690 # 5. install stage (optional) [IMAKE]
691 #       This stage installs a previously built world.
692 #
693
694 BOOTSTRAPPING?= 0
695 # Keep these in sync
696 MINIMUM_SUPPORTED_OSREL?= 1002501
697 MINIMUM_SUPPORTED_REL?= 10.3
698
699 # Common environment for world related stages
700 CROSSENV+=      \
701                 MACHINE_ARCH=${TARGET_ARCH} \
702                 MACHINE=${TARGET} \
703                 CPUTYPE=${TARGET_CPUTYPE}
704 .if ${MK_META_MODE} != "no"
705 # Don't rebuild build-tools targets during normal build.
706 CROSSENV+=      BUILD_TOOLS_META=.NOMETA
707 .endif
708 .if defined(TARGET_CFLAGS)
709 CROSSENV+=      ${TARGET_CFLAGS}
710 .endif
711 .if (${TARGET} != ${MACHINE} && !defined(WITH_LOCAL_MODULES)) || \
712     defined(WITHOUT_LOCAL_MODULES)
713 CROSSENV+=      LOCAL_MODULES=
714 .endif
715
716 BOOTSTRAPPING_OSRELDATE?=${OSRELDATE}
717
718 # bootstrap-tools stage
719 BMAKEENV=       INSTALL="sh ${.CURDIR}/tools/install.sh" \
720                 TOOLS_PREFIX=${TOOLS_PREFIX_UNDEF:U${WORLDTMP}} \
721                 PATH=${BPATH}:${PATH} \
722                 WORLDTMP=${WORLDTMP} \
723                 MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
724 # need to keep this in sync with targets/pseudo/bootstrap-tools/Makefile
725 BSARGS=         DESTDIR= \
726                 OBJTOP='${WORLDTMP}/obj-tools' \
727                 OBJROOT='$${OBJTOP}/' \
728                 MAKEOBJDIRPREFIX= \
729                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
730                 BWPHASE=${.TARGET:C,^_,,} \
731                 SSP_CFLAGS= \
732                 MK_HTML=no NO_LINT=yes MK_MAN=no MK_MAN_UTILS=yes \
733                 -DNO_PIC MK_PROFILE=no -DNO_SHARED \
734                 -DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
735                 MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
736                 MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no \
737                 MK_INCLUDES=yes
738
739 BMAKE=          \
740                 ${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
741                 ${BSARGS}
742 .if empty(.MAKEOVERRIDES:MMK_LLVM_TARGET_ALL)
743 BMAKE+=         MK_LLVM_TARGET_ALL=no
744 .endif
745
746 # build-tools stage
747 TMAKE=          \
748                 ${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
749                 TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
750                 DESTDIR= \
751                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
752                 BWPHASE=${.TARGET:C,^_,,} \
753                 SSP_CFLAGS= \
754                 -DNO_LINT \
755                 -DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
756                 MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
757                 MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no
758
759 # cross-tools stage
760 # TOOLS_PREFIX set in BMAKE
761 XMAKE=          ${BMAKE} \
762                 TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
763                 MK_CLANG_IS_CC=${MK_CLANG_BOOTSTRAP} \
764                 MK_TESTS=no
765
766 # kernel-tools stage
767 KTMAKEENV=      INSTALL="sh ${.CURDIR}/tools/install.sh" \
768                 PATH=${BPATH}:${PATH} \
769                 WORLDTMP=${WORLDTMP} \
770                 MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
771
772 KTMAKE=         \
773                 TOOLS_PREFIX=${TOOLS_PREFIX_UNDEF:U${WORLDTMP}} \
774                 ${KTMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
775                 DESTDIR= \
776                 OBJTOP='${WORLDTMP}/obj-kernel-tools' \
777                 OBJROOT='$${OBJTOP}/' \
778                 MAKEOBJDIRPREFIX= \
779                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
780                 SSP_CFLAGS= \
781                 MK_HTML=no -DNO_LINT MK_MAN=no \
782                 -DNO_PIC MK_PROFILE=no -DNO_SHARED \
783                 -DNO_CPU_CFLAGS MK_RETPOLINE=no MK_WARNS=no MK_CTF=no
784
785 # world stage
786 WMAKEENV=       ${CROSSENV} \
787                 INSTALL="${INSTALL_CMD} -U" \
788                 PATH=${TMPPATH} \
789                 SYSROOT=${WORLDTMP}
790
791 # make hierarchy
792 HMAKE=          PATH=${TMPPATH} ${MAKE} LOCAL_MTREE=${LOCAL_MTREE:Q}
793 .if defined(NO_ROOT)
794 HMAKE+=         PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT
795 .endif
796
797 CROSSENV+=      CC="${XCC} ${XCFLAGS}" CXX="${XCXX} ${XCXXFLAGS} ${XCFLAGS}" \
798                 CPP="${XCPP} ${XCFLAGS}" \
799                 AS="${XAS}" AR="${XAR}" LD="${XLD}" LLVM_LINK="${XLLVM_LINK}" \
800                 NM=${XNM} OBJCOPY="${XOBJCOPY}" \
801                 RANLIB=${XRANLIB} STRINGS=${XSTRINGS} \
802                 SIZE="${XSIZE}" STRIPBIN="${XSTRIPBIN}"
803
804 .if defined(CROSS_BINUTILS_PREFIX) && exists(${CROSS_BINUTILS_PREFIX})
805 # In the case of xdev-build tools, CROSS_BINUTILS_PREFIX won't be a
806 # directory, but the compiler will look in the right place for its
807 # tools so we don't need to tell it where to look.
808 BFLAGS+=        -B${CROSS_BINUTILS_PREFIX}
809 .endif
810
811
812 # The internal bootstrap compiler has a default sysroot set by TOOLS_PREFIX
813 # and target set by TARGET/TARGET_ARCH.  However, there are several needs to
814 # always pass an explicit --sysroot and -target.
815 # - External compiler needs sysroot and target flags.
816 # - External ld needs sysroot.
817 # - To be clear about the use of a sysroot when using the internal compiler.
818 # - Easier debugging.
819 # - Allowing WITH_SYSTEM_COMPILER+WITH_META_MODE to work together due to
820 #   the flip-flopping build command when sometimes using external and
821 #   sometimes using internal.
822 # - Allow using lld which has no support for default paths.
823 .if !defined(CROSS_BINUTILS_PREFIX) || !exists(${CROSS_BINUTILS_PREFIX})
824 BFLAGS+=        -B${WORLDTMP}/usr/bin
825 .endif
826 .if ${WANT_COMPILER_TYPE} == gcc || \
827     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
828 .elif ${WANT_COMPILER_TYPE} == clang || \
829     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == clang)
830 XCFLAGS+=       -target ${TARGET_TRIPLE}
831 .endif
832 XCFLAGS+=       --sysroot=${WORLDTMP}
833
834 .if !empty(BFLAGS)
835 XCFLAGS+=       ${BFLAGS}
836 .endif
837
838 .if ${MK_LIB32} == "yes"
839 _LIBCOMPAT= 32
840 .include "Makefile.libcompat"
841 .elif ${MK_LIBSOFT} == "yes"
842 _LIBCOMPAT= SOFT
843 .include "Makefile.libcompat"
844 .endif
845
846 # META_MODE normally ignores host file changes since every build updates
847 # timestamps (see NO_META_IGNORE_HOST in sys.mk).  There are known times
848 # when the ABI breaks though that we want to force rebuilding WORLDTMP
849 # to get updated host tools.
850 .if ${MK_META_MODE} == "yes" && ${MK_CLEAN} == "no" && \
851     !defined(NO_META_IGNORE_HOST) && !defined(NO_META_IGNORE_HOST_HEADERS) && \
852     !defined(_MKSHOWCONFIG)
853 # r318736 - ino64 major ABI breakage
854 META_MODE_BAD_ABI_VERS+=        1200031
855
856 .if !defined(OBJDIR_HOST_OSRELDATE)
857 .if exists(${OBJTOP}/host-osreldate.h)
858 OBJDIR_HOST_OSRELDATE!= \
859     awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
860     ${OBJTOP}/host-osreldate.h
861 .elif exists(${WORLDTMP}/usr/include/osreldate.h)
862 OBJDIR_HOST_OSRELDATE=  0
863 .endif
864 .export OBJDIR_HOST_OSRELDATE
865 .endif
866
867 # Note that this logic is the opposite of normal BOOTSTRAP handling.  We want
868 # to compare the WORLDTMP's OSRELDATE to the host's OSRELDATE.  If the WORLDTMP
869 # is older than the ABI-breakage OSRELDATE of the HOST then we rebuild.
870 .if defined(OBJDIR_HOST_OSRELDATE)
871 .for _ver in ${META_MODE_BAD_ABI_VERS}
872 .if ${OSRELDATE} >= ${_ver} && ${OBJDIR_HOST_OSRELDATE} < ${_ver}
873 _meta_mode_need_rebuild=        ${_ver}
874 .endif
875 .endfor
876 .if defined(_meta_mode_need_rebuild)
877 .info META_MODE: Rebuilding host tools due to ABI breakage in __FreeBSD_version ${_meta_mode_need_rebuild}.
878 NO_META_IGNORE_HOST_HEADERS=    1
879 .export NO_META_IGNORE_HOST_HEADERS
880 .endif  # defined(_meta_mode_need_rebuild)
881 .endif  # defined(OBJDIR_HOST_OSRELDATE)
882 .endif  # ${MK_META_MODE} == "yes" && ${MK_CLEAN} == "no" ...
883 # This is only used for META_MODE+filemon to track what the oldest
884 # __FreeBSD_version is in WORLDTMP.  This purposely does NOT have
885 # a make dependency on /usr/include/osreldate.h as the file should
886 # only be copied when it is missing or meta mode determines it has changed.
887 # Since host files are normally ignored without NO_META_IGNORE_HOST
888 # the file will never be updated unless that flag is specified.  This
889 # allows tracking the oldest osreldate to force rebuilds via
890 # META_MODE_BADABI_REVS above.
891 host-osreldate.h: # DO NOT ADD /usr/include/osreldate.h here
892 .if !defined(CROSSBUILD_HOST)
893         @cp -f /usr/include/osreldate.h ${.TARGET}
894 .else
895         @echo "#ifndef __FreeBSD_version" > ${.TARGET}
896         @echo "#define __FreeBSD_version ${OSRELDATE}" >> ${.TARGET}
897         @echo "#endif" >> ${.TARGET}
898 .endif
899
900 WMAKE=          ${WMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
901                 BWPHASE=${.TARGET:C,^_,,} \
902                 DESTDIR=${WORLDTMP}
903
904 IMAKEENV=       ${CROSSENV}
905 IMAKE=          ${IMAKEENV} ${MAKE} -f Makefile.inc1 \
906                 ${IMAKE_INSTALL} ${IMAKE_MTREE}
907 .if empty(.MAKEFLAGS:M-n)
908 IMAKEENV+=      PATH=${STRICTTMPPATH}:${INSTALLTMP} \
909                 LD_LIBRARY_PATH=${INSTALLTMP} \
910                 PATH_LOCALE=${INSTALLTMP}/locale
911 IMAKE+=         __MAKE_SHELL=${INSTALLTMP}/sh
912 .else
913 IMAKEENV+=      PATH=${TMPPATH}:${INSTALLTMP}
914 .endif
915
916 # When generating install media, do not allow user and group information from
917 # the build host to affect the contents of the distribution.
918 .if make(distributeworld) || make(distrib-dirs) || make(distribution) || \
919     make(stageworld)
920 DB_FROM_SRC=    yes
921 .endif
922
923 .if defined(DB_FROM_SRC)
924 INSTALLFLAGS+=  -N ${.CURDIR}/etc
925 MTREEFLAGS+=    -N ${.CURDIR}/etc
926 .endif
927 _INSTALL_DDIR=  ${DESTDIR}/${DISTDIR}
928 INSTALL_DDIR=   ${_INSTALL_DDIR:S://:/:g:C:/$::}
929 .if defined(NO_ROOT)
930 METALOG?=       ${DESTDIR}/${DISTDIR}/METALOG
931 METALOG:=       ${METALOG:C,//+,/,g}
932 IMAKE+=         -DNO_ROOT METALOG=${METALOG}
933 METALOG_INSTALLFLAGS=   -U -M ${METALOG} -D ${INSTALL_DDIR}
934 INSTALLFLAGS+=  ${METALOG_INSTALLFLAGS}
935 CERTCTLFLAGS=   ${METALOG_INSTALLFLAGS}
936 MTREEFLAGS+=    -W
937 .endif
938 .if defined(BUILD_PKGS)
939 INSTALLFLAGS+=  -h sha256
940 .endif
941 .if defined(DB_FROM_SRC) || defined(NO_ROOT)
942 IMAKE_INSTALL=  INSTALL="${INSTALL_CMD} ${INSTALLFLAGS}"
943 IMAKE_MTREE=    MTREE_CMD="${MTREE_CMD} ${MTREEFLAGS}"
944 .endif
945 .if make(distributeworld)
946 CERTCTLDESTDIR= ${DESTDIR}/${DISTDIR}/base
947 .else
948 CERTCTLDESTDIR= ${DESTDIR}
949 .endif
950
951 DESTDIR_MTREEFLAGS=     -deU
952 # When creating worldtmp we don't need to set the directories as owned by root
953 # so we also pass -W
954 WORLDTMP_MTREEFLAGS=    -deUW
955 .if defined(NO_ROOT)
956 # When building with -DNO_ROOT we shouldn't be changing the directories
957 # that are created by mtree to be owned by root/wheel.
958 DESTDIR_MTREEFLAGS+=    -W
959 .endif
960 DISTR_MTREE=    ${MTREE_CMD}
961 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
962 DISTR_MTREE=    ${WORLDTMP}/legacy/usr/sbin/mtree
963 .endif
964 WORLDTMP_MTREE= ${DISTR_MTREE} ${WORLDTMP_MTREEFLAGS}
965 DESTDIR_MTREE=  ${DISTR_MTREE} ${DESTDIR_MTREEFLAGS}
966
967 # kernel stage
968 KMAKEENV=       ${WMAKEENV:NSYSROOT=*}
969 KMAKE=          ${KMAKEENV} ${MAKE} ${.MAKEFLAGS} ${KERNEL_FLAGS} KERNEL=${INSTKERNNAME}
970
971 #
972 # buildworld
973 #
974 # Attempt to rebuild the entire system, with reasonable chance of
975 # success, regardless of how old your existing system is.
976 #
977 _sanity_check: .PHONY .MAKE
978 .if ${.CURDIR:C/[^,]//g} != ""
979 #       The m4 build of sendmail files doesn't like it if ',' is used
980 #       anywhere in the path of it's files.
981         @echo
982         @echo "*** Error: path to source tree contains a comma ','"
983         @echo
984         @false
985 .elif ${.CURDIR:M*\:*} != ""
986 #       Using ':' leaks into PATH and breaks finding cross-tools.
987         @echo
988         @echo "*** Error: path to source tree contains a colon ':'"
989         @echo
990         @false
991 .endif
992
993 # Our current approach to dependency tracking cannot cope with certain source
994 # tree changes, particularly with respect to removing source files and
995 # replacing generated files.  Handle these cases here in an ad-hoc fashion.
996 _cleanobj_fast_depend_hack: .PHONY
997         @echo ">>> Deleting stale dependencies...";
998         sh ${.CURDIR}/tools/build/depend-cleanup.sh ${OBJTOP}
999
1000 _worldtmp: .PHONY
1001         @echo
1002         @echo "--------------------------------------------------------------"
1003         @echo ">>> Rebuilding the temporary build tree"
1004         @echo "--------------------------------------------------------------"
1005 .if ${MK_CLEAN} == "yes"
1006         rm -rf ${WORLDTMP}
1007 .else
1008 # Note: for delete-old we need to set $PATH to also include the host $PATH
1009 # since otherwise a partial build with missing symlinks in ${WORLDTMP}/legacy/
1010 # will fail to run due to missing binaries. $WMAKE sets PATH to only ${TMPPATH}
1011 # so we remove that assingnment from $WMAKE and prepend the new $PATH
1012         ${_+_}@if [ -e "${WORLDTMP}" ]; then \
1013                 echo ">>> Deleting stale files in build tree..."; \
1014                 cd ${.CURDIR}; env PATH=${TMPPATH}:${PATH} ${WMAKE:NPATH=*} \
1015                     _NO_INCLUDE_COMPILERMK=t -DBATCH_DELETE_OLD_FILES delete-old \
1016                     delete-old-libs >/dev/null; \
1017         fi
1018         rm -rf ${WORLDTMP}/legacy/usr/include
1019 .if ${USING_SYSTEM_COMPILER} == "yes"
1020 .for cc in cc c++
1021         if [ -x ${WORLDTMP}/usr/bin/${cc} ]; then \
1022                 inum=$$(stat -f %i ${WORLDTMP}/usr/bin/${cc}); \
1023                 find ${WORLDTMP}/usr/bin -inum $${inum} -delete; \
1024         fi
1025 .endfor
1026 .endif  # ${USING_SYSTEM_COMPILER} == "yes"
1027 .if ${USING_SYSTEM_LINKER} == "yes"
1028         @rm -f ${WORLDTMP}/usr/bin/ld ${WORLDTMP}/usr/bin/ld.lld
1029 .endif  # ${USING_SYSTEM_LINKER} == "yes"
1030 .endif  # ${MK_CLEAN} == "yes"
1031         @mkdir -p ${WORLDTMP}
1032         @touch ${WORLDTMP}/${.TARGET}
1033 # We can't use mtree to create the worldtmp directories since it may not be
1034 # available on the target system (this happens e.g. when building on non-FreeBSD)
1035         cd ${.CURDIR}/tools/build; \
1036             ${MAKE} DIRPRFX=tools/build/ DESTDIR=${WORLDTMP}/legacy installdirs
1037 # In order to build without inheriting $PATH we need to add symlinks to the host
1038 # tools in $WORLDTMP for the tools that we don't build during bootstrap-tools
1039         cd ${.CURDIR}/tools/build; \
1040             ${MAKE} DIRPRFX=tools/build/ DESTDIR=${WORLDTMP}/legacy host-symlinks
1041
1042 _legacy:
1043         @echo
1044         @echo "--------------------------------------------------------------"
1045         @echo ">>> stage 1.1: legacy release compatibility shims"
1046         @echo "--------------------------------------------------------------"
1047         ${_+_}cd ${.CURDIR}; ${BMAKE} legacy
1048 _bootstrap-tools:
1049         @echo
1050         @echo "--------------------------------------------------------------"
1051         @echo ">>> stage 1.2: bootstrap tools"
1052         @echo "--------------------------------------------------------------"
1053         ${_+_}cd ${.CURDIR}; ${BMAKE} bootstrap-tools
1054         mkdir -p ${WORLDTMP}/usr ${WORLDTMP}/lib/casper ${WORLDTMP}/lib/geom \
1055             ${WORLDTMP}/bin
1056         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1057             -p ${WORLDTMP}/usr >/dev/null
1058         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1059             -p ${WORLDTMP}/usr/include >/dev/null
1060         ln -sf ${.CURDIR}/sys ${WORLDTMP}
1061 .if ${MK_DEBUG_FILES} != "no"
1062         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1063             -p ${WORLDTMP}/usr/lib >/dev/null
1064 .endif
1065 .for _mtree in ${LOCAL_MTREE}
1066         ${WORLDTMP_MTREE} -f ${.CURDIR}/${_mtree} -p ${WORLDTMP} > /dev/null
1067 .endfor
1068 _cleanobj:
1069 .if ${MK_CLEAN} == "yes"
1070         @echo
1071         @echo "--------------------------------------------------------------"
1072         @echo ">>> stage 2.1: cleaning up the object tree"
1073         @echo "--------------------------------------------------------------"
1074         # Avoid including bsd.compiler.mk in clean and obj with _NO_INCLUDE_COMPILERMK
1075         # since the restricted $PATH might not contain a valid cc binary
1076         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t ${CLEANDIR}
1077 .if defined(_LIBCOMPAT)
1078         ${_+_}cd ${.CURDIR}; ${LIBCOMPATWMAKE} _NO_INCLUDE_COMPILERMK=t -f Makefile.inc1 ${CLEANDIR}
1079 .endif
1080 .else
1081         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t _cleanobj_fast_depend_hack
1082 .endif  # ${MK_CLEAN} == "yes"
1083 _obj:
1084         @echo
1085         @echo "--------------------------------------------------------------"
1086         @echo ">>> stage 2.2: rebuilding the object tree"
1087         @echo "--------------------------------------------------------------"
1088         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t obj
1089 _build-tools:
1090         @echo
1091         @echo "--------------------------------------------------------------"
1092         @echo ">>> stage 2.3: build tools"
1093         @echo "--------------------------------------------------------------"
1094         ${_+_}cd ${.CURDIR}; ${TMAKE} build-tools
1095 _cross-tools:
1096         @echo
1097         @echo "--------------------------------------------------------------"
1098         @echo ">>> stage 3: cross tools"
1099         @echo "--------------------------------------------------------------"
1100         @rm -f ${OBJTOP}/toolchain-metadata.mk
1101         ${_+_}cd ${.CURDIR}; ${XMAKE} cross-tools
1102         ${_+_}cd ${.CURDIR}; ${XMAKE} kernel-tools
1103 _build-metadata:
1104         @echo
1105         @echo "--------------------------------------------------------------"
1106         @echo ">>> stage 3.1: recording build metadata"
1107         @echo "--------------------------------------------------------------"
1108         ${_+_}cd ${.CURDIR}; ${WMAKE} toolchain-metadata.mk
1109         ${_+_}cd ${.CURDIR}; ${WMAKE} host-osreldate.h
1110 _includes:
1111         @echo
1112         @echo "--------------------------------------------------------------"
1113         @echo ">>> stage 4.1: building includes"
1114         @echo "--------------------------------------------------------------"
1115 # Special handling for SUBDIR_OVERRIDE in buildworld as they most likely need
1116 # headers from default SUBDIR.  Do SUBDIR_OVERRIDE includes last.
1117         ${_+_}cd ${.CURDIR}; ${WMAKE} SUBDIR_OVERRIDE= SHARED=symlinks \
1118             MK_INCLUDES=yes includes
1119 .if !empty(SUBDIR_OVERRIDE) && make(buildworld)
1120         ${_+_}cd ${.CURDIR}; ${WMAKE} MK_INCLUDES=yes SHARED=symlinks includes
1121 .endif
1122 _libraries:
1123         @echo
1124         @echo "--------------------------------------------------------------"
1125         @echo ">>> stage 4.2: building libraries"
1126         @echo "--------------------------------------------------------------"
1127         ${_+_}cd ${.CURDIR}; \
1128             ${WMAKE} -DNO_FSCHG MK_HTML=no -DNO_LINT MK_MAN=no \
1129             MK_PROFILE=no MK_TESTS=no MK_TESTS_SUPPORT=${MK_TESTS_SUPPORT} \
1130             libraries
1131 everything: .PHONY
1132         @echo
1133         @echo "--------------------------------------------------------------"
1134         @echo ">>> stage 4.4: building everything"
1135         @echo "--------------------------------------------------------------"
1136         ${_+_}cd ${.CURDIR}; _PARALLEL_SUBDIR_OK=1 ${WMAKE} all
1137
1138 WMAKE_TGTS=
1139 .if !defined(WORLDFAST)
1140 WMAKE_TGTS+=    _sanity_check _worldtmp _legacy
1141 .if empty(SUBDIR_OVERRIDE)
1142 WMAKE_TGTS+=    _bootstrap-tools
1143 .endif
1144 WMAKE_TGTS+=    _cleanobj
1145 .if !defined(NO_OBJWALK)
1146 WMAKE_TGTS+=    _obj
1147 .endif
1148 WMAKE_TGTS+=    _build-tools _cross-tools
1149 WMAKE_TGTS+=    _build-metadata
1150 WMAKE_TGTS+=    _includes
1151 .endif
1152 .if !defined(NO_LIBS)
1153 WMAKE_TGTS+=    _libraries
1154 .endif
1155 .if defined(_LIBCOMPAT) && empty(SUBDIR_OVERRIDE)
1156 WMAKE_TGTS+=    build${libcompat}
1157 .endif
1158 WMAKE_TGTS+=    everything
1159
1160 # record buildworld time in seconds
1161 .if make(buildworld)
1162 _BUILDWORLD_START!= date '+%s'
1163 .export _BUILDWORLD_START
1164 .endif
1165
1166 buildworld: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue .PHONY
1167 .ORDER: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
1168
1169 _ncpu_cmd=sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo unknown
1170
1171 buildworld_prologue: .PHONY
1172         @echo "--------------------------------------------------------------"
1173         @echo ">>> World build started on `LC_ALL=C date`"
1174         @echo "--------------------------------------------------------------"
1175
1176 buildworld_epilogue: .PHONY
1177         @echo
1178         @echo "--------------------------------------------------------------"
1179         @echo ">>> World build completed on `LC_ALL=C date`"
1180         @seconds=$$(($$(date '+%s') - ${_BUILDWORLD_START})); \
1181           echo -n ">>> World built in $$seconds seconds, "; \
1182           echo "ncpu: $$(${_ncpu_cmd})${.MAKE.JOBS:S/^/, make -j/}"
1183         @echo "--------------------------------------------------------------"
1184
1185 #
1186 # We need to have this as a target because the indirection between Makefile
1187 # and Makefile.inc1 causes the correct PATH to be used, rather than a
1188 # modification of the current environment's PATH.  In addition, we need
1189 # to quote multiword values.
1190 #
1191 buildenvvars: .PHONY
1192         @echo ${WMAKEENV:Q} ${.MAKE.EXPORTED:@v@$v=\"${$v}\"@}
1193
1194 .if ${.TARGETS:Mbuildenv}
1195 .if ${.MAKEFLAGS:M-j}
1196 .error The buildenv target is incompatible with -j
1197 .endif
1198 .endif
1199 BUILDENV_DIR?=  ${.CURDIR}
1200 #
1201 # Note: make will report any errors the shell reports. This can
1202 # be odd if the last command in an interactive shell generates an
1203 # error or is terminated by SIGINT. These reported errors look bad,
1204 # but are harmless. Allowing them also allows BUIDLENV_SHELL to
1205 # be a complex command whose status will be returned to the caller.
1206 # Some scripts in tools rely on this behavior to report build errors.
1207 #
1208 buildenv: .PHONY
1209         @echo Entering world for ${TARGET_ARCH}:${TARGET}
1210 .if ${BUILDENV_SHELL:M*zsh*}
1211         @echo For ZSH you must run: export CPUTYPE=${TARGET_CPUTYPE}
1212 .endif
1213         @cd ${BUILDENV_DIR} && env ${WMAKEENV} \
1214         INSTALL="${INSTALL_CMD} ${INSTALLFLAGS}" \
1215         MTREE_CMD="${MTREE_CMD} ${MTREEFLAGS}" BUILDENV=1 ${BUILDENV_SHELL}
1216
1217 TOOLCHAIN_TGTS= ${WMAKE_TGTS:Neverything:Nbuild${libcompat}}
1218 toolchain: ${TOOLCHAIN_TGTS} .PHONY
1219 KERNEL_TOOLCHAIN_TGTS=  ${TOOLCHAIN_TGTS:N_obj:N_cleanobj:N_includes:N_libraries}
1220 .if make(kernel-toolchain)
1221 .ORDER: ${KERNEL_TOOLCHAIN_TGTS}
1222 .endif
1223 kernel-toolchain: ${KERNEL_TOOLCHAIN_TGTS} .PHONY
1224
1225 #
1226 # installcheck
1227 #
1228 # Checks to be sure system is ready for installworld/installkernel.
1229 #
1230 installcheck: _installcheck_world _installcheck_kernel .PHONY
1231 _installcheck_world: .PHONY
1232         @echo "--------------------------------------------------------------"
1233         @echo ">>> Install check world"
1234         @echo "--------------------------------------------------------------"
1235 _installcheck_kernel: .PHONY
1236         @echo "--------------------------------------------------------------"
1237         @echo ">>> Install check kernel"
1238         @echo "--------------------------------------------------------------"
1239
1240 #
1241 # Require DESTDIR to be set if installing for a different architecture or
1242 # using the user/group database in the source tree.
1243 #
1244 .if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${TARGET} != ${MACHINE} || \
1245     defined(DB_FROM_SRC)
1246 .if !make(distributeworld)
1247 _installcheck_world: __installcheck_DESTDIR
1248 _installcheck_kernel: __installcheck_DESTDIR
1249 __installcheck_DESTDIR: .PHONY
1250 .if !defined(DESTDIR) || empty(DESTDIR)
1251         @echo "ERROR: Please set DESTDIR!"; \
1252         false
1253 .endif
1254 .endif
1255 .endif
1256
1257 .if !defined(DB_FROM_SRC)
1258 #
1259 # Check for missing UIDs/GIDs.
1260 #
1261 CHECK_UIDS=     auditdistd
1262 CHECK_GIDS=     audit
1263 CHECK_UIDS+=    ntpd
1264 CHECK_GIDS+=    ntpd
1265 CHECK_UIDS+=    proxy
1266 CHECK_GIDS+=    proxy authpf
1267 CHECK_UIDS+=    smmsp
1268 CHECK_GIDS+=    smmsp
1269 CHECK_UIDS+=    unbound
1270 CHECK_GIDS+=    unbound
1271 _installcheck_world: __installcheck_UGID
1272 __installcheck_UGID: .PHONY
1273 .for uid in ${CHECK_UIDS}
1274         @if ! `id -u ${uid} >/dev/null 2>&1`; then \
1275                 echo "ERROR: Required ${uid} user is missing, see /usr/src/UPDATING."; \
1276                 false; \
1277         fi
1278 .endfor
1279 .for gid in ${CHECK_GIDS}
1280         @if ! `find / -prune -group ${gid} >/dev/null 2>&1`; then \
1281                 echo "ERROR: Required ${gid} group is missing, see /usr/src/UPDATING."; \
1282                 false; \
1283         fi
1284 .endfor
1285 .endif
1286 #
1287 # If installing over the running system (DESTDIR is / or unset) and the install
1288 # includes rescue, try running rescue from the objdir as a sanity check.  If
1289 # rescue is not functional (e.g., because it depends on a system call not
1290 # supported by the currently running kernel), abort the installation.
1291 #
1292 .if !make(distributeworld) && ${MK_RESCUE} != "no" && \
1293     (empty(DESTDIR) || ${DESTDIR} == "/") && empty(BYPASS_INSTALLCHECK_SH)
1294 _installcheck_world: __installcheck_sh_check
1295 __installcheck_sh_check: .PHONY
1296         @if [ "`${OBJTOP}/rescue/rescue/rescue sh -c 'echo OK'`" != \
1297             OK ]; then \
1298                 echo "rescue/sh check failed, installation aborted" >&2; \
1299                 false; \
1300         fi
1301 .endif
1302
1303 #
1304 # Required install tools to be saved in a scratch dir for safety.
1305 #
1306 .if ${MK_ZONEINFO} != "no"
1307 _zoneinfo=      zic tzsetup
1308 .endif
1309
1310 .if !defined(CROSSBUILD_HOST)
1311 _sysctl=sysctl
1312 .endif
1313
1314 ITOOLS= [ awk cap_mkdb cat chflags chmod chown cmp cp \
1315         date echo egrep find grep id install ${_install-info} \
1316         ln make mkdir mtree mv pwd_mkdb \
1317         rm sed services_mkdb sh sort strip ${_sysctl} test true uname wc \
1318         ${_zoneinfo} ${LOCAL_ITOOLS}
1319
1320 # Needed for share/man
1321 .if ${MK_MAN_UTILS} != "no"
1322 ITOOLS+=makewhatis
1323 .endif
1324
1325 #
1326 # distributeworld
1327 #
1328 # Distributes everything compiled by a `buildworld'.
1329 #
1330 # installworld
1331 #
1332 # Installs everything compiled by a 'buildworld'.
1333 #
1334
1335 # Non-base distributions produced by the base system
1336 EXTRA_DISTRIBUTIONS=
1337 .if defined(_LIBCOMPAT)
1338 EXTRA_DISTRIBUTIONS+=   lib${libcompat}
1339 .endif
1340 .if ${MK_TESTS} != "no"
1341 EXTRA_DISTRIBUTIONS+=   tests
1342 .endif
1343
1344 DEBUG_DISTRIBUTIONS=
1345 .if ${MK_DEBUG_FILES} != "no"
1346 DEBUG_DISTRIBUTIONS+=   base ${EXTRA_DISTRIBUTIONS:S,tests,,}
1347 .endif
1348
1349 MTREE_MAGIC?=   mtree 2.0
1350
1351 distributeworld installworld stageworld: _installcheck_world .PHONY
1352         mkdir -p ${INSTALLTMP}
1353         progs=$$(for prog in ${ITOOLS}; do \
1354                 if progpath=`env PATH=${TMPPATH} which $$prog`; then \
1355                         echo $$progpath; \
1356                 else \
1357                         echo "Required tool $$prog not found in PATH ($$PATH)." >&2; \
1358                         exit 1; \
1359                 fi; \
1360             done); \
1361         if [ -z "${CROSSBUILD_HOST}" ] ; then \
1362                 libs=$$(ldd -f "%o %p\n" -f "%o %p\n" $$progs 2>/dev/null | sort -u | \
1363                     while read line; do \
1364                         set -- $$line; \
1365                         if [ "$$2 $$3" != "not found" ]; then \
1366                                 echo $$2; \
1367                         else \
1368                                 echo "Required library $$1 not found." >&2; \
1369                                 exit 1; \
1370                         fi; \
1371                     done); \
1372         fi; \
1373         cp $$libs $$progs ${INSTALLTMP}
1374         cp -R $${PATH_LOCALE:-"/usr/share/locale"} ${INSTALLTMP}/locale
1375 .if defined(NO_ROOT)
1376         -mkdir -p ${METALOG:H}
1377         echo "#${MTREE_MAGIC}" > ${METALOG}
1378 .endif
1379 .if make(distributeworld)
1380 .for dist in ${EXTRA_DISTRIBUTIONS}
1381         -mkdir ${DESTDIR}/${DISTDIR}/${dist}
1382         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.root.dist \
1383             -p ${DESTDIR}/${DISTDIR}/${dist} >/dev/null
1384         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1385             -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1386         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1387             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/include >/dev/null
1388 .if ${MK_DEBUG_FILES} != "no"
1389         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1390             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib >/dev/null
1391 .endif
1392 .if defined(_LIBCOMPAT)
1393         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1394             -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1395 .if ${MK_DEBUG_FILES} != "no"
1396         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1397             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/usr >/dev/null
1398 .endif
1399 .endif
1400 .if ${MK_TESTS} != "no" && ${dist} == "tests"
1401         -mkdir -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE}
1402         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1403             -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE} >/dev/null
1404 .if ${MK_DEBUG_FILES} != "no"
1405         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1406             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/${TESTSBASE} >/dev/null
1407 .endif
1408 .endif
1409 .if defined(NO_ROOT)
1410         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.root.dist | \
1411             sed -e 's#^\./#./${dist}/#' >> ${METALOG}
1412         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.usr.dist | \
1413             sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1414         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.include.dist | \
1415             sed -e 's#^\./#./${dist}/usr/include/#' >> ${METALOG}
1416 .if defined(_LIBCOMPAT)
1417         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist | \
1418             sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1419 .endif
1420 .endif
1421 .endfor
1422         -mkdir ${DESTDIR}/${DISTDIR}/base
1423         ${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1424             METALOG=${METALOG} ${IMAKE_INSTALL} ${IMAKE_MTREE} \
1425             DISTBASE=/base DESTDIR=${DESTDIR}/${DISTDIR}/base \
1426             LOCAL_MTREE=${LOCAL_MTREE:Q} distrib-dirs
1427         ${INSTALL_SYMLINK} ${INSTALLFLAGS} usr/src/sys ${INSTALL_DDIR}/base/sys
1428 .endif # make(distributeworld)
1429         ${_+_}cd ${.CURDIR}; ${IMAKE} re${.TARGET:S/world$//}; \
1430             ${IMAKEENV} rm -rf ${INSTALLTMP}
1431 .if make(distributeworld)
1432 .for dist in ${EXTRA_DISTRIBUTIONS}
1433         find ${DESTDIR}/${DISTDIR}/${dist} -mindepth 1 -type d -empty -delete
1434 .endfor
1435 .if defined(NO_ROOT)
1436 .for dist in base ${EXTRA_DISTRIBUTIONS}
1437         @# For each file that exists in this dist, print the corresponding
1438         @# line from the METALOG.  This relies on the fact that
1439         @# a line containing only the filename will sort immediately before
1440         @# the relevant mtree line.
1441         cd ${DESTDIR}/${DISTDIR}; \
1442         find ./${dist} | sort -u ${METALOG} - | \
1443         awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1444         ${DESTDIR}/${DISTDIR}/${dist}.meta
1445 .endfor
1446 .for dist in ${DEBUG_DISTRIBUTIONS}
1447         @# For each file that exists in this dist, print the corresponding
1448         @# line from the METALOG.  This relies on the fact that
1449         @# a line containing only the filename will sort immediately before
1450         @# the relevant mtree line.
1451         cd ${DESTDIR}/${DISTDIR}; \
1452         find ./${dist}/usr/lib/debug | sort -u ${METALOG} - | \
1453         awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1454         ${DESTDIR}/${DISTDIR}/${dist}.debug.meta
1455 .endfor
1456 .endif
1457 .endif # make(distributeworld)
1458 .if !make(packageworld) && ${MK_CAROOT} != "no"
1459         @if which openssl>/dev/null; then \
1460                 DESTDIR=${CERTCTLDESTDIR} PATH=${TMPPATH}:${PATH} \
1461                     sh ${SRCTOP}/usr.sbin/certctl/certctl.sh ${CERTCTLFLAGS} rehash \
1462         else \
1463                 echo "No openssl on the host, not rehashing certificates target -- /etc/ssl may not be populated."; \
1464         fi
1465 .endif
1466
1467 packageworld: .PHONY
1468 .for dist in base ${EXTRA_DISTRIBUTIONS}
1469 .if defined(NO_ROOT)
1470         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1471             tar cvf - --exclude usr/lib/debug \
1472             @${DESTDIR}/${DISTDIR}/${dist}.meta | \
1473             ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1474 .else
1475         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1476             tar cvf - --exclude usr/lib/debug . | \
1477             ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1478 .endif
1479 .endfor
1480
1481 .for dist in ${DEBUG_DISTRIBUTIONS}
1482 . if defined(NO_ROOT)
1483         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1484             tar cvf - @${DESTDIR}/${DISTDIR}/${dist}.debug.meta | \
1485             ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1486 . else
1487         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1488             tar cvLf - usr/lib/debug | \
1489             ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1490 . endif
1491 .endfor
1492
1493 makeman: .PHONY
1494         ${_+_}cd ${.CURDIR}/tools/build/options; sh makeman > \
1495             ${.CURDIR}/share/man/man5/src.conf.5
1496
1497 # We can't assume here that ${TMPPATH} will include ${PATH} or /usr/libexec
1498 # because we may be building with a STRICTTMPPATH, so we explicitly include
1499 # /usr/libexec here for flua.  ${TMPPATH} still usefully includes anything else
1500 # we may need to function.
1501 _sysent_PATH=   ${TMPPATH}:/usr/libexec
1502 _sysent_dirs=   sys/kern
1503 _sysent_dirs+=  sys/compat/freebsd32
1504 _sysent_dirs+=  sys/compat/cloudabi32   \
1505                 sys/compat/cloudabi64
1506 _sysent_dirs+=  sys/amd64/linux         \
1507                 sys/amd64/linux32       \
1508                 sys/arm/linux           \
1509                 sys/arm64/linux         \
1510                 sys/i386/linux
1511
1512 sysent: .PHONY
1513 .for _dir in ${_sysent_dirs}
1514 sysent-${_dir}: .PHONY
1515         @echo "${MAKE} -C ${.CURDIR}/${_dir} sysent"
1516         ${_+_}@env PATH=${_sysent_PATH} ${MAKE} -C ${.CURDIR}/${_dir} sysent
1517
1518 sysent: sysent-${_dir}
1519 .endfor
1520
1521 #
1522 # reinstall
1523 #
1524 # If you have a build server, you can NFS mount the source and obj directories
1525 # and do a 'make reinstall' on the *client* to install new binaries from the
1526 # most recent server build.
1527 #
1528 restage reinstall: .MAKE .PHONY
1529         @echo "--------------------------------------------------------------"
1530         @echo ">>> Making hierarchy"
1531         @echo "--------------------------------------------------------------"
1532         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1533             LOCAL_MTREE=${LOCAL_MTREE:Q} hierarchy
1534 .if make(restage)
1535         @echo "--------------------------------------------------------------"
1536         @echo ">>> Making distribution"
1537         @echo "--------------------------------------------------------------"
1538         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1539             LOCAL_MTREE=${LOCAL_MTREE:Q} distribution
1540 .endif
1541         @echo
1542         @echo "--------------------------------------------------------------"
1543         @echo ">>> Installing everything started on `LC_ALL=C date`"
1544         @echo "--------------------------------------------------------------"
1545         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install
1546 .if defined(_LIBCOMPAT)
1547         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install${libcompat}
1548 .endif
1549         @echo "--------------------------------------------------------------"
1550         @echo ">>> Installing everything completed on `LC_ALL=C date`"
1551         @echo "--------------------------------------------------------------"
1552
1553 redistribute: .MAKE .PHONY
1554         @echo "--------------------------------------------------------------"
1555         @echo ">>> Distributing everything"
1556         @echo "--------------------------------------------------------------"
1557         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute
1558 .if defined(_LIBCOMPAT)
1559         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute${libcompat} \
1560             DISTRIBUTION=lib${libcompat}
1561 .endif
1562
1563 distrib-dirs distribution: .MAKE .PHONY
1564         ${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1565             ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
1566 .if make(distribution)
1567         ${_+_}cd ${.CURDIR}; ${CROSSENV} PATH=${TMPPATH} \
1568                 ${MAKE} -f Makefile.inc1 ${IMAKE_INSTALL} \
1569                 METALOG=${METALOG} MK_TESTS=no \
1570                 MK_TESTS_SUPPORT=${MK_TESTS_SUPPORT} installconfig
1571 .endif
1572
1573 #
1574 # buildkernel and installkernel
1575 #
1576 # Which kernels to build and/or install is specified by setting
1577 # KERNCONF. If not defined a GENERIC kernel is built/installed.
1578 # Only the existing (depending TARGET) config files are used
1579 # for building kernels and only the first of these is designated
1580 # as the one being installed.
1581 #
1582 # Note that we have to use TARGET instead of TARGET_ARCH when
1583 # we're in kernel-land. Since only TARGET_ARCH is (expected) to
1584 # be set to cross-build, we have to make sure TARGET is set
1585 # properly.
1586
1587 .if defined(KERNFAST)
1588 NO_KERNELCLEAN= t
1589 NO_KERNELCONFIG=        t
1590 NO_KERNELOBJ=           t
1591 # Shortcut for KERNCONF=Blah -DKERNFAST is now KERNFAST=Blah
1592 .if !defined(KERNCONF) && ${KERNFAST} != "1"
1593 KERNCONF=${KERNFAST}
1594 .endif
1595 .endif
1596 .if ${TARGET_ARCH} == "powerpc64"
1597 KERNCONF?=      GENERIC64
1598 .elif ${TARGET_ARCH} == "powerpc64le"
1599 KERNCONF?=      GENERIC64LE
1600 .elif ${TARGET_ARCH} == "powerpcspe"
1601 KERNCONF?=      MPC85XXSPE
1602 .else
1603 KERNCONF?=      GENERIC
1604 .endif
1605 INSTKERNNAME?=  kernel
1606
1607 KERNSRCDIR?=    ${.CURDIR}/sys
1608 KRNLCONFDIR=    ${KERNSRCDIR}/${TARGET}/conf
1609 KRNLOBJDIR=     ${OBJTOP}${KERNSRCDIR:C,^${.CURDIR},,}
1610 KERNCONFDIR?=   ${KRNLCONFDIR}
1611
1612 BUILDKERNELS=
1613 INSTALLKERNEL=
1614 .if defined(NO_INSTALLKERNEL)
1615 # All of the BUILDKERNELS loops start at index 1.
1616 BUILDKERNELS+= dummy
1617 .endif
1618 .for _kernel in ${KERNCONF}
1619 .if !defined(_MKSHOWCONFIG) && exists(${KERNCONFDIR}/${_kernel})
1620 BUILDKERNELS+=  ${_kernel}
1621 .if empty(INSTALLKERNEL) && !defined(NO_INSTALLKERNEL)
1622 INSTALLKERNEL= ${_kernel}
1623 .endif
1624 .else
1625 .if make(buildkernel)
1626 .error Missing KERNCONF ${KERNCONFDIR}/${_kernel}
1627 .endif
1628 .endif
1629 .endfor
1630
1631 _cleankernobj_fast_depend_hack: .PHONY
1632 # 20191009  r353340  removal of opensolaris_atomic.S (also r353381)
1633 .if ${MACHINE} != i386
1634 .for f in opensolaris_atomic
1635 .for m in opensolaris zfs
1636         @if [ -e "${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.o" ] && \
1637             grep -q ${f}.S "${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.o"; then \
1638                 echo "Removing stale dependencies for opensolaris_atomic"; \
1639                 rm -f ${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.*; \
1640         fi
1641 .endfor
1642 .endfor
1643 .endif
1644
1645 ${WMAKE_TGTS:N_worldtmp:Nbuild${libcompat}} ${.ALLTARGETS:M_*:N_worldtmp}: .MAKE .PHONY
1646
1647 # record kernel(s) build time in seconds
1648 .if make(buildkernel)
1649 _BUILDKERNEL_START!= date '+%s'
1650 .endif
1651
1652 #
1653 # buildkernel
1654 #
1655 # Builds all kernels defined by BUILDKERNELS.
1656 #
1657 buildkernel: .MAKE .PHONY
1658 .if empty(BUILDKERNELS:Ndummy)
1659         @echo "ERROR: Missing kernel configuration file(s) (${KERNCONF})."; \
1660         false
1661 .endif
1662         @echo
1663 .for _kernel in ${BUILDKERNELS:Ndummy}
1664         @echo "--------------------------------------------------------------"
1665         @echo ">>> Kernel build for ${_kernel} started on `LC_ALL=C date`"
1666         @echo "--------------------------------------------------------------"
1667         @echo "===> ${_kernel}"
1668         mkdir -p ${KRNLOBJDIR}
1669 .if !defined(NO_KERNELCONFIG)
1670         @echo
1671         @echo "--------------------------------------------------------------"
1672         @echo ">>> stage 1: configuring the kernel"
1673         @echo "--------------------------------------------------------------"
1674         cd ${KRNLCONFDIR}; \
1675                 PATH=${TMPPATH} \
1676                     config ${CONFIGARGS} -d ${KRNLOBJDIR}/${_kernel} \
1677                         -I '${KERNCONFDIR}' -I '${KRNLCONFDIR}' \
1678                         '${KERNCONFDIR}/${_kernel}'
1679 .endif
1680 .if ${MK_CLEAN} == "yes" && !defined(NO_KERNELCLEAN)
1681         @echo
1682         @echo "--------------------------------------------------------------"
1683         @echo ">>> stage 2.1: cleaning up the object tree"
1684         @echo "--------------------------------------------------------------"
1685         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} ${CLEANDIR}
1686 .else
1687         ${_+_}cd ${.CURDIR}; ${WMAKE} _cleankernobj_fast_depend_hack
1688 .endif
1689 .if !defined(NO_KERNELOBJ)
1690         @echo
1691         @echo "--------------------------------------------------------------"
1692         @echo ">>> stage 2.2: rebuilding the object tree"
1693         @echo "--------------------------------------------------------------"
1694         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} obj
1695 .endif
1696         @echo
1697         @echo "--------------------------------------------------------------"
1698         @echo ">>> stage 2.3: build tools"
1699         @echo "--------------------------------------------------------------"
1700         ${_+_}cd ${.CURDIR}; ${KTMAKE} kernel-tools
1701         @echo
1702         @echo "--------------------------------------------------------------"
1703         @echo ">>> stage 3.1: building everything"
1704         @echo "--------------------------------------------------------------"
1705         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} all -DNO_MODULES_OBJ
1706         @echo "--------------------------------------------------------------"
1707         @echo ">>> Kernel build for ${_kernel} completed on `LC_ALL=C date`"
1708         @echo "--------------------------------------------------------------"
1709
1710 .endfor
1711         @seconds=$$(($$(date '+%s') - ${_BUILDKERNEL_START})); \
1712           echo -n ">>> Kernel(s) ${BUILDKERNELS} built in $$seconds seconds, "; \
1713           echo "ncpu: $$(${_ncpu_cmd})${.MAKE.JOBS:S/^/, make -j/}"
1714         @echo "--------------------------------------------------------------"
1715
1716 NO_INSTALLEXTRAKERNELS?=        yes
1717
1718 #
1719 # installkernel, etc.
1720 #
1721 # Install the kernel defined by INSTALLKERNEL
1722 #
1723 installkernel installkernel.debug \
1724 reinstallkernel reinstallkernel.debug: _installcheck_kernel .PHONY
1725 .if !defined(NO_INSTALLKERNEL)
1726 .if empty(INSTALLKERNEL)
1727         @echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1728         false
1729 .endif
1730         @echo "--------------------------------------------------------------"
1731         @echo ">>> Installing kernel ${INSTALLKERNEL} on $$(LC_ALL=C date)"
1732         @echo "--------------------------------------------------------------"
1733         ${_+_}cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1734             ${CROSSENV} PATH=${TMPPATH} \
1735             ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME} ${.TARGET:S/kernel//}
1736         @echo "--------------------------------------------------------------"
1737         @echo ">>> Installing kernel ${INSTALLKERNEL} completed on $$(LC_ALL=C date)"
1738         @echo "--------------------------------------------------------------"
1739 .endif
1740 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1741 .for _kernel in ${BUILDKERNELS:[2..-1]}
1742         @echo "--------------------------------------------------------------"
1743         @echo ">>> Installing kernel ${_kernel} $$(LC_ALL=C date)"
1744         @echo "--------------------------------------------------------------"
1745         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; \
1746             ${CROSSENV} PATH=${TMPPATH} \
1747             ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME}.${_kernel} ${.TARGET:S/kernel//}
1748         @echo "--------------------------------------------------------------"
1749         @echo ">>> Installing kernel ${_kernel} completed on $$(LC_ALL=C date)"
1750         @echo "--------------------------------------------------------------"
1751 .endfor
1752 .endif
1753
1754 distributekernel distributekernel.debug: .PHONY
1755 .if !defined(NO_INSTALLKERNEL)
1756 .if empty(INSTALLKERNEL)
1757         @echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1758         false
1759 .endif
1760         mkdir -p ${DESTDIR}/${DISTDIR}
1761 .if defined(NO_ROOT)
1762         @echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.premeta
1763 .endif
1764         ${_+_}cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1765             ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.premeta/} \
1766             ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} KERNEL=${INSTKERNNAME} \
1767             DESTDIR=${INSTALL_DDIR}/kernel \
1768             ${.TARGET:S/distributekernel/install/}
1769 .if defined(NO_ROOT)
1770         @sed -e 's|^./kernel|.|' ${DESTDIR}/${DISTDIR}/kernel.premeta > \
1771             ${DESTDIR}/${DISTDIR}/kernel.meta
1772 .endif
1773 .endif
1774 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1775 .for _kernel in ${BUILDKERNELS:[2..-1]}
1776 .if defined(NO_ROOT)
1777         @echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta
1778 .endif
1779         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; \
1780             ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.${_kernel}.premeta/} \
1781             ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} \
1782             KERNEL=${INSTKERNNAME}.${_kernel} \
1783             DESTDIR=${INSTALL_DDIR}/kernel.${_kernel} \
1784             ${.TARGET:S/distributekernel/install/}
1785 .if defined(NO_ROOT)
1786         @sed -e "s|^./kernel.${_kernel}|.|" \
1787             ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta > \
1788             ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1789 .endif
1790 .endfor
1791 .endif
1792
1793 packagekernel: .PHONY
1794 .if defined(NO_ROOT)
1795 .if !defined(NO_INSTALLKERNEL)
1796         cd ${DESTDIR}/${DISTDIR}/kernel; \
1797             tar cvf - --exclude '*.debug' \
1798             @${DESTDIR}/${DISTDIR}/kernel.meta | \
1799             ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1800 .endif
1801 .if ${MK_DEBUG_FILES} != "no"
1802         cd ${DESTDIR}/${DISTDIR}/kernel; \
1803             tar cvf - --include '*/*/*.debug' \
1804             @${DESTDIR}/${DISTDIR}/kernel.meta | \
1805             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1806 .endif
1807 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1808 .for _kernel in ${BUILDKERNELS:[2..-1]}
1809         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1810             tar cvf - --exclude '*.debug' \
1811             @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1812             ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1813 .if ${MK_DEBUG_FILES} != "no"
1814         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1815             tar cvf - --include '*/*/*.debug' \
1816             @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1817             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1818 .endif
1819 .endfor
1820 .endif
1821 .else
1822 .if !defined(NO_INSTALLKERNEL)
1823         cd ${DESTDIR}/${DISTDIR}/kernel; \
1824             tar cvf - --exclude '*.debug' . | \
1825             ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1826 .endif
1827 .if ${MK_DEBUG_FILES} != "no"
1828         cd ${DESTDIR}/${DISTDIR}/kernel; \
1829             tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1830             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1831 .endif
1832 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1833 .for _kernel in ${BUILDKERNELS:[2..-1]}
1834         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1835             tar cvf - --exclude '*.debug' . | \
1836             ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1837 .if ${MK_DEBUG_FILES} != "no"
1838         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1839             tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1840             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1841 .endif
1842 .endfor
1843 .endif
1844 .endif
1845
1846 stagekernel: .PHONY
1847         ${_+_}${MAKE} -C ${.CURDIR} ${.MAKEFLAGS} distributekernel
1848
1849 PORTSDIR?=      /usr/ports
1850 WSTAGEDIR?=     ${OBJTOP}/worldstage
1851 KSTAGEDIR?=     ${OBJTOP}/kernelstage
1852 REPODIR?=       ${OBJROOT}repo
1853 PKG_FORMAT?=    txz
1854 PKG_REPO_SIGNING_KEY?=  # empty
1855 PKG_OUTPUT_DIR?=        ${PKG_VERSION}
1856
1857 .ORDER:         stage-packages create-packages
1858 .ORDER:         create-packages create-world-packages
1859 .ORDER:         create-packages create-kernel-packages
1860 .ORDER:         create-packages sign-packages
1861
1862 _pkgbootstrap: .PHONY
1863 .if make(*package*) && !exists(${LOCALBASE}/sbin/pkg)
1864         @env ASSUME_ALWAYS_YES=YES pkg bootstrap
1865 .endif
1866
1867 .if make(create-world-packages-jobs) || make(create-kernel-packages*) || make(real-update-packages)
1868 PKG_ABI!=${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI
1869 .endif
1870
1871 .if !defined(PKG_VERSION_FROM)
1872 .if defined(PKG_ABI)
1873 PKG_VERSION_FROM!=/usr/bin/readlink ${REPODIR}/${PKG_ABI}/latest
1874 .endif
1875 .endif
1876
1877 packages: .PHONY
1878         ${_+_}${MAKE} -C ${.CURDIR} PKG_VERSION=${PKG_VERSION} real-packages
1879
1880 update-packages: .PHONY
1881         ${_+_}${MAKE} -C ${.CURDIR} PKG_VERSION=${PKG_VERSION} real-update-packages
1882
1883 package-pkg: .PHONY
1884         rm -rf /tmp/ports.${TARGET} || :
1885         env ${WMAKEENV:Q} SRCDIR=${.CURDIR} PORTSDIR=${PORTSDIR} REVISION=${_REVISION} \
1886                 PKG_CMD=${PKG_CMD} PKG_VERSION=${PKG_VERSION} REPODIR=${REPODIR} \
1887                 WSTAGEDIR=${WSTAGEDIR} \
1888                 sh ${.CURDIR}/release/scripts/make-pkg-package.sh
1889
1890 real-packages:  stage-packages create-packages sign-packages .PHONY
1891
1892 real-update-packages: stage-packages .PHONY
1893         ${_+_}${MAKE} -C ${.CURDIR} PKG_VERSION=${PKG_VERSION} create-packages
1894         @echo "==> Checking for new packages (comparing ${PKG_VERSION} to ${PKG_VERSION_FROM})"
1895         @for pkg in ${REPODIR}/${PKG_ABI}/${PKG_VERSION_FROM}/${PKG_NAME_PREFIX}-*; do \
1896           pkgname=$$(pkg query -F $${pkg} '%n' | sed 's/${PKG_NAME_PREFIX}-\(.*\)/\1/') ; \
1897           newpkgname=${PKG_NAME_PREFIX}-$${pkgname}-${PKG_VERSION}.${PKG_FORMAT} ; \
1898           oldsum=$$(pkg query -F $${pkg} '%X') ; \
1899           if [ ! -f ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} ]; then \
1900             continue; \
1901           fi ; \
1902           newsum=$$(pkg query -F ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} '%X') ; \
1903           if [ "$${oldsum}" == "$${newsum}" ]; then \
1904            echo "==> Keeping old ${PKG_NAME_PREFIX}-$${pkgname}-${PKG_VERSION_FROM}.${PKG_FORMAT}" ; \
1905            rm ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} ; \
1906            cp $${pkg} ${REPODIR}/${PKG_ABI}/${PKG_VERSION} ; \
1907           else \
1908             echo "==> New package $${newpkgname}" ; \
1909           fi ; \
1910         done
1911         ${_+_}@cd ${.CURDIR}; \
1912                 ${MAKE} -f Makefile.inc1 PKG_VERSION=${PKG_VERSION} sign-packages
1913
1914 stage-packages-world: .PHONY
1915         @mkdir -p ${WSTAGEDIR}
1916         ${_+_}@cd ${.CURDIR}; \
1917                 ${MAKE} DESTDIR=${WSTAGEDIR} -DNO_ROOT stageworld
1918
1919 stage-packages-kernel: .PHONY
1920         @mkdir -p ${KSTAGEDIR}
1921         ${_+_}@cd ${.CURDIR}; \
1922                 ${MAKE} DESTDIR=${KSTAGEDIR} -DNO_ROOT stagekernel
1923
1924 stage-packages: .PHONY stage-packages-world stage-packages-kernel
1925
1926 _repodir: .PHONY
1927         @mkdir -p ${REPODIR}
1928
1929 create-packages-world:  _pkgbootstrap _repodir .PHONY
1930         ${_+_}@cd ${.CURDIR}; \
1931                 ${MAKE} -f Makefile.inc1 \
1932                         DESTDIR=${WSTAGEDIR} \
1933                         PKG_VERSION=${PKG_VERSION} create-world-packages
1934
1935 create-packages-kernel: _pkgbootstrap _repodir .PHONY
1936         ${_+_}@cd ${.CURDIR}; \
1937                 ${MAKE} -f Makefile.inc1 \
1938                         DESTDIR=${KSTAGEDIR} \
1939                         PKG_VERSION=${PKG_VERSION} DISTDIR=kernel \
1940                         SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
1941                         create-kernel-packages
1942
1943 create-packages: .PHONY create-packages-world create-packages-kernel
1944
1945 create-world-packages:  _pkgbootstrap .PHONY
1946         @rm -f ${WSTAGEDIR}/*.plist 2>/dev/null || :
1947         @cd ${WSTAGEDIR} ; \
1948                 env -i LC_COLLATE=C sort ${WSTAGEDIR}/${DISTDIR}/METALOG | \
1949                 awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk
1950         @for plist in ${WSTAGEDIR}/*.plist; do \
1951           plist=$${plist##*/} ; \
1952           pkgname=$${plist%.plist} ; \
1953           echo "_PKGS+= $${pkgname}" ; \
1954         done > ${WSTAGEDIR}/packages.mk
1955         ${_+_}@cd ${.CURDIR}; \
1956                 ${MAKE} -f Makefile.inc1 create-world-packages-jobs \
1957                 SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
1958                 .MAKE.JOB.PREFIX=
1959
1960 .if make(create-world-packages-jobs)
1961 .include "${WSTAGEDIR}/packages.mk"
1962 .endif
1963
1964 create-world-packages-jobs: .PHONY
1965 .for pkgname in ${_PKGS}
1966 create-world-packages-jobs: create-world-package-${pkgname}
1967 create-world-package-${pkgname}: .PHONY
1968         @sh ${SRCDIR}/release/packages/generate-ucl.sh -o ${pkgname} \
1969                 -s ${SRCDIR} -u ${WSTAGEDIR}/${pkgname}.ucl
1970         @awk -F\" ' \
1971                 /^name/ { printf("===> Creating %s-", $$2); next } \
1972                 /^version/ { print $$2; next } \
1973                 ' ${WSTAGEDIR}/${pkgname}.ucl
1974         @if [ "${pkgname}" == "runtime" ]; then \
1975                 sed -i '' -e "s/%VCS_REVISION%/${VCS_REVISION}/" ${WSTAGEDIR}/${pkgname}.ucl ; \
1976         fi
1977         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
1978                 create -f ${PKG_FORMAT} -M ${WSTAGEDIR}/${pkgname}.ucl \
1979                 -p ${WSTAGEDIR}/${pkgname}.plist \
1980                 -r ${WSTAGEDIR} \
1981                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
1982 .endfor
1983
1984 _default_flavor=        -default
1985 .if make(*package*) && exists(${KSTAGEDIR}/kernel.meta)
1986 . if ${MK_DEBUG_FILES} != "no"
1987 _debug=-dbg
1988 . endif
1989 create-kernel-packages: .PHONY
1990 . for flavor in "" ${_debug}
1991 create-kernel-packages: create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}
1992 create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}: _pkgbootstrap .PHONY
1993         @cd ${KSTAGEDIR}/${DISTDIR} ; \
1994         env -i LC_COLLATE=C sort ${KSTAGEDIR}/kernel.meta | \
1995         awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
1996                 -v kernel=yes -v _kernconf=${INSTALLKERNEL} ; \
1997         sed -e "s/%VERSION%/${PKG_VERSION}/" \
1998                 -e "s/%PKGNAME%/kernel-${INSTALLKERNEL:tl}${flavor}/" \
1999                 -e "s/%KERNELDIR%/kernel/" \
2000                 -e "s/%COMMENT%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
2001                 -e "s/%DESC%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
2002                 -e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
2003                 -e "s/%PKG_NAME_PREFIX%/${PKG_NAME_PREFIX}/" \
2004                 -e "s/%PKG_MAINTAINER%/${PKG_MAINTAINER}/" \
2005                 -e "s|%PKG_WWW%|${PKG_WWW}|" \
2006                 ${SRCDIR}/release/packages/kernel.ucl \
2007                 > ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
2008         awk -F\" ' \
2009                 /name/ { printf("===> Creating %s-", $$2); next } \
2010                 /version/ {print $$2; next } ' \
2011                 ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
2012         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
2013                 create -f ${PKG_FORMAT} \
2014                 -M ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl \
2015                 -p ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.plist \
2016                 -r ${KSTAGEDIR}/${DISTDIR} \
2017                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
2018 . endfor
2019 .endif
2020 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
2021 . for _kernel in ${BUILDKERNELS:[2..-1]}
2022 .  if exists(${KSTAGEDIR}/kernel.${_kernel}.meta)
2023 .   if ${MK_DEBUG_FILES} != "no"
2024 _debug=-dbg
2025 .   endif
2026 .   for flavor in "" ${_debug}
2027 create-kernel-packages: create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}
2028 create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}: _pkgbootstrap .PHONY
2029         @cd ${KSTAGEDIR}/kernel.${_kernel} ; \
2030         env -i LC_COLLATE=C sort ${KSTAGEDIR}/kernel.${_kernel}.meta | \
2031         awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
2032                 -v kernel=yes -v _kernconf=${_kernel} ; \
2033         sed -e "s/%VERSION%/${PKG_VERSION}/" \
2034                 -e "s/%PKGNAME%/kernel-${_kernel:tl}${flavor}/" \
2035                 -e "s/%KERNELDIR%/kernel.${_kernel}/" \
2036                 -e "s/%COMMENT%/FreeBSD ${_kernel} kernel ${flavor}/" \
2037                 -e "s/%DESC%/FreeBSD ${_kernel} kernel ${flavor}/" \
2038                 -e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
2039                 -e "s/%PKG_NAME_PREFIX%/${PKG_NAME_PREFIX}/" \
2040                 -e "s/%PKG_MAINTAINER%/${PKG_MAINTAINER}/" \
2041                 -e "s|%PKG_WWW%|${PKG_WWW}|" \
2042                 ${SRCDIR}/release/packages/kernel.ucl \
2043                 > ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
2044         awk -F\" ' \
2045                 /name/ { printf("===> Creating %s-", $$2); next } \
2046                 /version/ {print $$2; next } ' \
2047                 ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
2048         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
2049                 create -f ${PKG_FORMAT} \
2050                 -M ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl \
2051                 -p ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.plist \
2052                 -r ${KSTAGEDIR}/kernel.${_kernel} \
2053                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
2054 .   endfor
2055 .  endif
2056 . endfor
2057 .endif
2058
2059 sign-packages:  _pkgbootstrap .PHONY
2060         printf "version = 2;\npacking_format = \"${PKG_FORMAT}\";\n" > ${WSTAGEDIR}/meta
2061         @[ -L "${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/latest" ] && \
2062                 unlink ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/latest ; \
2063         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname repo \
2064                 -m ${WSTAGEDIR}/meta \
2065                 -o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/${PKG_VERSION} \
2066                 ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/${PKG_VERSION} \
2067                 ${PKG_REPO_SIGNING_KEY} ; \
2068         cd ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI); \
2069         ln -s ${PKG_OUTPUT_DIR} latest
2070
2071 #
2072 #
2073 # checkworld
2074 #
2075 # Run test suite on installed world.
2076 #
2077 checkworld: .PHONY
2078         @if [ ! -x "${LOCALBASE}/bin/kyua" ] && [ ! -x "/usr/bin/kyua" ]; then \
2079                 echo "You need kyua (devel/kyua) to run the test suite." | /usr/bin/fmt; \
2080                 exit 1; \
2081         fi
2082         ${_+_}PATH="$$PATH:${LOCALBASE}/bin" kyua test -k ${TESTSBASE}/Kyuafile
2083
2084 #
2085 #
2086 # doxygen
2087 #
2088 # Build the API documentation with doxygen
2089 #
2090 doxygen: .PHONY
2091         @if [ ! -x "${LOCALBASE}/bin/doxygen" ]; then \
2092                 echo "You need doxygen (devel/doxygen) to generate the API documentation of the kernel." | /usr/bin/fmt; \
2093                 exit 1; \
2094         fi
2095         ${_+_}cd ${.CURDIR}/tools/kerneldoc/subsys; ${MAKE} obj all
2096
2097 #
2098 # update
2099 #
2100 # Update the source tree(s), by running svn/svnup to update to the
2101 # latest copy.
2102 #
2103 update: .PHONY
2104 .if defined(SVN_UPDATE)
2105         @echo "--------------------------------------------------------------"
2106         @echo ">>> Updating ${.CURDIR} using Subversion"
2107         @echo "--------------------------------------------------------------"
2108         @(cd ${.CURDIR}; ${SVN_CMD} update ${SVNFLAGS})
2109 .endif
2110
2111 #
2112 # ------------------------------------------------------------------------
2113 #
2114 # From here onwards are utility targets used by the 'make world' and
2115 # related targets.  If your 'world' breaks, you may like to try to fix
2116 # the problem and manually run the following targets to attempt to
2117 # complete the build.  Beware, this is *not* guaranteed to work, you
2118 # need to have a pretty good grip on the current state of the system
2119 # to attempt to manually finish it.  If in doubt, 'make world' again.
2120 #
2121
2122 #
2123 # legacy: Build compatibility shims for the next three targets. This is a
2124 # minimal set of tools and shims necessary to compensate for older systems
2125 # which don't have the APIs required by the targets built in bootstrap-tools,
2126 # build-tools or cross-tools.
2127 #
2128 legacy: .PHONY
2129 .if ${BOOTSTRAPPING} < ${MINIMUM_SUPPORTED_OSREL} && ${BOOTSTRAPPING} != 0
2130         @echo "ERROR: Source upgrades from versions prior to ${MINIMUM_SUPPORTED_REL} are not supported."; \
2131         false
2132 .endif
2133
2134 .for _tool in \
2135   tools/build \
2136   ${LOCAL_LEGACY_DIRS}
2137         ${_+_}@${ECHODIR} "===> ${_tool} (obj,includes,all,install)"; \
2138             cd ${.CURDIR}/${_tool}; \
2139             if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2140             ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy includes; \
2141             ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no all; \
2142             ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no \
2143                 DESTDIR=${WORLDTMP}/legacy install
2144 .endfor
2145
2146 #
2147 # bootstrap-tools: Build tools needed for compatibility. These are binaries that
2148 # are built to build other binaries in the system. However, the focus of these
2149 # binaries is usually quite narrow. Bootstrap tools use the host's compiler and
2150 # libraries, augmented by -legacy, in addition to the libraries built during
2151 # bootstrap-tools.
2152 #
2153 _bt=            _bootstrap-tools
2154
2155 # We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
2156 # accidentally run tools that are incompatible but happen to be in $PATH.
2157 # This is especially important when building on Linux/MacOS where many of the
2158 # programs used during the build accept different flags or generate different
2159 # output. On those platforms we only symlink the tools known to be compatible
2160 # (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
2161 # from the FreeBSD sources during the bootstrap-tools stage.
2162 # We want to build without the user's $PATH starting in the bootstrap-tools
2163 # phase so the tools used in that phase (ln, cp, etc) must have already been
2164 # linked to $WORLDTMP. The tools are listed in the _host_tools_to_symlink
2165 # variable in tools/build/Makefile and are linked during the legacy phase.
2166 # Since they could be Linux or MacOS binaries, too we must only use flags that
2167 # are portable across operating systems.
2168
2169 # If BOOTSTRAP_ALL_TOOLS is set we will build all the required tools from the
2170 # current source tree. Otherwise we create a symlink to the version found in
2171 # $PATH during the bootstrap-tools stage.
2172 # When building on non-FreeBSD systems we can't assume that the host binaries
2173 # accept compatible flags or produce compatible output. Therefore we force
2174 # BOOTSTRAP_ALL_TOOLS and just build the FreeBSD version of the binary.
2175 .if defined(CROSSBUILD_HOST)
2176 BOOTSTRAP_ALL_TOOLS:=   1
2177 .endif
2178 .if defined(BOOTSTRAP_ALL_TOOLS)
2179 # BOOTSTRAPPING will be set on the command line so we can't override it here.
2180 # Instead set BOOTSTRAPPING_OSRELDATE so that the value 0 is set ${BSARGS}
2181 BOOTSTRAPPING_OSRELDATE:=       0
2182 .endif
2183
2184 # libnv and libsbuf are requirements for config(8), which is an unconditional
2185 # bootstrap-tool.
2186 _config=usr.sbin/config lib/libnv lib/libsbuf
2187 ${_bt}-usr.sbin/config: ${_bt}-lib/libnv ${_bt}-lib/libsbuf
2188
2189 .if ${MK_GAMES} != "no"
2190 _strfile=       usr.bin/fortune/strfile
2191 .endif
2192
2193 # vtfontcvt is used to build font files for loader and to generate
2194 # C source for loader built in font (8x16.c).
2195 _vtfontcvt=     usr.bin/vtfontcvt
2196
2197 # If we are not building the bootstrap because BOOTSTRAPPING is sufficient
2198 # we symlink the host version to $WORLDTMP instead. By doing this we can also
2199 # detect when a bootstrap tool is being used without the required MK_FOO.
2200 # If you add a new bootstrap tool where we could also use the host version,
2201 # please ensure that you also add a .else case where you add the tool to the
2202 # _bootstrap_tools_links variable.
2203 .if ${BOOTSTRAPPING} < 1000033
2204 # Note: lex needs m4 to build but m4 also depends on lex (which needs m4 to
2205 # generate any files). To fix this cyclic dependency we can build a bootstrap
2206 # version of m4 (with pre-generated files) then use that to build the real m4.
2207 # We can't simply use the host m4 since e.g. the macOS version does not accept
2208 # the flags that are passed by lex.
2209 # For lex we also use the pre-gerated files since we would otherwise need to
2210 # build awk and sed first (which need lex to build)
2211 # TODO: add a _bootstrap_lex and then build the real lex afterwards
2212 _lex=           usr.bin/lex
2213 _m4=            tools/build/bootstrap-m4 usr.bin/m4
2214 ${_bt}-tools/build/bootstrap-m4: ${_bt}-usr.bin/lex ${_bt}-lib/libopenbsd ${_bt}-usr.bin/yacc
2215 ${_bt}-usr.bin/m4: ${_bt}-lib/libopenbsd ${_bt}-usr.bin/yacc ${_bt}-usr.bin/lex ${_bt}-tools/build/bootstrap-m4
2216 _bt_m4_depend=${_bt}-usr.bin/m4
2217 _bt_lex_depend=${_bt}-usr.bin/lex ${_bt_m4_depend}
2218 .else
2219 _bootstrap_tools_links+=m4 lex
2220 .endif
2221
2222 # ELF Tool Chain libraries are needed for ELF tools and dtrace tools.
2223 # r296685 fix cross-endian objcopy
2224 # r310724 fixed PR 215350, a crash in libdwarf with objects built by GCC 6.2.
2225 # r334881 added libdwarf constants used by ctfconvert.
2226 # r338478 fixed a crash in objcopy for mips64el objects
2227 # r339083 libelf: correct mips64el test to use ELF header
2228 # r348347 Add missing powerpc64 relocation support to libdwarf
2229 .if ${BOOTSTRAPPING} < 1300030
2230 _elftoolchain_libs= lib/libelf lib/libdwarf lib/libzstd
2231 ${_bt}-lib/libelf: ${_bt_m4_depend}
2232 ${_bt}-lib/libdwarf: ${_bt_m4_depend}
2233 .endif
2234
2235 # flua is required to regenerate syscall files.  It first appeared during the
2236 # 13.0-CURRENT cycle, thus needs to be built on -older releases and stable
2237 # branches.
2238 .if ${BOOTSTRAPPING} < 1300059
2239 ${_bt}-libexec/flua: ${_bt}-lib/liblua ${_bt}-lib/libucl
2240 _flua= lib/liblua lib/libucl libexec/flua
2241 .endif
2242
2243 # r245440 mtree -N support added
2244 # r313404 requires sha384.h for libnetbsd, added to libmd in r292782
2245 .if ${BOOTSTRAPPING} < 1100093
2246 _nmtree=        lib/libmd \
2247                 lib/libnetbsd \
2248                 usr.sbin/nmtree
2249
2250 ${_bt}-lib/libnetbsd: ${_bt}-lib/libmd
2251 ${_bt}-usr.sbin/nmtree: ${_bt}-lib/libnetbsd
2252 .else
2253 _bootstrap_tools_links+=mtree
2254 .endif
2255
2256 # r246097: log addition login.conf.db, passwd, pwd.db, and spwd.db with cat -l
2257 .if ${BOOTSTRAPPING} < 1000027
2258 _cat=           bin/cat
2259 .else
2260 _bootstrap_tools_links+=cat
2261 .endif
2262
2263 # r277259 crunchide: Correct 64-bit section header offset
2264 # r281674 crunchide: always include both 32- and 64-bit ELF support
2265 .if ${BOOTSTRAPPING} < 1100078
2266 _crunchide=     usr.sbin/crunch/crunchide
2267 .else
2268 _bootstrap_tools_links+=crunchide
2269 .endif
2270
2271 # 1300115: Higher WARNS fixes
2272 .if ${BOOTSTRAPPING} < 1202502 || \
2273         (${BOOTSTRAPPING} > 1300000 && ${BOOTSTRAPPING} < 1300131)
2274 _crunchgen=     usr.sbin/crunch/crunchgen
2275 .else
2276 _bootstrap_tools_links+=crunchgen
2277 .endif
2278
2279 # r296926 -P keymap search path, MFC to stable/10 in r298297
2280 # Note: kbdcontrol can not be bootstrapped on non-FreeBSD systems
2281 .if !defined(CROSSBUILD_HOST)
2282 .if (${BOOTSTRAPPING} < 1003501 || \
2283         (${BOOTSTRAPPING} >= 1100000 && ${BOOTSTRAPPING} < 1100103))
2284 _kbdcontrol=    usr.sbin/kbdcontrol
2285 .else
2286 _bootstrap_tools_links+=kbdcontrol
2287 .endif
2288 .endif
2289
2290 # 1300102: VHDX support
2291 .if ${BOOTSTRAPPING} < 1201520 || \
2292         (${BOOTSTRAPPING} > 1300000 && ${BOOTSTRAPPING} < 1300102)
2293 _mkimg= usr.bin/mkimg
2294 .else
2295 _bootstrap_tools_links+=mkimg
2296 .endif
2297
2298 _yacc=          usr.bin/yacc
2299
2300 .if ${MK_BSNMP} != "no"
2301 _gensnmptree=   usr.sbin/bsnmpd/gensnmptree
2302 .endif
2303
2304
2305 # We need to build tblgen when we're building clang or lld, either as
2306 # bootstrap tools, or as the part of the normal build.
2307 .if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no" || \
2308     ${MK_LLD_BOOTSTRAP} != "no" || ${MK_LLD} != "no"
2309 _clang_tblgen= \
2310         lib/clang/libllvmminimal \
2311         usr.bin/clang/llvm-tblgen \
2312         usr.bin/clang/clang-tblgen \
2313         usr.bin/clang/lldb-tblgen
2314 # XXX: lldb-tblgen is not needed, if top-level MK_LLDB=no
2315
2316 ${_bt}-usr.bin/clang/clang-tblgen: ${_bt}-lib/clang/libllvmminimal
2317 ${_bt}-usr.bin/clang/llvm-tblgen: ${_bt}-lib/clang/libllvmminimal
2318 ${_bt}-usr.bin/clang/lldb-tblgen: ${_bt}-lib/clang/libllvmminimal
2319 .endif
2320
2321 .if ${MK_LOCALES} != "no"
2322 _localedef=     usr.bin/localedef
2323 ${_bt}-usr.bin/localedef: ${_bt}-usr.bin/yacc ${_bt_lex_depend}
2324 .endif
2325
2326 .if ${MK_KERBEROS} != "no"
2327 _kerberos5_bootstrap_tools= \
2328         kerberos5/tools/make-roken \
2329         kerberos5/lib/libroken \
2330         kerberos5/lib/libvers \
2331         kerberos5/tools/asn1_compile \
2332         kerberos5/tools/slc \
2333         usr.bin/compile_et
2334
2335 .ORDER: ${_kerberos5_bootstrap_tools:C/^/${_bt}-/g}
2336 .for _tool in ${_kerberos5_bootstrap_tools}
2337 ${_bt}-${_tool}: ${_bt}-usr.bin/yacc ${_bt_lex_depend}
2338 .endfor
2339 .endif
2340
2341 ${_bt}-usr.bin/mandoc: ${_bt}-lib/libopenbsd
2342
2343 # The tools listed in _basic_bootstrap_tools will generally not be
2344 # bootstrapped unless BOOTSTRAP_ALL_TOOL is set. However, when building on a
2345 # Linux or MacOS host the host versions are incompatible so we need to build
2346 # them from the source tree. Usually the link name will be the same as the subdir,
2347 # but some directories such as grep or test install multiple binaries. In that
2348 # case we use the _basic_bootstrap_tools_multilink variable which is a list of
2349 # subdirectory and comma-separated list of files.
2350 _basic_bootstrap_tools_multilink=usr.bin/grep grep,egrep,fgrep
2351 _basic_bootstrap_tools_multilink+=bin/test test,[
2352 # bootstrap tools needed by buildworld:
2353 _basic_bootstrap_tools+=usr.bin/cut bin/expr usr.bin/gencat usr.bin/join \
2354     usr.bin/mktemp bin/realpath bin/rmdir usr.bin/sed usr.bin/sort \
2355     usr.bin/truncate usr.bin/tsort
2356 # Some build scripts use nawk instead of awk (this happens at least in
2357 # cddl/contrib/opensolaris/lib/libdtrace/common/mknames.sh) so we need both awk
2358 # and nawk in ${WORLDTMP}/legacy/bin.
2359 _basic_bootstrap_tools_multilink+=usr.bin/awk awk,nawk
2360 # file2c is required for building usr.sbin/config:
2361 _basic_bootstrap_tools+=usr.bin/file2c
2362 # uuencode/uudecode required for share/tabset
2363 _basic_bootstrap_tools+=usr.bin/uuencode usr.bin/uudecode
2364 # xargs is required by mkioctls
2365 _basic_bootstrap_tools+=usr.bin/xargs
2366 # cap_mkdb is required for share/termcap:
2367 _basic_bootstrap_tools+=usr.bin/cap_mkdb
2368 # services_mkdb/pwd_mkdb are required for installworld:
2369 _basic_bootstrap_tools+=usr.sbin/services_mkdb usr.sbin/pwd_mkdb
2370 # ldd is required for installcheck (TODO: just always use /usr/bin/ldd instead?)
2371 .if !defined(CROSSBUILD_HOST)
2372 # ldd is only needed for updating the running system so we don't need to
2373 # bootstrap ldd on non-FreeBSD systems
2374 _basic_bootstrap_tools+=usr.bin/ldd
2375 .endif
2376 # sysctl/chflags are required for installkernel:
2377 .if !defined(CROSSBUILD_HOST)
2378 _basic_bootstrap_tools+=bin/chflags
2379 # Note: sysctl does not bootstrap on FreeBSD < 13 anymore, but that doesn't
2380 # matter since we don't need any of the new features for the build.
2381 _bootstrap_tools_links+=sysctl
2382 .else
2383 # When building on non-FreeBSD, install a fake chflags instead since the
2384 # version from the source tree cannot work. We also don't need sysctl since we
2385 # are install with -DNO_ROOT.
2386 _other_bootstrap_tools+=tools/build/cross-build/fake_chflags
2387 .endif
2388 # mkfifo is used by sys/conf/newvers.sh
2389 _basic_bootstrap_tools+=usr.bin/mkfifo
2390 # jot is needed for the mkimg tests
2391 _basic_bootstrap_tools+=usr.bin/jot
2392
2393 .if ${MK_BOOT} != "no"
2394 # md5 is used by boot/beri (and possibly others)
2395 _basic_bootstrap_tools+=sbin/md5
2396 .endif
2397
2398 .if ${MK_ZONEINFO} != "no"
2399 _basic_bootstrap_tools+=usr.sbin/zic usr.sbin/tzsetup
2400 .endif
2401
2402 .if defined(BOOTSTRAP_ALL_TOOLS)
2403 _other_bootstrap_tools+=${_basic_bootstrap_tools}
2404 .for _subdir _links in ${_basic_bootstrap_tools_multilink}
2405 _other_bootstrap_tools+=${_subdir}
2406 .endfor
2407 ${_bt}-usr.bin/awk: ${_bt_lex_depend} ${_bt}-usr.bin/yacc
2408 ${_bt}-bin/expr: ${_bt_lex_depend} ${_bt}-usr.bin/yacc
2409 # If we are bootstrapping file2c, we have to build it before config:
2410 ${_bt}-usr.sbin/config: ${_bt}-usr.bin/file2c ${_bt_lex_depend}
2411 # Note: no symlink to make/bmake in the !BOOTSTRAP_ALL_TOOLS case here since
2412 # the links to make/bmake make links will have already have been created in the
2413 # `make legacy` step. Not adding a link to make is important on non-FreeBSD
2414 # since "make" will usually point to GNU make there.
2415 _other_bootstrap_tools+=usr.bin/bmake
2416
2417 # Avoid dependency on host bz2 headers:
2418 _other_bootstrap_tools+=lib/libbz2
2419 ${_bt}-usr.bin/grep: ${_bt}-lib/libbz2
2420 .else
2421 # All tools in _basic_bootstrap_tools have the same name as the subdirectory
2422 # so we can use :T to get the name of the symlinks that we need to create.
2423 _bootstrap_tools_links+=${_basic_bootstrap_tools:T}
2424 .for _subdir _links in ${_basic_bootstrap_tools_multilink}
2425 _bootstrap_tools_links+=${_links:S/,/ /g}
2426 .endfor
2427 .endif  # defined(BOOTSTRAP_ALL_TOOLS)
2428
2429 # Link the tools that we need for building but don't need to bootstrap because
2430 # the host version is known to be compatible into ${WORLDTMP}/legacy
2431 # We do this before building any of the bootstrap tools in case they depend on
2432 # the presence of any of the links (e.g. as m4/lex/awk)
2433 ${_bt}-links: .PHONY
2434
2435 .for _tool in ${_bootstrap_tools_links}
2436 ${_bt}-link-${_tool}: .PHONY .MAKE
2437         @rm -f "${WORLDTMP}/legacy/bin/${_tool}"; \
2438         source_path=`which ${_tool}`; \
2439         if [ ! -e "$${source_path}" ] ; then \
2440                 echo "Cannot find host tool '${_tool}'"; false; \
2441         fi; \
2442         cp -pf "$${source_path}" "${WORLDTMP}/legacy/bin/${_tool}"
2443 ${_bt}-links: ${_bt}-link-${_tool}
2444 .endfor
2445
2446 bootstrap-tools: ${_bt}-links .PHONY
2447
2448 #       Please document (add comment) why something is in 'bootstrap-tools'.
2449 #       Try to bound the building of the bootstrap-tool to just the
2450 #       FreeBSD versions that need the tool built at this stage of the build.
2451 .for _tool in \
2452     ${_clang_tblgen} \
2453     ${_kerberos5_bootstrap_tools} \
2454     ${_strfile} \
2455     usr.bin/dtc \
2456     ${_cat} \
2457     ${_kbdcontrol} \
2458     ${_elftoolchain_libs} \
2459     usr.bin/lorder \
2460     lib/libopenbsd \
2461     usr.bin/mandoc \
2462     usr.bin/rpcgen \
2463     ${_yacc} \
2464     ${_m4} \
2465     ${_lex} \
2466     ${_other_bootstrap_tools} \
2467     usr.bin/xinstall \
2468     ${_gensnmptree} \
2469     ${_config} \
2470     ${_flua} \
2471     ${_crunchide} \
2472     ${_crunchgen} \
2473     ${_mkimg} \
2474     ${_nmtree} \
2475     ${_vtfontcvt} \
2476     ${_localedef} \
2477     ${LOCAL_BSTOOL_DIRS}
2478 ${_bt}-${_tool}: ${_bt}-links .PHONY .MAKE
2479         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2480                 cd ${.CURDIR}/${_tool}; \
2481                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2482                 if [ "${_tool}" = "usr.bin/lex" ]; then \
2483                         ${MAKE} DIRPRFX=${_tool}/ bootstrap; \
2484                 fi; \
2485                 ${MAKE} DIRPRFX=${_tool}/ all; \
2486                 ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy install
2487
2488 bootstrap-tools: ${_bt}-${_tool}
2489 .endfor
2490 .if target(${_bt}-lib/libmd)
2491 # If we are bootstrapping libmd (e.g. when building on macOS/Linux) add the
2492 # necessary dependencies:
2493 ${_bt}-usr.bin/sort: ${_bt}-lib/libmd
2494 ${_bt}-usr.bin/xinstall: ${_bt}-lib/libmd
2495 ${_bt}-sbin/md5: ${_bt}-lib/libmd
2496 .endif
2497
2498
2499 #
2500 # build-tools: Build special purpose build tools
2501 #
2502 .if !defined(NO_SHARE) && ${MK_SYSCONS} != "no"
2503 _share= share/syscons/scrnmaps
2504 .endif
2505
2506 .if ${MK_RESCUE} != "no"
2507 # rescue includes programs that have build-tools targets
2508 _rescue=rescue/rescue
2509 .endif
2510
2511 .if ${MK_TCSH} != "no"
2512 _tcsh=bin/csh
2513 .endif
2514 .if ${MK_FILE} != "no"
2515 _libmagic=lib/libmagic
2516 .endif
2517
2518 .if ${MK_PMC} != "no" && \
2519     (${TARGET_ARCH} == "aarch64" || ${TARGET_ARCH} == "amd64" || \
2520     ${TARGET_ARCH} == "i386")
2521 _jevents=lib/libpmc/pmu-events
2522 .endif
2523
2524 # kernel-toolchain skips _cleanobj, so handle cleaning up previous
2525 # build-tools directories if needed.
2526 .if ${MK_CLEAN} == "yes" && make(kernel-toolchain)
2527 _bt_clean=      ${CLEANDIR}
2528 .endif
2529
2530 .for _tool in \
2531     ${_tcsh} \
2532     bin/sh \
2533     ${LOCAL_TOOL_DIRS} \
2534     ${_jevents} \
2535     lib/ncurses/ncurses \
2536     ${_rescue} \
2537     ${_share} \
2538     usr.bin/awk \
2539     ${_libmagic} \
2540     usr.bin/mkesdb_static \
2541     usr.bin/mkcsmapper_static \
2542     usr.bin/vi/catalog
2543 build-tools_${_tool}: .PHONY
2544         ${_+_}@${ECHODIR} "===> ${_tool} (${_bt_clean:D${_bt_clean},}obj,build-tools)"; \
2545                 cd ${.CURDIR}/${_tool}; \
2546                 if [ -n "${_bt_clean}" ]; then ${MAKE} DIRPRFX=${_tool}/ ${_bt_clean}; fi; \
2547                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2548                 ${MAKE} DIRPRFX=${_tool}/ build-tools
2549 build-tools: build-tools_${_tool}
2550 .endfor
2551
2552 #
2553 # kernel-tools: Build kernel-building tools
2554 #
2555 kernel-tools: .PHONY
2556         mkdir -p ${WORLDTMP}/usr
2557         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2558             -p ${WORLDTMP}/usr >/dev/null
2559
2560 #
2561 # cross-tools: All the tools needed to build the rest of the system after
2562 # we get done with the earlier stages. It is the last set of tools needed
2563 # to begin building the target binaries.
2564 #
2565 .if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${BUILD_WITH_STRICT_TMPPATH} != 0
2566 .if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "i386"
2567 _btxld=         usr.sbin/btxld
2568 .endif
2569 .endif
2570
2571 # Rebuild ctfconvert and ctfmerge to avoid difficult-to-diagnose failures
2572 # resulting from missing bug fixes or ELF Toolchain updates.
2573 .if ${MK_CDDL} != "no"
2574 _dtrace_tools= cddl/lib/libctf cddl/lib/libspl cddl/usr.bin/ctfconvert \
2575     cddl/usr.bin/ctfmerge
2576 .endif
2577
2578 # If we're given an XAS, don't build binutils.
2579 .if ${XAS:M/*} == ""
2580 .if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2581 _elftctools=    lib/libelftc \
2582                 lib/libpe \
2583                 usr.bin/objcopy \
2584                 usr.bin/nm \
2585                 usr.bin/size \
2586                 usr.bin/strings
2587 # These are not required by the build, but can be useful for developers who
2588 # cross-build on a FreeBSD 10 host:
2589 _elftctools+=   usr.bin/addr2line
2590 .endif
2591 .elif ${TARGET_ARCH} != ${MACHINE_ARCH} && ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2592 # If cross-building with an external binutils we still need to build strip for
2593 # the target (for at least crunchide).
2594 _elftctools=    lib/libelftc \
2595                 lib/libpe \
2596                 usr.bin/objcopy
2597 .endif
2598
2599 .if ${MK_CLANG_BOOTSTRAP} != "no"
2600 _clang=         usr.bin/clang
2601 .endif
2602 .if ${MK_LLD_BOOTSTRAP} != "no"
2603 _lld=           usr.bin/clang/lld
2604 .endif
2605 .if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_LLD_BOOTSTRAP} != "no"
2606 _clang_libs=    lib/clang
2607 .endif
2608 .if ${MK_USB} != "no"
2609 _usb_tools=     stand/usb/tools
2610 .endif
2611
2612 .if ${BUILD_WITH_STRICT_TMPPATH} != 0 || defined(BOOTSTRAP_ALL_TOOLS)
2613 _ar=usr.bin/ar
2614 .endif
2615
2616 cross-tools: .MAKE .PHONY
2617 .for _tool in \
2618     ${LOCAL_XTOOL_DIRS} \
2619     ${_ar} \
2620     ${_clang_libs} \
2621     ${_clang} \
2622     ${_lld} \
2623     ${_elftctools} \
2624     ${_dtrace_tools} \
2625     ${_btxld} \
2626     ${_usb_tools}
2627         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2628                 cd ${.CURDIR}/${_tool}; \
2629                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2630                 ${MAKE} DIRPRFX=${_tool}/ all; \
2631                 ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP} install
2632 .endfor
2633
2634 #
2635 # native-xtools is the current target for qemu-user cross builds of ports
2636 # via poudriere and the imgact_binmisc kernel module.
2637 # This target merely builds a toolchan/sysroot, then builds the tools it wants
2638 # with the options it wants in a special MAKEOBJDIRPREFIX, using the toolchain
2639 # already built.  It then installs the static tools to NXBDESTDIR for Poudriere
2640 # to pickup.
2641 #
2642 NXBOBJROOT=     ${OBJROOT}${MACHINE}.${MACHINE_ARCH}/nxb/
2643 NXBOBJTOP=      ${NXBOBJROOT}${NXB_TARGET}.${NXB_TARGET_ARCH}
2644 NXTP?=          /nxb-bin
2645 .if ${NXTP:N/*}
2646 .error NXTP variable should be an absolute path
2647 .endif
2648 NXBDESTDIR?=    ${DESTDIR}${NXTP}
2649
2650 # This is the list of tools to be built/installed as static and where
2651 # appropriate to build for the given TARGET.TARGET_ARCH.
2652 NXBDIRS+= \
2653     bin/cat \
2654     bin/chmod \
2655     bin/cp \
2656     ${_tcsh} \
2657     bin/echo \
2658     bin/expr \
2659     bin/hostname \
2660     bin/ln \
2661     bin/ls \
2662     bin/mkdir \
2663     bin/mv \
2664     bin/ps \
2665     bin/realpath \
2666     bin/rm \
2667     bin/rmdir \
2668     bin/sh \
2669     bin/sleep \
2670     sbin/md5 \
2671     sbin/sysctl \
2672     usr.bin/addr2line \
2673     usr.bin/ar \
2674     usr.bin/awk \
2675     usr.bin/basename \
2676     usr.bin/bmake \
2677     usr.bin/bzip2 \
2678     usr.bin/cmp \
2679     usr.bin/diff \
2680     usr.bin/dirname \
2681     usr.bin/objcopy \
2682     usr.bin/env \
2683     usr.bin/fetch \
2684     usr.bin/find \
2685     usr.bin/grep \
2686     usr.bin/gzip \
2687     usr.bin/head \
2688     usr.bin/id \
2689     usr.bin/lex \
2690     usr.bin/limits \
2691     usr.bin/lorder \
2692     usr.bin/mandoc \
2693     usr.bin/mktemp \
2694     usr.bin/mt \
2695     usr.bin/nm \
2696     usr.bin/patch \
2697     usr.bin/readelf \
2698     usr.bin/sed \
2699     usr.bin/size \
2700     usr.bin/sort \
2701     usr.bin/strings \
2702     usr.bin/tar \
2703     usr.bin/touch \
2704     usr.bin/tr \
2705     usr.bin/true \
2706     usr.bin/uniq \
2707     usr.bin/unzip \
2708     usr.bin/wc \
2709     usr.bin/xargs \
2710     usr.bin/xinstall \
2711     usr.bin/xz \
2712     usr.bin/yacc \
2713     usr.sbin/chown
2714
2715 SUBDIR_DEPEND_usr.bin/clang=    lib/clang
2716 .if ${MK_CLANG} != "no"
2717 NXBDIRS+=       lib/clang
2718 NXBDIRS+=       usr.bin/clang
2719 .endif
2720 # XXX: native-xtools passes along ${NXBDIRS} in SUBDIR_OVERRIDE that needs
2721 # to be evaluated after NXBDIRS is set.
2722 .if make(install) && !empty(SUBDIR_OVERRIDE)
2723 SUBDIR= ${SUBDIR_OVERRIDE}
2724 .endif
2725
2726 NXBMAKEARGS+= \
2727         OBJTOP=${NXBOBJTOP:Q} \
2728         OBJROOT=${NXBOBJROOT:Q} \
2729         MAKEOBJDIRPREFIX= \
2730         -DNO_SHARED \
2731         -DNO_CPU_CFLAGS \
2732         -DNO_PIC \
2733         SSP_CFLAGS= \
2734         MK_CASPER=no \
2735         MK_CLANG_EXTRAS=no \
2736         MK_CLANG_FORMAT=no \
2737         MK_CLANG_FULL=no \
2738         MK_CTF=no \
2739         MK_DEBUG_FILES=no \
2740         MK_HTML=no \
2741         MK_LLDB=no \
2742         MK_MAN=no \
2743         MK_MAN_UTILS=yes \
2744         MK_OFED=no \
2745         MK_OPENSSH=no \
2746         MK_PROFILE=no \
2747         MK_RETPOLINE=no \
2748         MK_SENDMAIL=no \
2749         MK_SVNLITE=no \
2750         MK_TESTS=no \
2751         MK_WARNS=no \
2752         MK_ZFS=no
2753
2754 .if make(native-xtools*) && \
2755     (!defined(NXB_TARGET) || !defined(NXB_TARGET_ARCH))
2756 .error Missing NXB_TARGET / NXB_TARGET_ARCH
2757 .endif
2758 # For 'toolchain' we want to produce native binaries that themselves generate
2759 # native binaries.
2760 NXBTMAKE=       ${NXBMAKEENV} ${MAKE} ${NXBMAKEARGS:N-DNO_PIC:N-DNO_SHARED} \
2761                 TARGET=${MACHINE} TARGET_ARCH=${MACHINE_ARCH}
2762 # For 'everything' we want to produce native binaries (hence -target to
2763 # be MACHINE) that themselves generate TARGET.TARGET_ARCH binaries.
2764 # TARGET/TARGET_ARCH are still passed along from user.
2765 #
2766 # Use the toolchain we create as an external toolchain.
2767 .if ${USING_SYSTEM_COMPILER} == "yes" || ${XCC:N${CCACHE_BIN}:M/*}
2768 NXBMAKE+=       XCC="${XCC}" \
2769                 XCXX="${XCXX}" \
2770                 XCPP="${XCPP}"
2771 .else
2772 NXBMAKE+=       XCC="${NXBOBJTOP}/tmp/usr/bin/cc" \
2773                 XCXX="${NXBOBJTOP}/tmp/usr/bin/c++" \
2774                 XCPP="${NXBOBJTOP}/tmp/usr/bin/cpp"
2775 .endif
2776 NXBMAKE+=       ${NXBMAKEENV} ${MAKE} -f Makefile.inc1 ${NXBMAKEARGS} \
2777                 MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} \
2778                 TARGET=${NXB_TARGET} TARGET_ARCH=${NXB_TARGET_ARCH} \
2779                 TARGET_TRIPLE=${MACHINE_TRIPLE:Q}
2780 # NXBDIRS is improperly based on MACHINE rather than NXB_TARGET.  Need to
2781 # invoke a sub-make to reevaluate MK_CLANG, etc, for NXBDIRS.
2782 NXBMAKE+=       SUBDIR_OVERRIDE='$${NXBDIRS:M*}'
2783 # Need to avoid the -isystem logic when using clang as an external toolchain
2784 # even if the TARGET being built for wants GCC.
2785 NXBMAKE+=       WANT_COMPILER_TYPE='$${X_COMPILER_TYPE}'
2786 native-xtools: .PHONY
2787         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _cleanobj
2788         # Build the bootstrap/host/cross tools that produce native binaries
2789         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} kernel-toolchain
2790         # Populate includes/libraries sysroot that produce native binaries.
2791         # This is split out from 'toolchain' above mostly so that target LLVM
2792         # libraries have a proper LLVM_DEFAULT_TARGET_TRIPLE without
2793         # polluting the cross-compiler build.  The LLVM/GCC libs are skipped
2794         # here to avoid the problem but are kept in 'toolchain' so that
2795         # needed build tools are built.
2796         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _includes MK_CLANG=no
2797         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _libraries MK_CLANG=no
2798 .if !defined(NO_OBJWALK)
2799         ${_+_}cd ${.CURDIR}; ${NXBMAKE} _obj
2800 .endif
2801         ${_+_}cd ${.CURDIR}; ${NXBMAKE} everything
2802         @echo ">> native-xtools done.  Use 'make native-xtools-install' to install to a given DESTDIR"
2803
2804 native-xtools-install: .PHONY
2805         mkdir -p ${NXBDESTDIR}/bin ${NXBDESTDIR}/sbin ${NXBDESTDIR}/usr
2806         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2807             -p ${NXBDESTDIR}/usr >/dev/null
2808         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
2809             -p ${NXBDESTDIR}/usr/include >/dev/null
2810         ${_+_}cd ${.CURDIR}; ${NXBMAKE} \
2811             DESTDIR=${NXBDESTDIR} \
2812             -DNO_ROOT \
2813             install
2814
2815 #
2816 # hierarchy - ensure that all the needed directories are present
2817 #
2818 hierarchy hier: .MAKE .PHONY
2819         ${_+_}cd ${.CURDIR}/etc; ${HMAKE} distrib-dirs
2820
2821 #
2822 # libraries - build all libraries, and install them under ${DESTDIR}.
2823 #
2824 # The list of libraries with dependents (${_prebuild_libs}) and their
2825 # interdependencies (__L) are built automatically by the
2826 # ${.CURDIR}/tools/make_libdeps.sh script.
2827 #
2828 libraries: .MAKE .PHONY
2829         ${_+_}cd ${.CURDIR}; \
2830             ${MAKE} -f Makefile.inc1 _prereq_libs; \
2831             ${MAKE} -f Makefile.inc1 _startup_libs; \
2832             ${MAKE} -f Makefile.inc1 _prebuild_libs; \
2833             ${MAKE} -f Makefile.inc1 _generic_libs
2834
2835 #
2836 # static libgcc.a prerequisite for shared libc
2837 #
2838 _prereq_libs= lib/libcompiler_rt
2839 .if ${MK_SSP} != "no"
2840 _prereq_libs+= lib/libssp_nonshared
2841 .endif
2842
2843 # These dependencies are not automatically generated:
2844 #
2845 # lib/csu and lib/libc must be built before
2846 # all shared libraries for ELF.
2847 #
2848 _startup_libs=  lib/csu
2849 _startup_libs+= lib/libc
2850 _startup_libs+= lib/libc_nonshared
2851 .if ${MK_LIBCPLUSPLUS} != "no"
2852 _startup_libs+= lib/libcxxrt
2853 .endif
2854
2855 _prereq_libs+=  lib/libgcc_eh lib/libgcc_s
2856 _startup_libs+= lib/libgcc_eh lib/libgcc_s
2857
2858 lib/libgcc_s__L: lib/libc__L
2859 lib/libgcc_s__L: lib/libc_nonshared__L
2860 .if ${MK_LIBCPLUSPLUS} != "no"
2861 lib/libcxxrt__L: lib/libgcc_s__L
2862 .endif
2863
2864 _prebuild_libs= ${_kerberos5_lib_libasn1} \
2865                 ${_kerberos5_lib_libhdb} \
2866                 ${_kerberos5_lib_libheimbase} \
2867                 ${_kerberos5_lib_libheimntlm} \
2868                 ${_libsqlite3} \
2869                 ${_kerberos5_lib_libheimipcc} \
2870                 ${_kerberos5_lib_libhx509} ${_kerberos5_lib_libkrb5} \
2871                 ${_kerberos5_lib_libroken} \
2872                 ${_kerberos5_lib_libwind} \
2873                 lib/libbz2 ${_libcom_err} lib/libcrypt \
2874                 lib/libelf lib/libexpat \
2875                 lib/libfigpar \
2876                 ${_lib_libgssapi} \
2877                 lib/libjail \
2878                 lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
2879                 lib/libzstd \
2880                 ${_lib_casper} \
2881                 lib/ncurses/ncurses \
2882                 lib/libopie lib/libpam/libpam ${_lib_libthr} \
2883                 ${_lib_libradius} lib/libsbuf lib/libtacplus \
2884                 lib/libgeom \
2885                 ${_cddl_lib_libumem} ${_cddl_lib_libnvpair} \
2886                 ${_cddl_lib_libuutil} \
2887                 ${_cddl_lib_libavl} \
2888                 ${_cddl_lib_libicp} \
2889                 ${_cddl_lib_libicp_rescue} \
2890                 ${_cddl_lib_libspl} \
2891                 ${_cddl_lib_libtpool} \
2892                 ${_cddl_lib_libzfs_core} ${_cddl_lib_libzfs} \
2893                 ${_cddl_lib_libzutil} \
2894                 ${_cddl_lib_libctf} ${_cddl_lib_libzfsbootenv} \
2895                 lib/libufs \
2896                 lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \
2897                 ${_secure_lib_libcrypto} ${_secure_lib_libssl} \
2898                 ${_lib_libldns} ${_secure_lib_libssh}
2899
2900 .if ${MK_DIALOG} != "no"
2901 _prebuild_libs+= gnu/lib/libdialog
2902 gnu/lib/libdialog__L: lib/msun__L lib/ncurses/ncurses__L
2903 .endif
2904
2905 .if ${MK_GOOGLETEST} != "no"
2906 _prebuild_libs+= lib/libregex
2907 .endif
2908
2909 .if ${MK_LIBCPLUSPLUS} != "no"
2910 _prebuild_libs+= lib/libc++
2911 .endif
2912
2913 lib/libgeom__L: lib/libexpat__L lib/libsbuf__L
2914 lib/libkvm__L: lib/libelf__L
2915
2916 .if ${MK_LIBTHR} != "no"
2917 _lib_libthr=    lib/libthr
2918 .endif
2919
2920 .if ${MK_RADIUS_SUPPORT} != "no"
2921 _lib_libradius= lib/libradius
2922 .endif
2923
2924 .if ${MK_OFED} != "no"
2925 _prebuild_libs+= \
2926         lib/ofed/libibverbs \
2927         lib/ofed/libibmad \
2928         lib/ofed/libibumad \
2929         lib/ofed/complib \
2930         lib/ofed/libmlx5
2931
2932 lib/ofed/libibmad__L:   lib/ofed/libibumad__L
2933 lib/ofed/complib__L:    lib/libthr__L
2934 lib/ofed/libmlx5__L:    lib/ofed/libibverbs__L lib/libthr__L
2935 .endif
2936
2937 .if ${MK_CASPER} != "no"
2938 _lib_casper=    lib/libcasper
2939 .endif
2940
2941 lib/libpjdlog__L: lib/libutil__L
2942 lib/libcasper__L: lib/libnv__L
2943 lib/liblzma__L: lib/libmd__L lib/libthr__L
2944 lib/libzstd__L: lib/libthr__L
2945
2946 _generic_libs=  ${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib}
2947 .if ${MK_IPFILTER} != "no"
2948 _generic_libs+= sbin/ipf/libipf
2949 .endif
2950 .for _DIR in ${LOCAL_LIB_DIRS}
2951 .if ${_DIR} == ".WAIT"  || (empty(_generic_libs:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile))
2952 _generic_libs+= ${_DIR}
2953 .endif
2954 .endfor
2955
2956 lib/libopie__L lib/libtacplus__L: lib/libmd__L
2957
2958 .if ${MK_CDDL} != "no"
2959 _cddl_lib_libumem= cddl/lib/libumem
2960 _cddl_lib_libnvpair= cddl/lib/libnvpair
2961 _cddl_lib_libavl= cddl/lib/libavl
2962 _cddl_lib_libuutil= cddl/lib/libuutil
2963 _cddl_lib_libspl= cddl/lib/libspl
2964
2965 cddl/lib/libuutil__L: cddl/lib/libavl__L cddl/lib/libspl__L
2966
2967 .if ${MK_ZFS} != "no"
2968 _cddl_lib_libicp= cddl/lib/libicp
2969 _cddl_lib_libicp_rescue= cddl/lib/libicp_rescue
2970 _cddl_lib_libtpool= cddl/lib/libtpool
2971 _cddl_lib_libzutil= cddl/lib/libzutil
2972 _cddl_lib_libzfs_core= cddl/lib/libzfs_core
2973 _cddl_lib_libzfs= cddl/lib/libzfs
2974 _cddl_lib_libzfsbootenv= cddl/lib/libzfsbootenv
2975
2976 cddl/lib/libtpool__L: cddl/lib/libspl__L
2977
2978 cddl/lib/libzutil__L: cddl/lib/libavl__L cddl/lib/libtpool__L
2979
2980 cddl/lib/libzfs_core__L: cddl/lib/libnvpair__L
2981
2982 cddl/lib/libzfs__L: cddl/lib/libzfs_core__L lib/msun__L lib/libutil__L
2983 cddl/lib/libzfs__L: lib/libthr__L lib/libmd__L lib/libz__L cddl/lib/libumem__L
2984 cddl/lib/libzfs__L: cddl/lib/libuutil__L cddl/lib/libavl__L lib/libgeom__L
2985 cddl/lib/libzfs__L: cddl/lib/libnvpair__L cddl/lib/libzutil__L
2986 cddl/lib/libzfs__L: secure/lib/libcrypto__L
2987
2988 cddl/lib/libzfsbootenv__L: cddl/lib/libzfs__L
2989 lib/libbe__L: cddl/lib/libzfs__L cddl/lib/libzfsbootenv__L
2990 .endif
2991 _cddl_lib_libctf= cddl/lib/libctf
2992 _cddl_lib= cddl/lib
2993 cddl/lib/libctf__L: lib/libz__L cddl/lib/libspl__L
2994 .endif
2995 # cddl/lib/libdtrace requires lib/libproc and lib/librtld_db
2996 _prebuild_libs+=        lib/libprocstat lib/libproc lib/librtld_db
2997 lib/libprocstat__L: lib/libelf__L lib/libkvm__L lib/libutil__L
2998 lib/libproc__L: lib/libprocstat__L
2999 lib/librtld_db__L: lib/libprocstat__L
3000
3001 .if ${MK_CRYPT} != "no"
3002 .if ${MK_OPENSSL} != "no"
3003 _secure_lib_libcrypto= secure/lib/libcrypto
3004 _secure_lib_libssl= secure/lib/libssl
3005 lib/libradius__L secure/lib/libssl__L: secure/lib/libcrypto__L
3006 secure/lib/libcrypto__L: lib/libthr__L
3007 .if ${MK_LDNS} != "no"
3008 _lib_libldns= lib/libldns
3009 lib/libldns__L: secure/lib/libssl__L
3010 .endif
3011 .if ${MK_OPENSSH} != "no"
3012 _secure_lib_libssh= secure/lib/libssh
3013 secure/lib/libssh__L: lib/libz__L secure/lib/libcrypto__L lib/libcrypt__L
3014 .if ${MK_LDNS} != "no"
3015 secure/lib/libssh__L: lib/libldns__L
3016 .endif
3017 .if ${MK_GSSAPI} != "no" && ${MK_KERBEROS_SUPPORT} != "no"
3018 secure/lib/libssh__L: lib/libgssapi__L kerberos5/lib/libkrb5__L \
3019     kerberos5/lib/libhx509__L kerberos5/lib/libasn1__L lib/libcom_err__L \
3020     lib/libmd__L kerberos5/lib/libroken__L
3021 .endif
3022 .endif
3023 .endif
3024 _secure_lib=    secure/lib
3025 .endif
3026
3027 .if ${MK_KERBEROS} != "no"
3028 kerberos5/lib/libasn1__L: lib/libcom_err__L kerberos5/lib/libroken__L
3029 kerberos5/lib/libhdb__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3030     kerberos5/lib/libkrb5__L kerberos5/lib/libroken__L \
3031     kerberos5/lib/libwind__L lib/libsqlite3__L
3032 kerberos5/lib/libheimntlm__L: secure/lib/libcrypto__L kerberos5/lib/libkrb5__L \
3033     kerberos5/lib/libroken__L lib/libcom_err__L
3034 kerberos5/lib/libhx509__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3035     secure/lib/libcrypto__L kerberos5/lib/libroken__L kerberos5/lib/libwind__L
3036 kerberos5/lib/libkrb5__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3037     lib/libcrypt__L secure/lib/libcrypto__L kerberos5/lib/libhx509__L \
3038     kerberos5/lib/libroken__L kerberos5/lib/libwind__L \
3039     kerberos5/lib/libheimbase__L kerberos5/lib/libheimipcc__L
3040 kerberos5/lib/libroken__L: lib/libcrypt__L
3041 kerberos5/lib/libwind__L: kerberos5/lib/libroken__L lib/libcom_err__L
3042 kerberos5/lib/libheimbase__L: lib/libthr__L
3043 kerberos5/lib/libheimipcc__L: kerberos5/lib/libroken__L kerberos5/lib/libheimbase__L lib/libthr__L
3044 .endif
3045
3046 lib/libsqlite3__L: lib/libthr__L
3047
3048 .if ${MK_GSSAPI} != "no"
3049 _lib_libgssapi= lib/libgssapi
3050 .endif
3051
3052 .if ${MK_KERBEROS} != "no"
3053 _kerberos5_lib= kerberos5/lib
3054 _kerberos5_lib_libasn1= kerberos5/lib/libasn1
3055 _kerberos5_lib_libhdb= kerberos5/lib/libhdb
3056 _kerberos5_lib_libheimbase= kerberos5/lib/libheimbase
3057 _kerberos5_lib_libkrb5= kerberos5/lib/libkrb5
3058 _kerberos5_lib_libhx509= kerberos5/lib/libhx509
3059 _kerberos5_lib_libroken= kerberos5/lib/libroken
3060 _kerberos5_lib_libheimntlm= kerberos5/lib/libheimntlm
3061 _libsqlite3= lib/libsqlite3
3062 _kerberos5_lib_libheimipcc= kerberos5/lib/libheimipcc
3063 _kerberos5_lib_libwind= kerberos5/lib/libwind
3064 _libcom_err= lib/libcom_err
3065 .endif
3066
3067 .if ${MK_NIS} != "no"
3068 _lib_libypclnt= lib/libypclnt
3069 .endif
3070
3071 .if ${MK_OPENSSL} == "no"
3072 lib/libradius__L: lib/libmd__L
3073 .endif
3074
3075 lib/libproc__L: \
3076     ${_cddl_lib_libctf:D${_cddl_lib_libctf}__L} lib/libelf__L lib/librtld_db__L lib/libutil__L
3077 .if ${MK_CXX} != "no" && ${MK_LIBCPLUSPLUS} != "no"
3078 lib/libproc__L: lib/libcxxrt__L
3079 .endif
3080
3081 .for _lib in ${_prereq_libs}
3082 ${_lib}__PL: .PHONY .MAKE
3083 .if !defined(_MKSHOWCONFIG) && exists(${.CURDIR}/${_lib})
3084         ${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
3085                 cd ${.CURDIR}/${_lib}; \
3086                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
3087                 ${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
3088                     DIRPRFX=${_lib}/ all; \
3089                 ${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
3090                     DIRPRFX=${_lib}/ install
3091 .endif
3092 .endfor
3093
3094 .for _lib in ${_startup_libs} ${_prebuild_libs} ${_generic_libs}
3095 ${_lib}__L: .PHONY .MAKE
3096 .if !defined(_MKSHOWCONFIG) && exists(${.CURDIR}/${_lib})
3097         ${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
3098                 cd ${.CURDIR}/${_lib}; \
3099                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
3100                 ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ all; \
3101                 ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ install
3102 .endif
3103 .endfor
3104
3105 _prereq_libs: ${_prereq_libs:S/$/__PL/}
3106 _startup_libs: ${_startup_libs:S/$/__L/}
3107 _prebuild_libs: ${_prebuild_libs:S/$/__L/}
3108 _generic_libs: ${_generic_libs:S/$/__L/}
3109
3110 # Enable SUBDIR_PARALLEL when not calling 'make all', unless called from
3111 # 'everything' with _PARALLEL_SUBDIR_OK set.  This is because it is unlikely
3112 # that running 'make all' from the top-level, especially with a SUBDIR_OVERRIDE
3113 # or LOCAL_DIRS set, will have a reliable build if SUBDIRs are built in
3114 # parallel.  This is safe for the world stage of buildworld though since it has
3115 # already built libraries in a proper order and installed includes into
3116 # WORLDTMP. Special handling is done for SUBDIR ordering for 'install*' to
3117 # avoid trashing a system if it crashes mid-install.
3118 .if !make(all) || defined(_PARALLEL_SUBDIR_OK)
3119 SUBDIR_PARALLEL=
3120 .endif
3121
3122 .include <bsd.subdir.mk>
3123
3124 .if make(check-old) || make(check-old-dirs) || \
3125     make(check-old-files) || make(check-old-libs) || \
3126     make(delete-old) || make(delete-old-dirs) || \
3127     make(delete-old-files) || make(delete-old-libs)
3128
3129 #
3130 # check for / delete old files section
3131 #
3132
3133 .include "ObsoleteFiles.inc"
3134
3135 OLD_LIBS_MESSAGE="Please be sure no application still uses those libraries, \
3136 else you can not start such an application. Consult UPDATING for more \
3137 information regarding how to cope with the removal/revision bump of a \
3138 specific library."
3139
3140 .if !defined(BATCH_DELETE_OLD_FILES)
3141 RM_I=-i
3142 .else
3143 RM_I=-v
3144 .endif
3145
3146 delete-old-files: .PHONY
3147         @echo ">>> Removing old files (only deletes safe to delete libs)"
3148 # Ask for every old file if the user really wants to remove it.
3149 # It's annoying, but better safe than sorry.
3150 # NB: We cannot pass the list of OLD_FILES as a parameter because the
3151 # argument list will get too long. Using .for/.endfor make "loops" will make
3152 # the Makefile parser segfault.
3153         @exec 3<&0; \
3154         cd ${.CURDIR}; \
3155         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3156             -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | sort | \
3157         while read file; do \
3158                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3159                         chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
3160                         rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
3161                 fi; \
3162                 for ext in debug symbols; do \
3163                   if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
3164                       "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3165                           rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
3166                               <&3; \
3167                   fi; \
3168                 done; \
3169         done
3170 # Remove catpages without corresponding manpages.
3171         @exec 3<&0; \
3172         find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | sort | \
3173         sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
3174         while read catpage; do \
3175                 read manpage; \
3176                 if [ ! -e "$${manpage}" ]; then \
3177                         rm ${RM_I} $${catpage} <&3; \
3178                 fi; \
3179         done
3180         @echo ">>> Old files removed"
3181
3182 check-old-files: .PHONY
3183         @echo ">>> Checking for old files"
3184         @cd ${.CURDIR}; \
3185         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3186             -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
3187         while read file; do \
3188                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3189                         echo "${DESTDIR}/$${file}"; \
3190                 fi; \
3191                 for ext in debug symbols; do \
3192                   if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3193                           echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
3194                   fi; \
3195                 done; \
3196         done | sort
3197 # Check for catpages without corresponding manpages.
3198         @find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | \
3199         sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
3200         while read catpage; do \
3201                 read manpage; \
3202                 if [ ! -e "$${manpage}" ]; then \
3203                         echo $${catpage}; \
3204                 fi; \
3205         done | sort
3206
3207 delete-old-libs: .PHONY
3208         @echo ">>> Removing old libraries"
3209         @echo "${OLD_LIBS_MESSAGE}" | fmt
3210         @exec 3<&0; \
3211         cd ${.CURDIR}; \
3212         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3213             -V OLD_LIBS | xargs -n1 | sort | \
3214         while read file; do \
3215                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3216                         chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
3217                         rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
3218                 fi; \
3219                 for ext in debug symbols; do \
3220                   if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
3221                       "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3222                           rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
3223                               <&3; \
3224                   fi; \
3225                 done; \
3226         done
3227         @echo ">>> Old libraries removed"
3228
3229 check-old-libs: .PHONY
3230         @echo ">>> Checking for old libraries"
3231         @cd ${.CURDIR}; \
3232         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3233             -V OLD_LIBS | xargs -n1 | \
3234         while read file; do \
3235                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3236                         echo "${DESTDIR}/$${file}"; \
3237                 fi; \
3238                 for ext in debug symbols; do \
3239                   if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3240                           echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
3241                   fi; \
3242                 done; \
3243         done | sort
3244
3245 delete-old-dirs: .PHONY
3246         @echo ">>> Removing old directories"
3247         @cd ${.CURDIR}; \
3248         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3249             -V OLD_DIRS | xargs -n1 | sort -r | \
3250         while read dir; do \
3251                 if [ -d "${DESTDIR}/$${dir}" ]; then \
3252                         rmdir -v "${DESTDIR}/$${dir}" || true; \
3253                 elif [ -L "${DESTDIR}/$${dir}" ]; then \
3254                         echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
3255                 fi; \
3256                 if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3257                         rmdir -v "${DESTDIR}${DEBUGDIR}/$${dir}" || true; \
3258                 elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3259                         echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
3260                 fi; \
3261         done
3262         @echo ">>> Old directories removed"
3263
3264 check-old-dirs: .PHONY
3265         @echo ">>> Checking for old directories"
3266         @cd ${.CURDIR}; \
3267         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3268             -V OLD_DIRS | xargs -n1 | sort -r | \
3269         while read dir; do \
3270                 if [ -d "${DESTDIR}/$${dir}" ]; then \
3271                         echo "${DESTDIR}/$${dir}"; \
3272                 elif [ -L "${DESTDIR}/$${dir}" ]; then \
3273                         echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
3274                 fi; \
3275                 if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3276                         echo "${DESTDIR}${DEBUGDIR}/$${dir}"; \
3277                 elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3278                         echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
3279                 fi; \
3280         done
3281
3282 delete-old: delete-old-files delete-old-dirs .PHONY
3283         @echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
3284
3285 check-old: check-old-files check-old-libs check-old-dirs .PHONY
3286         @echo "To remove old files and directories run '${MAKE_CMD} delete-old'."
3287         @echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
3288
3289 .endif
3290
3291 #
3292 # showconfig - show build configuration.
3293 #
3294 showconfig: .PHONY
3295         @(${MAKE} -n -f ${.CURDIR}/sys/conf/kern.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes; \
3296           ${MAKE} -n -f ${.CURDIR}/share/mk/src.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes) 2>&1 | grep ^MK_ | sort -u
3297
3298 .if !empty(KRNLOBJDIR) && !empty(KERNCONF)
3299 DTBOUTPUTPATH= ${KRNLOBJDIR}/${KERNCONF}/
3300
3301 .if !defined(FDT_DTS_FILE) || empty(FDT_DTS_FILE)
3302 .if !defined(_MKSHOWCONFIG) && exists(${KERNCONFDIR}/${KERNCONF})
3303 FDT_DTS_FILE!= awk 'BEGIN {FS="="} /^makeoptions[[:space:]]+FDT_DTS_FILE/ {print $$2}' \
3304         '${KERNCONFDIR}/${KERNCONF}' ; echo
3305 .endif
3306 .endif
3307
3308 .endif
3309
3310 .if !defined(DTBOUTPUTPATH) || !exists(${DTBOUTPUTPATH})
3311 DTBOUTPUTPATH= ${.CURDIR}
3312 .endif
3313
3314 #
3315 # Build 'standalone' Device Tree Blob
3316 #
3317 builddtb: .PHONY
3318         @PATH=${TMPPATH} MACHINE=${TARGET} \
3319         sh ${.CURDIR}/sys/tools/fdt/make_dtb.sh ${.CURDIR}/sys \
3320             "${FDT_DTS_FILE}" ${DTBOUTPUTPATH}
3321
3322 ###############
3323
3324 # cleanworld
3325 # In the following, the first 'rm' in a series will usually remove all
3326 # files and directories.  If it does not, then there are probably some
3327 # files with file flags set, so this unsets them and tries the 'rm' a
3328 # second time.  There are situations where this target will be cleaning
3329 # some directories via more than one method, but that duplication is
3330 # needed to correctly handle all the possible situations.  Removing all
3331 # files without file flags set in the first 'rm' instance saves time,
3332 # because 'chflags' will need to operate on fewer files afterwards.
3333 #
3334 # It is expected that BW_CANONICALOBJDIR == the CANONICALOBJDIR as would be
3335 # created by bsd.obj.mk, except that we don't want to .include that file
3336 # in this makefile.  We don't do a cleandir walk if MK_AUTO_OBJ is yes
3337 # since it is not possible for files to land in the wrong place.
3338 #
3339 .if make(cleanworld)
3340 BW_CANONICALOBJDIR:=${OBJTOP}/
3341 .elif make(cleanuniverse)
3342 BW_CANONICALOBJDIR:=${OBJROOT}
3343 .if ${MK_UNIFIED_OBJDIR} == "no"
3344 .error ${.TARGETS} only supported with WITH_UNIFIED_OBJDIR enabled.
3345 .endif
3346 .endif
3347 cleanworld cleanuniverse: .PHONY
3348 .if !empty(BW_CANONICALOBJDIR) && exists(${BW_CANONICALOBJDIR}) && \
3349     ${.CURDIR:tA} != ${BW_CANONICALOBJDIR:tA}
3350         -rm -rf ${BW_CANONICALOBJDIR}*
3351         -chflags -R 0 ${BW_CANONICALOBJDIR}
3352         rm -rf ${BW_CANONICALOBJDIR}*
3353 .endif
3354 .if make(cleanworld) && ${MK_AUTO_OBJ} == "no" && \
3355     (empty(BW_CANONICALOBJDIR) || ${.CURDIR:tA} == ${BW_CANONICALOBJDIR:tA})
3356 .if ${.CURDIR} == ${.OBJDIR} || ${.CURDIR}/obj == ${.OBJDIR}
3357         #   To be safe in this case, fall back to a 'make cleandir'
3358         ${_+_}@cd ${.CURDIR}; ${MAKE} cleandir
3359 .endif
3360 .endif
3361
3362 .if ${TARGET} == ${MACHINE} && ${TARGET_ARCH} == ${MACHINE_ARCH}
3363 XDEV_CPUTYPE?=${CPUTYPE}
3364 .else
3365 XDEV_CPUTYPE?=${TARGET_CPUTYPE}
3366 .endif
3367
3368 NOFUN=-DNO_FSCHG MK_HTML=no -DNO_LINT \
3369         MK_MAN=no MK_NLS=no MK_PROFILE=no \
3370         MK_KERBEROS=no MK_RESCUE=no MK_TESTS=no MK_WARNS=no \
3371         TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
3372         CPUTYPE=${XDEV_CPUTYPE}
3373
3374 XDDIR=${TARGET_ARCH}-freebsd
3375 XDTP?=/usr/${XDDIR}
3376 .if ${XDTP:N/*}
3377 .error XDTP variable should be an absolute path
3378 .endif
3379
3380 CDBOBJROOT=     ${OBJROOT}${MACHINE}.${MACHINE_ARCH}/xdev/
3381 CDBOBJTOP=      ${CDBOBJROOT}${XDDIR}
3382 CDBENV= \
3383         INSTALL="sh ${.CURDIR}/tools/install.sh"
3384 CDENV= ${CDBENV} \
3385         TOOLS_PREFIX=${XDTP}
3386 CDMAKEARGS= \
3387         OBJTOP=${CDBOBJTOP:Q} \
3388         OBJROOT=${CDBOBJROOT:Q}
3389 CD2MAKEARGS= ${CDMAKEARGS}
3390
3391 .if ${WANT_COMPILER_TYPE} == gcc || \
3392     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
3393 # GCC requires -isystem and -L when using a cross-compiler.  --sysroot
3394 # won't set header path and -L is used to ensure the base library path
3395 # is added before the port PREFIX library path.
3396 CD2CFLAGS+=     -isystem ${XDDESTDIR}/usr/include -L${XDDESTDIR}/usr/lib
3397 # GCC requires -B to find /usr/lib/crti.o when using a cross-compiler
3398 # combined with --sysroot.
3399 CD2CFLAGS+=     -B${XDDESTDIR}/usr/lib
3400 # Force using libc++ for external GCC.
3401 .if defined(X_COMPILER_TYPE) && \
3402     ${X_COMPILER_TYPE} == gcc && ${X_COMPILER_VERSION} >= 40800
3403 CD2CXXFLAGS+=   -isystem ${XDDESTDIR}/usr/include/c++/v1 -std=c++11 \
3404                 -nostdinc++
3405 .endif
3406 .endif
3407 CD2CFLAGS+=     --sysroot=${XDDESTDIR}/
3408 CD2ENV=${CDENV} CC="${CC} ${CD2CFLAGS}" CXX="${CXX} ${CD2CXXFLAGS} ${CD2CFLAGS}" \
3409         CPP="${CPP} ${CD2CFLAGS}" \
3410         MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH}
3411
3412 CDTMP=  ${OBJTOP}/${XDDIR}/tmp
3413 CDMAKE=${CDENV} PATH=${CDTMP}/usr/bin:${PATH} ${MAKE} ${CDMAKEARGS} ${NOFUN}
3414 CD2MAKE=${CD2ENV} PATH=${CDTMP}/usr/bin:${XDDESTDIR}/usr/bin:${PATH} \
3415         ${MAKE} ${CD2MAKEARGS} ${NOFUN}
3416 .if ${MK_META_MODE} != "no"
3417 # Don't rebuild build-tools targets during normal build.
3418 CD2MAKE+=       BUILD_TOOLS_META=.NOMETA
3419 .endif
3420 XDDESTDIR=${DESTDIR}${XDTP}
3421
3422 .ORDER: xdev-build xdev-install xdev-links
3423 xdev: xdev-build xdev-install .PHONY
3424
3425 .ORDER: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
3426 xdev-build: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools .PHONY
3427
3428 _xb-worldtmp: .PHONY
3429         mkdir -p ${CDTMP}/usr
3430         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
3431             -p ${CDTMP}/usr >/dev/null
3432
3433 _xb-bootstrap-tools: .PHONY
3434 .for _tool in \
3435     ${_clang_tblgen} \
3436     ${_yacc}
3437         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
3438         cd ${.CURDIR}/${_tool}; \
3439         if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
3440         ${CDMAKE} DIRPRFX=${_tool}/ all; \
3441         ${CDMAKE} DIRPRFX=${_tool}/ DESTDIR=${CDTMP} install
3442 .endfor
3443
3444 _xb-build-tools: .PHONY
3445         ${_+_}@cd ${.CURDIR}; \
3446         ${CDBENV} ${MAKE} ${CDMAKEARGS} -f Makefile.inc1 ${NOFUN} build-tools
3447
3448 XDEVDIRS= \
3449     ${_clang_libs} \
3450     ${_lld} \
3451     ${_elftctools} \
3452     usr.bin/ar \
3453     ${_clang}
3454
3455 _xb-cross-tools: .PHONY
3456 .for _tool in ${XDEVDIRS}
3457         ${_+_}@${ECHODIR} "===> xdev ${_tool} (obj,all)"; \
3458         cd ${.CURDIR}/${_tool}; \
3459         if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
3460         ${CDMAKE} DIRPRFX=${_tool}/ all
3461 .endfor
3462
3463 _xi-mtree: .PHONY
3464         ${_+_}@${ECHODIR} "mtree populating ${XDDESTDIR}"
3465         mkdir -p ${XDDESTDIR}
3466         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.root.dist \
3467             -p ${XDDESTDIR} >/dev/null
3468         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
3469             -p ${XDDESTDIR}/usr >/dev/null
3470         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
3471             -p ${XDDESTDIR}/usr/include >/dev/null
3472 .if defined(_LIBCOMPAT)
3473         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
3474             -p ${XDDESTDIR}/usr >/dev/null
3475 .endif
3476 .if ${MK_TESTS} != "no"
3477         mkdir -p ${XDDESTDIR}${TESTSBASE}
3478         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
3479             -p ${XDDESTDIR}${TESTSBASE} >/dev/null
3480 .endif
3481
3482 .ORDER: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
3483 xdev-install: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries .PHONY
3484
3485 _xi-cross-tools: .PHONY
3486         @echo "_xi-cross-tools"
3487 .for _tool in ${XDEVDIRS}
3488         ${_+_}@${ECHODIR} "===> xdev ${_tool} (install)"; \
3489         cd ${.CURDIR}/${_tool}; \
3490         ${CDMAKE} DIRPRFX=${_tool}/ install DESTDIR=${XDDESTDIR}
3491 .endfor
3492
3493 _xi-includes: .PHONY
3494 .if !defined(NO_OBJWALK)
3495         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 _obj \
3496                 DESTDIR=${XDDESTDIR}
3497 .endif
3498         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 includes \
3499                 DESTDIR=${XDDESTDIR}
3500
3501 _xi-libraries: .PHONY
3502         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 libraries \
3503                 DESTDIR=${XDDESTDIR}
3504
3505 xdev-links: .PHONY
3506         ${_+_}cd ${XDDESTDIR}/usr/bin; \
3507         mkdir -p ../../../../usr/bin; \
3508                 for i in *; do \
3509                         ln -sf ../../${XDTP}/usr/bin/$$i \
3510                             ../../../../usr/bin/${XDDIR}-$$i; \
3511                         ln -sf ../../${XDTP}/usr/bin/$$i \
3512                             ../../../../usr/bin/${XDDIR}${_REVISION}-$$i; \
3513                 done