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