xref: /freebsd/Makefile.inc1 (revision fc7284da06408923f7850a0e4e954a903b8ee038)
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,DEPEND,OBJ}
11#	-DNO_KERNELCONFIG do not run config in ${MAKE} buildkernel
12#	-DNO_KERNELCLEAN do not run ${MAKE} clean in ${MAKE} buildkernel
13#	-DNO_KERNELDEPEND do not run ${MAKE} depend in ${MAKE} buildkernel
14#	-DNO_KERNELOBJ do not run ${MAKE} obj in ${MAKE} buildkernel
15#	-DNO_PORTSUPDATE do not update ports in ${MAKE} update
16#	-DNO_ROOT install without using root privilege
17#	-DNO_DOCUPDATE do not update doc in ${MAKE} update
18#	-DWITHOUT_CTF do not run the DTrace CTF conversion tools on built objects
19#	LOCAL_DIRS="list of dirs" to add additional dirs to the SUBDIR list
20#	LOCAL_ITOOLS="list of tools" to add additional tools to the ITOOLS list
21#	LOCAL_LIB_DIRS="list of dirs" to add additional dirs to libraries target
22#	LOCAL_MTREE="list of mtree files" to process to allow local directories
23#	    to be created before files are installed
24#	LOCAL_TOOL_DIRS="list of dirs" to add additional dirs to the build-tools
25#	    list
26#	METALOG="path to metadata log" to write permission and ownership
27#	    when NO_ROOT is set.  (default: ${DESTDIR}/METALOG)
28#	TARGET="machine" to crossbuild world for a different machine type
29#	TARGET_ARCH= may be required when a TARGET supports multiple endians
30#	BUILDENV_SHELL= shell to launch for the buildenv target (def:${SHELL})
31#	WORLD_FLAGS= additional flags to pass to make(1) during buildworld
32#	KERNEL_FLAGS= additional flags to pass to make(1) during buildkernel
33#	SUBDIR_OVERRIDE="list of dirs" to build rather than everything.
34#	    All libraries and includes, and some build tools will still build.
35
36#
37# The intended user-driven targets are:
38# buildworld  - rebuild *everything*, including glue to help do upgrades
39# installworld- install everything built by "buildworld"
40# doxygen     - build API documentation of the kernel
41# update      - convenient way to update your source tree (eg: svn/svnup)
42#
43# Standard targets (not defined here) are documented in the makefiles in
44# /usr/share/mk.  These include:
45#		obj depend all install clean cleandepend cleanobj
46
47.if !defined(TARGET) || !defined(TARGET_ARCH)
48.error "Both TARGET and TARGET_ARCH must be defined."
49.endif
50
51# Cross toolchain changes must be in effect before bsd.compiler.mk
52# so that gets the right CC, and pass CROSS_TOOLCHAIN to submakes.
53.if defined(CROSS_TOOLCHAIN)
54LOCALBASE?=	/usr/local
55.include "${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk"
56CROSSENV+=CROSS_TOOLCHAIN="${CROSS_TOOLCHAIN}"
57.endif
58.include <bsd.compiler.mk>		# don't depend on src.opts.mk doing it
59.include "share/mk/src.opts.mk"
60
61# We must do lib/ and libexec/ before bin/ in case of a mid-install error to
62# keep the users system reasonably usable.  For static->dynamic root upgrades,
63# we don't want to install a dynamic binary without rtld and the needed
64# libraries.  More commonly, for dynamic root, we don't want to install a
65# binary that requires a newer library version that hasn't been installed yet.
66# This ordering is not a guarantee though.  The only guarantee of a working
67# system here would require fine-grained ordering of all components based
68# on their dependencies.
69SRCDIR?=	${.CURDIR}
70.if !empty(SUBDIR_OVERRIDE)
71SUBDIR=	${SUBDIR_OVERRIDE}
72.else
73SUBDIR=	lib libexec
74.if make(install*)
75# Ensure libraries are installed before progressing.
76SUBDIR+=.WAIT
77.endif
78SUBDIR+=bin
79.if ${MK_CDDL} != "no"
80SUBDIR+=cddl
81.endif
82SUBDIR+=gnu include
83.if ${MK_KERBEROS} != "no"
84SUBDIR+=kerberos5
85.endif
86.if ${MK_RESCUE} != "no"
87SUBDIR+=rescue
88.endif
89SUBDIR+=sbin
90.if ${MK_CRYPT} != "no"
91SUBDIR+=secure
92.endif
93.if !defined(NO_SHARE)
94SUBDIR+=share
95.endif
96SUBDIR+=sys usr.bin usr.sbin
97.if ${MK_TESTS} != "no"
98SUBDIR+=	tests
99.endif
100.if ${MK_OFED} != "no"
101SUBDIR+=contrib/ofed
102.endif
103
104# Local directories are last, since it is nice to at least get the base
105# system rebuilt before you do them.
106.for _DIR in ${LOCAL_DIRS}
107.if exists(${.CURDIR}/${_DIR}/Makefile)
108SUBDIR+=	${_DIR}
109.endif
110.endfor
111# Add LOCAL_LIB_DIRS, but only if they will not be picked up as a SUBDIR
112# of a LOCAL_DIRS directory.  This allows LOCAL_DIRS=foo and
113# LOCAL_LIB_DIRS=foo/lib to behave as expected.
114.for _DIR in ${LOCAL_DIRS:M*/} ${LOCAL_DIRS:N*/:S|$|/|}
115_REDUNDENT_LIB_DIRS+=    ${LOCAL_LIB_DIRS:M${_DIR}*}
116.endfor
117.for _DIR in ${LOCAL_LIB_DIRS}
118.if empty(_REDUNDENT_LIB_DIRS:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile)
119SUBDIR+=	${_DIR}
120.else
121.warning ${_DIR} not added to SUBDIR list.  See UPDATING 20141121.
122.endif
123.endfor
124
125# We must do etc/ last as it hooks into building the man whatis file
126# by calling 'makedb' in share/man.  This is only relevant for
127# install/distribute so they build the whatis file after every manpage is
128# installed.
129.if make(install*)
130SUBDIR+=.WAIT
131.endif
132SUBDIR+=etc
133
134.endif	# !empty(SUBDIR_OVERRIDE)
135
136.if defined(NOCLEAN)
137.warning NOCLEAN option is deprecated. Use NO_CLEAN instead.
138NO_CLEAN=	${NOCLEAN}
139.endif
140.if defined(NO_CLEANDIR)
141CLEANDIR=	clean cleandepend
142.else
143CLEANDIR=	cleandir
144.endif
145
146LOCAL_TOOL_DIRS?=
147PACKAGEDIR?=	${DESTDIR}/${DISTDIR}
148
149BUILDENV_SHELL?=${SHELL}
150
151SVN?=		/usr/local/bin/svn
152SVNFLAGS?=	-r HEAD
153
154MAKEOBJDIRPREFIX?=	/usr/obj
155.if !defined(OSRELDATE)
156.if exists(/usr/include/osreldate.h)
157OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
158		/usr/include/osreldate.h
159.else
160OSRELDATE=	0
161.endif
162.export OSRELDATE
163.endif
164
165# Set VERSION for CTFMERGE to use via the default CTFFLAGS=-L VERSION.
166.if !defined(VERSION)
167REVISION!=	${MAKE} -C ${SRCDIR}/release -V REVISION
168BRANCH!=	${MAKE} -C ${SRCDIR}/release -V BRANCH
169SRCRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
170		${SRCDIR}/sys/sys/param.h
171VERSION=	FreeBSD ${REVISION}-${BRANCH:C/-p[0-9]+$//} ${TARGET_ARCH} ${SRCRELDATE}
172.export VERSION
173.endif
174
175KNOWN_ARCHES?=	aarch64/arm64 amd64 arm armeb/arm armv6/arm armv6hf/arm i386 i386/pc98 mips mipsel/mips mips64el/mips mips64/mips mipsn32el/mips mipsn32/mips powerpc powerpc64/powerpc sparc64
176.if ${TARGET} == ${TARGET_ARCH}
177_t=		${TARGET}
178.else
179_t=		${TARGET_ARCH}/${TARGET}
180.endif
181.for _t in ${_t}
182.if empty(KNOWN_ARCHES:M${_t})
183.error Unknown target ${TARGET_ARCH}:${TARGET}.
184.endif
185.endfor
186
187.if ${TARGET} == ${MACHINE}
188TARGET_CPUTYPE?=${CPUTYPE}
189.else
190TARGET_CPUTYPE?=
191.endif
192
193.if !empty(TARGET_CPUTYPE)
194_TARGET_CPUTYPE=${TARGET_CPUTYPE}
195.else
196_TARGET_CPUTYPE=dummy
197.endif
198_CPUTYPE!=	MAKEFLAGS= CPUTYPE=${_TARGET_CPUTYPE} ${MAKE} \
199		-f /dev/null -m ${.CURDIR}/share/mk -V CPUTYPE
200.if ${_CPUTYPE} != ${_TARGET_CPUTYPE}
201.error CPUTYPE global should be set with ?=.
202.endif
203.if make(buildworld)
204BUILD_ARCH!=	uname -p
205.if ${MACHINE_ARCH} != ${BUILD_ARCH}
206.error To cross-build, set TARGET_ARCH.
207.endif
208.endif
209.if ${MACHINE} == ${TARGET} && ${MACHINE_ARCH} == ${TARGET_ARCH} && !defined(CROSS_BUILD_TESTING)
210OBJTREE=	${MAKEOBJDIRPREFIX}
211.else
212OBJTREE=	${MAKEOBJDIRPREFIX}/${TARGET}.${TARGET_ARCH}
213.endif
214WORLDTMP=	${OBJTREE}${.CURDIR}/tmp
215BPATH=		${WORLDTMP}/legacy/usr/sbin:${WORLDTMP}/legacy/usr/bin:${WORLDTMP}/legacy/bin
216XPATH=		${WORLDTMP}/usr/sbin:${WORLDTMP}/usr/bin
217STRICTTMPPATH=	${BPATH}:${XPATH}
218TMPPATH=	${STRICTTMPPATH}:${PATH}
219
220#
221# Avoid running mktemp(1) unless actually needed.
222# It may not be functional, e.g., due to new ABI
223# when in the middle of installing over this system.
224#
225.if make(distributeworld) || make(installworld)
226INSTALLTMP!=	/usr/bin/mktemp -d -u -t install
227.endif
228
229#
230# Building a world goes through the following stages
231#
232# 1. legacy stage [BMAKE]
233#	This stage is responsible for creating compatibility
234#	shims that are needed by the bootstrap-tools,
235#	build-tools and cross-tools stages. These are generally
236#	APIs that tools from one of those three stages need to
237#	build that aren't present on the host.
238# 1. bootstrap-tools stage [BMAKE]
239#	This stage is responsible for creating programs that
240#	are needed for backward compatibility reasons. They
241#	are not built as cross-tools.
242# 2. build-tools stage [TMAKE]
243#	This stage is responsible for creating the object
244#	tree and building any tools that are needed during
245#	the build process. Some programs are listed during
246#	this phase because they build binaries to generate
247#	files needed to build these programs. This stage also
248#	builds the 'build-tools' target rather than 'all'.
249# 3. cross-tools stage [XMAKE]
250#	This stage is responsible for creating any tools that
251#	are needed for building the system. A cross-compiler is one
252#	of them. This differs from build tools in two ways:
253#	1. the 'all' target is built rather than 'build-tools'
254#	2. these tools are installed into TMPPATH for stage 4.
255# 4. world stage [WMAKE]
256#	This stage actually builds the world.
257# 5. install stage (optional) [IMAKE]
258#	This stage installs a previously built world.
259#
260
261BOOTSTRAPPING?=	0
262
263# Common environment for world related stages
264CROSSENV+=	MAKEOBJDIRPREFIX=${OBJTREE} \
265		MACHINE_ARCH=${TARGET_ARCH} \
266		MACHINE=${TARGET} \
267		CPUTYPE=${TARGET_CPUTYPE}
268.if ${MK_GROFF} != "no"
269CROSSENV+=	GROFF_BIN_PATH=${WORLDTMP}/legacy/usr/bin \
270		GROFF_FONT_PATH=${WORLDTMP}/legacy/usr/share/groff_font \
271		GROFF_TMAC_PATH=${WORLDTMP}/legacy/usr/share/tmac
272.endif
273.if defined(TARGET_CFLAGS)
274CROSSENV+=	${TARGET_CFLAGS}
275.endif
276
277# bootstrap-tools stage
278BMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
279		PATH=${BPATH}:${PATH} \
280		WORLDTMP=${WORLDTMP} \
281		MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
282# need to keep this in sync with targets/pseudo/bootstrap-tools/Makefile
283BSARGS= 	DESTDIR= \
284		BOOTSTRAPPING=${OSRELDATE} \
285		SSP_CFLAGS= \
286		MK_HTML=no NO_LINT=yes MK_MAN=no \
287		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
288		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
289		MK_CLANG_EXTRAS=no MK_CLANG_FULL=no \
290		MK_LLDB=no MK_TESTS=no \
291		MK_INCLUDES=yes
292
293BMAKE=		MAKEOBJDIRPREFIX=${WORLDTMP} \
294		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
295		${BSARGS}
296
297# build-tools stage
298TMAKE=		MAKEOBJDIRPREFIX=${OBJTREE} \
299		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
300		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
301		DESTDIR= \
302		BOOTSTRAPPING=${OSRELDATE} \
303		SSP_CFLAGS= \
304		-DNO_LINT \
305		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
306		MK_CLANG_EXTRAS=no MK_CLANG_FULL=no \
307		MK_LLDB=no MK_TESTS=no
308
309# cross-tools stage
310XMAKE=		TOOLS_PREFIX=${WORLDTMP} ${BMAKE} \
311		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
312		MK_GDB=no MK_TESTS=no
313
314# kernel-tools stage
315KTMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
316		PATH=${BPATH}:${PATH} \
317		WORLDTMP=${WORLDTMP}
318KTMAKE=		TOOLS_PREFIX=${WORLDTMP} MAKEOBJDIRPREFIX=${WORLDTMP} \
319		${KTMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
320		DESTDIR= \
321		BOOTSTRAPPING=${OSRELDATE} \
322		SSP_CFLAGS= \
323		MK_HTML=no -DNO_LINT MK_MAN=no \
324		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
325		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no
326
327# world stage
328WMAKEENV=	${CROSSENV} \
329		_LDSCRIPTROOT= \
330		INSTALL="sh ${.CURDIR}/tools/install.sh" \
331		PATH=${TMPPATH}
332
333# make hierarchy
334HMAKE=		PATH=${TMPPATH} ${MAKE} LOCAL_MTREE=${LOCAL_MTREE:Q}
335.if defined(NO_ROOT)
336HMAKE+=		PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT
337.endif
338
339.if defined(CROSS_TOOLCHAIN_PREFIX)
340CROSS_COMPILER_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
341CROSS_BINUTILS_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
342.endif
343
344# If we do not have a bootstrap binutils (because the in-tree one does not
345# support the target architecture), provide a default cross-binutils prefix.
346# This allows aarch64 builds, for example, to automatically use the
347# aarch64-binutils port or package.
348.if !make(showconfig)
349.if !empty(BROKEN_OPTIONS:MBINUTILS_BOOTSTRAP) && \
350    !defined(CROSS_BINUTILS_PREFIX)
351CROSS_BINUTILS_PREFIX=/usr/local/${TARGET_ARCH}-freebsd/bin/
352.if !exists(${CROSS_BINUTILS_PREFIX})
353.error In-tree binutils does not support the ${TARGET_ARCH} architecture. Install the ${TARGET_ARCH}-binutils port or package or set CROSS_BINUTILS_PREFIX.
354.endif
355.endif
356.endif
357
358XCOMPILERS=	CC CXX CPP
359.for COMPILER in ${XCOMPILERS}
360.if defined(CROSS_COMPILER_PREFIX)
361X${COMPILER}?=	${CROSS_COMPILER_PREFIX}${${COMPILER}}
362.else
363X${COMPILER}?=	${${COMPILER}}
364.endif
365.endfor
366XBINUTILS=	AS AR LD NM OBJCOPY OBJDUMP RANLIB SIZE STRINGS
367.for BINUTIL in ${XBINUTILS}
368.if defined(CROSS_BINUTILS_PREFIX) && \
369    exists(${CROSS_BINUTILS_PREFIX}${${BINUTIL}})
370X${BINUTIL}?=	${CROSS_BINUTILS_PREFIX}${${BINUTIL}}
371.else
372X${BINUTIL}?=	${${BINUTIL}}
373.endif
374.endfor
375CROSSENV+=	CC="${XCC} ${XCFLAGS}" CXX="${XCXX} ${XCFLAGS} ${XCXXFLAGS}" \
376		DEPFLAGS="${DEPFLAGS}" \
377		CPP="${XCPP} ${XCFLAGS}" \
378		AS="${XAS}" AR="${XAR}" LD="${XLD}" NM=${XNM} \
379		OBJDUMP=${XOBJDUMP} OBJCOPY="${XOBJCOPY}" \
380		RANLIB=${XRANLIB} STRINGS=${XSTRINGS} \
381		SIZE="${XSIZE}"
382
383.if ${XCC:N${CCACHE_BIN}:M/*}
384.if defined(CROSS_BINUTILS_PREFIX)
385# In the case of xdev-build tools, CROSS_BINUTILS_PREFIX won't be a
386# directory, but the compiler will look in the right place for it's
387# tools so we don't need to tell it where to look.
388.if exists(${CROSS_BINUTILS_PREFIX})
389BFLAGS+=	-B${CROSS_BINUTILS_PREFIX}
390.endif
391.else
392BFLAGS+=	-B${WORLDTMP}/usr/bin
393.endif
394.if ${TARGET} == "arm"
395.if ${TARGET_ARCH:M*hf*} != ""
396TARGET_ABI=	gnueabihf
397.else
398TARGET_ABI=	gnueabi
399.endif
400.endif
401.if defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc
402XCFLAGS+=	-isystem ${WORLDTMP}/usr/include -L${WORLDTMP}/usr/lib
403XCXXFLAGS+=	-I${WORLDTMP}/usr/include/c++/v1 -std=gnu++11 -L${WORLDTMP}/../lib/libc++
404# XXX: DEPFLAGS is a workaround for not properly passing CXXFLAGS to sub-makes
405# due to CXX="${XCXX} ${XCXXFLAGS}".  bsd.dep.mk does use CXXFLAGS when
406# building C++ files so this can come out if passing CXXFLAGS down is fixed.
407DEPFLAGS+=	-I${WORLDTMP}/usr/include/c++/v1
408.else
409TARGET_ABI?=	unknown
410TARGET_TRIPLE?=	${TARGET_ARCH:C/amd64/x86_64/}-${TARGET_ABI}-freebsd11.0
411XCFLAGS+=	-target ${TARGET_TRIPLE}
412.endif
413XCFLAGS+=	--sysroot=${WORLDTMP} ${BFLAGS}
414XCXXFLAGS+=	--sysroot=${WORLDTMP} ${BFLAGS}
415.else
416.if defined(CROSS_BINUTILS_PREFIX) && exists(${CROSS_BINUTILS_PREFIX})
417BFLAGS+=	-B${CROSS_BINUTILS_PREFIX}
418XCFLAGS+=	${BFLAGS}
419XCXXFLAGS+=	${BFLAGS}
420.endif
421.endif # ${XCC:M/*}
422
423WMAKE=		${WMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 DESTDIR=${WORLDTMP}
424
425.if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "powerpc64"
426# 32 bit world
427LIB32_OBJTREE=	${OBJTREE}${.CURDIR}/world32
428LIB32TMP=	${OBJTREE}${.CURDIR}/lib32
429
430.if ${TARGET_ARCH} == "amd64"
431.if empty(TARGET_CPUTYPE)
432LIB32CPUFLAGS=	-march=i686 -mmmx -msse -msse2
433.else
434LIB32CPUFLAGS=	-march=${TARGET_CPUTYPE}
435.endif
436LIB32WMAKEENV=	MACHINE=i386 MACHINE_ARCH=i386 \
437		MACHINE_CPU="i686 mmx sse sse2"
438LIB32WMAKEFLAGS=	\
439		AS="${XAS} --32" \
440		LD="${XLD} -m elf_i386_fbsd -Y P,${LIB32TMP}/usr/lib32" \
441		OBJCOPY="${XOBJCOPY}"
442
443.elif ${TARGET_ARCH} == "powerpc64"
444.if empty(TARGET_CPUTYPE)
445LIB32CPUFLAGS=	-mcpu=powerpc
446.else
447LIB32CPUFLAGS=	-mcpu=${TARGET_CPUTYPE}
448.endif
449LIB32WMAKEENV=	MACHINE=powerpc MACHINE_ARCH=powerpc
450LIB32WMAKEFLAGS=	\
451		LD="${XLD} -m elf32ppc_fbsd" \
452		OBJCOPY="${XOBJCOPY}"
453.endif
454
455
456LIB32FLAGS=	-m32 ${LIB32CPUFLAGS} -DCOMPAT_32BIT \
457		-isystem ${LIB32TMP}/usr/include/ \
458		-L${LIB32TMP}/usr/lib32 \
459		-B${LIB32TMP}/usr/lib32
460.if ${XCC:N${CCACHE_BIN}:M/*}
461LIB32FLAGS+=		--sysroot=${WORLDTMP}
462.endif
463
464# Yes, the flags are redundant.
465LIB32WMAKEENV+=	MAKEOBJDIRPREFIX=${LIB32_OBJTREE} \
466		_LDSCRIPTROOT=${LIB32TMP} \
467		INSTALL="sh ${.CURDIR}/tools/install.sh" \
468		PATH=${TMPPATH} \
469		LIBDIR=/usr/lib32 \
470		SHLIBDIR=/usr/lib32 \
471		DTRACE="${DTRACE} -32"
472LIB32WMAKEFLAGS+= CC="${XCC} ${LIB32FLAGS}" \
473		CXX="${XCXX} ${LIB32FLAGS}" \
474		DESTDIR=${LIB32TMP} \
475		-DCOMPAT_32BIT \
476		-DLIBRARIES_ONLY \
477		-DNO_CPU_CFLAGS \
478		MK_CTF=no \
479		-DNO_LINT \
480		MK_TESTS=no
481
482LIB32WMAKE=	${LIB32WMAKEENV} ${MAKE} ${LIB32WMAKEFLAGS} \
483		MK_MAN=no MK_HTML=no
484LIB32IMAKE=	${LIB32WMAKE:NINSTALL=*:NDESTDIR=*:N_LDSCRIPTROOT=*} \
485		MK_TOOLCHAIN=no ${IMAKE_INSTALL}
486.endif
487
488IMAKEENV=	${CROSSENV:N_LDSCRIPTROOT=*}
489IMAKE=		${IMAKEENV} ${MAKE} -f Makefile.inc1 \
490		${IMAKE_INSTALL} ${IMAKE_MTREE}
491.if empty(.MAKEFLAGS:M-n)
492IMAKEENV+=	PATH=${STRICTTMPPATH}:${INSTALLTMP} \
493		LD_LIBRARY_PATH=${INSTALLTMP} \
494		PATH_LOCALE=${INSTALLTMP}/locale
495IMAKE+=		__MAKE_SHELL=${INSTALLTMP}/sh
496.else
497IMAKEENV+=	PATH=${TMPPATH}:${INSTALLTMP}
498.endif
499.if defined(DB_FROM_SRC)
500INSTALLFLAGS+=	-N ${.CURDIR}/etc
501MTREEFLAGS+=	-N ${.CURDIR}/etc
502.endif
503_INSTALL_DDIR=	${DESTDIR}/${DISTDIR}
504INSTALL_DDIR=	${_INSTALL_DDIR:S://:/:g:C:/$::}
505.if defined(NO_ROOT)
506METALOG?=	${DESTDIR}/${DISTDIR}/METALOG
507IMAKE+=		-DNO_ROOT METALOG=${METALOG}
508INSTALLFLAGS+=	-U -M ${METALOG} -D ${INSTALL_DDIR}
509MTREEFLAGS+=	-W
510.endif
511.if defined(DB_FROM_SRC) || defined(NO_ROOT)
512IMAKE_INSTALL=	INSTALL="install ${INSTALLFLAGS}"
513IMAKE_MTREE=	MTREE_CMD="mtree ${MTREEFLAGS}"
514.endif
515
516# kernel stage
517KMAKEENV=	${WMAKEENV}
518KMAKE=		${KMAKEENV} ${MAKE} ${.MAKEFLAGS} ${KERNEL_FLAGS} KERNEL=${INSTKERNNAME}
519
520#
521# buildworld
522#
523# Attempt to rebuild the entire system, with reasonable chance of
524# success, regardless of how old your existing system is.
525#
526_worldtmp: .PHONY
527.if ${.CURDIR:C/[^,]//g} != ""
528#	The m4 build of sendmail files doesn't like it if ',' is used
529#	anywhere in the path of it's files.
530	@echo
531	@echo "*** Error: path to source tree contains a comma ','"
532	@echo
533	false
534.endif
535	@echo
536	@echo "--------------------------------------------------------------"
537	@echo ">>> Rebuilding the temporary build tree"
538	@echo "--------------------------------------------------------------"
539.if !defined(NO_CLEAN)
540	rm -rf ${WORLDTMP}
541.if defined(LIB32TMP)
542	rm -rf ${LIB32TMP}
543.endif
544.else
545	rm -rf ${WORLDTMP}/legacy/usr/include
546#	XXX - These three can depend on any header file.
547	rm -f ${OBJTREE}${.CURDIR}/usr.bin/kdump/ioctl.c
548	rm -f ${OBJTREE}${.CURDIR}/usr.bin/kdump/kdump_subr.c
549	rm -f ${OBJTREE}${.CURDIR}/usr.bin/truss/ioctl.c
550.endif
551.for _dir in \
552    lib usr legacy/bin legacy/usr
553	mkdir -p ${WORLDTMP}/${_dir}
554.endfor
555	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
556	    -p ${WORLDTMP}/legacy/usr >/dev/null
557.if ${MK_GROFF} != "no"
558	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.groff.dist \
559	    -p ${WORLDTMP}/legacy/usr >/dev/null
560.endif
561	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
562	    -p ${WORLDTMP}/usr >/dev/null
563	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
564	    -p ${WORLDTMP}/usr/include >/dev/null
565	ln -sf ${.CURDIR}/sys ${WORLDTMP}
566.if ${MK_DEBUG_FILES} != "no"
567	# We could instead disable debug files for these build stages
568	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
569	    -p ${WORLDTMP}/legacy/usr/lib >/dev/null
570	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
571	    -p ${WORLDTMP}/usr/lib >/dev/null
572.endif
573.if ${MK_LIB32} != "no"
574	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
575	    -p ${WORLDTMP}/usr >/dev/null
576.if ${MK_DEBUG_FILES} != "no"
577	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
578	    -p ${WORLDTMP}/legacy/usr/lib/debug/usr >/dev/null
579	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
580	    -p ${WORLDTMP}/usr/lib/debug/usr >/dev/null
581.endif
582.endif
583.if ${MK_TESTS} != "no"
584	mkdir -p ${WORLDTMP}${TESTSBASE}
585	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
586	    -p ${WORLDTMP}${TESTSBASE} >/dev/null
587.if ${MK_DEBUG_FILES} != "no"
588	mkdir -p ${WORLDTMP}/usr/lib/debug/${TESTSBASE}
589	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
590	    -p ${WORLDTMP}/usr/lib/debug/${TESTSBASE} >/dev/null
591.endif
592.endif
593.for _mtree in ${LOCAL_MTREE}
594	mtree -deU -f ${.CURDIR}/${_mtree} -p ${WORLDTMP} > /dev/null
595.endfor
596_legacy:
597	@echo
598	@echo "--------------------------------------------------------------"
599	@echo ">>> stage 1.1: legacy release compatibility shims"
600	@echo "--------------------------------------------------------------"
601	${_+_}cd ${.CURDIR}; ${BMAKE} legacy
602_bootstrap-tools:
603	@echo
604	@echo "--------------------------------------------------------------"
605	@echo ">>> stage 1.2: bootstrap tools"
606	@echo "--------------------------------------------------------------"
607	${_+_}cd ${.CURDIR}; ${BMAKE} bootstrap-tools
608_cleanobj:
609.if !defined(NO_CLEAN)
610	@echo
611	@echo "--------------------------------------------------------------"
612	@echo ">>> stage 2.1: cleaning up the object tree"
613	@echo "--------------------------------------------------------------"
614	${_+_}cd ${.CURDIR}; ${WMAKE} ${CLEANDIR}
615.if defined(LIB32TMP)
616	${_+_}cd ${.CURDIR}; ${LIB32WMAKE} -f Makefile.inc1 ${CLEANDIR}
617.endif
618.endif
619_obj:
620	@echo
621	@echo "--------------------------------------------------------------"
622	@echo ">>> stage 2.2: rebuilding the object tree"
623	@echo "--------------------------------------------------------------"
624	${_+_}cd ${.CURDIR}; ${WMAKE} obj
625_build-tools:
626	@echo
627	@echo "--------------------------------------------------------------"
628	@echo ">>> stage 2.3: build tools"
629	@echo "--------------------------------------------------------------"
630	${_+_}cd ${.CURDIR}; ${TMAKE} build-tools
631_cross-tools:
632	@echo
633	@echo "--------------------------------------------------------------"
634	@echo ">>> stage 3: cross tools"
635	@echo "--------------------------------------------------------------"
636	${_+_}cd ${.CURDIR}; ${XMAKE} cross-tools
637	${_+_}cd ${.CURDIR}; ${XMAKE} kernel-tools
638_includes:
639	@echo
640	@echo "--------------------------------------------------------------"
641	@echo ">>> stage 4.1: building includes"
642	@echo "--------------------------------------------------------------"
643# Special handling for SUBDIR_OVERRIDE in buildworld as they most likely need
644# headers from default SUBDIR.  Do SUBDIR_OVERRIDE includes last.
645	${_+_}cd ${.CURDIR}; ${WMAKE} SUBDIR_OVERRIDE= SHARED=symlinks \
646	    includes
647.if !empty(SUBDIR_OVERRIDE) && make(buildworld)
648	${_+_}cd ${.CURDIR}; ${WMAKE} SHARED=symlinks includes
649.endif
650_libraries:
651	@echo
652	@echo "--------------------------------------------------------------"
653	@echo ">>> stage 4.2: building libraries"
654	@echo "--------------------------------------------------------------"
655	${_+_}cd ${.CURDIR}; \
656	    ${WMAKE} -DNO_FSCHG MK_HTML=no -DNO_LINT MK_MAN=no \
657	    MK_PROFILE=no MK_TESTS=no MK_TESTS_SUPPORT=${MK_TESTS} libraries
658_depend:
659	@echo
660	@echo "--------------------------------------------------------------"
661	@echo ">>> stage 4.3: make dependencies"
662	@echo "--------------------------------------------------------------"
663	${_+_}cd ${.CURDIR}; ${WMAKE} depend
664everything:
665	@echo
666	@echo "--------------------------------------------------------------"
667	@echo ">>> stage 4.4: building everything"
668	@echo "--------------------------------------------------------------"
669	${_+_}cd ${.CURDIR}; _PARALLEL_SUBDIR_OK=1 ${WMAKE} all
670.if defined(LIB32TMP)
671build32: .PHONY
672	@echo
673	@echo "--------------------------------------------------------------"
674	@echo ">>> stage 5.1: building 32 bit shim libraries"
675	@echo "--------------------------------------------------------------"
676	mkdir -p ${LIB32TMP}/usr/include
677	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
678	    -p ${LIB32TMP}/usr >/dev/null
679	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
680	    -p ${LIB32TMP}/usr/include >/dev/null
681	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
682	    -p ${LIB32TMP}/usr >/dev/null
683.if ${MK_DEBUG_FILES} != "no"
684	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
685	    -p ${LIB32TMP}/usr/lib >/dev/null
686	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
687	    -p ${LIB32TMP}/usr/lib/debug/usr >/dev/null
688.endif
689	mkdir -p ${WORLDTMP}
690	ln -sf ${.CURDIR}/sys ${WORLDTMP}
691.for _t in obj includes
692	${_+_}cd ${.CURDIR}/include; ${LIB32WMAKE} DIRPRFX=include/ ${_t}
693	${_+_}cd ${.CURDIR}/lib; ${LIB32WMAKE} DIRPRFX=lib/ ${_t}
694.if ${MK_CDDL} != "no"
695	${_+_}cd ${.CURDIR}/cddl/lib; ${LIB32WMAKE} DIRPRFX=cddl/lib/ ${_t}
696.endif
697	${_+_}cd ${.CURDIR}/gnu/lib; ${LIB32WMAKE} DIRPRFX=gnu/lib/ ${_t}
698.if ${MK_CRYPT} != "no"
699	${_+_}cd ${.CURDIR}/secure/lib; ${LIB32WMAKE} DIRPRFX=secure/lib/ ${_t}
700.endif
701.if ${MK_KERBEROS} != "no"
702	${_+_}cd ${.CURDIR}/kerberos5/lib; ${LIB32WMAKE} DIRPRFX=kerberos5/lib ${_t}
703.endif
704.endfor
705.for _dir in usr.bin/lex/lib
706	${_+_}cd ${.CURDIR}/${_dir}; ${LIB32WMAKE} DIRPRFX=${_dir}/ obj
707.endfor
708.for _dir in lib/ncurses/ncurses lib/ncurses/ncursesw lib/libmagic
709	${_+_}cd ${.CURDIR}/${_dir}; \
710	    WORLDTMP=${WORLDTMP} \
711	    MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}" \
712	    MAKEOBJDIRPREFIX=${LIB32_OBJTREE} ${MAKE} SSP_CFLAGS= DESTDIR= \
713	    DIRPRFX=${_dir}/ -DNO_LINT -DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
714	    build-tools
715.endfor
716	${_+_}cd ${.CURDIR}; \
717	    ${LIB32WMAKE} -f Makefile.inc1 -DNO_FSCHG libraries
718.for _t in obj depend all
719	${_+_}cd ${.CURDIR}/libexec/rtld-elf; PROG=ld-elf32.so.1 ${LIB32WMAKE} \
720	    -DNO_FSCHG DIRPRFX=libexec/rtld-elf/ ${_t}
721	${_+_}cd ${.CURDIR}/usr.bin/ldd; PROG=ldd32 ${LIB32WMAKE} \
722	    DIRPRFX=usr.bin/ldd ${_t}
723.endfor
724
725distribute32 install32: .MAKE .PHONY
726	${_+_}cd ${.CURDIR}/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
727.if ${MK_CDDL} != "no"
728	${_+_}cd ${.CURDIR}/cddl/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
729.endif
730	${_+_}cd ${.CURDIR}/gnu/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
731.if ${MK_CRYPT} != "no"
732	${_+_}cd ${.CURDIR}/secure/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
733.endif
734.if ${MK_KERBEROS} != "no"
735	${_+_}cd ${.CURDIR}/kerberos5/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
736.endif
737	${_+_}cd ${.CURDIR}/libexec/rtld-elf; \
738	    PROG=ld-elf32.so.1 ${LIB32IMAKE} ${.TARGET:S/32$//}
739	${_+_}cd ${.CURDIR}/usr.bin/ldd; PROG=ldd32 ${LIB32IMAKE} \
740	    ${.TARGET:S/32$//}
741.endif
742
743WMAKE_TGTS=
744WMAKE_TGTS+=	_worldtmp _legacy
745.if empty(SUBDIR_OVERRIDE)
746WMAKE_TGTS+=	_bootstrap-tools
747.endif
748WMAKE_TGTS+=	_cleanobj _obj _build-tools _cross-tools
749WMAKE_TGTS+=	_includes _libraries _depend everything
750.if defined(LIB32TMP) && ${MK_LIB32} != "no" && empty(SUBDIR_OVERRIDE)
751WMAKE_TGTS+=	build32
752.endif
753
754buildworld: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
755.ORDER: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
756
757buildworld_prologue:
758	@echo "--------------------------------------------------------------"
759	@echo ">>> World build started on `LC_ALL=C date`"
760	@echo "--------------------------------------------------------------"
761
762buildworld_epilogue:
763	@echo
764	@echo "--------------------------------------------------------------"
765	@echo ">>> World build completed on `LC_ALL=C date`"
766	@echo "--------------------------------------------------------------"
767
768#
769# We need to have this as a target because the indirection between Makefile
770# and Makefile.inc1 causes the correct PATH to be used, rather than a
771# modification of the current environment's PATH.  In addition, we need
772# to quote multiword values.
773#
774buildenvvars: .PHONY
775	@echo ${WMAKEENV:Q} ${.MAKE.EXPORTED:@v@$v=\"${$v}\"@}
776
777.if ${.TARGETS:Mbuildenv}
778.if ${.MAKEFLAGS:M-j}
779.error The buildenv target is incompatible with -j
780.endif
781.endif
782BUILDENV_DIR?=	${.CURDIR}
783buildenv: .PHONY
784	@echo Entering world for ${TARGET_ARCH}:${TARGET}
785.if ${BUILDENV_SHELL:M*zsh*}
786	@echo For ZSH you must run: export CPUTYPE=${TARGET_CPUTYPE}
787.endif
788	@cd ${BUILDENV_DIR} && env ${WMAKEENV} BUILDENV=1 ${BUILDENV_SHELL} \
789	    || true
790
791TOOLCHAIN_TGTS=	${WMAKE_TGTS:N_depend:Neverything:Nbuild32}
792toolchain: ${TOOLCHAIN_TGTS}
793kernel-toolchain: ${TOOLCHAIN_TGTS:N_includes:N_libraries}
794
795#
796# installcheck
797#
798# Checks to be sure system is ready for installworld/installkernel.
799#
800installcheck: _installcheck_world _installcheck_kernel
801_installcheck_world:
802_installcheck_kernel:
803
804#
805# Require DESTDIR to be set if installing for a different architecture or
806# using the user/group database in the source tree.
807#
808.if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${TARGET} != ${MACHINE} || \
809    defined(DB_FROM_SRC)
810.if !make(distributeworld)
811_installcheck_world: __installcheck_DESTDIR
812_installcheck_kernel: __installcheck_DESTDIR
813__installcheck_DESTDIR:
814.if !defined(DESTDIR) || empty(DESTDIR)
815	@echo "ERROR: Please set DESTDIR!"; \
816	false
817.endif
818.endif
819.endif
820
821.if !defined(DB_FROM_SRC)
822#
823# Check for missing UIDs/GIDs.
824#
825CHECK_UIDS=	auditdistd
826CHECK_GIDS=	audit
827.if ${MK_SENDMAIL} != "no"
828CHECK_UIDS+=	smmsp
829CHECK_GIDS+=	smmsp
830.endif
831.if ${MK_PF} != "no"
832CHECK_UIDS+=	proxy
833CHECK_GIDS+=	proxy authpf
834.endif
835.if ${MK_UNBOUND} != "no"
836CHECK_UIDS+=	unbound
837CHECK_GIDS+=	unbound
838.endif
839_installcheck_world: __installcheck_UGID
840__installcheck_UGID:
841.for uid in ${CHECK_UIDS}
842	@if ! `id -u ${uid} >/dev/null 2>&1`; then \
843		echo "ERROR: Required ${uid} user is missing, see /usr/src/UPDATING."; \
844		false; \
845	fi
846.endfor
847.for gid in ${CHECK_GIDS}
848	@if ! `find / -prune -group ${gid} >/dev/null 2>&1`; then \
849		echo "ERROR: Required ${gid} group is missing, see /usr/src/UPDATING."; \
850		false; \
851	fi
852.endfor
853.endif
854
855#
856# Required install tools to be saved in a scratch dir for safety.
857#
858.if ${MK_ZONEINFO} != "no"
859_zoneinfo=	zic tzsetup
860.endif
861
862ITOOLS=	[ awk cap_mkdb cat chflags chmod chown cmp cp \
863	date echo egrep find grep id install ${_install-info} \
864	ln make mkdir mtree mv pwd_mkdb \
865	rm sed services_mkdb sh strip sysctl test true uname wc ${_zoneinfo} \
866	${LOCAL_ITOOLS}
867
868# Needed for share/man
869.if ${MK_MAN} != "no"
870ITOOLS+=makewhatis
871.endif
872
873#
874# distributeworld
875#
876# Distributes everything compiled by a `buildworld'.
877#
878# installworld
879#
880# Installs everything compiled by a 'buildworld'.
881#
882
883# Non-base distributions produced by the base system
884EXTRA_DISTRIBUTIONS=	doc
885.if defined(LIB32TMP) && ${MK_LIB32} != "no"
886EXTRA_DISTRIBUTIONS+=	lib32
887.endif
888.if ${MK_TESTS} != "no"
889EXTRA_DISTRIBUTIONS+=	tests
890.endif
891
892DEBUG_DISTRIBUTIONS=
893.if ${MK_DEBUG_FILES} != "no"
894DEBUG_DISTRIBUTIONS+=	base ${EXTRA_DISTRIBUTIONS:S,doc,,:S,tests,,}
895.endif
896
897MTREE_MAGIC?=	mtree 2.0
898
899distributeworld installworld: _installcheck_world
900	mkdir -p ${INSTALLTMP}
901	progs=$$(for prog in ${ITOOLS}; do \
902		if progpath=`which $$prog`; then \
903			echo $$progpath; \
904		else \
905			echo "Required tool $$prog not found in PATH." >&2; \
906			exit 1; \
907		fi; \
908	    done); \
909	libs=$$(ldd -f "%o %p\n" -f "%o %p\n" $$progs 2>/dev/null | sort -u | \
910	    while read line; do \
911		set -- $$line; \
912		if [ "$$2 $$3" != "not found" ]; then \
913			echo $$2; \
914		else \
915			echo "Required library $$1 not found." >&2; \
916			exit 1; \
917		fi; \
918	    done); \
919	cp $$libs $$progs ${INSTALLTMP}
920	cp -R $${PATH_LOCALE:-"/usr/share/locale"} ${INSTALLTMP}/locale
921.if defined(NO_ROOT)
922	echo "#${MTREE_MAGIC}" > ${METALOG}
923.endif
924.if make(distributeworld)
925.for dist in ${EXTRA_DISTRIBUTIONS}
926	-mkdir ${DESTDIR}/${DISTDIR}/${dist}
927	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
928	    -p ${DESTDIR}/${DISTDIR}/${dist} >/dev/null
929	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
930	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
931	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
932	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/include >/dev/null
933.if ${MK_DEBUG_FILES} != "no"
934	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
935	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib >/dev/null
936.endif
937.if ${MK_LIB32} != "no"
938	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
939	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
940.if ${MK_DEBUG_FILES} != "no"
941	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
942	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/usr >/dev/null
943.endif
944.endif
945.if ${MK_TESTS} != "no" && ${dist} == "tests"
946	-mkdir -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE}
947	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
948	    -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE} >/dev/null
949.if ${MK_DEBUG_FILES} != "no"
950	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
951	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/${TESTSBASE} >/dev/null
952.endif
953.endif
954.if defined(NO_ROOT)
955	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.root.dist | \
956	    sed -e 's#^\./#./${dist}/#' >> ${METALOG}
957	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.usr.dist | \
958	    sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
959	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.include.dist | \
960	    sed -e 's#^\./#./${dist}/usr/include/#' >> ${METALOG}
961.if ${MK_LIB32} != "no"
962	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.lib32.dist | \
963	    sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
964.endif
965.endif
966.endfor
967	-mkdir ${DESTDIR}/${DISTDIR}/base
968	${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
969	    METALOG=${METALOG} ${IMAKE_INSTALL} ${IMAKE_MTREE} \
970	    DISTBASE=/base DESTDIR=${DESTDIR}/${DISTDIR}/base \
971	    LOCAL_MTREE=${LOCAL_MTREE:Q} distrib-dirs
972.endif
973	${_+_}cd ${.CURDIR}; ${IMAKE} re${.TARGET:S/world$//}; \
974	    ${IMAKEENV} rm -rf ${INSTALLTMP}
975.if make(distributeworld)
976.for dist in ${EXTRA_DISTRIBUTIONS}
977	find ${DESTDIR}/${DISTDIR}/${dist} -mindepth 1 -empty -delete
978.endfor
979.if defined(NO_ROOT)
980.for dist in base ${EXTRA_DISTRIBUTIONS}
981	@# For each file that exists in this dist, print the corresponding
982	@# line from the METALOG.  This relies on the fact that
983	@# a line containing only the filename will sort immediatly before
984	@# the relevant mtree line.
985	cd ${DESTDIR}/${DISTDIR}; \
986	find ./${dist} | sort -u ${METALOG} - | \
987	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
988	${DESTDIR}/${DISTDIR}/${dist}.meta
989.endfor
990.for dist in ${DEBUG_DISTRIBUTIONS}
991	@# For each file that exists in this dist, print the corresponding
992	@# line from the METALOG.  This relies on the fact that
993	@# a line containing only the filename will sort immediatly before
994	@# the relevant mtree line.
995	cd ${DESTDIR}/${DISTDIR}; \
996	find ./${dist}/usr/lib/debug | sort -u ${METALOG} - | \
997	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
998	${DESTDIR}/${DISTDIR}/${dist}.debug.meta
999.endfor
1000.endif
1001.endif
1002
1003packageworld:
1004.for dist in base ${EXTRA_DISTRIBUTIONS}
1005.if defined(NO_ROOT)
1006	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1007	    tar cvf - --exclude usr/lib/debug \
1008	    @${DESTDIR}/${DISTDIR}/${dist}.meta | \
1009	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1010.else
1011	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1012	    tar cvf - --exclude usr/lib/debug . | \
1013	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1014.endif
1015.endfor
1016
1017.for dist in ${DEBUG_DISTRIBUTIONS}
1018. if defined(NO_ROOT)
1019	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1020	    tar cvf - @${DESTDIR}/${DISTDIR}/${dist}.debug.meta | \
1021	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1022. else
1023	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1024	    tar cvLf - usr/lib/debug | \
1025	    ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1026. endif
1027.endfor
1028
1029#
1030# reinstall
1031#
1032# If you have a build server, you can NFS mount the source and obj directories
1033# and do a 'make reinstall' on the *client* to install new binaries from the
1034# most recent server build.
1035#
1036reinstall: .MAKE .PHONY
1037	@echo "--------------------------------------------------------------"
1038	@echo ">>> Making hierarchy"
1039	@echo "--------------------------------------------------------------"
1040	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1041	    LOCAL_MTREE=${LOCAL_MTREE:Q} hierarchy
1042	@echo
1043	@echo "--------------------------------------------------------------"
1044	@echo ">>> Installing everything"
1045	@echo "--------------------------------------------------------------"
1046	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install
1047.if defined(LIB32TMP) && ${MK_LIB32} != "no"
1048	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install32
1049.endif
1050
1051redistribute: .MAKE .PHONY
1052	@echo "--------------------------------------------------------------"
1053	@echo ">>> Distributing everything"
1054	@echo "--------------------------------------------------------------"
1055	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute
1056.if defined(LIB32TMP) && ${MK_LIB32} != "no"
1057	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute32 \
1058	    DISTRIBUTION=lib32
1059.endif
1060
1061distrib-dirs: .MAKE .PHONY
1062	${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1063	    ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
1064
1065distribution: .MAKE .PHONY
1066	${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1067	    ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
1068	${_+_}cd ${.CURDIR}; ${CROSSENV} PATH=${TMPPATH} \
1069		${MAKE} -f Makefile.inc1 ${IMAKE_INSTALL} \
1070		METALOG=${METALOG} installconfig
1071
1072#
1073# buildkernel and installkernel
1074#
1075# Which kernels to build and/or install is specified by setting
1076# KERNCONF. If not defined a GENERIC kernel is built/installed.
1077# Only the existing (depending TARGET) config files are used
1078# for building kernels and only the first of these is designated
1079# as the one being installed.
1080#
1081# Note that we have to use TARGET instead of TARGET_ARCH when
1082# we're in kernel-land. Since only TARGET_ARCH is (expected) to
1083# be set to cross-build, we have to make sure TARGET is set
1084# properly.
1085
1086.if defined(KERNFAST)
1087NO_KERNELCLEAN=	t
1088NO_KERNELCONFIG=	t
1089NO_KERNELDEPEND=	t
1090NO_KERNELOBJ=		t
1091# Shortcut for KERNCONF=Blah -DKERNFAST is now KERNFAST=Blah
1092.if !defined(KERNCONF) && ${KERNFAST} != "1"
1093KERNCONF=${KERNFAST}
1094.endif
1095.endif
1096.if ${TARGET_ARCH} == "powerpc64"
1097KERNCONF?=	GENERIC64
1098.else
1099KERNCONF?=	GENERIC
1100.endif
1101INSTKERNNAME?=	kernel
1102
1103KERNSRCDIR?=	${.CURDIR}/sys
1104KRNLCONFDIR=	${KERNSRCDIR}/${TARGET}/conf
1105KRNLOBJDIR=	${OBJTREE}${KERNSRCDIR}
1106KERNCONFDIR?=	${KRNLCONFDIR}
1107
1108BUILDKERNELS=
1109INSTALLKERNEL=
1110.if defined(NO_INSTALLKERNEL)
1111# All of the BUILDKERNELS loops start at index 1.
1112BUILDKERNELS+= dummy
1113.endif
1114.for _kernel in ${KERNCONF}
1115.if exists(${KERNCONFDIR}/${_kernel})
1116BUILDKERNELS+=	${_kernel}
1117.if empty(INSTALLKERNEL) && !defined(NO_INSTALLKERNEL)
1118INSTALLKERNEL= ${_kernel}
1119.endif
1120.endif
1121.endfor
1122
1123${WMAKE_TGTS:N_worldtmp:Nbuild32} ${.ALLTARGETS:M_*:N_worldtmp}: .MAKE .PHONY
1124
1125#
1126# buildkernel
1127#
1128# Builds all kernels defined by BUILDKERNELS.
1129#
1130buildkernel: .MAKE .PHONY
1131.if empty(BUILDKERNELS:Ndummy)
1132	@echo "ERROR: Missing kernel configuration file(s) (${KERNCONF})."; \
1133	false
1134.endif
1135	@echo
1136.for _kernel in ${BUILDKERNELS:Ndummy}
1137	@echo "--------------------------------------------------------------"
1138	@echo ">>> Kernel build for ${_kernel} started on `LC_ALL=C date`"
1139	@echo "--------------------------------------------------------------"
1140	@echo "===> ${_kernel}"
1141	mkdir -p ${KRNLOBJDIR}
1142.if !defined(NO_KERNELCONFIG)
1143	@echo
1144	@echo "--------------------------------------------------------------"
1145	@echo ">>> stage 1: configuring the kernel"
1146	@echo "--------------------------------------------------------------"
1147	cd ${KRNLCONFDIR}; \
1148		PATH=${TMPPATH} \
1149		    config ${CONFIGARGS} -d ${KRNLOBJDIR}/${_kernel} \
1150			-I '${KERNCONFDIR}' '${KERNCONFDIR}/${_kernel}'
1151.endif
1152.if !defined(NO_CLEAN) && !defined(NO_KERNELCLEAN)
1153	@echo
1154	@echo "--------------------------------------------------------------"
1155	@echo ">>> stage 2.1: cleaning up the object tree"
1156	@echo "--------------------------------------------------------------"
1157	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} ${CLEANDIR}
1158.endif
1159.if !defined(NO_KERNELOBJ)
1160	@echo
1161	@echo "--------------------------------------------------------------"
1162	@echo ">>> stage 2.2: rebuilding the object tree"
1163	@echo "--------------------------------------------------------------"
1164	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} obj
1165.endif
1166	@echo
1167	@echo "--------------------------------------------------------------"
1168	@echo ">>> stage 2.3: build tools"
1169	@echo "--------------------------------------------------------------"
1170	${_+_}cd ${.CURDIR}; ${KTMAKE} kernel-tools
1171.if !defined(NO_KERNELDEPEND)
1172	@echo
1173	@echo "--------------------------------------------------------------"
1174	@echo ">>> stage 3.1: making dependencies"
1175	@echo "--------------------------------------------------------------"
1176	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} depend -DNO_MODULES_OBJ
1177.endif
1178	@echo
1179	@echo "--------------------------------------------------------------"
1180	@echo ">>> stage 3.2: building everything"
1181	@echo "--------------------------------------------------------------"
1182	${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} all -DNO_MODULES_OBJ
1183	@echo "--------------------------------------------------------------"
1184	@echo ">>> Kernel build for ${_kernel} completed on `LC_ALL=C date`"
1185	@echo "--------------------------------------------------------------"
1186.endfor
1187
1188#
1189# installkernel, etc.
1190#
1191# Install the kernel defined by INSTALLKERNEL
1192#
1193installkernel installkernel.debug \
1194reinstallkernel reinstallkernel.debug: _installcheck_kernel
1195.if !defined(NO_INSTALLKERNEL)
1196.if empty(INSTALLKERNEL)
1197	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1198	false
1199.endif
1200	@echo "--------------------------------------------------------------"
1201	@echo ">>> Installing kernel ${INSTALLKERNEL}"
1202	@echo "--------------------------------------------------------------"
1203	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1204	    ${CROSSENV} PATH=${TMPPATH} \
1205	    ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME} ${.TARGET:S/kernel//}
1206.endif
1207.if ${BUILDKERNELS:[#]} > 1
1208.for _kernel in ${BUILDKERNELS:[2..-1]}
1209	@echo "--------------------------------------------------------------"
1210	@echo ">>> Installing kernel ${_kernel}"
1211	@echo "--------------------------------------------------------------"
1212	cd ${KRNLOBJDIR}/${_kernel}; \
1213	    ${CROSSENV} PATH=${TMPPATH} \
1214	    ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME}.${_kernel} ${.TARGET:S/kernel//}
1215.endfor
1216.endif
1217
1218distributekernel distributekernel.debug:
1219.if !defined(NO_INSTALLKERNEL)
1220.if empty(INSTALLKERNEL)
1221	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1222	false
1223.endif
1224	mkdir -p ${DESTDIR}/${DISTDIR}
1225.if defined(NO_ROOT)
1226	echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.premeta
1227.endif
1228	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1229	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.premeta/} \
1230	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} KERNEL=${INSTKERNNAME} \
1231	    DESTDIR=${INSTALL_DDIR}/kernel \
1232	    ${.TARGET:S/distributekernel/install/}
1233.if defined(NO_ROOT)
1234	sed -e 's|^./kernel|.|' ${DESTDIR}/${DISTDIR}/kernel.premeta > \
1235	    ${DESTDIR}/${DISTDIR}/kernel.meta
1236.endif
1237.endif
1238.if ${BUILDKERNELS:[#]} > 1
1239.for _kernel in ${BUILDKERNELS:[2..-1]}
1240.if defined(NO_ROOT)
1241	echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta
1242.endif
1243	cd ${KRNLOBJDIR}/${_kernel}; \
1244	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.${_kernel}.premeta/} \
1245	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} \
1246	    KERNEL=${INSTKERNNAME}.${_kernel} \
1247	    DESTDIR=${INSTALL_DDIR}/kernel.${_kernel} \
1248	    ${.TARGET:S/distributekernel/install/}
1249.if defined(NO_ROOT)
1250	sed -e 's|^./kernel|.|' \
1251	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta > \
1252	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1253.endif
1254.endfor
1255.endif
1256
1257packagekernel:
1258.if defined(NO_ROOT)
1259.if !defined(NO_INSTALLKERNEL)
1260	cd ${DESTDIR}/${DISTDIR}/kernel; \
1261	    tar cvf - @${DESTDIR}/${DISTDIR}/kernel.meta | \
1262	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1263.endif
1264.if ${BUILDKERNELS:[#]} > 1
1265.for _kernel in ${BUILDKERNELS:[2..-1]}
1266	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1267	    tar cvf - @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1268	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1269.endfor
1270.endif
1271.else
1272.if !defined(NO_INSTALLKERNEL)
1273	cd ${DESTDIR}/${DISTDIR}/kernel; \
1274	    tar cvf - . | \
1275	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1276.endif
1277.if ${BUILDKERNELS:[#]} > 1
1278.for _kernel in ${BUILDKERNELS:[2..-1]}
1279	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1280	    tar cvf - . | \
1281	    ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1282.endfor
1283.endif
1284.endif
1285
1286#
1287# doxygen
1288#
1289# Build the API documentation with doxygen
1290#
1291doxygen: .PHONY
1292	@if [ ! -x `/usr/bin/which doxygen` ]; then \
1293		echo "You need doxygen (devel/doxygen) to generate the API documentation of the kernel." | /usr/bin/fmt; \
1294		exit 1; \
1295	fi
1296	${_+_}cd ${.CURDIR}/tools/kerneldoc/subsys; ${MAKE} obj all
1297
1298#
1299# update
1300#
1301# Update the source tree(s), by running svn/svnup to update to the
1302# latest copy.
1303#
1304update:
1305.if (defined(CVS_UPDATE) || defined(SUP_UPDATE)) && !defined(SVN_UPDATE)
1306	@echo "--------------------------------------------------------------"
1307	@echo "CVS_UPDATE and SUP_UPDATE are no longer supported."
1308	@echo "Please see: https://wiki.freebsd.org/CvsIsDeprecated"
1309	@echo "--------------------------------------------------------------"
1310	@exit 1
1311.endif
1312.if defined(SVN_UPDATE)
1313	@echo "--------------------------------------------------------------"
1314	@echo ">>> Updating ${.CURDIR} using Subversion"
1315	@echo "--------------------------------------------------------------"
1316	@(cd ${.CURDIR}; ${SVN} update ${SVNFLAGS})
1317.endif
1318
1319#
1320# ------------------------------------------------------------------------
1321#
1322# From here onwards are utility targets used by the 'make world' and
1323# related targets.  If your 'world' breaks, you may like to try to fix
1324# the problem and manually run the following targets to attempt to
1325# complete the build.  Beware, this is *not* guaranteed to work, you
1326# need to have a pretty good grip on the current state of the system
1327# to attempt to manually finish it.  If in doubt, 'make world' again.
1328#
1329
1330#
1331# legacy: Build compatibility shims for the next three targets. This is a
1332# minimal set of tools and shims necessary to compensate for older systems
1333# which don't have the APIs required by the targets built in bootstrap-tools,
1334# build-tools or cross-tools.
1335#
1336
1337# ELF Tool Chain libraries are needed for ELF tools and dtrace tools.
1338.if ${BOOTSTRAPPING} < 1100006
1339_elftoolchain_libs= lib/libelf lib/libdwarf
1340.endif
1341
1342legacy:
1343.if ${BOOTSTRAPPING} < 800107 && ${BOOTSTRAPPING} != 0
1344	@echo "ERROR: Source upgrades from versions prior to 8.0 are not supported."; \
1345	false
1346.endif
1347.for _tool in tools/build ${_elftoolchain_libs}
1348	${_+_}@${ECHODIR} "===> ${_tool} (obj,includes,depend,all,install)"; \
1349	    cd ${.CURDIR}/${_tool}; \
1350	    ${MAKE} DIRPRFX=${_tool}/ obj; \
1351	    ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy includes; \
1352	    ${MAKE} DIRPRFX=${_tool}/ depend; \
1353	    ${MAKE} DIRPRFX=${_tool}/ all; \
1354	    ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy install
1355.endfor
1356
1357#
1358# bootstrap-tools: Build tools needed for compatibility. These are binaries that
1359# are built to build other binaries in the system. However, the focus of these
1360# binaries is usually quite narrow. Bootstrap tools use the host's compiler and
1361# libraries, augmented by -legacy.
1362#
1363_bt=		_bootstrap-tools
1364
1365.if ${MK_GAMES} != "no"
1366_strfile=	usr.bin/fortune/strfile
1367.endif
1368
1369.if ${MK_GCC} != "no" && ${MK_CXX} != "no"
1370_gperf=		gnu/usr.bin/gperf
1371.endif
1372
1373.if ${MK_GROFF} != "no"
1374_groff=		gnu/usr.bin/groff \
1375		usr.bin/soelim
1376.endif
1377
1378.if ${MK_VT} != "no"
1379_vtfontcvt=	usr.bin/vtfontcvt
1380.endif
1381
1382.if ${BOOTSTRAPPING} < 900002
1383_sed=		usr.bin/sed
1384.endif
1385
1386.if ${BOOTSTRAPPING} < 1000002
1387_libopenbsd=	lib/libopenbsd
1388_m4=		usr.bin/m4
1389
1390${_bt}-usr.bin/m4: ${_bt}-lib/libopenbsd
1391.endif
1392
1393.if ${BOOTSTRAPPING} < 1000026
1394_nmtree=	lib/libnetbsd \
1395		usr.sbin/nmtree
1396
1397${_bt}-usr.sbin/nmtree: ${_bt}-lib/libnetbsd
1398.endif
1399
1400.if ${BOOTSTRAPPING} < 1000027
1401_cat=		bin/cat
1402.endif
1403
1404.if ${BOOTSTRAPPING} < 1000033
1405_lex=		usr.bin/lex
1406
1407${_bt}-usr.bin/lex: ${_bt}-usr.bin/m4
1408.endif
1409
1410# r277259 crunchide: Correct 64-bit section header offset
1411# r281674 crunchide: always include both 32- and 64-bit ELF support
1412# r285986 crunchen: use STRIPBIN rather than STRIP
1413.if ${BOOTSTRAPPING} < 1100078
1414_crunch=	usr.sbin/crunch
1415.endif
1416
1417.if ${BOOTSTRAPPING} >= 900040 && ${BOOTSTRAPPING} < 900041
1418_awk=		usr.bin/awk
1419.endif
1420
1421_yacc=		lib/liby \
1422		usr.bin/yacc
1423
1424${_bt}-usr.bin/yacc: ${_bt}-lib/liby
1425
1426.if ${MK_BSNMP} != "no"
1427_gensnmptree=	usr.sbin/bsnmpd/gensnmptree
1428.endif
1429
1430# We need to build tblgen when we're building clang either as
1431# the bootstrap compiler, or as the part of the normal build.
1432.if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no"
1433_clang_tblgen= \
1434	lib/clang/libllvmsupport \
1435	lib/clang/libllvmtablegen \
1436	usr.bin/clang/tblgen \
1437	usr.bin/clang/clang-tblgen
1438
1439${_bt}-usr.bin/clang/clang-tblgen: ${_bt}-lib/clang/libllvmtablegen ${_bt}-lib/clang/libllvmsupport
1440${_bt}-usr.bin/clang/tblgen: ${_bt}-lib/clang/libllvmtablegen ${_bt}-lib/clang/libllvmsupport
1441.endif
1442
1443# Default to building the GPL DTC, but build the BSDL one if users explicitly
1444# request it.
1445_dtc= usr.bin/dtc
1446.if ${MK_GPL_DTC} != "no"
1447_dtc= gnu/usr.bin/dtc
1448.endif
1449
1450.if ${MK_KERBEROS} != "no"
1451_kerberos5_bootstrap_tools= \
1452	kerberos5/tools/make-roken \
1453	kerberos5/lib/libroken \
1454	kerberos5/lib/libvers \
1455	kerberos5/tools/asn1_compile \
1456	kerberos5/tools/slc \
1457	usr.bin/compile_et
1458
1459.ORDER: ${_kerberos5_bootstrap_tools:C/^/${_bt}-/g}
1460.endif
1461
1462.if ${MK_MANDOCDB} != "no"
1463_libopenbsd?=	lib/libopenbsd
1464_makewhatis=	lib/libsqlite3 \
1465		usr.bin/mandoc
1466${_bt}-usr.bin/mandoc: ${_bt}-lib/libopenbsd ${_bt}-lib/libsqlite3
1467.else
1468_makewhatis=usr.bin/makewhatis
1469.endif
1470
1471bootstrap-tools: .PHONY
1472
1473#	Please document (add comment) why something is in 'bootstrap-tools'.
1474#	Try to bound the building of the bootstrap-tool to just the
1475#	FreeBSD versions that need the tool built at this stage of the build.
1476.for _tool in \
1477    ${_clang_tblgen} \
1478    ${_kerberos5_bootstrap_tools} \
1479    ${_strfile} \
1480    ${_gperf} \
1481    ${_groff} \
1482    ${_dtc} \
1483    ${_awk} \
1484    ${_cat} \
1485    usr.bin/lorder \
1486    ${_libopenbsd} \
1487    ${_makewhatis} \
1488    usr.bin/rpcgen \
1489    ${_sed} \
1490    ${_yacc} \
1491    ${_m4} \
1492    ${_lex} \
1493    usr.bin/xinstall \
1494    ${_gensnmptree} \
1495    usr.sbin/config \
1496    ${_crunch} \
1497    ${_nmtree} \
1498    ${_vtfontcvt} \
1499    usr.bin/localedef
1500${_bt}-${_tool}: .PHONY .MAKE
1501	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1502		cd ${.CURDIR}/${_tool}; \
1503		${MAKE} DIRPRFX=${_tool}/ obj; \
1504		${MAKE} DIRPRFX=${_tool}/ depend; \
1505		${MAKE} DIRPRFX=${_tool}/ all; \
1506		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy install
1507
1508bootstrap-tools: ${_bt}-${_tool}
1509.endfor
1510
1511#
1512# build-tools: Build special purpose build tools
1513#
1514.if !defined(NO_SHARE)
1515_share=	share/syscons/scrnmaps
1516.endif
1517
1518.if ${MK_GCC} != "no"
1519_gcc_tools= gnu/usr.bin/cc/cc_tools
1520.endif
1521
1522.if ${MK_RESCUE} != "no"
1523# rescue includes programs that have build-tools targets
1524_rescue=rescue/rescue
1525.endif
1526
1527.for _tool in \
1528    bin/csh \
1529    bin/sh \
1530    ${LOCAL_TOOL_DIRS} \
1531    lib/ncurses/ncurses \
1532    lib/ncurses/ncursesw \
1533    ${_rescue} \
1534    ${_share} \
1535    usr.bin/awk \
1536    lib/libmagic \
1537    usr.bin/mkesdb_static \
1538    usr.bin/mkcsmapper_static \
1539    usr.bin/vi/catalog
1540build-tools_${_tool}: .PHONY
1541	${_+_}@${ECHODIR} "===> ${_tool} (obj,build-tools)"; \
1542		cd ${.CURDIR}/${_tool}; \
1543		${MAKE} DIRPRFX=${_tool}/ obj; \
1544		${MAKE} DIRPRFX=${_tool}/ build-tools
1545build-tools: build-tools_${_tool}
1546.endfor
1547.for _tool in \
1548    ${_gcc_tools}
1549build-tools_${_tool}: .PHONY
1550	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all)"; \
1551		cd ${.CURDIR}/${_tool}; \
1552		${MAKE} DIRPRFX=${_tool}/ obj; \
1553		${MAKE} DIRPRFX=${_tool}/ depend; \
1554		${MAKE} DIRPRFX=${_tool}/ all
1555build-tools: build-tools_${_tool}
1556.endfor
1557
1558#
1559# kernel-tools: Build kernel-building tools
1560#
1561kernel-tools:
1562	mkdir -p ${MAKEOBJDIRPREFIX}/usr
1563	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1564	    -p ${MAKEOBJDIRPREFIX}/usr >/dev/null
1565
1566#
1567# cross-tools: All the tools needed to build the rest of the system after
1568# we get done with the earlier stages. It is the last set of tools needed
1569# to begin building the target binaries.
1570#
1571.if ${TARGET_ARCH} != ${MACHINE_ARCH}
1572.if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "i386"
1573_btxld=		usr.sbin/btxld
1574.endif
1575.endif
1576
1577# Rebuild ctfconvert and ctfmerge to avoid difficult-to-diagnose failures
1578# resulting from missing bug fixes or ELF Toolchain updates.
1579.if ${MK_CDDL} != "no"
1580_dtrace_tools= cddl/lib/libctf cddl/usr.bin/ctfconvert \
1581    cddl/usr.bin/ctfmerge
1582.endif
1583
1584# If we're given an XAS, don't build binutils.
1585.if ${XAS:M/*} == ""
1586.if ${MK_BINUTILS_BOOTSTRAP} != "no"
1587_binutils=	gnu/usr.bin/binutils
1588.endif
1589.if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
1590_elftctools=	lib/libelftc \
1591		usr.bin/elfcopy \
1592		usr.bin/nm \
1593		usr.bin/size \
1594		usr.bin/strings
1595# These are not required by the build, but can be useful for developers who
1596# cross-build on a FreeBSD 10 host:
1597_elftctools+=	usr.bin/addr2line
1598.endif
1599.elif ${TARGET_ARCH} != ${MACHINE_ARCH} && ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
1600# If cross-building with an external binutils we still need to build strip for
1601# the target (for at least crunchide).
1602_elftctools=	lib/libelftc \
1603		usr.bin/elfcopy
1604.endif
1605
1606# If an full path to an external cross compiler is given, don't build
1607# a cross compiler.
1608.if ${XCC:N${CCACHE_BIN}:M/*} == "" && ${MK_CROSS_COMPILER} != "no"
1609.if ${MK_CLANG_BOOTSTRAP} != "no"
1610_clang=		usr.bin/clang
1611_clang_libs=	lib/clang
1612.endif
1613.if ${MK_GCC_BOOTSTRAP} != "no"
1614_cc=		gnu/usr.bin/cc
1615.endif
1616.endif
1617.if ${MK_USB} != "no"
1618_usb_tools=	sys/boot/usb/tools
1619.endif
1620
1621cross-tools: .MAKE .PHONY
1622.for _tool in \
1623    ${_clang_libs} \
1624    ${_clang} \
1625    ${_binutils} \
1626    ${_elftctools} \
1627    ${_dtrace_tools} \
1628    ${_cc} \
1629    ${_btxld} \
1630    ${_crunchide} \
1631    ${_usb_tools}
1632	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1633		cd ${.CURDIR}/${_tool}; \
1634		${MAKE} DIRPRFX=${_tool}/ obj; \
1635		${MAKE} DIRPRFX=${_tool}/ depend; \
1636		${MAKE} DIRPRFX=${_tool}/ all; \
1637		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX} install
1638.endfor
1639
1640NXBDESTDIR=	${OBJTREE}/nxb-bin
1641NXBENV=		MAKEOBJDIRPREFIX=${OBJTREE}/nxb \
1642		INSTALL="sh ${.CURDIR}/tools/install.sh" \
1643		PATH=${PATH}:${OBJTREE}/gperf_for_gcc/usr/bin
1644NXBMAKE=	${NXBENV} ${MAKE} \
1645		TBLGEN=${NXBDESTDIR}/usr/bin/tblgen \
1646		CLANG_TBLGEN=${NXBDESTDIR}/usr/bin/clang-tblgen \
1647		MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH} \
1648		MK_GDB=no MK_TESTS=no \
1649		SSP_CFLAGS= \
1650		MK_HTML=no NO_LINT=yes MK_MAN=no \
1651		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
1652		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
1653		MK_CLANG_EXTRAS=no MK_CLANG_FULL=no \
1654		MK_LLDB=no
1655
1656# native-xtools is the current target for qemu-user cross builds of ports
1657# via poudriere and the imgact_binmisc kernel module.
1658# For non-clang enabled targets that are still using the in tree gcc
1659# we must build a gperf binary for one instance of its Makefiles.  On
1660# clang-enabled systems, the gperf binary is obsolete.
1661native-xtools: .PHONY
1662.if ${MK_GCC_BOOTSTRAP} != "no"
1663	mkdir -p ${OBJTREE}/gperf_for_gcc/usr/bin
1664	${_+_}@${ECHODIR} "===> ${_gperf} (obj,depend,all,install)"; \
1665	cd ${.CURDIR}/${_gperf}; \
1666	${NXBMAKE} DIRPRFX=${_gperf}/ obj; \
1667	${NXBMAKE} DIRPRFX=${_gperf}/ depend; \
1668	${NXBMAKE} DIRPRFX=${_gperf}/ all; \
1669	${NXBMAKE} DIRPRFX=${_gperf}/ DESTDIR=${OBJTREE}/gperf_for_gcc install
1670.endif
1671	mkdir -p ${NXBDESTDIR}/bin ${NXBDESTDIR}/sbin ${NXBDESTDIR}/usr
1672	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1673	    -p ${NXBDESTDIR}/usr >/dev/null
1674	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1675	    -p ${NXBDESTDIR}/usr/include >/dev/null
1676.if ${MK_DEBUG_FILES} != "no"
1677	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1678	    -p ${NXBDESTDIR}/usr/lib >/dev/null
1679.endif
1680.for _tool in \
1681    bin/cat \
1682    bin/chmod \
1683    bin/cp \
1684    bin/csh \
1685    bin/echo \
1686    bin/expr \
1687    bin/hostname \
1688    bin/ln \
1689    bin/ls \
1690    bin/mkdir \
1691    bin/mv \
1692    bin/ps \
1693    bin/realpath \
1694    bin/rm \
1695    bin/rmdir \
1696    bin/sh \
1697    bin/sleep \
1698    ${_clang_tblgen} \
1699    usr.bin/ar \
1700    ${_binutils} \
1701    ${_elftctools} \
1702    ${_cc} \
1703    ${_gcc_tools} \
1704    ${_clang_libs} \
1705    ${_clang} \
1706    sbin/md5 \
1707    sbin/sysctl \
1708    gnu/usr.bin/diff \
1709    usr.bin/awk \
1710    usr.bin/basename \
1711    usr.bin/bmake \
1712    usr.bin/bzip2 \
1713    usr.bin/cmp \
1714    usr.bin/dirname \
1715    usr.bin/env \
1716    usr.bin/fetch \
1717    usr.bin/find \
1718    usr.bin/grep \
1719    usr.bin/gzip \
1720    usr.bin/id \
1721    usr.bin/lex \
1722    usr.bin/lorder \
1723    usr.bin/mktemp \
1724    usr.bin/mt \
1725    usr.bin/patch \
1726    usr.bin/sed \
1727    usr.bin/sort \
1728    usr.bin/tar \
1729    usr.bin/touch \
1730    usr.bin/tr \
1731    usr.bin/true \
1732    usr.bin/uniq \
1733    usr.bin/unzip \
1734    usr.bin/xargs \
1735    usr.bin/xinstall \
1736    usr.bin/xz \
1737    usr.bin/yacc \
1738    usr.sbin/chown
1739	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1740		cd ${.CURDIR}/${_tool}; \
1741		${NXBMAKE} DIRPRFX=${_tool}/ obj; \
1742		${NXBMAKE} DIRPRFX=${_tool}/ depend; \
1743		${NXBMAKE} DIRPRFX=${_tool}/ all; \
1744		${NXBMAKE} DIRPRFX=${_tool}/ DESTDIR=${NXBDESTDIR} install
1745.endfor
1746
1747#
1748# hierarchy - ensure that all the needed directories are present
1749#
1750hierarchy hier: .MAKE .PHONY
1751	${_+_}cd ${.CURDIR}/etc; ${HMAKE} distrib-dirs
1752
1753#
1754# libraries - build all libraries, and install them under ${DESTDIR}.
1755#
1756# The list of libraries with dependents (${_prebuild_libs}) and their
1757# interdependencies (__L) are built automatically by the
1758# ${.CURDIR}/tools/make_libdeps.sh script.
1759#
1760libraries: .MAKE .PHONY
1761	${_+_}cd ${.CURDIR}; \
1762	    ${MAKE} -f Makefile.inc1 _prereq_libs; \
1763	    ${MAKE} -f Makefile.inc1 _startup_libs; \
1764	    ${MAKE} -f Makefile.inc1 _prebuild_libs; \
1765	    ${MAKE} -f Makefile.inc1 _generic_libs
1766
1767#
1768# static libgcc.a prerequisite for shared libc
1769#
1770_prereq_libs= gnu/lib/libssp/libssp_nonshared gnu/lib/libgcc lib/libcompiler_rt
1771
1772# These dependencies are not automatically generated:
1773#
1774# gnu/lib/csu, gnu/lib/libgcc, lib/csu and lib/libc must be built before
1775# all shared libraries for ELF.
1776#
1777_startup_libs=	gnu/lib/csu
1778_startup_libs+=	lib/csu
1779_startup_libs+=	gnu/lib/libgcc
1780_startup_libs+=	lib/libcompiler_rt
1781_startup_libs+=	lib/libc
1782_startup_libs+=	lib/libc_nonshared
1783.if ${MK_LIBCPLUSPLUS} != "no"
1784_startup_libs+=	lib/libcxxrt
1785.endif
1786
1787gnu/lib/libgcc__L: lib/libc__L
1788gnu/lib/libgcc__L: lib/libc_nonshared__L
1789.if ${MK_LIBCPLUSPLUS} != "no"
1790lib/libcxxrt__L: gnu/lib/libgcc__L
1791.endif
1792
1793_prebuild_libs=	${_kerberos5_lib_libasn1} \
1794		${_kerberos5_lib_libhdb} \
1795		${_kerberos5_lib_libheimbase} \
1796		${_kerberos5_lib_libheimntlm} \
1797		${_libsqlite3} \
1798		${_kerberos5_lib_libheimipcc} \
1799		${_kerberos5_lib_libhx509} ${_kerberos5_lib_libkrb5} \
1800		${_kerberos5_lib_libroken} \
1801		${_kerberos5_lib_libwind} \
1802		lib/libbz2 ${_libcom_err} lib/libcrypt \
1803		lib/libelf lib/libexpat \
1804		lib/libfigpar \
1805		${_lib_libgssapi} \
1806		lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
1807		${_lib_libcapsicum} \
1808		lib/ncurses/ncurses lib/ncurses/ncursesw \
1809		lib/libopie lib/libpam ${_lib_libthr} \
1810		${_lib_libradius} lib/libsbuf lib/libtacplus \
1811		lib/libgeom \
1812		${_cddl_lib_libumem} ${_cddl_lib_libnvpair} \
1813		${_cddl_lib_libuutil} \
1814		${_cddl_lib_libavl} \
1815		${_cddl_lib_libzfs_core} \
1816		${_cddl_lib_libctf} \
1817		lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \
1818		${_secure_lib_libcrypto} ${_lib_libldns} \
1819		${_secure_lib_libssh} ${_secure_lib_libssl} \
1820		gnu/lib/libdialog
1821.if ${MK_GNUCXX} != "no"
1822_prebuild_libs+= gnu/lib/libstdc++ gnu/lib/libsupc++
1823gnu/lib/libstdc++__L: lib/msun__L
1824gnu/lib/libsupc++__L: gnu/lib/libstdc++__L
1825.endif
1826
1827.if ${MK_LIBCPLUSPLUS} != "no"
1828_prebuild_libs+= lib/libc++
1829.endif
1830
1831lib/libgeom__L: lib/libexpat__L
1832lib/libkvm__L: lib/libelf__L
1833
1834.if ${MK_LIBTHR} != "no"
1835_lib_libthr=	lib/libthr
1836.endif
1837
1838.if ${MK_RADIUS_SUPPORT} != "no"
1839_lib_libradius=	lib/libradius
1840.endif
1841
1842.if ${MK_OFED} != "no"
1843_ofed_lib=	contrib/ofed/usr.lib/
1844.endif
1845
1846.if ${MK_CASPER} != "no"
1847_lib_libcapsicum=lib/libcapsicum
1848.endif
1849
1850lib/libcapsicum__L: lib/libnv__L
1851lib/libpjdlog__L: lib/libutil__L
1852lib/liblzma__L: lib/libthr__L
1853
1854_generic_libs=	${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib} usr.bin/lex/lib ${_ofed_lib}
1855.for _DIR in ${LOCAL_LIB_DIRS}
1856.if exists(${.CURDIR}/${_DIR}/Makefile)
1857_generic_libs+= ${_DIR}
1858.endif
1859.endfor
1860
1861lib/libopie__L lib/libtacplus__L: lib/libmd__L
1862
1863.if ${MK_CDDL} != "no"
1864_cddl_lib_libumem= cddl/lib/libumem
1865_cddl_lib_libnvpair= cddl/lib/libnvpair
1866_cddl_lib_libavl= cddl/lib/libavl
1867_cddl_lib_libuutil= cddl/lib/libuutil
1868_cddl_lib_libzfs_core= cddl/lib/libzfs_core
1869_cddl_lib_libctf= cddl/lib/libctf
1870_cddl_lib= cddl/lib
1871cddl/lib/libzfs_core__L: cddl/lib/libnvpair__L
1872cddl/lib/libzfs__L: lib/libgeom__L
1873cddl/lib/libctf__L: lib/libz__L
1874.endif
1875# cddl/lib/libdtrace requires lib/libproc and lib/librtld_db; it's only built
1876# on select architectures though (see cddl/lib/Makefile)
1877.if ${MACHINE_CPUARCH} != "sparc64"
1878_prebuild_libs+=	lib/libproc lib/librtld_db
1879.endif
1880
1881.if ${MK_CRYPT} != "no"
1882.if ${MK_OPENSSL} != "no"
1883_secure_lib_libcrypto= secure/lib/libcrypto
1884_secure_lib_libssl= secure/lib/libssl
1885lib/libradius__L secure/lib/libssl__L: secure/lib/libcrypto__L
1886.if ${MK_LDNS} != "no"
1887_lib_libldns= lib/libldns
1888lib/libldns__L: secure/lib/libcrypto__L
1889.endif
1890.if ${MK_OPENSSH} != "no"
1891_secure_lib_libssh= secure/lib/libssh
1892secure/lib/libssh__L: lib/libz__L secure/lib/libcrypto__L lib/libcrypt__L
1893.if ${MK_LDNS} != "no"
1894secure/lib/libssh__L: lib/libldns__L
1895.endif
1896.if ${MK_KERBEROS_SUPPORT} != "no"
1897secure/lib/libssh__L: lib/libgssapi__L kerberos5/lib/libkrb5__L \
1898    kerberos5/lib/libhx509__L kerberos5/lib/libasn1__L lib/libcom_err__L \
1899    lib/libmd__L kerberos5/lib/libroken__L
1900.endif
1901.endif
1902.endif
1903_secure_lib=	secure/lib
1904.endif
1905
1906.if ${MK_KERBEROS} != "no"
1907kerberos5/lib/libasn1__L: lib/libcom_err__L kerberos5/lib/libroken__L
1908kerberos5/lib/libhdb__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1909    kerberos5/lib/libkrb5__L kerberos5/lib/libroken__L \
1910    kerberos5/lib/libwind__L lib/libsqlite3__L
1911kerberos5/lib/libheimntlm__L: secure/lib/libcrypto__L kerberos5/lib/libkrb5__L \
1912    kerberos5/lib/libroken__L lib/libcom_err__L
1913kerberos5/lib/libhx509__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1914    secure/lib/libcrypto__L kerberos5/lib/libroken__L kerberos5/lib/libwind__L
1915kerberos5/lib/libkrb5__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1916    lib/libcrypt__L secure/lib/libcrypto__L kerberos5/lib/libhx509__L \
1917    kerberos5/lib/libroken__L kerberos5/lib/libwind__L \
1918    kerberos5/lib/libheimbase__L kerberos5/lib/libheimipcc__L
1919kerberos5/lib/libroken__L: lib/libcrypt__L
1920kerberos5/lib/libwind__L: kerberos5/lib/libroken__L lib/libcom_err__L
1921kerberos5/lib/libheimbase__L: lib/libthr__L
1922kerberos5/lib/libheimipcc__L: kerberos5/lib/libroken__L kerberos5/lib/libheimbase__L lib/libthr__L
1923.endif
1924
1925lib/libsqlite3__L: lib/libthr__L
1926
1927.if ${MK_GSSAPI} != "no"
1928_lib_libgssapi=	lib/libgssapi
1929.endif
1930
1931.if ${MK_KERBEROS} != "no"
1932_kerberos5_lib=	kerberos5/lib
1933_kerberos5_lib_libasn1= kerberos5/lib/libasn1
1934_kerberos5_lib_libhdb= kerberos5/lib/libhdb
1935_kerberos5_lib_libheimbase= kerberos5/lib/libheimbase
1936_kerberos5_lib_libkrb5= kerberos5/lib/libkrb5
1937_kerberos5_lib_libhx509= kerberos5/lib/libhx509
1938_kerberos5_lib_libroken= kerberos5/lib/libroken
1939_kerberos5_lib_libheimntlm= kerberos5/lib/libheimntlm
1940_libsqlite3= lib/libsqlite3
1941_kerberos5_lib_libheimipcc= kerberos5/lib/libheimipcc
1942_kerberos5_lib_libwind= kerberos5/lib/libwind
1943_libcom_err= lib/libcom_err
1944.endif
1945
1946.if ${MK_NIS} != "no"
1947_lib_libypclnt=	lib/libypclnt
1948.endif
1949
1950.if ${MK_OPENSSL} == "no"
1951lib/libradius__L: lib/libmd__L
1952.endif
1953
1954lib/libproc__L: \
1955    ${_cddl_lib_libctf:D${_cddl_lib_libctf}__L} lib/libelf__L lib/librtld_db__L lib/libutil__L
1956.if ${MK_CXX} != "no"
1957.if ${MK_LIBCPLUSPLUS} != "no"
1958lib/libproc__L: lib/libcxxrt__L
1959.else # This implies MK_GNUCXX != "no"; see lib/libproc
1960lib/libproc__L: gnu/lib/libsupc++__L
1961.endif
1962.endif
1963
1964gnu/lib/libdialog__L: lib/msun__L lib/ncurses/ncursesw__L
1965
1966.for _lib in ${_prereq_libs}
1967${_lib}__PL: .PHONY .MAKE
1968.if exists(${.CURDIR}/${_lib})
1969	${_+_}@${ECHODIR} "===> ${_lib} (obj,depend,all,install)"; \
1970		cd ${.CURDIR}/${_lib}; \
1971		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; \
1972		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ depend; \
1973		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
1974		    DIRPRFX=${_lib}/ all; \
1975		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
1976		    DIRPRFX=${_lib}/ install
1977.endif
1978.endfor
1979
1980.for _lib in ${_startup_libs} ${_prebuild_libs:Nlib/libpam} ${_generic_libs}
1981${_lib}__L: .PHONY .MAKE
1982.if exists(${.CURDIR}/${_lib})
1983	${_+_}@${ECHODIR} "===> ${_lib} (obj,depend,all,install)"; \
1984		cd ${.CURDIR}/${_lib}; \
1985		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; \
1986		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ depend; \
1987		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ all; \
1988		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ install
1989.endif
1990.endfor
1991
1992# libpam is special: we need to build static PAM modules before
1993# static PAM library, and dynamic PAM library before dynamic PAM
1994# modules.
1995lib/libpam__L: .PHONY .MAKE
1996	${_+_}@${ECHODIR} "===> lib/libpam (obj,depend,all,install)"; \
1997		cd ${.CURDIR}/lib/libpam; \
1998		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ obj; \
1999		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ depend; \
2000		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ \
2001		    -D_NO_LIBPAM_SO_YET all; \
2002		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ \
2003		    -D_NO_LIBPAM_SO_YET install
2004
2005_prereq_libs: ${_prereq_libs:S/$/__PL/}
2006_startup_libs: ${_startup_libs:S/$/__L/}
2007_prebuild_libs: ${_prebuild_libs:S/$/__L/}
2008_generic_libs: ${_generic_libs:S/$/__L/}
2009
2010# Enable SUBDIR_PARALLEL when not calling 'make all', unless called from
2011# 'everything' with _PARALLEL_SUBDIR_OK set.  This is because it is unlikely
2012# that running 'make all' from the top-level, especially with a SUBDIR_OVERRIDE
2013# or LOCAL_DIRS set, will have a reliable build if SUBDIRs are built in
2014# parallel.  This is safe for the world stage of buildworld though since it has
2015# already built libraries in a proper order and installed includes into
2016# WORLDTMP. Special handling is done for SUBDIR ordering for 'install*' to
2017# avoid trashing a system if it crashes mid-install.
2018.if !make(all) || defined(_PARALLEL_SUBDIR_OK)
2019SUBDIR_PARALLEL=
2020.endif
2021
2022.include <bsd.subdir.mk>
2023
2024.if make(check-old) || make(check-old-dirs) || \
2025    make(check-old-files) || make(check-old-libs) || \
2026    make(delete-old) || make(delete-old-dirs) || \
2027    make(delete-old-files) || make(delete-old-libs)
2028
2029#
2030# check for / delete old files section
2031#
2032
2033.include "ObsoleteFiles.inc"
2034
2035OLD_LIBS_MESSAGE="Please be sure no application still uses those libraries, \
2036else you can not start such an application. Consult UPDATING for more \
2037information regarding how to cope with the removal/revision bump of a \
2038specific library."
2039
2040.if !defined(BATCH_DELETE_OLD_FILES)
2041RM_I=-i
2042.else
2043RM_I=-v
2044.endif
2045
2046delete-old-files:
2047	@echo ">>> Removing old files (only deletes safe to delete libs)"
2048# Ask for every old file if the user really wants to remove it.
2049# It's annoying, but better safe than sorry.
2050# NB: We cannot pass the list of OLD_FILES as a parameter because the
2051# argument list will get too long. Using .for/.endfor make "loops" will make
2052# the Makefile parser segfault.
2053	@exec 3<&0; \
2054	cd ${.CURDIR}; \
2055	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2056	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
2057	while read file; do \
2058		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2059			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
2060			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
2061		fi; \
2062		for ext in debug symbols; do \
2063		  if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
2064		      "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2065			  rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
2066			      <&3; \
2067		  fi; \
2068		done; \
2069	done
2070# Remove catpages without corresponding manpages.
2071	@exec 3<&0; \
2072	find ${DESTDIR}/usr/share/man/cat* ! -type d | \
2073	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
2074	while read catpage; do \
2075		read manpage; \
2076		if [ ! -e "$${manpage}" ]; then \
2077			rm ${RM_I} $${catpage} <&3; \
2078	        fi; \
2079	done
2080	@echo ">>> Old files removed"
2081
2082check-old-files:
2083	@echo ">>> Checking for old files"
2084	@cd ${.CURDIR}; \
2085	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2086	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
2087	while read file; do \
2088		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2089		 	echo "${DESTDIR}/$${file}"; \
2090		fi; \
2091		for ext in debug symbols; do \
2092		  if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2093			  echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
2094		  fi; \
2095		done; \
2096	done
2097# Check for catpages without corresponding manpages.
2098	@find ${DESTDIR}/usr/share/man/cat* ! -type d | \
2099	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
2100	while read catpage; do \
2101		read manpage; \
2102		if [ ! -e "$${manpage}" ]; then \
2103			echo $${catpage}; \
2104	        fi; \
2105	done
2106
2107delete-old-libs:
2108	@echo ">>> Removing old libraries"
2109	@echo "${OLD_LIBS_MESSAGE}" | fmt
2110	@exec 3<&0; \
2111	cd ${.CURDIR}; \
2112	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2113	    -V OLD_LIBS | xargs -n1 | \
2114	while read file; do \
2115		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2116			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
2117			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
2118		fi; \
2119		for ext in debug symbols; do \
2120		  if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
2121		      "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2122			  rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
2123			      <&3; \
2124		  fi; \
2125		done; \
2126	done
2127	@echo ">>> Old libraries removed"
2128
2129check-old-libs:
2130	@echo ">>> Checking for old libraries"
2131	@cd ${.CURDIR}; \
2132	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2133	    -V OLD_LIBS | xargs -n1 | \
2134	while read file; do \
2135		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
2136			echo "${DESTDIR}/$${file}"; \
2137		fi; \
2138		for ext in debug symbols; do \
2139		  if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
2140			  echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
2141		  fi; \
2142		done; \
2143	done
2144
2145delete-old-dirs:
2146	@echo ">>> Removing old directories"
2147	@cd ${.CURDIR}; \
2148	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2149	    -V OLD_DIRS | xargs -n1 | sort -r | \
2150	while read dir; do \
2151		if [ -d "${DESTDIR}/$${dir}" ]; then \
2152			rmdir -v "${DESTDIR}/$${dir}" || true; \
2153		elif [ -L "${DESTDIR}/$${dir}" ]; then \
2154			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
2155		fi; \
2156	done
2157	@echo ">>> Old directories removed"
2158
2159check-old-dirs:
2160	@echo ">>> Checking for old directories"
2161	@cd ${.CURDIR}; \
2162	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
2163	    -V OLD_DIRS | xargs -n1 | \
2164	while read dir; do \
2165		if [ -d "${DESTDIR}/$${dir}" ]; then \
2166			echo "${DESTDIR}/$${dir}"; \
2167		elif [ -L "${DESTDIR}/$${dir}" ]; then \
2168			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
2169		fi; \
2170	done
2171
2172delete-old: delete-old-files delete-old-dirs
2173	@echo "To remove old libraries run '${MAKE} delete-old-libs'."
2174
2175check-old: check-old-files check-old-libs check-old-dirs
2176	@echo "To remove old files and directories run '${MAKE} delete-old'."
2177	@echo "To remove old libraries run '${MAKE} delete-old-libs'."
2178
2179.endif
2180
2181#
2182# showconfig - show build configuration.
2183#
2184showconfig:
2185	@(${MAKE} -n -f ${.CURDIR}/sys/conf/kern.opts.mk -V dummy -dg1; \
2186	  ${MAKE} -n -f ${.CURDIR}/share/mk/src.opts.mk -V dummy -dg1) 2>&1 | grep ^MK_ | sort -u
2187
2188.if !empty(KRNLOBJDIR) && !empty(KERNCONF)
2189DTBOUTPUTPATH= ${KRNLOBJDIR}/${KERNCONF}/
2190
2191.if !defined(FDT_DTS_FILE) || empty(FDT_DTS_FILE)
2192.if exists(${KERNCONFDIR}/${KERNCONF})
2193FDT_DTS_FILE!= awk 'BEGIN {FS="="} /^makeoptions[[:space:]]+FDT_DTS_FILE/ {print $$2}' \
2194	'${KERNCONFDIR}/${KERNCONF}' ; echo
2195.endif
2196.endif
2197
2198.endif
2199
2200.if !defined(DTBOUTPUTPATH) || !exists(${DTBOUTPUTPATH})
2201DTBOUTPUTPATH= ${.CURDIR}
2202.endif
2203
2204#
2205# Build 'standalone' Device Tree Blob
2206#
2207builddtb:
2208	@PATH=${TMPPATH} MACHINE=${TARGET} \
2209	${.CURDIR}/sys/tools/fdt/make_dtb.sh ${.CURDIR}/sys \
2210	    "${FDT_DTS_FILE}" ${DTBOUTPUTPATH}
2211
2212###############
2213
2214# cleanworld
2215# In the following, the first 'rm' in a series will usually remove all
2216# files and directories.  If it does not, then there are probably some
2217# files with file flags set, so this unsets them and tries the 'rm' a
2218# second time.  There are situations where this target will be cleaning
2219# some directories via more than one method, but that duplication is
2220# needed to correctly handle all the possible situations.  Removing all
2221# files without file flags set in the first 'rm' instance saves time,
2222# because 'chflags' will need to operate on fewer files afterwards.
2223#
2224# It is expected that BW_CANONICALOBJDIR == the CANONICALOBJDIR as would be
2225# created by bsd.obj.mk, except that we don't want to .include that file
2226# in this makefile.
2227#
2228BW_CANONICALOBJDIR:=${OBJTREE}${.CURDIR}
2229cleanworld: .PHONY
2230.if exists(${BW_CANONICALOBJDIR}/)
2231	-rm -rf ${BW_CANONICALOBJDIR}/*
2232	-chflags -R 0 ${BW_CANONICALOBJDIR}
2233	rm -rf ${BW_CANONICALOBJDIR}/*
2234.endif
2235.if ${.CURDIR} == ${.OBJDIR} || ${.CURDIR}/obj == ${.OBJDIR}
2236	#   To be safe in this case, fall back to a 'make cleandir'
2237	${_+_}@cd ${.CURDIR}; ${MAKE} cleandir
2238.endif
2239
2240.if defined(TARGET) && defined(TARGET_ARCH)
2241
2242.if ${TARGET} == ${MACHINE} && ${TARGET_ARCH} == ${MACHINE_ARCH}
2243XDEV_CPUTYPE?=${CPUTYPE}
2244.else
2245XDEV_CPUTYPE?=${TARGET_CPUTYPE}
2246.endif
2247
2248NOFUN=-DNO_FSCHG MK_HTML=no -DNO_LINT \
2249	MK_MAN=no MK_NLS=no MK_PROFILE=no \
2250	MK_KERBEROS=no MK_RESCUE=no MK_TESTS=no MK_WARNS=no \
2251	TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
2252	CPUTYPE=${XDEV_CPUTYPE}
2253
2254XDDIR=${TARGET_ARCH}-freebsd
2255XDTP?=/usr/${XDDIR}
2256.if ${XDTP:N/*}
2257.error XDTP variable should be an absolute path
2258.endif
2259
2260CDBENV=MAKEOBJDIRPREFIX=${MAKEOBJDIRPREFIX}/${XDDIR} \
2261	INSTALL="sh ${.CURDIR}/tools/install.sh"
2262CDENV= ${CDBENV} \
2263	TOOLS_PREFIX=${XDTP}
2264CD2CFLAGS=-isystem ${XDDESTDIR}/usr/include -L${XDDESTDIR}/usr/lib \
2265	--sysroot=${XDDESTDIR}/ -B${XDDESTDIR}/usr/libexec \
2266	-B${XDDESTDIR}/usr/bin -B${XDDESTDIR}/usr/lib
2267CD2ENV=${CDENV} CC="${CC} ${CD2CFLAGS}" CXX="${CXX} ${CD2CFLAGS}" \
2268	CPP="${CPP} ${CD2CFLAGS}" \
2269	MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH}
2270
2271CDTMP=	${MAKEOBJDIRPREFIX}/${XDDIR}/${.CURDIR}/tmp
2272CDMAKE=${CDENV} PATH=${CDTMP}/usr/bin:${PATH} ${MAKE} ${NOFUN}
2273CD2MAKE=${CD2ENV} PATH=${CDTMP}/usr/bin:${XDDESTDIR}/usr/bin:${PATH} ${MAKE} ${NOFUN}
2274XDDESTDIR=${DESTDIR}/${XDTP}
2275.if !defined(OSREL)
2276OSREL!= uname -r | sed -e 's/[-(].*//'
2277.endif
2278
2279.ORDER: xdev-build xdev-install xdev-links
2280xdev: xdev-build xdev-install
2281
2282.ORDER: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
2283xdev-build: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
2284
2285_xb-worldtmp: .PHONY
2286	mkdir -p ${CDTMP}/usr
2287	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2288	    -p ${CDTMP}/usr >/dev/null
2289
2290_xb-bootstrap-tools: .PHONY
2291.for _tool in \
2292    ${_clang_tblgen} \
2293    ${_gperf}
2294	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
2295	cd ${.CURDIR}/${_tool}; \
2296	${CDMAKE} DIRPRFX=${_tool}/ obj; \
2297	${CDMAKE} DIRPRFX=${_tool}/ depend; \
2298	${CDMAKE} DIRPRFX=${_tool}/ all; \
2299	${CDMAKE} DIRPRFX=${_tool}/ DESTDIR=${CDTMP} install
2300.endfor
2301
2302_xb-build-tools: .PHONY
2303	${_+_}@cd ${.CURDIR}; \
2304	${CDBENV} ${MAKE} -f Makefile.inc1 ${NOFUN} build-tools
2305
2306_xb-cross-tools: .PHONY
2307.for _tool in \
2308    ${_binutils} \
2309    ${_elftctools} \
2310    usr.bin/ar \
2311    ${_clang_libs} \
2312    ${_clang} \
2313    ${_cc}
2314	${_+_}@${ECHODIR} "===> xdev ${_tool} (obj,depend,all)"; \
2315	cd ${.CURDIR}/${_tool}; \
2316	${CDMAKE} DIRPRFX=${_tool}/ obj; \
2317	${CDMAKE} DIRPRFX=${_tool}/ depend; \
2318	${CDMAKE} DIRPRFX=${_tool}/ all
2319.endfor
2320
2321_xi-mtree: .PHONY
2322	${_+_}@${ECHODIR} "mtree populating ${XDDESTDIR}"
2323	mkdir -p ${XDDESTDIR}
2324	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
2325	    -p ${XDDESTDIR} >/dev/null
2326	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2327	    -p ${XDDESTDIR}/usr >/dev/null
2328	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
2329	    -p ${XDDESTDIR}/usr/include >/dev/null
2330.if ${MK_LIB32} != "no"
2331	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.lib32.dist \
2332	    -p ${XDDESTDIR}/usr >/dev/null
2333.endif
2334.if ${MK_TESTS} != "no"
2335	mkdir -p ${XDDESTDIR}${TESTSBASE}
2336	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
2337	    -p ${XDDESTDIR}${TESTSBASE} >/dev/null
2338.endif
2339
2340.ORDER: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
2341xdev-install: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
2342
2343_xi-cross-tools: .PHONY
2344	@echo "_xi-cross-tools"
2345.for _tool in \
2346    ${_binutils} \
2347    ${_elftctools} \
2348    usr.bin/ar \
2349    ${_clang_libs} \
2350    ${_clang} \
2351    ${_cc}
2352	${_+_}@${ECHODIR} "===> xdev ${_tool} (install)"; \
2353	cd ${.CURDIR}/${_tool}; \
2354	${CDMAKE} DIRPRFX=${_tool}/ install DESTDIR=${XDDESTDIR}
2355.endfor
2356
2357_xi-includes: .PHONY
2358	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 includes \
2359		DESTDIR=${XDDESTDIR}
2360
2361_xi-libraries: .PHONY
2362	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 libraries \
2363		DESTDIR=${XDDESTDIR}
2364
2365xdev-links: .PHONY
2366	${_+_}cd ${XDDESTDIR}/usr/bin; \
2367	mkdir -p ../../../../usr/bin; \
2368		for i in *; do \
2369			ln -sf ../../${XDTP}/usr/bin/$$i \
2370			    ../../../../usr/bin/${XDDIR}-$$i; \
2371			ln -sf ../../${XDTP}/usr/bin/$$i \
2372			    ../../../../usr/bin/${XDDIR}${OSREL}-$$i; \
2373		done
2374.else
2375xdev xdev-build xdev-install xdev-links:
2376	@echo "*** Error: Both TARGET and TARGET_ARCH must be defined for \"${.TARGET}\" target"
2377.endif
2378