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