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