xref: /freebsd/Makefile.inc1 (revision 2e36db147e0edeb1b213f283f9b41a8d1a902a88)
1#
2# $FreeBSD$
3#
4# Make command line options:
5#	-DNO_CLEANDIR run ${MAKE} clean, instead of ${MAKE} cleandir
6#	-DNO_CLEAN do not clean at all
7#	-DDB_FROM_SRC use the user/group databases in src/etc instead of
8#	    the system database when installing.
9#	-DNO_SHARE do not go into share subdir
10#	-DKERNFAST define NO_KERNEL{CONFIG,CLEAN,OBJ}
11#	-DNO_KERNELCONFIG do not run config in ${MAKE} buildkernel
12#	-DNO_KERNELCLEAN do not run ${MAKE} clean in ${MAKE} buildkernel
13#	-DNO_KERNELOBJ do not run ${MAKE} obj in ${MAKE} buildkernel
14#	-DNO_PORTSUPDATE do not update ports in ${MAKE} update
15#	-DNO_ROOT install without using root privilege
16#	-DNO_DOCUPDATE do not update doc in ${MAKE} update
17#	-DWITHOUT_CTF do not run the DTrace CTF conversion tools on built objects
18#	LOCAL_DIRS="list of dirs" to add additional dirs to the SUBDIR list
19#	LOCAL_ITOOLS="list of tools" to add additional tools to the ITOOLS list
20#	LOCAL_LIB_DIRS="list of dirs" to add additional dirs to libraries target
21#	LOCAL_MTREE="list of mtree files" to process to allow local directories
22#	    to be created before files are installed
23#	LOCAL_TOOL_DIRS="list of dirs" to add additional dirs to the build-tools
24#	    list
25#	LOCAL_XTOOL_DIRS="list of dirs" to add additional dirs to the
26#	    cross-tools target
27#	METALOG="path to metadata log" to write permission and ownership
28#	    when NO_ROOT is set.  (default: ${DESTDIR}/METALOG)
29#	TARGET="machine" to crossbuild world for a different machine type
30#	TARGET_ARCH= may be required when a TARGET supports multiple endians
31#	BUILDENV_SHELL= shell to launch for the buildenv target (def:${SHELL})
32#	WORLD_FLAGS= additional flags to pass to make(1) during buildworld
33#	KERNEL_FLAGS= additional flags to pass to make(1) during buildkernel
34#	SUBDIR_OVERRIDE="list of dirs" to build rather than everything.
35#	    All libraries and includes, and some build tools will still build.
36
37#
38# The intended user-driven targets are:
39# buildworld  - rebuild *everything*, including glue to help do upgrades
40# installworld- install everything built by "buildworld"
41# checkworld  - run test suite on installed world
42# doxygen     - build API documentation of the kernel
43# update      - convenient way to update your source tree (eg: svn/svnup)
44#
45# Standard targets (not defined here) are documented in the makefiles in
46# /usr/share/mk.  These include:
47#		obj depend all install clean cleandepend cleanobj
48
49.if !defined(TARGET) || !defined(TARGET_ARCH)
50.error "Both TARGET and TARGET_ARCH must be defined."
51.endif
52
53SRCDIR?=	${.CURDIR}
54LOCALBASE?=	/usr/local
55
56# Cross toolchain changes must be in effect before bsd.compiler.mk
57# so that gets the right CC, and pass CROSS_TOOLCHAIN to submakes.
58.if defined(CROSS_TOOLCHAIN)
59.include "${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk"
60CROSSENV+=CROSS_TOOLCHAIN="${CROSS_TOOLCHAIN}"
61.endif
62.if defined(CROSS_TOOLCHAIN_PREFIX)
63CROSS_COMPILER_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
64.endif
65
66XCOMPILERS=	CC CXX CPP
67.for COMPILER in ${XCOMPILERS}
68.if defined(CROSS_COMPILER_PREFIX)
69X${COMPILER}?=	${CROSS_COMPILER_PREFIX}${${COMPILER}}
70.else
71X${COMPILER}?=	${${COMPILER}}
72.endif
73.endfor
74# If a full path to an external cross compiler is given, don't build
75# a cross compiler.
76.if ${XCC:N${CCACHE_BIN}:M/*}
77MK_CLANG_BOOTSTRAP=	no
78MK_GCC_BOOTSTRAP=	no
79.endif
80
81# Pull in compiler metadata from buildworld/toolchain if possible to avoid
82# running CC from bsd.compiler.mk.
83.if make(installworld) || make(install) || make(distributeworld) || \
84    make(stageworld)
85.-include "${OBJTOP}/compiler-metadata.mk"
86.endif
87
88# Pull in COMPILER_TYPE and COMPILER_FREEBSD_VERSION early.
89.include <bsd.compiler.mk>
90.include "share/mk/src.opts.mk"
91
92# Check if there is a local compiler that can satisfy as an external compiler.
93# Which compiler is expected to be used?
94.if ${MK_CLANG_BOOTSTRAP} == "yes"
95WANT_COMPILER_TYPE=	clang
96.elif ${MK_GCC_BOOTSTRAP} == "yes"
97WANT_COMPILER_TYPE=	gcc
98.else
99WANT_COMPILER_TYPE=
100.endif
101.if !defined(WANT_COMPILER_FREEBSD_VERSION)
102.if ${WANT_COMPILER_TYPE} == "clang"
103WANT_COMPILER_FREEBSD_VERSION_FILE= lib/clang/freebsd_cc_version.h
104WANT_COMPILER_FREEBSD_VERSION!= \
105	awk '$$2 == "FREEBSD_CC_VERSION" {printf("%d\n", $$3)}' \
106	${SRCDIR}/${WANT_COMPILER_FREEBSD_VERSION_FILE} || echo unknown
107WANT_COMPILER_VERSION_FILE= lib/clang/include/clang/Basic/Version.inc
108WANT_COMPILER_VERSION!= \
109	awk '$$2 == "CLANG_VERSION" {split($$3, a, "."); print a[1] * 10000 + a[2] * 100 + a[3]}' \
110	${SRCDIR}/${WANT_COMPILER_VERSION_FILE} || echo unknown
111.elif ${WANT_COMPILER_TYPE} == "gcc"
112WANT_COMPILER_FREEBSD_VERSION_FILE= gnu/usr.bin/cc/cc_tools/freebsd-native.h
113WANT_COMPILER_FREEBSD_VERSION!= \
114	awk '$$2 == "FBSD_CC_VER" {printf("%d\n", $$3)}' \
115	${SRCDIR}/${WANT_COMPILER_FREEBSD_VERSION_FILE} || echo unknown
116WANT_COMPILER_VERSION_FILE= contrib/gcc/BASE-VER
117WANT_COMPILER_VERSION!= \
118	awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3}' \
119	${SRCDIR}/${WANT_COMPILER_VERSION_FILE} || echo unknown
120.endif
121.export WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_VERSION
122.endif	# !defined(WANT_COMPILER_FREEBSD_VERSION)
123# It needs to be the same revision as we would build for the bootstrap.
124# If the expected vs CC is different then we can't skip.
125# GCC cannot be used for cross-arch yet.  For clang we pass -target later if
126# TARGET_ARCH!=MACHINE_ARCH.
127.if ${MK_SYSTEM_COMPILER} == "yes" && \
128    (${MK_CLANG_BOOTSTRAP} == "yes" || ${MK_GCC_BOOTSTRAP} == "yes") && \
129    !make(showconfig) && !make(xdev*) && \
130    ${WANT_COMPILER_TYPE} == ${COMPILER_TYPE} && \
131    (${COMPILER_TYPE} == "clang" || ${TARGET_ARCH} == ${MACHINE_ARCH}) && \
132    ${COMPILER_VERSION} == ${WANT_COMPILER_VERSION} && \
133    ${COMPILER_FREEBSD_VERSION} == ${WANT_COMPILER_FREEBSD_VERSION}
134# Everything matches, disable the bootstrap compiler.
135MK_CLANG_BOOTSTRAP=	no
136MK_GCC_BOOTSTRAP=	no
137USING_SYSTEM_COMPILER=	yes
138.endif	# ${WANT_COMPILER_TYPE} == ${COMPILER_TYPE}
139USING_SYSTEM_COMPILER?=	no
140TEST_SYSTEM_COMPILER_VARS= \
141	USING_SYSTEM_COMPILER MK_SYSTEM_COMPILER \
142	MK_CROSS_COMPILER MK_CLANG_BOOTSTRAP MK_GCC_BOOTSTRAP \
143	WANT_COMPILER_TYPE WANT_COMPILER_VERSION WANT_COMPILER_VERSION_FILE \
144	WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_FREEBSD_VERSION_FILE \
145	CC COMPILER_TYPE COMPILER_FEATURES COMPILER_VERSION \
146	COMPILER_FREEBSD_VERSION \
147	LINKER_TYPE LINKER_VERSION
148test-system-compiler: .PHONY
149.for v in ${TEST_SYSTEM_COMPILER_VARS}
150	${_+_}@printf "%-35s= %s\n" "${v}" "${${v}}"
151.endfor
152.if ${USING_SYSTEM_COMPILER} == "yes" && \
153    (make(buildworld) || make(buildkernel) || make(kernel-toolchain) || \
154    make(toolchain) || make(_cross-tools))
155.info SYSTEM_COMPILER: Determined that CC=${CC} matches the source tree.  Not bootstrapping a cross-compiler.
156.endif
157
158# For installworld need to ensure that the looked-up compiler metadata is
159# passed along rather than trying to run cc from the restricted
160# STRICTTMPPATH.
161.if ${MK_CLANG_BOOTSTRAP} == "no" && ${MK_GCC_BOOTSTRAP} == "no"
162.if !defined(X_COMPILER_TYPE)
163CROSSENV+=	COMPILER_VERSION=${COMPILER_VERSION} \
164		COMPILER_TYPE=${COMPILER_TYPE} \
165		COMPILER_FEATURES=${COMPILER_FEATURES} \
166		COMPILER_FREEBSD_VERSION=${COMPILER_FREEBSD_VERSION}
167.else
168CROSSENV+=	COMPILER_VERSION=${X_COMPILER_VERSION} \
169		COMPILER_FEATURES=${X_COMPILER_FEATURES} \
170		COMPILER_TYPE=${X_COMPILER_TYPE} \
171		COMPILER_FREEBSD_VERSION=${X_COMPILER_FREEBSD_VERSION}
172.endif
173.endif
174# Store some compiler metadata for use in installworld where we don't
175# want to invoke CC at all.
176_COMPILER_METADATA_VARS=	COMPILER_VERSION \
177				COMPILER_TYPE \
178				COMPILER_FEATURES \
179				COMPILER_FREEBSD_VERSION \
180				LINKER_VERSION \
181				LINKER_TYPE
182compiler-metadata.mk: .PHONY .META
183	@: > ${.TARGET}
184	@echo ".info Using cached compiler metadata from build at $$(hostname) on $$(date)" \
185	    > ${.TARGET}
186.for v in ${_COMPILER_METADATA_VARS}
187	@echo "${v}=${${v}}" >> ${.TARGET}
188.endfor
189	@echo ".export ${_COMPILER_METADATA_VARS}" >> ${.TARGET}
190
191# Handle external binutils.
192.if defined(CROSS_TOOLCHAIN_PREFIX)
193CROSS_BINUTILS_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
194.endif
195# If we do not have a bootstrap binutils (because the in-tree one does not
196# support the target architecture), provide a default cross-binutils prefix.
197# This allows riscv64 builds, for example, to automatically use the
198# riscv64-binutils port or package.
199.if !make(showconfig)
200.if !empty(BROKEN_OPTIONS:MBINUTILS_BOOTSTRAP) && \
201    ${MK_LLD_BOOTSTRAP} == "no" && \
202    !defined(CROSS_BINUTILS_PREFIX)
203CROSS_BINUTILS_PREFIX=/usr/local/${TARGET_ARCH}-freebsd/bin/
204.if !exists(${CROSS_BINUTILS_PREFIX})
205.error In-tree binutils does not support the ${TARGET_ARCH} architecture. Install the ${TARGET_ARCH}-binutils port or package or set CROSS_BINUTILS_PREFIX.
206.endif
207.endif
208.endif
209XBINUTILS=	AS AR LD NM OBJCOPY RANLIB SIZE STRINGS
210.for BINUTIL in ${XBINUTILS}
211.if defined(CROSS_BINUTILS_PREFIX) && \
212    exists(${CROSS_BINUTILS_PREFIX}${${BINUTIL}})
213X${BINUTIL}?=	${CROSS_BINUTILS_PREFIX}${${BINUTIL}}
214.else
215X${BINUTIL}?=	${${BINUTIL}}
216.endif
217.endfor
218
219
220# We must do lib/ and libexec/ before bin/ in case of a mid-install error to
221# keep the users system reasonably usable.  For static->dynamic root upgrades,
222# we don't want to install a dynamic binary without rtld and the needed
223# libraries.  More commonly, for dynamic root, we don't want to install a
224# binary that requires a newer library version that hasn't been installed yet.
225# This ordering is not a guarantee though.  The only guarantee of a working
226# system here would require fine-grained ordering of all components based
227# on their dependencies.
228.if !empty(SUBDIR_OVERRIDE)
229SUBDIR=	${SUBDIR_OVERRIDE}
230.else
231SUBDIR=	lib libexec
232.if !defined(NO_ROOT) && (make(installworld) || make(install))
233# Ensure libraries are installed before progressing.
234SUBDIR+=.WAIT
235.endif
236SUBDIR+=bin
237.if ${MK_CDDL} != "no"
238SUBDIR+=cddl
239.endif
240SUBDIR+=gnu include
241.if ${MK_KERBEROS} != "no"
242SUBDIR+=kerberos5
243.endif
244.if ${MK_RESCUE} != "no"
245SUBDIR+=rescue
246.endif
247SUBDIR+=sbin
248.if ${MK_CRYPT} != "no"
249SUBDIR+=secure
250.endif
251.if !defined(NO_SHARE)
252SUBDIR+=share
253.endif
254SUBDIR+=sys usr.bin usr.sbin
255.if ${MK_TESTS} != "no"
256SUBDIR+=	tests
257.endif
258.if ${MK_OFED} != "no"
259SUBDIR+=contrib/ofed
260.endif
261
262# Local directories are last, since it is nice to at least get the base
263# system rebuilt before you do them.
264.for _DIR in ${LOCAL_DIRS}
265.if exists(${.CURDIR}/${_DIR}/Makefile)
266SUBDIR+=	${_DIR}
267.endif
268.endfor
269# Add LOCAL_LIB_DIRS, but only if they will not be picked up as a SUBDIR
270# of a LOCAL_DIRS directory.  This allows LOCAL_DIRS=foo and
271# LOCAL_LIB_DIRS=foo/lib to behave as expected.
272.for _DIR in ${LOCAL_DIRS:M*/} ${LOCAL_DIRS:N*/:S|$|/|}
273_REDUNDANT_LIB_DIRS+=    ${LOCAL_LIB_DIRS:M${_DIR}*}
274.endfor
275.for _DIR in ${LOCAL_LIB_DIRS}
276.if empty(_REDUNDANT_LIB_DIRS:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile)
277SUBDIR+=	${_DIR}
278.endif
279.endfor
280
281# We must do etc/ last as it hooks into building the man whatis file
282# by calling 'makedb' in share/man.  This is only relevant for
283# install/distribute so they build the whatis file after every manpage is
284# installed.
285.if make(installworld) || make(install)
286SUBDIR+=.WAIT
287.endif
288SUBDIR+=etc
289
290.endif	# !empty(SUBDIR_OVERRIDE)
291
292.if defined(NOCLEAN)
293.warning NOCLEAN option is deprecated. Use NO_CLEAN instead.
294NO_CLEAN=	${NOCLEAN}
295.endif
296.if defined(NO_CLEANDIR)
297CLEANDIR=	clean cleandepend
298.else
299CLEANDIR=	cleandir
300.endif
301
302.if defined(WORLDFAST)
303NO_CLEAN=	t
304NO_OBJWALK=	t
305.endif
306
307.if ${MK_META_MODE} == "yes"
308# If filemon is used then we can rely on the build being incremental-safe.
309# The .meta files will also track the build command and rebuild should
310# it change.
311.if empty(.MAKE.MODE:Mnofilemon)
312NO_CLEAN=	t
313.endif
314.endif
315.if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
316NO_OBJWALK=	t
317NO_KERNELOBJ=	t
318.endif
319.if !defined(NO_OBJWALK)
320_obj=		obj
321.endif
322
323LOCAL_TOOL_DIRS?=
324PACKAGEDIR?=	${DESTDIR}/${DISTDIR}
325
326.if empty(SHELL:M*csh*)
327BUILDENV_SHELL?=${SHELL}
328.else
329BUILDENV_SHELL?=/bin/sh
330.endif
331
332.if !defined(SVN) || empty(SVN)
333. for _P in /usr/bin /usr/local/bin
334.  for _S in svn svnlite
335.   if exists(${_P}/${_S})
336SVN=   ${_P}/${_S}
337.   endif
338.  endfor
339. endfor
340.endif
341SVNFLAGS?=	-r HEAD
342.if !defined(VCS_REVISION) && empty(VCS_REVISION)
343_VCS_REVISION?=	$$(eval ${SVNVERSION_CMD} ${SRCDIR})
344. if !empty(_VCS_REVISION)
345VCS_REVISION=	$$(echo r${_VCS_REVISION})
346. endif
347.endif
348
349.if !defined(OSRELDATE)
350.if exists(/usr/include/osreldate.h)
351OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
352		/usr/include/osreldate.h
353.else
354OSRELDATE=	0
355.endif
356.export OSRELDATE
357.endif
358
359# Set VERSION for CTFMERGE to use via the default CTFFLAGS=-L VERSION.
360.if !defined(_REVISION)
361_REVISION!=	${MAKE} -C ${SRCDIR}/release MK_AUTO_OBJ=no -V REVISION
362.export _REVISION
363.endif
364.if !defined(_BRANCH)
365_BRANCH!=	${MAKE} -C ${SRCDIR}/release MK_AUTO_OBJ=no -V BRANCH
366.export _BRANCH
367.endif
368.if !defined(SRCRELDATE)
369SRCRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
370		${SRCDIR}/sys/sys/param.h
371.export SRCRELDATE
372.endif
373.if !defined(VERSION)
374VERSION=	FreeBSD ${_REVISION}-${_BRANCH:C/-p[0-9]+$//} ${TARGET_ARCH} ${SRCRELDATE}
375.export VERSION
376.endif
377
378.if !defined(PKG_VERSION)
379.if ${_BRANCH:MSTABLE*} || ${_BRANCH:MCURRENT*} || ${_BRANCH:MALPHA*}
380TIMENOW=	%Y%m%d%H%M%S
381EXTRA_REVISION=	.s${TIMENOW:gmtime}
382.endif
383.if ${_BRANCH:M*-p*}
384EXTRA_REVISION=	_${_BRANCH:C/.*-p([0-9]+$)/\1/}
385.endif
386PKG_VERSION=	${_REVISION}${EXTRA_REVISION}
387.endif
388
389KNOWN_ARCHES?=	aarch64/arm64 \
390		amd64 \
391		arm \
392		armeb/arm \
393		armv6/arm \
394		armv7/arm \
395		i386 \
396		mips \
397		mipsel/mips \
398		mips64el/mips \
399		mipsn32el/mips \
400		mips64/mips \
401		mipsn32/mips \
402		mipshf/mips \
403		mipselhf/mips \
404		mips64elhf/mips \
405		mips64hf/mips \
406		powerpc \
407		powerpc64/powerpc \
408		powerpcspe/powerpc \
409		riscv64/riscv \
410		riscv64sf/riscv \
411		sparc64
412
413.if ${TARGET} == ${TARGET_ARCH}
414_t=		${TARGET}
415.else
416_t=		${TARGET_ARCH}/${TARGET}
417.endif
418.for _t in ${_t}
419.if empty(KNOWN_ARCHES:M${_t})
420.error Unknown target ${TARGET_ARCH}:${TARGET}.
421.endif
422.endfor
423
424.if ${TARGET} == ${MACHINE}
425TARGET_CPUTYPE?=${CPUTYPE}
426.else
427TARGET_CPUTYPE?=
428.endif
429
430.if !empty(TARGET_CPUTYPE)
431_TARGET_CPUTYPE=${TARGET_CPUTYPE}
432.else
433_TARGET_CPUTYPE=dummy
434.endif
435_CPUTYPE!=	MAKEFLAGS= CPUTYPE=${_TARGET_CPUTYPE} ${MAKE} -f /dev/null \
436		-m ${.CURDIR}/share/mk MK_AUTO_OBJ=no -V CPUTYPE
437.if ${_CPUTYPE} != ${_TARGET_CPUTYPE}
438.error CPUTYPE global should be set with ?=.
439.endif
440.if make(buildworld)
441BUILD_ARCH!=	uname -p
442.if ${MACHINE_ARCH} != ${BUILD_ARCH}
443.error To cross-build, set TARGET_ARCH.
444.endif
445.endif
446WORLDTMP?=	${OBJTOP}/tmp
447BPATH=		${CCACHE_WRAPPER_PATH_PFX}${WORLDTMP}/legacy/usr/sbin:${WORLDTMP}/legacy/usr/bin:${WORLDTMP}/legacy/bin
448XPATH=		${WORLDTMP}/usr/sbin:${WORLDTMP}/usr/bin
449STRICTTMPPATH=	${BPATH}:${XPATH}
450TMPPATH=	${STRICTTMPPATH}:${PATH}
451
452#
453# Avoid running mktemp(1) unless actually needed.
454# It may not be functional, e.g., due to new ABI
455# when in the middle of installing over this system.
456#
457.if make(distributeworld) || make(installworld) || make(stageworld)
458INSTALLTMP!=	/usr/bin/mktemp -d -u -t install
459.endif
460
461.if make(stagekernel) || make(distributekernel)
462TAGS+=		kernel
463PACKAGE=	kernel
464.endif
465
466#
467# Building a world goes through the following stages
468#
469# 1. legacy stage [BMAKE]
470#	This stage is responsible for creating compatibility
471#	shims that are needed by the bootstrap-tools,
472#	build-tools and cross-tools stages. These are generally
473#	APIs that tools from one of those three stages need to
474#	build that aren't present on the host.
475# 1. bootstrap-tools stage [BMAKE]
476#	This stage is responsible for creating programs that
477#	are needed for backward compatibility reasons. They
478#	are not built as cross-tools.
479# 2. build-tools stage [TMAKE]
480#	This stage is responsible for creating the object
481#	tree and building any tools that are needed during
482#	the build process. Some programs are listed during
483#	this phase because they build binaries to generate
484#	files needed to build these programs. This stage also
485#	builds the 'build-tools' target rather than 'all'.
486# 3. cross-tools stage [XMAKE]
487#	This stage is responsible for creating any tools that
488#	are needed for building the system. A cross-compiler is one
489#	of them. This differs from build tools in two ways:
490#	1. the 'all' target is built rather than 'build-tools'
491#	2. these tools are installed into TMPPATH for stage 4.
492# 4. world stage [WMAKE]
493#	This stage actually builds the world.
494# 5. install stage (optional) [IMAKE]
495#	This stage installs a previously built world.
496#
497
498BOOTSTRAPPING?=	0
499# Keep these in sync -- see below for special case exception
500MINIMUM_SUPPORTED_OSREL?= 900044
501MINIMUM_SUPPORTED_REL?= 9.1
502
503# Common environment for world related stages
504CROSSENV+=	\
505		MACHINE_ARCH=${TARGET_ARCH} \
506		MACHINE=${TARGET} \
507		CPUTYPE=${TARGET_CPUTYPE}
508.if ${MK_META_MODE} != "no"
509# Don't rebuild build-tools targets during normal build.
510CROSSENV+=	BUILD_TOOLS_META=.NOMETA
511.endif
512.if defined(TARGET_CFLAGS)
513CROSSENV+=	${TARGET_CFLAGS}
514.endif
515
516# bootstrap-tools stage
517BMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
518		TOOLS_PREFIX=${WORLDTMP} \
519		PATH=${BPATH}:${PATH} \
520		WORLDTMP=${WORLDTMP} \
521		MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
522# need to keep this in sync with targets/pseudo/bootstrap-tools/Makefile
523BSARGS= 	DESTDIR= \
524		OBJTOP='${WORLDTMP}/obj-tools' \
525		OBJROOT='$${OBJTOP}/' \
526		MAKEOBJDIRPREFIX= \
527		BOOTSTRAPPING=${OSRELDATE} \
528		BWPHASE=${.TARGET:C,^_,,} \
529		SSP_CFLAGS= \
530		MK_HTML=no NO_LINT=yes MK_MAN=no \
531		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
532		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
533		MK_CLANG_EXTRAS=no MK_CLANG_FULL=no \
534		MK_LLDB=no MK_TESTS=no \
535		MK_INCLUDES=yes
536
537BMAKE=		\
538		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
539		${BSARGS}
540
541# build-tools stage
542TMAKE=		\
543		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
544		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
545		DESTDIR= \
546		BOOTSTRAPPING=${OSRELDATE} \
547		BWPHASE=${.TARGET:C,^_,,} \
548		SSP_CFLAGS= \
549		-DNO_LINT \
550		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
551		MK_CLANG_EXTRAS=no MK_CLANG_FULL=no \
552		MK_LLDB=no MK_TESTS=no
553
554# cross-tools stage
555# TOOLS_PREFIX set in BMAKE
556XMAKE=		${BMAKE} \
557		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
558		MK_GDB=no MK_LLD_IS_LD=${MK_LLD_BOOTSTRAP} MK_TESTS=no
559
560# kernel-tools stage
561KTMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
562		PATH=${BPATH}:${PATH} \
563		WORLDTMP=${WORLDTMP}
564KTMAKE=		TOOLS_PREFIX=${WORLDTMP} \
565		${KTMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
566		DESTDIR= \
567		OBJTOP='${WORLDTMP}/obj-kernel-tools' \
568		OBJROOT='$${OBJTOP}/' \
569		MAKEOBJDIRPREFIX= \
570		BOOTSTRAPPING=${OSRELDATE} \
571		SSP_CFLAGS= \
572		MK_HTML=no -DNO_LINT MK_MAN=no \
573		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
574		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no
575
576# world stage
577WMAKEENV=	${CROSSENV} \
578		INSTALL="sh ${.CURDIR}/tools/install.sh" \
579		PATH=${TMPPATH} \
580		SYSROOT=${WORLDTMP}
581
582# make hierarchy
583HMAKE=		PATH=${TMPPATH} ${MAKE} LOCAL_MTREE=${LOCAL_MTREE:Q}
584.if defined(NO_ROOT)
585HMAKE+=		PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT
586.endif
587
588CROSSENV+=	CC="${XCC} ${XCFLAGS}" CXX="${XCXX} ${XCXXFLAGS} ${XCFLAGS}" \
589		CPP="${XCPP} ${XCFLAGS}" \
590		AS="${XAS}" AR="${XAR}" LD="${XLD}" LLVM_LINK="${XLLVM_LINK}" \
591		NM=${XNM} OBJCOPY="${XOBJCOPY}" \
592		RANLIB=${XRANLIB} STRINGS=${XSTRINGS} \
593		SIZE="${XSIZE}"
594
595.if defined(CROSS_BINUTILS_PREFIX) && exists(${CROSS_BINUTILS_PREFIX})
596# In the case of xdev-build tools, CROSS_BINUTILS_PREFIX won't be a
597# directory, but the compiler will look in the right place for its
598# tools so we don't need to tell it where to look.
599BFLAGS+=	-B${CROSS_BINUTILS_PREFIX}
600.endif
601
602
603# The internal bootstrap compiler has a default sysroot set by TOOLS_PREFIX
604# and target set by TARGET/TARGET_ARCH.  However, there are several needs to
605# always pass an explicit --sysroot and -target.
606# - External compiler needs sysroot and target flags.
607# - External ld needs sysroot.
608# - To be clear about the use of a sysroot when using the internal compiler.
609# - Easier debugging.
610# - Allowing WITH_SYSTEM_COMPILER+WITH_META_MODE to work together due to
611#   the flip-flopping build command when sometimes using external and
612#   sometimes using internal.
613# - Allow using lld which has no support for default paths.
614.if !defined(CROSS_BINUTILS_PREFIX) || !exists(${CROSS_BINUTILS_PREFIX})
615BFLAGS+=	-B${WORLDTMP}/usr/bin
616.endif
617.if ${TARGET} == "arm"
618.if ${TARGET_ARCH:Marmv[67]*} != "" && ${TARGET_CPUTYPE:M*soft*} == ""
619TARGET_ABI=	gnueabihf
620.else
621TARGET_ABI=	gnueabi
622.endif
623.endif
624.if ${WANT_COMPILER_TYPE} == gcc || \
625    (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
626# GCC requires -isystem and -L when using a cross-compiler.  --sysroot
627# won't set header path and -L is used to ensure the base library path
628# is added before the port PREFIX library path.
629XCFLAGS+=	-isystem ${WORLDTMP}/usr/include -L${WORLDTMP}/usr/lib
630# GCC requires -B to find /usr/lib/crti.o when using a cross-compiler
631# combined with --sysroot.
632XCFLAGS+=	-B${WORLDTMP}/usr/lib
633# Force using libc++ for external GCC.
634.if ${X_COMPILER_TYPE} == gcc && ${X_COMPILER_VERSION} >= 40800
635XCXXFLAGS+=	-isystem ${WORLDTMP}/usr/include/c++/v1 -std=c++11 \
636		-nostdinc++
637.endif
638.elif ${WANT_COMPILER_TYPE} == clang || \
639    (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == clang)
640MACHINE_ABI?=	unknown
641MACHINE_TRIPLE?=${MACHINE_ARCH:C/amd64/x86_64/}-${MACHINE_ABI}-freebsd12.0
642TARGET_ABI?=	unknown
643TARGET_TRIPLE?=	${TARGET_ARCH:C/amd64/x86_64/}-${TARGET_ABI}-freebsd12.0
644XCFLAGS+=	-target ${TARGET_TRIPLE}
645.endif
646XCFLAGS+=	--sysroot=${WORLDTMP}
647
648.if !empty(BFLAGS)
649XCFLAGS+=	${BFLAGS}
650.endif
651
652.if ${MK_LIB32} != "no" && (${TARGET_ARCH} == "amd64" || \
653    ${TARGET_ARCH} == "powerpc64" || ${TARGET_ARCH:Mmips64*} != "")
654LIBCOMPAT= 32
655.include "Makefile.libcompat"
656.elif ${MK_LIBSOFT} != "no" && ${TARGET_ARCH:Marmv[67]*} != ""
657LIBCOMPAT= SOFT
658.include "Makefile.libcompat"
659.endif
660
661# META_MODE normally ignores host file changes since every build updates
662# timestamps (see NO_META_IGNORE_HOST in sys.mk).  There are known times
663# when the ABI breaks though that we want to force rebuilding WORLDTMP
664# to get updated host tools.
665.if ${MK_META_MODE} == "yes" && defined(NO_CLEAN) && \
666    !defined(NO_META_IGNORE_HOST) && !defined(NO_META_IGNORE_HOST_HEADERS) && \
667    !make(showconfig)
668# r318736 - ino64 major ABI breakage
669META_MODE_BAD_ABI_VERS+=	1200031
670
671.if !defined(OBJDIR_HOST_OSRELDATE)
672.if exists(${OBJTOP}/host-osreldate.h)
673OBJDIR_HOST_OSRELDATE!=	\
674    awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
675    ${OBJTOP}/host-osreldate.h
676.elif exists(${WORLDTMP}/usr/include/osreldate.h)
677OBJDIR_HOST_OSRELDATE=	0
678.endif
679.export OBJDIR_HOST_OSRELDATE
680.endif
681
682# Note that this logic is the opposite of normal BOOTSTRAP handling.  We want
683# to compare the WORLDTMP's OSRELDATE to the host's OSRELDATE.  If the WORLDTMP
684# is older than the ABI-breakage OSRELDATE of the HOST then we rebuild.
685.if defined(OBJDIR_HOST_OSRELDATE)
686.for _ver in ${META_MODE_BAD_ABI_VERS}
687.if ${OSRELDATE} >= ${_ver} && ${OBJDIR_HOST_OSRELDATE} < ${_ver}
688_meta_mode_need_rebuild=	${_ver}
689.endif
690.endfor
691.if defined(_meta_mode_need_rebuild)
692.info META_MODE: Rebuilding host tools due to ABI breakage in __FreeBSD_version ${_meta_mode_need_rebuild}.
693NO_META_IGNORE_HOST_HEADERS=	1
694.export NO_META_IGNORE_HOST_HEADERS
695.endif	# defined(_meta_mode_need_rebuild)
696.endif	# defined(OBJDIR_HOST_OSRELDATE)
697.endif	# ${MK_META_MODE} == "yes" && defined(NO_CLEAN) ...
698# This is only used for META_MODE+filemon to track what the oldest
699# __FreeBSD_version is in WORLDTMP.  This purposely does NOT have
700# a make dependency on /usr/include/osreldate.h as the file should
701# only be copied when it is missing or meta mode determines it has changed.
702# Since host files are normally ignored without NO_META_IGNORE_HOST
703# the file will never be updated unless that flag is specified.  This
704# allows tracking the oldest osreldate to force rebuilds via
705# META_MODE_BADABI_REVS above.
706host-osreldate.h: # DO NOT ADD /usr/include/osreldate.h here
707	@cp -f /usr/include/osreldate.h ${.TARGET}
708
709WMAKE=		${WMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
710		BWPHASE=${.TARGET:C,^_,,} \
711		DESTDIR=${WORLDTMP}
712
713IMAKEENV=	${CROSSENV}
714IMAKE=		${IMAKEENV} ${MAKE} -f Makefile.inc1 \
715		${IMAKE_INSTALL} ${IMAKE_MTREE}
716.if empty(.MAKEFLAGS:M-n)
717IMAKEENV+=	PATH=${STRICTTMPPATH}:${INSTALLTMP} \
718		LD_LIBRARY_PATH=${INSTALLTMP} \
719		PATH_LOCALE=${INSTALLTMP}/locale
720IMAKE+=		__MAKE_SHELL=${INSTALLTMP}/sh
721.else
722IMAKEENV+=	PATH=${TMPPATH}:${INSTALLTMP}
723.endif
724.if defined(DB_FROM_SRC)
725INSTALLFLAGS+=	-N ${.CURDIR}/etc
726MTREEFLAGS+=	-N ${.CURDIR}/etc
727.endif
728_INSTALL_DDIR=	${DESTDIR}/${DISTDIR}
729INSTALL_DDIR=	${_INSTALL_DDIR:S://:/:g:C:/$::}
730.if defined(NO_ROOT)
731METALOG?=	${DESTDIR}/${DISTDIR}/METALOG
732METALOG:=	${METALOG:C,//+,/,g}
733IMAKE+=		-DNO_ROOT METALOG=${METALOG}
734INSTALLFLAGS+=	-U -M ${METALOG} -D ${INSTALL_DDIR}
735MTREEFLAGS+=	-W
736.endif
737.if defined(BUILD_PKGS)
738INSTALLFLAGS+=	-h sha256
739.endif
740.if defined(DB_FROM_SRC) || defined(NO_ROOT)
741IMAKE_INSTALL=	INSTALL="install ${INSTALLFLAGS}"
742IMAKE_MTREE=	MTREE_CMD="mtree ${MTREEFLAGS}"
743.endif
744
745# kernel stage
746KMAKEENV=	${WMAKEENV:NSYSROOT=*}
747KMAKE=		${KMAKEENV} ${MAKE} ${.MAKEFLAGS} ${KERNEL_FLAGS} KERNEL=${INSTKERNNAME}
748
749#
750# buildworld
751#
752# Attempt to rebuild the entire system, with reasonable chance of
753# success, regardless of how old your existing system is.
754#
755_sanity_check: .PHONY .MAKE
756.if ${.CURDIR:C/[^,]//g} != ""
757#	The m4 build of sendmail files doesn't like it if ',' is used
758#	anywhere in the path of it's files.
759	@echo
760	@echo "*** Error: path to source tree contains a comma ','"
761	@echo
762	@false
763.elif ${.CURDIR:M*\:*} != ""
764#	Using ':' leaks into PATH and breaks finding cross-tools.
765	@echo
766	@echo "*** Error: path to source tree contains a colon ':'"
767	@echo
768	@false
769.endif
770
771# Our current approach to dependency tracking cannot cope with certain source
772# tree changes, particularly with respect to removing source files and
773# replacing generated files.  Handle these cases here in an ad-hoc fashion.
774_cleanobj_fast_depend_hack: .PHONY
775# Syscall stubs rewritten in C
776# Date      SVN Rev  Syscalls
777# 20160829  r305012  ptrace
778# 20170624  r320278  fstat fstatat fstatfs getdirentries getfsstat statfs
779.for f in fstat fstatat fstatfs getdirentries getfsstat ptrace statfs
780.if exists(${OBJTOP}/lib/libc/.depend.${f}.o)
781	@if egrep -qw '${f}\.[sS]' \
782	    ${OBJTOP}/lib/libc/.depend.${f}.o; then \
783		echo Removing stale dependencies for ${f} syscall wrappers; \
784		rm -f ${OBJTOP}/lib/libc/.depend.${f}.* \
785		   ${LIBCOMPAT:D${LIBCOMPAT_OBJTOP}/lib/libc/.depend.${f}.*}; \
786	fi
787.endif
788.endfor
789# 20170607 remove stale dependencies for utimens* wrappers removed in r319663
790.for f in futimens utimensat
791.if exists(${OBJTOP}/lib/libc/.depend.${f}.o)
792	@if egrep -q '/${f}.c' \
793	    ${OBJTOP}/lib/libc/.depend.${f}.o; then \
794		echo Removing stale dependencies for ${f} syscall wrappers; \
795		rm -f ${OBJTOP}/lib/libc/.depend.${f}.* \
796		   ${LIBCOMPAT:D${LIBCOMPAT_OBJTOP}/lib/libc/.depend.${f}.*}; \
797	fi
798.endif
799.endfor
800# 20170523 remove stale generated asm files for functions which are no longer
801# syscalls after r302092 (pipe) and r318736 (others)
802.for f in getdents lstat mknod pipe stat
803.if exists(${OBJTOP}/lib/libc/${f}.s) || \
804    exists(${OBJTOP}/lib/libc/${f}.S)
805	@echo Removing stale generated ${f} syscall files
806	@rm -f ${OBJTOP}/lib/libc/${f}.* \
807	    ${OBJTOP}/lib/libc/.depend.${f}.* \
808	    ${LIBCOMPAT:D${LIBCOMPAT_OBJTOP}/lib/libc/${f}.*} \
809	    ${LIBCOMPAT:D${LIBCOMPAT_OBJTOP}/lib/libc/.depend.${f}.*}
810.endif
811.endfor
812
813_worldtmp: .PHONY
814	@echo
815	@echo "--------------------------------------------------------------"
816	@echo ">>> Rebuilding the temporary build tree"
817	@echo "--------------------------------------------------------------"
818.if !defined(NO_CLEAN)
819	rm -rf ${WORLDTMP}
820.else
821.if exists(${WORLDTMP})
822	@echo ">>> Deleting stale files in build tree..."
823	${_+_}cd ${.CURDIR}; ${WMAKE} -DBATCH_DELETE_OLD_FILES \
824	    delete-old delete-old-libs >/dev/null
825.endif
826	rm -rf ${WORLDTMP}/legacy/usr/include
827.if ${USING_SYSTEM_COMPILER} == "yes"
828.for cc in cc c++
829	if [ -x ${WORLDTMP}/usr/bin/${cc} ]; then \
830		inum=$$(stat -f %i ${WORLDTMP}/usr/bin/${cc}); \
831		find ${WORLDTMP}/usr/bin -inum $${inum} -delete; \
832	fi
833.endfor
834.endif	# ${USING_SYSTEM_COMPILER} == "yes"
835.endif	# !defined(NO_CLEAN)
836	@mkdir -p ${WORLDTMP}
837	@touch ${WORLDTMP}/${.TARGET}
838
839.for _dir in \
840    lib lib/casper usr legacy/bin legacy/usr
841	mkdir -p ${WORLDTMP}/${_dir}
842.endfor
843	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
844	    -p ${WORLDTMP}/legacy/usr >/dev/null
845	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
846	    -p ${WORLDTMP}/legacy/usr/include >/dev/null
847	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
848	    -p ${WORLDTMP}/usr >/dev/null
849	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
850	    -p ${WORLDTMP}/usr/include >/dev/null
851	ln -sf ${.CURDIR}/sys ${WORLDTMP}
852.if ${MK_DEBUG_FILES} != "no"
853	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
854	    -p ${WORLDTMP}/legacy/usr/lib >/dev/null
855	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
856	    -p ${WORLDTMP}/usr/lib >/dev/null
857.endif
858.for _mtree in ${LOCAL_MTREE}
859	mtree -deU -f ${.CURDIR}/${_mtree} -p ${WORLDTMP} > /dev/null
860.endfor
861_legacy:
862	@echo
863	@echo "--------------------------------------------------------------"
864	@echo ">>> stage 1.1: legacy release compatibility shims"
865	@echo "--------------------------------------------------------------"
866	${_+_}cd ${.CURDIR}; ${BMAKE} legacy
867_bootstrap-tools:
868	@echo
869	@echo "--------------------------------------------------------------"
870	@echo ">>> stage 1.2: bootstrap tools"
871	@echo "--------------------------------------------------------------"
872	${_+_}cd ${.CURDIR}; ${BMAKE} bootstrap-tools
873_cleanobj:
874.if !defined(NO_CLEAN)
875	@echo
876	@echo "--------------------------------------------------------------"
877	@echo ">>> stage 2.1: cleaning up the object tree"
878	@echo "--------------------------------------------------------------"
879	${_+_}cd ${.CURDIR}; ${WMAKE} ${CLEANDIR}
880.if defined(LIBCOMPAT)
881	${_+_}cd ${.CURDIR}; ${LIBCOMPATWMAKE} -f Makefile.inc1 ${CLEANDIR}
882.endif
883.else
884	${_+_}cd ${.CURDIR}; ${WMAKE} _cleanobj_fast_depend_hack
885.endif	# !defined(NO_CLEAN)
886_obj:
887	@echo
888	@echo "--------------------------------------------------------------"
889	@echo ">>> stage 2.2: rebuilding the object tree"
890	@echo "--------------------------------------------------------------"
891	${_+_}cd ${.CURDIR}; ${WMAKE} obj
892_build-tools:
893	@echo
894	@echo "--------------------------------------------------------------"
895	@echo ">>> stage 2.3: build tools"
896	@echo "--------------------------------------------------------------"
897	${_+_}cd ${.CURDIR}; ${TMAKE} build-tools
898_cross-tools:
899	@echo
900	@echo "--------------------------------------------------------------"
901	@echo ">>> stage 3: cross tools"
902	@echo "--------------------------------------------------------------"
903	@rm -f ${OBJTOP}/compiler-metadata.mk
904	${_+_}cd ${.CURDIR}; ${XMAKE} cross-tools
905	${_+_}cd ${.CURDIR}; ${XMAKE} kernel-tools
906_build-metadata:
907	@echo
908	@echo "--------------------------------------------------------------"
909	@echo ">>> stage 3.1: recording build metadata"
910	@echo "--------------------------------------------------------------"
911	${_+_}cd ${.CURDIR}; ${WMAKE} compiler-metadata.mk
912	${_+_}cd ${.CURDIR}; ${WMAKE} host-osreldate.h
913_includes:
914	@echo
915	@echo "--------------------------------------------------------------"
916	@echo ">>> stage 4.1: building includes"
917	@echo "--------------------------------------------------------------"
918# Special handling for SUBDIR_OVERRIDE in buildworld as they most likely need
919# headers from default SUBDIR.  Do SUBDIR_OVERRIDE includes last.
920	${_+_}cd ${.CURDIR}; ${WMAKE} SUBDIR_OVERRIDE= SHARED=symlinks \
921	    MK_INCLUDES=yes includes
922.if !empty(SUBDIR_OVERRIDE) && make(buildworld)
923	${_+_}cd ${.CURDIR}; ${WMAKE} MK_INCLUDES=yes SHARED=symlinks includes
924.endif
925_libraries:
926	@echo
927	@echo "--------------------------------------------------------------"
928	@echo ">>> stage 4.2: building libraries"
929	@echo "--------------------------------------------------------------"
930	${_+_}cd ${.CURDIR}; \
931	    ${WMAKE} -DNO_FSCHG MK_HTML=no -DNO_LINT MK_MAN=no \
932	    MK_PROFILE=no MK_TESTS=no MK_TESTS_SUPPORT=${MK_TESTS} libraries
933everything: .PHONY
934	@echo
935	@echo "--------------------------------------------------------------"
936	@echo ">>> stage 4.3: building everything"
937	@echo "--------------------------------------------------------------"
938	${_+_}cd ${.CURDIR}; _PARALLEL_SUBDIR_OK=1 ${WMAKE} all
939
940WMAKE_TGTS=
941.if !defined(WORLDFAST)
942WMAKE_TGTS+=	_sanity_check _worldtmp _legacy
943.if empty(SUBDIR_OVERRIDE)
944WMAKE_TGTS+=	_bootstrap-tools
945.endif
946WMAKE_TGTS+=	_cleanobj
947.if !defined(NO_OBJWALK)
948WMAKE_TGTS+=	_obj
949.endif
950WMAKE_TGTS+=	_build-tools _cross-tools
951WMAKE_TGTS+=	_build-metadata
952WMAKE_TGTS+=	_includes
953.endif
954.if !defined(NO_LIBS)
955WMAKE_TGTS+=	_libraries
956.endif
957WMAKE_TGTS+=	everything
958.if defined(LIBCOMPAT) && empty(SUBDIR_OVERRIDE)
959WMAKE_TGTS+=	build${libcompat}
960.endif
961
962buildworld: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue .PHONY
963.ORDER: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
964
965buildworld_prologue: .PHONY
966	@echo "--------------------------------------------------------------"
967	@echo ">>> World build started on `LC_ALL=C date`"
968	@echo "--------------------------------------------------------------"
969
970buildworld_epilogue: .PHONY
971	@echo
972	@echo "--------------------------------------------------------------"
973	@echo ">>> World build completed on `LC_ALL=C date`"
974	@echo "--------------------------------------------------------------"
975
976#
977# We need to have this as a target because the indirection between Makefile
978# and Makefile.inc1 causes the correct PATH to be used, rather than a
979# modification of the current environment's PATH.  In addition, we need
980# to quote multiword values.
981#
982buildenvvars: .PHONY
983	@echo ${WMAKEENV:Q} ${.MAKE.EXPORTED:@v@$v=\"${$v}\"@}
984
985.if ${.TARGETS:Mbuildenv}
986.if ${.MAKEFLAGS:M-j}
987.error The buildenv target is incompatible with -j
988.endif
989.endif
990BUILDENV_DIR?=	${.CURDIR}
991#
992# Note: make will report any errors the shell reports. This can
993# be odd if the last command in an interactive shell generates an
994# error or is terminated by SIGINT. These reported errors look bad,
995# but are harmless. Allowing them also allows BUIDLENV_SHELL to
996# be a complex command whose status will be returned to the caller.
997# Some scripts in tools rely on this behavior to report build errors.
998#
999buildenv: .PHONY
1000	@echo Entering world for ${TARGET_ARCH}:${TARGET}
1001.if ${BUILDENV_SHELL:M*zsh*}
1002	@echo For ZSH you must run: export CPUTYPE=${TARGET_CPUTYPE}
1003.endif
1004	@cd ${BUILDENV_DIR} && env ${WMAKEENV} BUILDENV=1 ${BUILDENV_SHELL}
1005
1006TOOLCHAIN_TGTS=	${WMAKE_TGTS:Neverything:Nbuild${libcompat}}
1007toolchain: ${TOOLCHAIN_TGTS} .PHONY
1008KERNEL_TOOLCHAIN_TGTS=	${TOOLCHAIN_TGTS:N_obj:N_cleanobj:N_includes:N_libraries}
1009.if make(kernel-toolchain)
1010.ORDER: ${KERNEL_TOOLCHAIN_TGTS}
1011.endif
1012kernel-toolchain: ${KERNEL_TOOLCHAIN_TGTS} .PHONY
1013
1014#
1015# installcheck
1016#
1017# Checks to be sure system is ready for installworld/installkernel.
1018#
1019installcheck: _installcheck_world _installcheck_kernel .PHONY
1020_installcheck_world: .PHONY
1021_installcheck_kernel: .PHONY
1022
1023#
1024# Require DESTDIR to be set if installing for a different architecture or
1025# using the user/group database in the source tree.
1026#
1027.if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${TARGET} != ${MACHINE} || \
1028    defined(DB_FROM_SRC)
1029.if !make(distributeworld)
1030_installcheck_world: __installcheck_DESTDIR
1031_installcheck_kernel: __installcheck_DESTDIR
1032__installcheck_DESTDIR: .PHONY
1033.if !defined(DESTDIR) || empty(DESTDIR)
1034	@echo "ERROR: Please set DESTDIR!"; \
1035	false
1036.endif
1037.endif
1038.endif
1039
1040.if !defined(DB_FROM_SRC)
1041#
1042# Check for missing UIDs/GIDs.
1043#
1044CHECK_UIDS=	auditdistd
1045CHECK_GIDS=	audit
1046.if ${MK_SENDMAIL} != "no"
1047CHECK_UIDS+=	smmsp
1048CHECK_GIDS+=	smmsp
1049.endif
1050.if ${MK_PF} != "no"
1051CHECK_UIDS+=	proxy
1052CHECK_GIDS+=	proxy authpf
1053.endif
1054.if ${MK_UNBOUND} != "no"
1055CHECK_UIDS+=	unbound
1056CHECK_GIDS+=	unbound
1057.endif
1058_installcheck_world: __installcheck_UGID
1059__installcheck_UGID: .PHONY
1060.for uid in ${CHECK_UIDS}
1061	@if ! `id -u ${uid} >/dev/null 2>&1`; then \
1062		echo "ERROR: Required ${uid} user is missing, see /usr/src/UPDATING."; \
1063		false; \
1064	fi
1065.endfor
1066.for gid in ${CHECK_GIDS}
1067	@if ! `find / -prune -group ${gid} >/dev/null 2>&1`; then \
1068		echo "ERROR: Required ${gid} group is missing, see /usr/src/UPDATING."; \
1069		false; \
1070	fi
1071.endfor
1072.endif
1073#
1074# If installing over the running system (DESTDIR is / or unset) and the install
1075# includes rescue, try running rescue from the objdir as a sanity check.  If
1076# rescue is not functional (e.g., because it depends on a system call not
1077# supported by the currently running kernel), abort the installation.
1078#
1079.if !make(distributeworld) && ${MK_RESCUE} != "no" && \
1080    (empty(DESTDIR) || ${DESTDIR} == "/") && empty(BYPASS_INSTALLCHECK_SH)
1081_installcheck_world: __installcheck_sh_check
1082__installcheck_sh_check: .PHONY
1083	@if [ "`${OBJTOP}/rescue/rescue/rescue sh -c 'echo OK'`" != \
1084	    OK ]; then \
1085		echo "rescue/sh check failed, installation aborted" >&2; \
1086		false; \
1087	fi
1088.endif
1089
1090#
1091# Required install tools to be saved in a scratch dir for safety.
1092#
1093.if ${MK_ZONEINFO} != "no"
1094_zoneinfo=	zic tzsetup
1095.endif
1096
1097ITOOLS=	[ awk cap_mkdb cat chflags chmod chown cmp cp \
1098	date echo egrep find grep id install ${_install-info} \
1099	ln make mkdir mtree mv pwd_mkdb \
1100	rm sed services_mkdb sh strip sysctl test true uname wc ${_zoneinfo} \
1101	${LOCAL_ITOOLS}
1102
1103# Needed for share/man
1104.if ${MK_MAN_UTILS} != "no"
1105ITOOLS+=makewhatis
1106.endif
1107
1108#
1109# distributeworld
1110#
1111# Distributes everything compiled by a `buildworld'.
1112#
1113# installworld
1114#
1115# Installs everything compiled by a 'buildworld'.
1116#
1117
1118# Non-base distributions produced by the base system
1119EXTRA_DISTRIBUTIONS=	doc
1120.if defined(LIBCOMPAT)
1121EXTRA_DISTRIBUTIONS+=	lib${libcompat}
1122.endif
1123.if ${MK_TESTS} != "no"
1124EXTRA_DISTRIBUTIONS+=	tests
1125.endif
1126
1127DEBUG_DISTRIBUTIONS=
1128.if ${MK_DEBUG_FILES} != "no"
1129DEBUG_DISTRIBUTIONS+=	base ${EXTRA_DISTRIBUTIONS:S,doc,,:S,tests,,}
1130.endif
1131
1132MTREE_MAGIC?=	mtree 2.0
1133
1134distributeworld installworld stageworld: _installcheck_world .PHONY
1135	mkdir -p ${INSTALLTMP}
1136	progs=$$(for prog in ${ITOOLS}; do \
1137		if progpath=`which $$prog`; then \
1138			echo $$progpath; \
1139		else \
1140			echo "Required tool $$prog not found in PATH." >&2; \
1141			exit 1; \
1142		fi; \
1143	    done); \
1144	libs=$$(ldd -f "%o %p\n" -f "%o %p\n" $$progs 2>/dev/null | sort -u | \
1145	    while read line; do \
1146		set -- $$line; \
1147		if [ "$$2 $$3" != "not found" ]; then \
1148			echo $$2; \
1149		else \
1150			echo "Required library $$1 not found." >&2; \
1151			exit 1; \
1152		fi; \
1153	    done); \
1154	cp $$libs $$progs ${INSTALLTMP}
1155	cp -R $${PATH_LOCALE:-"/usr/share/locale"} ${INSTALLTMP}/locale
1156.if defined(NO_ROOT)
1157	-mkdir -p ${METALOG:H}
1158	echo "#${MTREE_MAGIC}" > ${METALOG}
1159.endif
1160.if make(distributeworld)
1161.for dist in ${EXTRA_DISTRIBUTIONS}
1162	-mkdir ${DESTDIR}/${DISTDIR}/${dist}
1163	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
1164	    -p ${DESTDIR}/${DISTDIR}/${dist} >/dev/null
1165	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1166	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1167	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1168	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/include >/dev/null
1169.if ${MK_DEBUG_FILES} != "no"
1170	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1171	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib >/dev/null
1172.endif
1173.if defined(LIBCOMPAT)
1174	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1175	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1176.if ${MK_DEBUG_FILES} != "no"
1177	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1178	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/usr >/dev/null
1179.endif
1180.endif
1181.if ${MK_TESTS} != "no" && ${dist} == "tests"
1182	-mkdir -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE}
1183	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1184	    -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE} >/dev/null
1185.if ${MK_DEBUG_FILES} != "no"
1186	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1187	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/${TESTSBASE} >/dev/null
1188.endif
1189.endif
1190.if defined(NO_ROOT)
1191	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.root.dist | \
1192	    sed -e 's#^\./#./${dist}/#' >> ${METALOG}
1193	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.usr.dist | \
1194	    sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1195	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.include.dist | \
1196	    sed -e 's#^\./#./${dist}/usr/include/#' >> ${METALOG}
1197.if defined(LIBCOMPAT)
1198	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist | \
1199	    sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1200.endif
1201.endif
1202.endfor
1203	-mkdir ${DESTDIR}/${DISTDIR}/base
1204	${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1205	    METALOG=${METALOG} ${IMAKE_INSTALL} ${IMAKE_MTREE} \
1206	    DISTBASE=/base DESTDIR=${DESTDIR}/${DISTDIR}/base \
1207	    LOCAL_MTREE=${LOCAL_MTREE:Q} distrib-dirs
1208.endif
1209	${_+_}cd ${.CURDIR}; ${IMAKE} re${.TARGET:S/world$//}; \
1210	    ${IMAKEENV} rm -rf ${INSTALLTMP}
1211.if make(distributeworld)
1212.for dist in ${EXTRA_DISTRIBUTIONS}
1213	find ${DESTDIR}/${DISTDIR}/${dist} -mindepth 1 -type d -empty -delete
1214.endfor
1215.if defined(NO_ROOT)
1216.for dist in base ${EXTRA_DISTRIBUTIONS}
1217	@# For each file that exists in this dist, print the corresponding
1218	@# line from the METALOG.  This relies on the fact that
1219	@# a line containing only the filename will sort immediately before
1220	@# the relevant mtree line.
1221	cd ${DESTDIR}/${DISTDIR}; \
1222	find ./${dist} | sort -u ${METALOG} - | \
1223	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1224	${DESTDIR}/${DISTDIR}/${dist}.meta
1225.endfor
1226.for dist in ${DEBUG_DISTRIBUTIONS}
1227	@# For each file that exists in this dist, print the corresponding
1228	@# line from the METALOG.  This relies on the fact that
1229	@# a line containing only the filename will sort immediately before
1230	@# the relevant mtree line.
1231	cd ${DESTDIR}/${DISTDIR}; \
1232	find ./${dist}/usr/lib/debug | sort -u ${METALOG} - | \
1233	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1234	${DESTDIR}/${DISTDIR}/${dist}.debug.meta
1235.endfor
1236.endif
1237.endif
1238
1239packageworld: .PHONY
1240.for dist in base ${EXTRA_DISTRIBUTIONS}
1241.if defined(NO_ROOT)
1242	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1243	    tar cvf - --exclude usr/lib/debug \
1244	    @${DESTDIR}/${DISTDIR}/${dist}.meta | \
1245	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1246.else
1247	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1248	    tar cvf - --exclude usr/lib/debug . | \
1249	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1250.endif
1251.endfor
1252
1253.for dist in ${DEBUG_DISTRIBUTIONS}
1254. if defined(NO_ROOT)
1255	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1256	    tar cvf - @${DESTDIR}/${DISTDIR}/${dist}.debug.meta | \
1257	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1258. else
1259	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1260	    tar cvLf - usr/lib/debug | \
1261	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1262. endif
1263.endfor
1264
1265#
1266# reinstall
1267#
1268# If you have a build server, you can NFS mount the source and obj directories
1269# and do a 'make reinstall' on the *client* to install new binaries from the
1270# most recent server build.
1271#
1272restage reinstall: .MAKE .PHONY
1273	@echo "--------------------------------------------------------------"
1274	@echo ">>> Making hierarchy"
1275	@echo "--------------------------------------------------------------"
1276	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1277	    LOCAL_MTREE=${LOCAL_MTREE:Q} hierarchy
1278.if make(restage)
1279	@echo "--------------------------------------------------------------"
1280	@echo ">>> Making distribution"
1281	@echo "--------------------------------------------------------------"
1282	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1283	    LOCAL_MTREE=${LOCAL_MTREE:Q} distribution
1284.endif
1285	@echo
1286	@echo "--------------------------------------------------------------"
1287	@echo ">>> Installing everything"
1288	@echo "--------------------------------------------------------------"
1289	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install
1290.if defined(LIBCOMPAT)
1291	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install${libcompat}
1292.endif
1293
1294redistribute: .MAKE .PHONY
1295	@echo "--------------------------------------------------------------"
1296	@echo ">>> Distributing everything"
1297	@echo "--------------------------------------------------------------"
1298	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute
1299.if defined(LIBCOMPAT)
1300	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute${libcompat} \
1301	    DISTRIBUTION=lib${libcompat}
1302.endif
1303
1304distrib-dirs distribution: .MAKE .PHONY
1305	${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1306	    ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
1307.if make(distribution)
1308	${_+_}cd ${.CURDIR}; ${CROSSENV} PATH=${TMPPATH} \
1309		${MAKE} -f Makefile.inc1 ${IMAKE_INSTALL} \
1310		METALOG=${METALOG} MK_TESTS=no installconfig
1311.endif
1312
1313#
1314# buildkernel and installkernel
1315#
1316# Which kernels to build and/or install is specified by setting
1317# KERNCONF. If not defined a GENERIC kernel is built/installed.
1318# Only the existing (depending TARGET) config files are used
1319# for building kernels and only the first of these is designated
1320# as the one being installed.
1321#
1322# Note that we have to use TARGET instead of TARGET_ARCH when
1323# we're in kernel-land. Since only TARGET_ARCH is (expected) to
1324# be set to cross-build, we have to make sure TARGET is set
1325# properly.
1326
1327.if defined(KERNFAST)
1328NO_KERNELCLEAN=	t
1329NO_KERNELCONFIG=	t
1330NO_KERNELOBJ=		t
1331# Shortcut for KERNCONF=Blah -DKERNFAST is now KERNFAST=Blah
1332.if !defined(KERNCONF) && ${KERNFAST} != "1"
1333KERNCONF=${KERNFAST}
1334.endif
1335.endif
1336.if ${TARGET_ARCH} == "powerpc64"
1337KERNCONF?=	GENERIC64
1338.else
1339KERNCONF?=	GENERIC
1340.endif
1341INSTKERNNAME?=	kernel
1342
1343KERNSRCDIR?=	${.CURDIR}/sys
1344KRNLCONFDIR=	${KERNSRCDIR}/${TARGET}/conf
1345KRNLOBJDIR=	${OBJTOP}${KERNSRCDIR:C,^${.CURDIR},,}
1346KERNCONFDIR?=	${KRNLCONFDIR}
1347
1348BUILDKERNELS=
1349INSTALLKERNEL=
1350.if defined(NO_INSTALLKERNEL)
1351# All of the BUILDKERNELS loops start at index 1.
1352BUILDKERNELS+= dummy
1353.endif
1354.for _kernel in ${KERNCONF}
1355.if exists(${KERNCONFDIR}/${_kernel})
1356BUILDKERNELS+=	${_kernel}
1357.if empty(INSTALLKERNEL) && !defined(NO_INSTALLKERNEL)
1358INSTALLKERNEL= ${_kernel}
1359.endif
1360.else
1361.if make(buildkernel)
1362.error Missing KERNCONF ${KERNCONFDIR}/${_kernel}
1363.endif
1364.endif
1365.endfor
1366
1367${WMAKE_TGTS:N_worldtmp:Nbuild${libcompat}} ${.ALLTARGETS:M_*:N_worldtmp}: .MAKE .PHONY
1368
1369#
1370# buildkernel
1371#
1372# Builds all kernels defined by BUILDKERNELS.
1373#
1374buildkernel: .MAKE .PHONY
1375.if empty(BUILDKERNELS:Ndummy)
1376	@echo "ERROR: Missing kernel configuration file(s) (${KERNCONF})."; \
1377	false
1378.endif
1379	@echo
1380.for _kernel in ${BUILDKERNELS:Ndummy}
1381	@echo "--------------------------------------------------------------"
1382	@echo ">>> Kernel build for ${_kernel} started on `LC_ALL=C date`"
1383	@echo "--------------------------------------------------------------"
1384	@echo "===> ${_kernel}"
1385	mkdir -p ${KRNLOBJDIR}
1386.if !defined(NO_KERNELCONFIG)
1387	@echo
1388	@echo "--------------------------------------------------------------"
1389	@echo ">>> stage 1: configuring the kernel"
1390	@echo "--------------------------------------------------------------"
1391	cd ${KRNLCONFDIR}; \
1392		PATH=${TMPPATH} \
1393		    config ${CONFIGARGS} -d ${KRNLOBJDIR}/${_kernel} \
1394			-I '${KERNCONFDIR}' '${KERNCONFDIR}/${_kernel}'
1395.endif
1396.if !defined(NO_CLEAN) && !defined(NO_KERNELCLEAN)
1397	@echo
1398	@echo "--------------------------------------------------------------"
1399	@echo ">>> stage 2.1: cleaning up the object tree"
1400	@echo "--------------------------------------------------------------"
1401	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} ${CLEANDIR}
1402.endif
1403.if !defined(NO_KERNELOBJ)
1404	@echo
1405	@echo "--------------------------------------------------------------"
1406	@echo ">>> stage 2.2: rebuilding the object tree"
1407	@echo "--------------------------------------------------------------"
1408	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} obj
1409.endif
1410	@echo
1411	@echo "--------------------------------------------------------------"
1412	@echo ">>> stage 2.3: build tools"
1413	@echo "--------------------------------------------------------------"
1414	${_+_}cd ${.CURDIR}; ${KTMAKE} kernel-tools
1415	@echo
1416	@echo "--------------------------------------------------------------"
1417	@echo ">>> stage 3.1: building everything"
1418	@echo "--------------------------------------------------------------"
1419	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} all -DNO_MODULES_OBJ
1420	@echo "--------------------------------------------------------------"
1421	@echo ">>> Kernel build for ${_kernel} completed on `LC_ALL=C date`"
1422	@echo "--------------------------------------------------------------"
1423.endfor
1424
1425NO_INSTALLEXTRAKERNELS?=	yes
1426
1427#
1428# installkernel, etc.
1429#
1430# Install the kernel defined by INSTALLKERNEL
1431#
1432installkernel installkernel.debug \
1433reinstallkernel reinstallkernel.debug: _installcheck_kernel .PHONY
1434.if !defined(NO_INSTALLKERNEL)
1435.if empty(INSTALLKERNEL)
1436	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1437	false
1438.endif
1439	@echo "--------------------------------------------------------------"
1440	@echo ">>> Installing kernel ${INSTALLKERNEL}"
1441	@echo "--------------------------------------------------------------"
1442	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1443	    ${CROSSENV} PATH=${TMPPATH} \
1444	    ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME} ${.TARGET:S/kernel//}
1445.endif
1446.if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1447.for _kernel in ${BUILDKERNELS:[2..-1]}
1448	@echo "--------------------------------------------------------------"
1449	@echo ">>> Installing kernel ${_kernel}"
1450	@echo "--------------------------------------------------------------"
1451	cd ${KRNLOBJDIR}/${_kernel}; \
1452	    ${CROSSENV} PATH=${TMPPATH} \
1453	    ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME}.${_kernel} ${.TARGET:S/kernel//}
1454.endfor
1455.endif
1456
1457distributekernel distributekernel.debug: .PHONY
1458.if !defined(NO_INSTALLKERNEL)
1459.if empty(INSTALLKERNEL)
1460	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1461	false
1462.endif
1463	mkdir -p ${DESTDIR}/${DISTDIR}
1464.if defined(NO_ROOT)
1465	@echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.premeta
1466.endif
1467	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1468	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.premeta/} \
1469	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} KERNEL=${INSTKERNNAME} \
1470	    DESTDIR=${INSTALL_DDIR}/kernel \
1471	    ${.TARGET:S/distributekernel/install/}
1472.if defined(NO_ROOT)
1473	@sed -e 's|^./kernel|.|' ${DESTDIR}/${DISTDIR}/kernel.premeta > \
1474	    ${DESTDIR}/${DISTDIR}/kernel.meta
1475.endif
1476.endif
1477.if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1478.for _kernel in ${BUILDKERNELS:[2..-1]}
1479.if defined(NO_ROOT)
1480	@echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta
1481.endif
1482	cd ${KRNLOBJDIR}/${_kernel}; \
1483	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.${_kernel}.premeta/} \
1484	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} \
1485	    KERNEL=${INSTKERNNAME}.${_kernel} \
1486	    DESTDIR=${INSTALL_DDIR}/kernel.${_kernel} \
1487	    ${.TARGET:S/distributekernel/install/}
1488.if defined(NO_ROOT)
1489	@sed -e "s|^./kernel.${_kernel}|.|" \
1490	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta > \
1491	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1492.endif
1493.endfor
1494.endif
1495
1496packagekernel: .PHONY
1497.if defined(NO_ROOT)
1498.if !defined(NO_INSTALLKERNEL)
1499	cd ${DESTDIR}/${DISTDIR}/kernel; \
1500	    tar cvf - --exclude '*.debug' \
1501	    @${DESTDIR}/${DISTDIR}/kernel.meta | \
1502	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1503.endif
1504.if ${MK_DEBUG_FILES} != "no"
1505	cd ${DESTDIR}/${DISTDIR}/kernel; \
1506	    tar cvf - --include '*/*/*.debug' \
1507	    @${DESTDIR}/${DISTDIR}/kernel.meta | \
1508	    ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1509.endif
1510.if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1511.for _kernel in ${BUILDKERNELS:[2..-1]}
1512	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1513	    tar cvf - --exclude '*.debug' \
1514	    @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1515	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1516.if ${MK_DEBUG_FILES} != "no"
1517	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1518	    tar cvf - --include '*/*/*.debug' \
1519	    @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1520	    ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1521.endif
1522.endfor
1523.endif
1524.else
1525.if !defined(NO_INSTALLKERNEL)
1526	cd ${DESTDIR}/${DISTDIR}/kernel; \
1527	    tar cvf - --exclude '*.debug' . | \
1528	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1529.endif
1530.if ${MK_DEBUG_FILES} != "no"
1531	cd ${DESTDIR}/${DISTDIR}/kernel; \
1532	    tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1533	    ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1534.endif
1535.if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1536.for _kernel in ${BUILDKERNELS:[2..-1]}
1537	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1538	    tar cvf - --exclude '*.debug' . | \
1539	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1540.if ${MK_DEBUG_FILES} != "no"
1541	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1542	    tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1543	    ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1544.endif
1545.endfor
1546.endif
1547.endif
1548
1549stagekernel: .PHONY
1550	${_+_}${MAKE} -C ${.CURDIR} ${.MAKEFLAGS} distributekernel
1551
1552PORTSDIR?=	/usr/ports
1553WSTAGEDIR?=	${OBJTOP}/worldstage
1554KSTAGEDIR?=	${OBJTOP}/kernelstage
1555REPODIR?=	${OBJROOT}repo
1556PKGSIGNKEY?=	# empty
1557
1558.ORDER:		stage-packages create-packages
1559.ORDER:		create-packages create-world-packages
1560.ORDER:		create-packages create-kernel-packages
1561.ORDER:		create-packages sign-packages
1562
1563_pkgbootstrap: .PHONY
1564.if !exists(${LOCALBASE}/sbin/pkg)
1565	@env ASSUME_ALWAYS_YES=YES pkg bootstrap
1566.endif
1567
1568packages: .PHONY
1569	${_+_}${MAKE} -C ${.CURDIR} PKG_VERSION=${PKG_VERSION} real-packages
1570
1571package-pkg: .PHONY
1572	rm -rf /tmp/ports.${TARGET} || :
1573	env ${WMAKEENV:Q} SRCDIR=${.CURDIR} PORTSDIR=${PORTSDIR} REVISION=${_REVISION} \
1574		PKG_CMD=${PKG_CMD} PKG_VERSION=${PKG_VERSION} REPODIR=${REPODIR} \
1575		WSTAGEDIR=${WSTAGEDIR} \
1576		sh ${.CURDIR}/release/scripts/make-pkg-package.sh
1577
1578real-packages:	stage-packages create-packages sign-packages .PHONY
1579
1580stage-packages-world: .PHONY
1581	@mkdir -p ${WSTAGEDIR}
1582	${_+_}@cd ${.CURDIR}; \
1583		${MAKE} DESTDIR=${WSTAGEDIR} -DNO_ROOT stageworld
1584
1585stage-packages-kernel: .PHONY
1586	@mkdir -p ${KSTAGEDIR}
1587	${_+_}@cd ${.CURDIR}; \
1588		${MAKE} DESTDIR=${KSTAGEDIR} -DNO_ROOT stagekernel
1589
1590stage-packages: .PHONY stage-packages-world stage-packages-kernel
1591
1592_repodir: .PHONY
1593	@mkdir -p ${REPODIR}
1594
1595create-packages-world:	_pkgbootstrap _repodir .PHONY
1596	${_+_}@cd ${.CURDIR}; \
1597		${MAKE} -f Makefile.inc1 \
1598			DESTDIR=${WSTAGEDIR} \
1599			PKG_VERSION=${PKG_VERSION} create-world-packages
1600
1601create-packages-kernel:	_pkgbootstrap _repodir .PHONY
1602	${_+_}@cd ${.CURDIR}; \
1603		${MAKE} -f Makefile.inc1 \
1604			DESTDIR=${KSTAGEDIR} \
1605			PKG_VERSION=${PKG_VERSION} DISTDIR=kernel \
1606			create-kernel-packages
1607
1608create-packages: .PHONY create-packages-world create-packages-kernel
1609
1610create-world-packages:	_pkgbootstrap .PHONY
1611	@rm -f ${WSTAGEDIR}/*.plist 2>/dev/null || :
1612	@cd ${WSTAGEDIR} ; \
1613		awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
1614		${WSTAGEDIR}/METALOG
1615	@for plist in ${WSTAGEDIR}/*.plist; do \
1616	  plist=$${plist##*/} ; \
1617	  pkgname=$${plist%.plist} ; \
1618	  echo "_PKGS+= $${pkgname}" ; \
1619	done > ${WSTAGEDIR}/packages.mk
1620	${_+_}@cd ${.CURDIR}; \
1621		${MAKE} -f Makefile.inc1 create-world-packages-jobs \
1622		.MAKE.JOB.PREFIX=
1623
1624.if make(create-world-packages-jobs)
1625.include "${WSTAGEDIR}/packages.mk"
1626.endif
1627
1628create-world-packages-jobs: .PHONY
1629.for pkgname in ${_PKGS}
1630create-world-packages-jobs: create-world-package-${pkgname}
1631create-world-package-${pkgname}: .PHONY
1632	@sh ${SRCDIR}/release/packages/generate-ucl.sh -o ${pkgname} \
1633		-s ${SRCDIR} -u ${WSTAGEDIR}/${pkgname}.ucl
1634	@awk -F\" ' \
1635		/^name/ { printf("===> Creating %s-", $$2); next } \
1636		/^version/ { print $$2; next } \
1637		' ${WSTAGEDIR}/${pkgname}.ucl
1638	@if [ "${pkgname}" == "runtime" ]; then \
1639		sed -i '' -e "s/%VCS_REVISION%/${VCS_REVISION}/" ${WSTAGEDIR}/${pkgname}.ucl ; \
1640	fi
1641	${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh -o ALLOW_BASE_SHLIBS=yes \
1642		create -M ${WSTAGEDIR}/${pkgname}.ucl \
1643		-p ${WSTAGEDIR}/${pkgname}.plist \
1644		-r ${WSTAGEDIR} \
1645		-o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/${PKG_VERSION}
1646.endfor
1647
1648create-kernel-packages:	.PHONY
1649_default_flavor=	-default
1650.if exists(${KSTAGEDIR}/kernel.meta)
1651. if ${MK_DEBUG_FILES} != "no"
1652_debug=-debug
1653. endif
1654. for flavor in "" ${_debug}
1655create-kernel-packages: create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}
1656create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}: _pkgbootstrap .PHONY
1657	@cd ${KSTAGEDIR}/${DISTDIR} ; \
1658	awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
1659		-v kernel=yes -v _kernconf=${INSTALLKERNEL} \
1660		${KSTAGEDIR}/kernel.meta ; \
1661	cap_arg=`cd ${SRCDIR}/etc ; ${MAKE} -VCAP_MKDB_ENDIAN` ; \
1662	pwd_arg=`cd ${SRCDIR}/etc ; ${MAKE} -VPWD_MKDB_ENDIAN` ; \
1663	sed -e "s/%VERSION%/${PKG_VERSION}/" \
1664		-e "s/%PKGNAME%/kernel-${INSTALLKERNEL:tl}${flavor}/" \
1665		-e "s/%COMMENT%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
1666		-e "s/%DESC%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
1667		-e "s/%CAP_MKDB_ENDIAN%/$${cap_arg}/g" \
1668		-e "s/%PWD_MKDB_ENDIAN%/$${pwd_arg}/g" \
1669		-e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
1670		${SRCDIR}/release/packages/kernel.ucl \
1671		> ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
1672	awk -F\" ' \
1673		/name/ { printf("===> Creating %s-", $$2); next } \
1674		/version/ {print $$2; next } ' \
1675		${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
1676	${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh -o ALLOW_BASE_SHLIBS=yes \
1677		create -M ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl \
1678		-p ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.plist \
1679		-r ${KSTAGEDIR}/${DISTDIR} \
1680		-o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/${PKG_VERSION}
1681. endfor
1682.endif
1683.if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1684. for _kernel in ${BUILDKERNELS:[2..-1]}
1685.  if exists(${KSTAGEDIR}/kernel.${_kernel}.meta)
1686.   if ${MK_DEBUG_FILES} != "no"
1687_debug=-debug
1688.   endif
1689.   for flavor in "" ${_debug}
1690create-kernel-packages: create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}
1691create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}: _pkgbootstrap .PHONY
1692	@cd ${KSTAGEDIR}/kernel.${_kernel} ; \
1693	awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
1694		-v kernel=yes -v _kernconf=${_kernel} \
1695		${KSTAGEDIR}/kernel.${_kernel}.meta ; \
1696	cap_arg=`cd ${SRCDIR}/etc ; ${MAKE} -VCAP_MKDB_ENDIAN` ; \
1697	pwd_arg=`cd ${SRCDIR}/etc ; ${MAKE} -VPWD_MKDB_ENDIAN` ; \
1698	sed -e "s/%VERSION%/${PKG_VERSION}/" \
1699		-e "s/%PKGNAME%/kernel-${_kernel:tl}${flavor}/" \
1700		-e "s/%COMMENT%/FreeBSD ${_kernel} kernel ${flavor}/" \
1701		-e "s/%DESC%/FreeBSD ${_kernel} kernel ${flavor}/" \
1702		-e "s/%CAP_MKDB_ENDIAN%/$${cap_arg}/g" \
1703		-e "s/%PWD_MKDB_ENDIAN%/$${pwd_arg}/g" \
1704		-e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
1705		${SRCDIR}/release/packages/kernel.ucl \
1706		> ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
1707	awk -F\" ' \
1708		/name/ { printf("===> Creating %s-", $$2); next } \
1709		/version/ {print $$2; next } ' \
1710		${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
1711	${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh -o ALLOW_BASE_SHLIBS=yes \
1712		create -M ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl \
1713		-p ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.plist \
1714		-r ${KSTAGEDIR}/kernel.${_kernel} \
1715		-o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/${PKG_VERSION}
1716.   endfor
1717.  endif
1718. endfor
1719.endif
1720
1721sign-packages:	_pkgbootstrap .PHONY
1722	@[ -L "${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/latest" ] && \
1723		unlink ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/latest ; \
1724	${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh repo \
1725		-o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/${PKG_VERSION} \
1726		${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI)/${PKG_VERSION} \
1727		${PKGSIGNKEY} ; \
1728	cd ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/bin/sh config ABI); \
1729	ln -s ${PKG_VERSION} latest
1730
1731#
1732#
1733# checkworld
1734#
1735# Run test suite on installed world.
1736#
1737checkworld: .PHONY
1738	@if [ ! -x "${LOCALBASE}/bin/kyua" ]; then \
1739		echo "You need kyua (devel/kyua) to run the test suite." | /usr/bin/fmt; \
1740		exit 1; \
1741	fi
1742	${_+_}PATH="$$PATH:${LOCALBASE}/bin" kyua test -k ${TESTSBASE}/Kyuafile
1743
1744#
1745#
1746# doxygen
1747#
1748# Build the API documentation with doxygen
1749#
1750doxygen: .PHONY
1751	@if [ ! -x "${LOCALBASE}/bin/doxygen" ]; then \
1752		echo "You need doxygen (devel/doxygen) to generate the API documentation of the kernel." | /usr/bin/fmt; \
1753		exit 1; \
1754	fi
1755	${_+_}cd ${.CURDIR}/tools/kerneldoc/subsys; ${MAKE} obj all
1756
1757#
1758# update
1759#
1760# Update the source tree(s), by running svn/svnup to update to the
1761# latest copy.
1762#
1763update: .PHONY
1764.if defined(SVN_UPDATE)
1765	@echo "--------------------------------------------------------------"
1766	@echo ">>> Updating ${.CURDIR} using Subversion"
1767	@echo "--------------------------------------------------------------"
1768	@(cd ${.CURDIR}; ${SVN} update ${SVNFLAGS})
1769.endif
1770
1771#
1772# ------------------------------------------------------------------------
1773#
1774# From here onwards are utility targets used by the 'make world' and
1775# related targets.  If your 'world' breaks, you may like to try to fix
1776# the problem and manually run the following targets to attempt to
1777# complete the build.  Beware, this is *not* guaranteed to work, you
1778# need to have a pretty good grip on the current state of the system
1779# to attempt to manually finish it.  If in doubt, 'make world' again.
1780#
1781
1782#
1783# legacy: Build compatibility shims for the next three targets. This is a
1784# minimal set of tools and shims necessary to compensate for older systems
1785# which don't have the APIs required by the targets built in bootstrap-tools,
1786# build-tools or cross-tools.
1787#
1788
1789# ELF Tool Chain libraries are needed for ELF tools and dtrace tools.
1790# r296685 fix cross-endian objcopy
1791# r310724 fixed PR 215350, a crash in libdwarf with objects built by GCC 6.2.
1792.if ${BOOTSTRAPPING} < 1200020
1793_elftoolchain_libs= lib/libelf lib/libdwarf
1794.endif
1795
1796legacy: .PHONY
1797# Temporary special case for automatically detecting the clang compiler issue
1798# Note: 9.x didn't have FreeBSD_version bumps often enough, so you may need to
1799# set BOOTSTRAPPING to 0 if you're stable/9 tree post-dates r286035 but is before
1800# the version bump in r296219 (from July 29, 2015 -> Feb 29, 2016).
1801.if ${BOOTSTRAPPING} != 0 && \
1802	${WANT_COMPILER_TYPE} == "clang" && ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 30601
1803.if   ${BOOTSTRAPPING} > 10000000 && ${BOOTSTRAPPING} < 1002501
1804	@echo "ERROR: Source upgrades from stable/10 prior to r286033 are not supported."; false
1805.elif ${BOOTSTRAPPING} >  9000000 && ${BOOTSTRAPPING} <  903509
1806	@echo "ERROR: Source upgrades from stable/9 prior to r286035 are not supported."; false
1807.endif
1808.endif
1809.if ${BOOTSTRAPPING} < ${MINIMUM_SUPPORTED_OSREL} && ${BOOTSTRAPPING} != 0
1810	@echo "ERROR: Source upgrades from versions prior to ${MINIMUM_SUPPORTED_REL} are not supported."; \
1811	false
1812.endif
1813
1814.for _tool in tools/build ${_elftoolchain_libs}
1815	${_+_}@${ECHODIR} "===> ${_tool} (obj,includes,all,install)"; \
1816	    cd ${.CURDIR}/${_tool}; \
1817	    if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
1818	    ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy includes; \
1819	    ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no all; \
1820	    ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no \
1821	        DESTDIR=${WORLDTMP}/legacy install
1822.endfor
1823
1824#
1825# bootstrap-tools: Build tools needed for compatibility. These are binaries that
1826# are built to build other binaries in the system. However, the focus of these
1827# binaries is usually quite narrow. Bootstrap tools use the host's compiler and
1828# libraries, augmented by -legacy.
1829#
1830_bt=		_bootstrap-tools
1831
1832.if ${MK_GAMES} != "no"
1833_strfile=	usr.bin/fortune/strfile
1834.endif
1835
1836.if ${MK_GCC} != "no" && ${MK_CXX} != "no"
1837_gperf=		gnu/usr.bin/gperf
1838.endif
1839
1840.if ${MK_VT} != "no"
1841_vtfontcvt=	usr.bin/vtfontcvt
1842.endif
1843
1844.if ${BOOTSTRAPPING} < 1000033
1845_m4=		usr.bin/m4
1846_lex=		usr.bin/lex
1847
1848${_bt}-usr.bin/m4: ${_bt}-lib/libopenbsd
1849${_bt}-usr.bin/lex: ${_bt}-usr.bin/m4
1850.endif
1851
1852# r245440 mtree -N support added
1853# r313404 requires sha384.h for libnetbsd, added to libmd in r292782
1854.if ${BOOTSTRAPPING} < 1100093
1855_nmtree=	lib/libmd \
1856		lib/libnetbsd \
1857		usr.sbin/nmtree
1858
1859${_bt}-lib/libnetbsd: ${_bt}-lib/libmd
1860${_bt}-usr.sbin/nmtree: ${_bt}-lib/libnetbsd
1861.endif
1862
1863# r246097: log addition login.conf.db, passwd, pwd.db, and spwd.db with cat -l
1864.if ${BOOTSTRAPPING} < 1000027
1865_cat=		bin/cat
1866.endif
1867
1868# r277259 crunchide: Correct 64-bit section header offset
1869# r281674 crunchide: always include both 32- and 64-bit ELF support
1870.if ${BOOTSTRAPPING} < 1100078
1871_crunchide=	usr.sbin/crunch/crunchide
1872.endif
1873
1874# r285986 crunchen: use STRIPBIN rather than STRIP
1875# 1100113: Support MK_AUTO_OBJ
1876# 1200006: META_MODE fixes
1877.if ${BOOTSTRAPPING} < 1100078 || \
1878    (${MK_AUTO_OBJ} == "yes" && ${BOOTSTRAPPING} < 1100114) || \
1879    (${MK_META_MODE} == "yes" && ${BOOTSTRAPPING} < 1200006)
1880_crunchgen=	usr.sbin/crunch/crunchgen
1881.endif
1882
1883# r296926 -P keymap search path, MFC to stable/10 in r298297
1884.if ${BOOTSTRAPPING} < 1003501 || \
1885	(${BOOTSTRAPPING} >= 1100000 && ${BOOTSTRAPPING} < 1100103)
1886_kbdcontrol=	usr.sbin/kbdcontrol
1887.endif
1888
1889_yacc=		lib/liby \
1890		usr.bin/yacc
1891
1892${_bt}-usr.bin/yacc: ${_bt}-lib/liby
1893
1894.if ${MK_BSNMP} != "no"
1895_gensnmptree=	usr.sbin/bsnmpd/gensnmptree
1896.endif
1897
1898# We need to build tblgen when we're building clang or lld, either as
1899# bootstrap tools, or as the part of the normal build.
1900.if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no" || \
1901    ${MK_LLD_BOOTSTRAP} != "no" || ${MK_LLD} != "no"
1902_clang_tblgen= \
1903	lib/clang/libllvmminimal \
1904	usr.bin/clang/llvm-tblgen \
1905	usr.bin/clang/clang-tblgen
1906
1907${_bt}-usr.bin/clang/clang-tblgen: ${_bt}-lib/clang/libllvmminimal
1908${_bt}-usr.bin/clang/llvm-tblgen: ${_bt}-lib/clang/libllvmminimal
1909.endif
1910
1911# Default to building the GPL DTC, but build the BSDL one if users explicitly
1912# request it.
1913_dtc= usr.bin/dtc
1914.if ${MK_GPL_DTC} != "no"
1915_dtc= gnu/usr.bin/dtc
1916.endif
1917
1918.if ${MK_KERBEROS} != "no"
1919_kerberos5_bootstrap_tools= \
1920	kerberos5/tools/make-roken \
1921	kerberos5/lib/libroken \
1922	kerberos5/lib/libvers \
1923	kerberos5/tools/asn1_compile \
1924	kerberos5/tools/slc \
1925	usr.bin/compile_et
1926
1927.ORDER: ${_kerberos5_bootstrap_tools:C/^/${_bt}-/g}
1928.endif
1929
1930${_bt}-usr.bin/mandoc: ${_bt}-lib/libopenbsd
1931
1932bootstrap-tools: .PHONY
1933
1934#	Please document (add comment) why something is in 'bootstrap-tools'.
1935#	Try to bound the building of the bootstrap-tool to just the
1936#	FreeBSD versions that need the tool built at this stage of the build.
1937.for _tool in \
1938    ${_clang_tblgen} \
1939    ${_kerberos5_bootstrap_tools} \
1940    ${_strfile} \
1941    ${_gperf} \
1942    ${_dtc} \
1943    ${_cat} \
1944    ${_kbdcontrol} \
1945    usr.bin/lorder \
1946    lib/libopenbsd \
1947    usr.bin/mandoc \
1948    usr.bin/rpcgen \
1949    ${_yacc} \
1950    ${_m4} \
1951    ${_lex} \
1952    usr.bin/xinstall \
1953    ${_gensnmptree} \
1954    usr.sbin/config \
1955    ${_crunchide} \
1956    ${_crunchgen} \
1957    ${_nmtree} \
1958    ${_vtfontcvt} \
1959    usr.bin/localedef
1960${_bt}-${_tool}: .PHONY .MAKE
1961	${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
1962		cd ${.CURDIR}/${_tool}; \
1963		if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
1964		${MAKE} DIRPRFX=${_tool}/ all; \
1965		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy install
1966
1967bootstrap-tools: ${_bt}-${_tool}
1968.endfor
1969
1970#
1971# build-tools: Build special purpose build tools
1972#
1973.if !defined(NO_SHARE)
1974_share=	share/syscons/scrnmaps
1975.endif
1976
1977.if ${MK_GCC} != "no"
1978_gcc_tools= gnu/usr.bin/cc/cc_tools
1979.endif
1980
1981.if ${MK_RESCUE} != "no"
1982# rescue includes programs that have build-tools targets
1983_rescue=rescue/rescue
1984.endif
1985
1986.if ${MK_TCSH} != "no"
1987_tcsh=bin/csh
1988.endif
1989.if ${MK_FILE} != "no"
1990_libmagic=lib/libmagic
1991.endif
1992
1993# kernel-toolchain skips _cleanobj, so handle cleaning up previous
1994# build-tools directories if needed.
1995.if !defined(NO_CLEAN) && make(kernel-toolchain)
1996_bt_clean=	${CLEANDIR}
1997.endif
1998
1999.for _tool in \
2000    ${_tcsh} \
2001    bin/sh \
2002    ${LOCAL_TOOL_DIRS} \
2003    lib/ncurses/ncurses \
2004    lib/ncurses/ncursesw \
2005    ${_rescue} \
2006    ${_share} \
2007    usr.bin/awk \
2008    ${_libmagic} \
2009    usr.bin/mkesdb_static \
2010    usr.bin/mkcsmapper_static \
2011    usr.bin/vi/catalog \
2012    ${_gcc_tools}
2013build-tools_${_tool}: .PHONY
2014	${_+_}@${ECHODIR} "===> ${_tool} (${_bt_clean:D${_bt_clean},}obj,build-tools)"; \
2015		cd ${.CURDIR}/${_tool}; \
2016		if [ -n "${_bt_clean}" ]; then ${MAKE} DIRPRFX=${_tool}/ ${_bt_clean}; fi; \
2017		if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2018		${MAKE} DIRPRFX=${_tool}/ build-tools
2019build-tools: build-tools_${_tool}
2020.endfor
2021
2022#
2023# kernel-tools: Build kernel-building tools
2024#
2025kernel-tools: .PHONY
2026	mkdir -p ${WORLDTMP}/usr
2027	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2028	    -p ${WORLDTMP}/usr >/dev/null
2029
2030#
2031# cross-tools: All the tools needed to build the rest of the system after
2032# we get done with the earlier stages. It is the last set of tools needed
2033# to begin building the target binaries.
2034#
2035.if ${TARGET_ARCH} != ${MACHINE_ARCH}
2036.if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "i386"
2037_btxld=		usr.sbin/btxld
2038.endif
2039.endif
2040
2041# Rebuild ctfconvert and ctfmerge to avoid difficult-to-diagnose failures
2042# resulting from missing bug fixes or ELF Toolchain updates.
2043.if ${MK_CDDL} != "no"
2044_dtrace_tools= cddl/lib/libctf cddl/usr.bin/ctfconvert \
2045    cddl/usr.bin/ctfmerge
2046.endif
2047
2048# If we're given an XAS, don't build binutils.
2049.if ${XAS:M/*} == ""
2050.if ${MK_BINUTILS_BOOTSTRAP} != "no"
2051_binutils=	gnu/usr.bin/binutils
2052.endif
2053.if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2054_elftctools=	lib/libelftc \
2055		lib/libpe \
2056		usr.bin/elfcopy \
2057		usr.bin/nm \
2058		usr.bin/size \
2059		usr.bin/strings
2060# These are not required by the build, but can be useful for developers who
2061# cross-build on a FreeBSD 10 host:
2062_elftctools+=	usr.bin/addr2line
2063.endif
2064.elif ${TARGET_ARCH} != ${MACHINE_ARCH} && ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2065# If cross-building with an external binutils we still need to build strip for
2066# the target (for at least crunchide).
2067_elftctools=	lib/libelftc \
2068		lib/libpe \
2069		usr.bin/elfcopy
2070.endif
2071
2072.if ${MK_CLANG_BOOTSTRAP} != "no"
2073_clang=		usr.bin/clang
2074.endif
2075.if ${MK_LLD_BOOTSTRAP} != "no"
2076_lld=		usr.bin/clang/lld
2077.endif
2078.if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_LLD_BOOTSTRAP} != "no"
2079_clang_libs=	lib/clang
2080.endif
2081.if ${MK_GCC_BOOTSTRAP} != "no"
2082_gcc=		gnu/usr.bin/cc
2083.endif
2084.if ${MK_USB} != "no"
2085_usb_tools=	sys/boot/usb/tools
2086.endif
2087
2088cross-tools: .MAKE .PHONY
2089.for _tool in \
2090    ${LOCAL_XTOOL_DIRS} \
2091    ${_clang_libs} \
2092    ${_clang} \
2093    ${_lld} \
2094    ${_binutils} \
2095    ${_elftctools} \
2096    ${_dtrace_tools} \
2097    ${_gcc} \
2098    ${_btxld} \
2099    ${_usb_tools}
2100	${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2101		cd ${.CURDIR}/${_tool}; \
2102		if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2103		${MAKE} DIRPRFX=${_tool}/ all; \
2104		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP} install
2105.endfor
2106
2107#
2108# native-xtools is the current target for qemu-user cross builds of ports
2109# via poudriere and the imgact_binmisc kernel module.
2110# This target merely builds a toolchan/sysroot, then builds the tools it wants
2111# with the options it wants in a special MAKEOBJDIRPREFIX, using the toolchain
2112# already built.  It then installs the static tools to NXBDESTDIR for Poudriere
2113# to pickup.
2114#
2115NXBOBJROOT=	${OBJROOT}${MACHINE}.${MACHINE_ARCH}/nxb/
2116NXBOBJTOP=	${NXBOBJROOT}${NXB_TARGET}.${NXB_TARGET_ARCH}
2117NXTP?=		/nxb-bin
2118.if ${NXTP:N/*}
2119.error NXTP variable should be an absolute path
2120.endif
2121NXBDESTDIR?=	${DESTDIR}${NXTP}
2122
2123# This is the list of tools to be built/installed as static and where
2124# appropriate to build for the given TARGET.TARGET_ARCH.
2125NXBDIRS+= \
2126    bin/cat \
2127    bin/chmod \
2128    bin/cp \
2129    ${_tcsh} \
2130    bin/echo \
2131    bin/expr \
2132    bin/hostname \
2133    bin/ln \
2134    bin/ls \
2135    bin/mkdir \
2136    bin/mv \
2137    bin/ps \
2138    bin/realpath \
2139    bin/rm \
2140    bin/rmdir \
2141    bin/sh \
2142    bin/sleep \
2143    sbin/md5 \
2144    sbin/sysctl \
2145    usr.bin/addr2line \
2146    usr.bin/ar \
2147    usr.bin/awk \
2148    usr.bin/basename \
2149    usr.bin/bmake \
2150    usr.bin/bzip2 \
2151    usr.bin/cmp \
2152    usr.bin/diff \
2153    usr.bin/dirname \
2154    usr.bin/elfcopy \
2155    usr.bin/env \
2156    usr.bin/fetch \
2157    usr.bin/find \
2158    usr.bin/grep \
2159    usr.bin/gzip \
2160    usr.bin/id \
2161    usr.bin/lex \
2162    usr.bin/limits \
2163    usr.bin/lorder \
2164    usr.bin/mandoc \
2165    usr.bin/mktemp \
2166    usr.bin/mt \
2167    usr.bin/nm \
2168    usr.bin/patch \
2169    usr.bin/readelf \
2170    usr.bin/sed \
2171    usr.bin/size \
2172    usr.bin/sort \
2173    usr.bin/strings \
2174    usr.bin/tar \
2175    usr.bin/touch \
2176    usr.bin/tr \
2177    usr.bin/true \
2178    usr.bin/uniq \
2179    usr.bin/unzip \
2180    usr.bin/xargs \
2181    usr.bin/xinstall \
2182    usr.bin/xz \
2183    usr.bin/yacc \
2184    usr.sbin/chown
2185
2186SUBDIR_DEPEND_usr.bin/clang=	lib/clang
2187.if ${MK_CLANG} != "no"
2188NXBDIRS+=	lib/clang
2189NXBDIRS+=	usr.bin/clang
2190.endif
2191.if ${MK_GCC} != "no"
2192NXBDIRS+=	gnu/usr.bin/cc
2193.endif
2194.if ${MK_BINUTILS} != "no"
2195NXBDIRS+=	gnu/usr.bin/binutils
2196.endif
2197# XXX: native-xtools passes along ${NXBDIRS} in SUBDIR_OVERRIDE that needs
2198# to be evaluated after NXBDIRS is set.
2199.if make(install) && !empty(SUBDIR_OVERRIDE)
2200SUBDIR=	${SUBDIR_OVERRIDE}
2201.endif
2202
2203NXBMAKEARGS+= \
2204	OBJTOP=${NXBOBJTOP:Q} \
2205	OBJROOT=${NXBOBJROOT:Q} \
2206	MAKEOBJDIRPREFIX= \
2207	-DNO_SHARED \
2208	-DNO_CPU_CFLAGS \
2209	-DNO_PIC \
2210	SSP_CFLAGS= \
2211	MK_CLANG_EXTRAS=no \
2212	MK_CLANG_FULL=no \
2213	MK_CTF=no \
2214	MK_DEBUG_FILES=no \
2215	MK_GDB=no \
2216	MK_HTML=no \
2217	MK_LLDB=no \
2218	MK_MAN=no \
2219	MK_MAN_UTILS=yes \
2220	MK_OFED=no \
2221	MK_OPENSSH=no \
2222	MK_PROFILE=no \
2223	MK_SENDMAIL=no \
2224	MK_SVNLITE=no \
2225	MK_TESTS=no \
2226	MK_WARNS=no \
2227	MK_ZFS=no
2228
2229.if make(native-xtools*) && \
2230    (!defined(NXB_TARGET) || !defined(NXB_TARGET_ARCH))
2231.error Missing NXB_TARGET / NXB_TARGET_ARCH
2232.endif
2233# For 'toolchain' we want to produce native binaries that themselves generate
2234# native binaries.
2235NXBTMAKE=	${NXBMAKEENV} ${MAKE} ${NXBMAKEARGS:N-DNO_PIC:N-DNO_SHARED} \
2236		TARGET=${MACHINE} TARGET_ARCH=${MACHINE_ARCH}
2237# For 'everything' we want to produce native binaries (hence -target to
2238# be MACHINE) that themselves generate TARGET.TARGET_ARCH binaries.
2239# TARGET/TARGET_ARCH are still passed along from user.
2240#
2241# Use the toolchain we create as an external toolchain.
2242.if ${USING_SYSTEM_COMPILER} == "yes" || ${XCC:N${CCACHE_BIN}:M/*}
2243NXBMAKE+=	XCC="${XCC}" \
2244		XCXX="${XCXX}" \
2245		XCPP="${XCPP}"
2246.else
2247NXBMAKE+=	XCC="${NXBOBJTOP}/tmp/usr/bin/cc" \
2248		XCXX="${NXBOBJTOP}/tmp/usr/bin/c++" \
2249		XCPP="${NXBOBJTOP}/tmp/usr/bin/cpp"
2250.endif
2251NXBMAKE+=	${NXBMAKEENV} ${MAKE} -f Makefile.inc1 ${NXBMAKEARGS} \
2252		TARGET=${NXB_TARGET} TARGET_ARCH=${NXB_TARGET_ARCH} \
2253		TARGET_TRIPLE=${MACHINE_TRIPLE:Q}
2254# NXBDIRS is improperly based on MACHINE rather than NXB_TARGET.  Need to
2255# invoke a sub-make to reevaluate MK_GCC, etc, for NXBDIRS.
2256NXBMAKE+=	SUBDIR_OVERRIDE='$${NXBDIRS:M*}'
2257# Need to avoid the -isystem logic when using clang as an external toolchain
2258# even if the TARGET being built for wants GCC.
2259NXBMAKE+=	WANT_COMPILER_TYPE='$${X_COMPILER_TYPE}'
2260native-xtools: .PHONY
2261	${_+_}cd ${.CURDIR}; ${NXBTMAKE} _cleanobj
2262	# Build the bootstrap/host/cross tools that produce native binaries
2263	# Pass along MK_GCC=yes to ensure GCC-needed build tools are built.
2264	# We don't quite know what the NXB_TARGET wants so just build it.
2265	${_+_}cd ${.CURDIR}; ${NXBTMAKE} kernel-toolchain MK_GCC=yes
2266	# Populate includes/libraries sysroot that produce native binaries.
2267	# This is split out from 'toolchain' above mostly so that target LLVM
2268	# libraries have a proper LLVM_DEFAULT_TARGET_TRIPLE without
2269	# polluting the cross-compiler build.  The LLVM/GCC libs are skipped
2270	# here to avoid the problem but are kept in 'toolchain' so that
2271	# needed build tools are built.
2272	${_+_}cd ${.CURDIR}; ${NXBTMAKE} _includes MK_CLANG=no MK_GCC=no
2273	${_+_}cd ${.CURDIR}; ${NXBTMAKE} _libraries MK_CLANG=no MK_GCC=no
2274.if !defined(NO_OBJWALK)
2275	${_+_}cd ${.CURDIR}; ${NXBMAKE} _obj
2276.endif
2277	${_+_}cd ${.CURDIR}; ${NXBMAKE} everything
2278	@echo ">> native-xtools done.  Use 'make native-xtools-install' to install to a given DESTDIR"
2279
2280native-xtools-install: .PHONY
2281	mkdir -p ${NXBDESTDIR}/bin ${NXBDESTDIR}/sbin ${NXBDESTDIR}/usr
2282	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2283	    -p ${NXBDESTDIR}/usr >/dev/null
2284	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
2285	    -p ${NXBDESTDIR}/usr/include >/dev/null
2286	${_+_}cd ${.CURDIR}; ${NXBMAKE} \
2287	    DESTDIR=${NXBDESTDIR} \
2288	    -DNO_ROOT \
2289	    install
2290
2291#
2292# hierarchy - ensure that all the needed directories are present
2293#
2294hierarchy hier: .MAKE .PHONY
2295	${_+_}cd ${.CURDIR}/etc; ${HMAKE} distrib-dirs
2296
2297#
2298# libraries - build all libraries, and install them under ${DESTDIR}.
2299#
2300# The list of libraries with dependents (${_prebuild_libs}) and their
2301# interdependencies (__L) are built automatically by the
2302# ${.CURDIR}/tools/make_libdeps.sh script.
2303#
2304libraries: .MAKE .PHONY
2305	${_+_}cd ${.CURDIR}; \
2306	    ${MAKE} -f Makefile.inc1 _prereq_libs; \
2307	    ${MAKE} -f Makefile.inc1 _startup_libs; \
2308	    ${MAKE} -f Makefile.inc1 _prebuild_libs; \
2309	    ${MAKE} -f Makefile.inc1 _generic_libs
2310
2311#
2312# static libgcc.a prerequisite for shared libc
2313#
2314_prereq_libs= lib/libcompiler_rt
2315.if ${MK_SSP} != "no"
2316_prereq_libs+= gnu/lib/libssp/libssp_nonshared
2317.endif
2318
2319# These dependencies are not automatically generated:
2320#
2321# gnu/lib/csu, gnu/lib/libgcc, lib/csu and lib/libc must be built before
2322# all shared libraries for ELF.
2323#
2324_startup_libs=	gnu/lib/csu
2325_startup_libs+=	lib/csu
2326_startup_libs+=	lib/libcompiler_rt
2327_startup_libs+=	lib/libc
2328_startup_libs+=	lib/libc_nonshared
2329.if ${MK_LIBCPLUSPLUS} != "no"
2330_startup_libs+=	lib/libcxxrt
2331.endif
2332
2333.if ${MK_LLVM_LIBUNWIND} != "no"
2334_prereq_libs+=	lib/libgcc_eh lib/libgcc_s
2335_startup_libs+=	lib/libgcc_eh lib/libgcc_s
2336
2337lib/libgcc_s__L: lib/libc__L
2338lib/libgcc_s__L: lib/libc_nonshared__L
2339.if ${MK_LIBCPLUSPLUS} != "no"
2340lib/libcxxrt__L: lib/libgcc_s__L
2341.endif
2342
2343.else # MK_LLVM_LIBUNWIND == no
2344
2345_prereq_libs+=	gnu/lib/libgcc
2346_startup_libs+=	gnu/lib/libgcc
2347
2348gnu/lib/libgcc__L: lib/libc__L
2349gnu/lib/libgcc__L: lib/libc_nonshared__L
2350.if ${MK_LIBCPLUSPLUS} != "no"
2351lib/libcxxrt__L: gnu/lib/libgcc__L
2352.endif
2353.endif
2354
2355_prebuild_libs=	${_kerberos5_lib_libasn1} \
2356		${_kerberos5_lib_libhdb} \
2357		${_kerberos5_lib_libheimbase} \
2358		${_kerberos5_lib_libheimntlm} \
2359		${_libsqlite3} \
2360		${_kerberos5_lib_libheimipcc} \
2361		${_kerberos5_lib_libhx509} ${_kerberos5_lib_libkrb5} \
2362		${_kerberos5_lib_libroken} \
2363		${_kerberos5_lib_libwind} \
2364		lib/libbz2 ${_libcom_err} lib/libcrypt \
2365		lib/libelf lib/libexpat \
2366		lib/libfigpar \
2367		${_lib_libgssapi} \
2368		lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
2369		${_lib_casper} \
2370		lib/ncurses/ncurses lib/ncurses/ncursesw \
2371		lib/libopie lib/libpam/libpam ${_lib_libthr} \
2372		${_lib_libradius} lib/libsbuf lib/libtacplus \
2373		lib/libgeom \
2374		${_cddl_lib_libumem} ${_cddl_lib_libnvpair} \
2375		${_cddl_lib_libuutil} \
2376		${_cddl_lib_libavl} \
2377		${_cddl_lib_libzfs_core} \
2378		${_cddl_lib_libctf} \
2379		lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \
2380		${_secure_lib_libcrypto} ${_lib_libldns} \
2381		${_secure_lib_libssh} ${_secure_lib_libssl}
2382
2383.if ${MK_GNUCXX} != "no"
2384_prebuild_libs+= gnu/lib/libstdc++ gnu/lib/libsupc++
2385gnu/lib/libstdc++__L: lib/msun__L
2386gnu/lib/libsupc++__L: gnu/lib/libstdc++__L
2387.endif
2388
2389.if ${MK_DIALOG} != "no"
2390_prebuild_libs+= gnu/lib/libdialog
2391gnu/lib/libdialog__L: lib/msun__L lib/ncurses/ncursesw__L
2392.endif
2393
2394.if ${MK_LIBCPLUSPLUS} != "no"
2395_prebuild_libs+= lib/libc++
2396.endif
2397
2398lib/libgeom__L: lib/libexpat__L
2399lib/libkvm__L: lib/libelf__L
2400
2401.if ${MK_LIBTHR} != "no"
2402_lib_libthr=	lib/libthr
2403.endif
2404
2405.if ${MK_RADIUS_SUPPORT} != "no"
2406_lib_libradius=	lib/libradius
2407.endif
2408
2409.if ${MK_OFED} != "no"
2410_ofed_lib=		contrib/ofed/usr.lib
2411_prebuild_libs+=	contrib/ofed/usr.lib/libosmcomp
2412_prebuild_libs+=	contrib/ofed/usr.lib/libopensm
2413_prebuild_libs+=	contrib/ofed/usr.lib/libibcommon
2414_prebuild_libs+=	contrib/ofed/usr.lib/libibverbs
2415_prebuild_libs+=	contrib/ofed/usr.lib/libibumad
2416
2417contrib/ofed/usr.lib/libopensm__L: lib/libthr__L
2418contrib/ofed/usr.lib/libosmcomp__L: lib/libthr__L
2419contrib/ofed/usr.lib/libibumad__L: contrib/ofed/usr.lib/libibcommon__L
2420.endif
2421
2422.if ${MK_CASPER} != "no"
2423_lib_casper=	lib/libcasper
2424.endif
2425
2426lib/libpjdlog__L: lib/libutil__L
2427lib/libcasper__L: lib/libnv__L
2428lib/liblzma__L: lib/libthr__L
2429
2430_generic_libs=	${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib} usr.bin/lex/lib ${_ofed_lib}
2431.if ${MK_IPFILTER} != "no"
2432_generic_libs+=	sbin/ipf/libipf
2433.endif
2434.for _DIR in ${LOCAL_LIB_DIRS}
2435.if exists(${.CURDIR}/${_DIR}/Makefile) && empty(_generic_libs:M${_DIR})
2436_generic_libs+= ${_DIR}
2437.endif
2438.endfor
2439
2440lib/libopie__L lib/libtacplus__L: lib/libmd__L
2441
2442.if ${MK_CDDL} != "no"
2443_cddl_lib_libumem= cddl/lib/libumem
2444_cddl_lib_libnvpair= cddl/lib/libnvpair
2445_cddl_lib_libavl= cddl/lib/libavl
2446_cddl_lib_libuutil= cddl/lib/libuutil
2447.if ${MK_ZFS} != "no"
2448_cddl_lib_libzfs_core= cddl/lib/libzfs_core
2449cddl/lib/libzfs_core__L: cddl/lib/libnvpair__L
2450.endif
2451_cddl_lib_libctf= cddl/lib/libctf
2452_cddl_lib= cddl/lib
2453cddl/lib/libctf__L: lib/libz__L
2454.endif
2455# cddl/lib/libdtrace requires lib/libproc and lib/librtld_db; it's only built
2456# on select architectures though (see cddl/lib/Makefile)
2457.if ${MACHINE_CPUARCH} != "sparc64"
2458_prebuild_libs+=	lib/libprocstat lib/libproc lib/librtld_db
2459lib/libprocstat__L: lib/libelf__L lib/libkvm__L lib/libutil__L
2460lib/libproc__L: lib/libprocstat__L
2461lib/librtld_db__L: lib/libprocstat__L
2462.endif
2463
2464.if ${MK_CRYPT} != "no"
2465.if ${MK_OPENSSL} != "no"
2466_secure_lib_libcrypto= secure/lib/libcrypto
2467_secure_lib_libssl= secure/lib/libssl
2468lib/libradius__L secure/lib/libssl__L: secure/lib/libcrypto__L
2469.if ${MK_LDNS} != "no"
2470_lib_libldns= lib/libldns
2471lib/libldns__L: secure/lib/libcrypto__L
2472.endif
2473.if ${MK_OPENSSH} != "no"
2474_secure_lib_libssh= secure/lib/libssh
2475secure/lib/libssh__L: lib/libz__L secure/lib/libcrypto__L lib/libcrypt__L
2476.if ${MK_LDNS} != "no"
2477secure/lib/libssh__L: lib/libldns__L
2478.endif
2479.if ${MK_GSSAPI} != "no" && ${MK_KERBEROS_SUPPORT} != "no"
2480secure/lib/libssh__L: lib/libgssapi__L kerberos5/lib/libkrb5__L \
2481    kerberos5/lib/libhx509__L kerberos5/lib/libasn1__L lib/libcom_err__L \
2482    lib/libmd__L kerberos5/lib/libroken__L
2483.endif
2484.endif
2485.endif
2486_secure_lib=	secure/lib
2487.endif
2488
2489.if ${MK_KERBEROS} != "no"
2490kerberos5/lib/libasn1__L: lib/libcom_err__L kerberos5/lib/libroken__L
2491kerberos5/lib/libhdb__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
2492    kerberos5/lib/libkrb5__L kerberos5/lib/libroken__L \
2493    kerberos5/lib/libwind__L lib/libsqlite3__L
2494kerberos5/lib/libheimntlm__L: secure/lib/libcrypto__L kerberos5/lib/libkrb5__L \
2495    kerberos5/lib/libroken__L lib/libcom_err__L
2496kerberos5/lib/libhx509__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
2497    secure/lib/libcrypto__L kerberos5/lib/libroken__L kerberos5/lib/libwind__L
2498kerberos5/lib/libkrb5__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
2499    lib/libcrypt__L secure/lib/libcrypto__L kerberos5/lib/libhx509__L \
2500    kerberos5/lib/libroken__L kerberos5/lib/libwind__L \
2501    kerberos5/lib/libheimbase__L kerberos5/lib/libheimipcc__L
2502kerberos5/lib/libroken__L: lib/libcrypt__L
2503kerberos5/lib/libwind__L: kerberos5/lib/libroken__L lib/libcom_err__L
2504kerberos5/lib/libheimbase__L: lib/libthr__L
2505kerberos5/lib/libheimipcc__L: kerberos5/lib/libroken__L kerberos5/lib/libheimbase__L lib/libthr__L
2506.endif
2507
2508lib/libsqlite3__L: lib/libthr__L
2509
2510.if ${MK_GSSAPI} != "no"
2511_lib_libgssapi=	lib/libgssapi
2512.endif
2513
2514.if ${MK_KERBEROS} != "no"
2515_kerberos5_lib=	kerberos5/lib
2516_kerberos5_lib_libasn1= kerberos5/lib/libasn1
2517_kerberos5_lib_libhdb= kerberos5/lib/libhdb
2518_kerberos5_lib_libheimbase= kerberos5/lib/libheimbase
2519_kerberos5_lib_libkrb5= kerberos5/lib/libkrb5
2520_kerberos5_lib_libhx509= kerberos5/lib/libhx509
2521_kerberos5_lib_libroken= kerberos5/lib/libroken
2522_kerberos5_lib_libheimntlm= kerberos5/lib/libheimntlm
2523_libsqlite3= lib/libsqlite3
2524_kerberos5_lib_libheimipcc= kerberos5/lib/libheimipcc
2525_kerberos5_lib_libwind= kerberos5/lib/libwind
2526_libcom_err= lib/libcom_err
2527.endif
2528
2529.if ${MK_NIS} != "no"
2530_lib_libypclnt=	lib/libypclnt
2531.endif
2532
2533.if ${MK_OPENSSL} == "no"
2534lib/libradius__L: lib/libmd__L
2535.endif
2536
2537lib/libproc__L: \
2538    ${_cddl_lib_libctf:D${_cddl_lib_libctf}__L} lib/libelf__L lib/librtld_db__L lib/libutil__L
2539.if ${MK_CXX} != "no"
2540.if ${MK_LIBCPLUSPLUS} != "no"
2541lib/libproc__L: lib/libcxxrt__L
2542.else # This implies MK_GNUCXX != "no"; see lib/libproc
2543lib/libproc__L: gnu/lib/libsupc++__L
2544.endif
2545.endif
2546
2547.for _lib in ${_prereq_libs}
2548${_lib}__PL: .PHONY .MAKE
2549.if exists(${.CURDIR}/${_lib})
2550	${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
2551		cd ${.CURDIR}/${_lib}; \
2552		if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
2553		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
2554		    DIRPRFX=${_lib}/ all; \
2555		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
2556		    DIRPRFX=${_lib}/ install
2557.endif
2558.endfor
2559
2560.for _lib in ${_startup_libs} ${_prebuild_libs} ${_generic_libs}
2561${_lib}__L: .PHONY .MAKE
2562.if exists(${.CURDIR}/${_lib})
2563	${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
2564		cd ${.CURDIR}/${_lib}; \
2565		if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
2566		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ all; \
2567		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ install
2568.endif
2569.endfor
2570
2571_prereq_libs: ${_prereq_libs:S/$/__PL/}
2572_startup_libs: ${_startup_libs:S/$/__L/}
2573_prebuild_libs: ${_prebuild_libs:S/$/__L/}
2574_generic_libs: ${_generic_libs:S/$/__L/}
2575
2576# Enable SUBDIR_PARALLEL when not calling 'make all', unless called from
2577# 'everything' with _PARALLEL_SUBDIR_OK set.  This is because it is unlikely
2578# that running 'make all' from the top-level, especially with a SUBDIR_OVERRIDE
2579# or LOCAL_DIRS set, will have a reliable build if SUBDIRs are built in
2580# parallel.  This is safe for the world stage of buildworld though since it has
2581# already built libraries in a proper order and installed includes into
2582# WORLDTMP. Special handling is done for SUBDIR ordering for 'install*' to
2583# avoid trashing a system if it crashes mid-install.
2584.if !make(all) || defined(_PARALLEL_SUBDIR_OK)
2585SUBDIR_PARALLEL=
2586.endif
2587
2588.include <bsd.subdir.mk>
2589
2590.if make(check-old) || make(check-old-dirs) || \
2591    make(check-old-files) || make(check-old-libs) || \
2592    make(delete-old) || make(delete-old-dirs) || \
2593    make(delete-old-files) || make(delete-old-libs)
2594
2595#
2596# check for / delete old files section
2597#
2598
2599.include "ObsoleteFiles.inc"
2600
2601OLD_LIBS_MESSAGE="Please be sure no application still uses those libraries, \
2602else you can not start such an application. Consult UPDATING for more \
2603information regarding how to cope with the removal/revision bump of a \
2604specific library."
2605
2606.if !defined(BATCH_DELETE_OLD_FILES)
2607RM_I=-i
2608.else
2609RM_I=-v
2610.endif
2611
2612delete-old-files: .PHONY
2613	@echo ">>> Removing old files (only deletes safe to delete libs)"
2614# Ask for every old file if the user really wants to remove it.
2615# It's annoying, but better safe than sorry.
2616# NB: We cannot pass the list of OLD_FILES as a parameter because the
2617# argument list will get too long. Using .for/.endfor make "loops" will make
2618# the Makefile parser segfault.
2619	@exec 3<&0; \
2620	cd ${.CURDIR}; \
2621	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2622	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
2623	while read file; do \
2624		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2625			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
2626			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
2627		fi; \
2628		for ext in debug symbols; do \
2629		  if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
2630		      "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2631			  rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
2632			      <&3; \
2633		  fi; \
2634		done; \
2635	done
2636# Remove catpages without corresponding manpages.
2637	@exec 3<&0; \
2638	find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | \
2639	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
2640	while read catpage; do \
2641		read manpage; \
2642		if [ ! -e "$${manpage}" ]; then \
2643			rm ${RM_I} $${catpage} <&3; \
2644	        fi; \
2645	done
2646	@echo ">>> Old files removed"
2647
2648check-old-files: .PHONY
2649	@echo ">>> Checking for old files"
2650	@cd ${.CURDIR}; \
2651	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2652	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
2653	while read file; do \
2654		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2655		 	echo "${DESTDIR}/$${file}"; \
2656		fi; \
2657		for ext in debug symbols; do \
2658		  if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2659			  echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
2660		  fi; \
2661		done; \
2662	done
2663# Check for catpages without corresponding manpages.
2664	@find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | \
2665	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
2666	while read catpage; do \
2667		read manpage; \
2668		if [ ! -e "$${manpage}" ]; then \
2669			echo $${catpage}; \
2670	        fi; \
2671	done
2672
2673delete-old-libs: .PHONY
2674	@echo ">>> Removing old libraries"
2675	@echo "${OLD_LIBS_MESSAGE}" | fmt
2676	@exec 3<&0; \
2677	cd ${.CURDIR}; \
2678	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2679	    -V OLD_LIBS | xargs -n1 | \
2680	while read file; do \
2681		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2682			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
2683			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
2684		fi; \
2685		for ext in debug symbols; do \
2686		  if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
2687		      "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2688			  rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
2689			      <&3; \
2690		  fi; \
2691		done; \
2692	done
2693	@echo ">>> Old libraries removed"
2694
2695check-old-libs: .PHONY
2696	@echo ">>> Checking for old libraries"
2697	@cd ${.CURDIR}; \
2698	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2699	    -V OLD_LIBS | xargs -n1 | \
2700	while read file; do \
2701		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2702			echo "${DESTDIR}/$${file}"; \
2703		fi; \
2704		for ext in debug symbols; do \
2705		  if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2706			  echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
2707		  fi; \
2708		done; \
2709	done
2710
2711delete-old-dirs: .PHONY
2712	@echo ">>> Removing old directories"
2713	@cd ${.CURDIR}; \
2714	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2715	    -V OLD_DIRS | xargs -n1 | sort -r | \
2716	while read dir; do \
2717		if [ -d "${DESTDIR}/$${dir}" ]; then \
2718			rmdir -v "${DESTDIR}/$${dir}" || true; \
2719		elif [ -L "${DESTDIR}/$${dir}" ]; then \
2720			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
2721		fi; \
2722		if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
2723			rmdir -v "${DESTDIR}${DEBUGDIR}/$${dir}" || true; \
2724		elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
2725			echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
2726		fi; \
2727	done
2728	@echo ">>> Old directories removed"
2729
2730check-old-dirs: .PHONY
2731	@echo ">>> Checking for old directories"
2732	@cd ${.CURDIR}; \
2733	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2734	    -V OLD_DIRS | xargs -n1 | \
2735	while read dir; do \
2736		if [ -d "${DESTDIR}/$${dir}" ]; then \
2737			echo "${DESTDIR}/$${dir}"; \
2738		elif [ -L "${DESTDIR}/$${dir}" ]; then \
2739			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
2740		fi; \
2741		if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
2742			echo "${DESTDIR}${DEBUGDIR}/$${dir}"; \
2743		elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
2744			echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
2745		fi; \
2746	done
2747
2748delete-old: delete-old-files delete-old-dirs .PHONY
2749	@echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
2750
2751check-old: check-old-files check-old-libs check-old-dirs .PHONY
2752	@echo "To remove old files and directories run '${MAKE_CMD} delete-old'."
2753	@echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
2754
2755.endif
2756
2757#
2758# showconfig - show build configuration.
2759#
2760showconfig: .PHONY
2761	@(${MAKE} -n -f ${.CURDIR}/sys/conf/kern.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes; \
2762	  ${MAKE} -n -f ${.CURDIR}/share/mk/src.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes) 2>&1 | grep ^MK_ | sort -u
2763
2764.if !empty(KRNLOBJDIR) && !empty(KERNCONF)
2765DTBOUTPUTPATH= ${KRNLOBJDIR}/${KERNCONF}/
2766
2767.if !defined(FDT_DTS_FILE) || empty(FDT_DTS_FILE)
2768.if exists(${KERNCONFDIR}/${KERNCONF})
2769FDT_DTS_FILE!= awk 'BEGIN {FS="="} /^makeoptions[[:space:]]+FDT_DTS_FILE/ {print $$2}' \
2770	'${KERNCONFDIR}/${KERNCONF}' ; echo
2771.endif
2772.endif
2773
2774.endif
2775
2776.if !defined(DTBOUTPUTPATH) || !exists(${DTBOUTPUTPATH})
2777DTBOUTPUTPATH= ${.CURDIR}
2778.endif
2779
2780#
2781# Build 'standalone' Device Tree Blob
2782#
2783builddtb: .PHONY
2784	@PATH=${TMPPATH} MACHINE=${TARGET} \
2785	${.CURDIR}/sys/tools/fdt/make_dtb.sh ${.CURDIR}/sys \
2786	    "${FDT_DTS_FILE}" ${DTBOUTPUTPATH}
2787
2788###############
2789
2790# cleanworld
2791# In the following, the first 'rm' in a series will usually remove all
2792# files and directories.  If it does not, then there are probably some
2793# files with file flags set, so this unsets them and tries the 'rm' a
2794# second time.  There are situations where this target will be cleaning
2795# some directories via more than one method, but that duplication is
2796# needed to correctly handle all the possible situations.  Removing all
2797# files without file flags set in the first 'rm' instance saves time,
2798# because 'chflags' will need to operate on fewer files afterwards.
2799#
2800# It is expected that BW_CANONICALOBJDIR == the CANONICALOBJDIR as would be
2801# created by bsd.obj.mk, except that we don't want to .include that file
2802# in this makefile.  We don't do a cleandir walk if MK_AUTO_OBJ is yes
2803# since it is not possible for files to land in the wrong place.
2804#
2805.if make(cleanworld)
2806BW_CANONICALOBJDIR:=${OBJTOP}/
2807.elif make(cleanuniverse)
2808BW_CANONICALOBJDIR:=${OBJROOT}
2809.if ${MK_UNIFIED_OBJDIR} == "no"
2810.error ${.TARGETS} only supported with WITH_UNIFIED_OBJDIR enabled.
2811.endif
2812.endif
2813cleanworld cleanuniverse: .PHONY
2814.if !empty(BW_CANONICALOBJDIR) && exists(${BW_CANONICALOBJDIR}) && \
2815    ${.CURDIR:tA} != ${BW_CANONICALOBJDIR:tA}
2816	-rm -rf ${BW_CANONICALOBJDIR}*
2817	-chflags -R 0 ${BW_CANONICALOBJDIR}
2818	rm -rf ${BW_CANONICALOBJDIR}*
2819.endif
2820.if make(cleanworld) && ${MK_AUTO_OBJ} == "no" && \
2821    (empty(BW_CANONICALOBJDIR) || ${.CURDIR:tA} == ${BW_CANONICALOBJDIR:tA})
2822.if ${.CURDIR} == ${.OBJDIR} || ${.CURDIR}/obj == ${.OBJDIR}
2823	#   To be safe in this case, fall back to a 'make cleandir'
2824	${_+_}@cd ${.CURDIR}; ${MAKE} cleandir
2825.endif
2826.endif
2827
2828.if ${TARGET} == ${MACHINE} && ${TARGET_ARCH} == ${MACHINE_ARCH}
2829XDEV_CPUTYPE?=${CPUTYPE}
2830.else
2831XDEV_CPUTYPE?=${TARGET_CPUTYPE}
2832.endif
2833
2834NOFUN=-DNO_FSCHG MK_HTML=no -DNO_LINT \
2835	MK_MAN=no MK_NLS=no MK_PROFILE=no \
2836	MK_KERBEROS=no MK_RESCUE=no MK_TESTS=no MK_WARNS=no \
2837	TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
2838	CPUTYPE=${XDEV_CPUTYPE}
2839
2840XDDIR=${TARGET_ARCH}-freebsd
2841XDTP?=/usr/${XDDIR}
2842.if ${XDTP:N/*}
2843.error XDTP variable should be an absolute path
2844.endif
2845
2846CDBOBJROOT=	${OBJROOT}${MACHINE}.${MACHINE_ARCH}/xdev/
2847CDBOBJTOP=	${CDBOBJROOT}${XDDIR}
2848CDBENV= \
2849	INSTALL="sh ${.CURDIR}/tools/install.sh"
2850CDENV= ${CDBENV} \
2851	TOOLS_PREFIX=${XDTP}
2852CDMAKEARGS= \
2853	OBJTOP=${CDBOBJTOP:Q} \
2854	OBJROOT=${CDBOBJROOT:Q}
2855CD2MAKEARGS= ${CDMAKEARGS}
2856
2857.if ${WANT_COMPILER_TYPE} == gcc || \
2858    (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
2859# GCC requires -isystem and -L when using a cross-compiler.  --sysroot
2860# won't set header path and -L is used to ensure the base library path
2861# is added before the port PREFIX library path.
2862CD2CFLAGS+=	-isystem ${XDDESTDIR}/usr/include -L${XDDESTDIR}/usr/lib
2863# GCC requires -B to find /usr/lib/crti.o when using a cross-compiler
2864# combined with --sysroot.
2865CD2CFLAGS+=	-B${XDDESTDIR}/usr/lib
2866# Force using libc++ for external GCC.
2867.if ${X_COMPILER_TYPE} == gcc && ${X_COMPILER_VERSION} >= 40800
2868CD2CXXFLAGS+=	-isystem ${XDDESTDIR}/usr/include/c++/v1 -std=c++11 \
2869		-nostdinc++
2870.endif
2871.endif
2872CD2CFLAGS+=	--sysroot=${XDDESTDIR}/
2873CD2ENV=${CDENV} CC="${CC} ${CD2CFLAGS}" CXX="${CXX} ${CD2CXXFLAGS} ${CD2CFLAGS}" \
2874	CPP="${CPP} ${CD2CFLAGS}" \
2875	MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH}
2876
2877CDTMP=	${OBJTOP}/${XDDIR}/tmp
2878CDMAKE=${CDENV} PATH=${CDTMP}/usr/bin:${PATH} ${MAKE} ${CDMAKEARGS} ${NOFUN}
2879CD2MAKE=${CD2ENV} PATH=${CDTMP}/usr/bin:${XDDESTDIR}/usr/bin:${PATH} \
2880	${MAKE} ${CD2MAKEARGS} ${NOFUN}
2881.if ${MK_META_MODE} != "no"
2882# Don't rebuild build-tools targets during normal build.
2883CD2MAKE+=	BUILD_TOOLS_META=.NOMETA
2884.endif
2885XDDESTDIR=${DESTDIR}${XDTP}
2886
2887.ORDER: xdev-build xdev-install xdev-links
2888xdev: xdev-build xdev-install .PHONY
2889
2890.ORDER: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
2891xdev-build: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools .PHONY
2892
2893_xb-worldtmp: .PHONY
2894	mkdir -p ${CDTMP}/usr
2895	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2896	    -p ${CDTMP}/usr >/dev/null
2897
2898_xb-bootstrap-tools: .PHONY
2899.for _tool in \
2900    ${_clang_tblgen} \
2901    ${_gperf} \
2902    ${_yacc}
2903	${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2904	cd ${.CURDIR}/${_tool}; \
2905	if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
2906	${CDMAKE} DIRPRFX=${_tool}/ all; \
2907	${CDMAKE} DIRPRFX=${_tool}/ DESTDIR=${CDTMP} install
2908.endfor
2909
2910_xb-build-tools: .PHONY
2911	${_+_}@cd ${.CURDIR}; \
2912	${CDBENV} ${MAKE} ${CDMAKEARGS} -f Makefile.inc1 ${NOFUN} build-tools
2913
2914XDEVDIRS= \
2915    ${_clang_libs} \
2916    ${_lld} \
2917    ${_binutils} \
2918    ${_elftctools} \
2919    usr.bin/ar \
2920    ${_clang} \
2921    ${_gcc}
2922
2923_xb-cross-tools: .PHONY
2924.for _tool in ${XDEVDIRS}
2925	${_+_}@${ECHODIR} "===> xdev ${_tool} (obj,all)"; \
2926	cd ${.CURDIR}/${_tool}; \
2927	if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
2928	${CDMAKE} DIRPRFX=${_tool}/ all
2929.endfor
2930
2931_xi-mtree: .PHONY
2932	${_+_}@${ECHODIR} "mtree populating ${XDDESTDIR}"
2933	mkdir -p ${XDDESTDIR}
2934	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
2935	    -p ${XDDESTDIR} >/dev/null
2936	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2937	    -p ${XDDESTDIR}/usr >/dev/null
2938	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
2939	    -p ${XDDESTDIR}/usr/include >/dev/null
2940.if defined(LIBCOMPAT)
2941	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
2942	    -p ${XDDESTDIR}/usr >/dev/null
2943.endif
2944.if ${MK_TESTS} != "no"
2945	mkdir -p ${XDDESTDIR}${TESTSBASE}
2946	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
2947	    -p ${XDDESTDIR}${TESTSBASE} >/dev/null
2948.endif
2949
2950.ORDER: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
2951xdev-install: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries .PHONY
2952
2953_xi-cross-tools: .PHONY
2954	@echo "_xi-cross-tools"
2955.for _tool in ${XDEVDIRS}
2956	${_+_}@${ECHODIR} "===> xdev ${_tool} (install)"; \
2957	cd ${.CURDIR}/${_tool}; \
2958	${CDMAKE} DIRPRFX=${_tool}/ install DESTDIR=${XDDESTDIR}
2959.endfor
2960
2961_xi-includes: .PHONY
2962.if !defined(NO_OBJWALK)
2963	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 _obj \
2964		DESTDIR=${XDDESTDIR}
2965.endif
2966	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 includes \
2967		DESTDIR=${XDDESTDIR}
2968
2969_xi-libraries: .PHONY
2970	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 libraries \
2971		DESTDIR=${XDDESTDIR}
2972
2973xdev-links: .PHONY
2974	${_+_}cd ${XDDESTDIR}/usr/bin; \
2975	mkdir -p ../../../../usr/bin; \
2976		for i in *; do \
2977			ln -sf ../../${XDTP}/usr/bin/$$i \
2978			    ../../../../usr/bin/${XDDIR}-$$i; \
2979			ln -sf ../../${XDTP}/usr/bin/$$i \
2980			    ../../../../usr/bin/${XDDIR}${_REVISION}-$$i; \
2981		done
2982