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