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