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