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