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