xref: /freebsd/Makefile.inc1 (revision a3cbca537ef1d8ac03a693cd51d98fb8087acc8d)
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:/bin/sh)
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
34#
35# The intended user-driven targets are:
36# buildworld  - rebuild *everything*, including glue to help do upgrades
37# installworld- install everything built by "buildworld"
38# doxygen     - build API documentation of the kernel
39# update      - convenient way to update your source tree (eg: svn/svnup)
40#
41# Standard targets (not defined here) are documented in the makefiles in
42# /usr/share/mk.  These include:
43#		obj depend all install clean cleandepend cleanobj
44
45.if !defined(TARGET) || !defined(TARGET_ARCH)
46.error "Both TARGET and TARGET_ARCH must be defined."
47.endif
48
49.include "share/mk/src.opts.mk"
50.include <bsd.arch.inc.mk>
51.include <bsd.compiler.mk>
52
53# We must do share/info early so that installation of info `dir'
54# entries works correctly.  Do it first since it is less likely to
55# grow dependencies on include and lib than vice versa.
56#
57# We must do lib/ and libexec/ before bin/, because if installworld
58# installs a new /bin/sh, the 'make' command will *immediately*
59# use that new version.  And the new (dynamically-linked) /bin/sh
60# will expect to find appropriate libraries in /lib and /libexec.
61#
62SRCDIR?=	${.CURDIR}
63.if defined(SUBDIR_OVERRIDE)
64SUBDIR=	${SUBDIR_OVERRIDE}
65.else
66SUBDIR=	share/info lib libexec
67SUBDIR+=bin
68.if ${MK_GAMES} != "no"
69SUBDIR+=games
70.endif
71.if ${MK_CDDL} != "no"
72SUBDIR+=cddl
73.endif
74SUBDIR+=gnu include
75.if ${MK_KERBEROS} != "no"
76SUBDIR+=kerberos5
77.endif
78.if ${MK_RESCUE} != "no"
79SUBDIR+=rescue
80.endif
81SUBDIR+=sbin
82.if ${MK_CRYPT} != "no"
83SUBDIR+=secure
84.endif
85.if !defined(NO_SHARE)
86SUBDIR+=share
87.endif
88SUBDIR+=sys usr.bin usr.sbin
89.if ${MK_TESTS} != "no"
90SUBDIR+=	tests
91.endif
92.if ${MK_OFED} != "no"
93SUBDIR+=contrib/ofed
94.endif
95#
96# We must do etc/ last for install/distribute to work.
97#
98SUBDIR+=etc
99
100# These are last, since it is nice to at least get the base system
101# rebuilt before you do them.
102.for _DIR in ${LOCAL_LIB_DIRS} ${LOCAL_DIRS}
103.if exists(${.CURDIR}/${_DIR}/Makefile)
104SUBDIR+= ${_DIR}
105.endif
106.endfor
107.endif
108
109.if defined(NOCLEAN)
110NO_CLEAN=	${NOCLEAN}
111.endif
112.if defined(NO_CLEANDIR)
113CLEANDIR=	clean cleandepend
114.else
115CLEANDIR=	cleandir
116.endif
117
118LOCAL_TOOL_DIRS?=
119
120BUILDENV_SHELL?=/bin/sh
121
122SVN?=		/usr/local/bin/svn
123SVNFLAGS?=	-r HEAD
124
125MAKEOBJDIRPREFIX?=	/usr/obj
126.if !defined(OSRELDATE)
127.if exists(/usr/include/osreldate.h)
128OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
129		/usr/include/osreldate.h
130.else
131OSRELDATE=	0
132.endif
133.endif
134
135.if !defined(VERSION)
136REVISION!=	${MAKE} -C ${SRCDIR}/release -V REVISION
137BRANCH!=	${MAKE} -C ${SRCDIR}/release -V BRANCH
138SRCRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
139		${SRCDIR}/sys/sys/param.h
140VERSION=	FreeBSD ${REVISION}-${BRANCH:C/-p[0-9]+$//} ${TARGET_ARCH} ${SRCRELDATE}
141.endif
142
143KNOWN_ARCHES?=	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
144.if ${TARGET} == ${TARGET_ARCH}
145_t=		${TARGET}
146.else
147_t=		${TARGET_ARCH}/${TARGET}
148.endif
149.for _t in ${_t}
150.if empty(KNOWN_ARCHES:M${_t})
151.error Unknown target ${TARGET_ARCH}:${TARGET}.
152.endif
153.endfor
154
155.if ${TARGET} == ${MACHINE}
156TARGET_CPUTYPE?=${CPUTYPE}
157.else
158TARGET_CPUTYPE?=
159.endif
160
161.if !empty(TARGET_CPUTYPE)
162_TARGET_CPUTYPE=${TARGET_CPUTYPE}
163.else
164_TARGET_CPUTYPE=dummy
165.endif
166_CPUTYPE!=	MAKEFLAGS= CPUTYPE=${_TARGET_CPUTYPE} ${MAKE} \
167		-f /dev/null -m ${.CURDIR}/share/mk -V CPUTYPE
168.if ${_CPUTYPE} != ${_TARGET_CPUTYPE}
169.error CPUTYPE global should be set with ?=.
170.endif
171.if make(buildworld)
172BUILD_ARCH!=	uname -p
173.if ${MACHINE_ARCH} != ${BUILD_ARCH}
174.error To cross-build, set TARGET_ARCH.
175.endif
176.endif
177.if ${MACHINE} == ${TARGET} && ${MACHINE_ARCH} == ${TARGET_ARCH} && !defined(CROSS_BUILD_TESTING)
178OBJTREE=	${MAKEOBJDIRPREFIX}
179.else
180OBJTREE=	${MAKEOBJDIRPREFIX}/${TARGET}.${TARGET_ARCH}
181.endif
182WORLDTMP=	${OBJTREE}${.CURDIR}/tmp
183# /usr/games added for fortune which depend on strfile
184BPATH=		${WORLDTMP}/legacy/usr/sbin:${WORLDTMP}/legacy/usr/bin:${WORLDTMP}/legacy/usr/games:${WORLDTMP}/legacy/bin
185XPATH=		${WORLDTMP}/usr/sbin:${WORLDTMP}/usr/bin:${WORLDTMP}/usr/games
186STRICTTMPPATH=	${BPATH}:${XPATH}
187TMPPATH=	${STRICTTMPPATH}:${PATH}
188
189#
190# Avoid running mktemp(1) unless actually needed.
191# It may not be functional, e.g., due to new ABI
192# when in the middle of installing over this system.
193#
194.if make(distributeworld) || make(installworld)
195INSTALLTMP!=	/usr/bin/mktemp -d -u -t install
196.endif
197
198#
199# Building a world goes through the following stages
200#
201# 1. legacy stage [BMAKE]
202#	This stage is responsible for creating compatibility
203#	shims that are needed by the bootstrap-tools,
204#	build-tools and cross-tools stages.
205# 1. bootstrap-tools stage [BMAKE]
206#	This stage is responsible for creating programs that
207#	are needed for backward compatibility reasons. They
208#	are not built as cross-tools.
209# 2. build-tools stage [TMAKE]
210#	This stage is responsible for creating the object
211#	tree and building any tools that are needed during
212#	the build process.
213# 3. cross-tools stage [XMAKE]
214#	This stage is responsible for creating any tools that
215#	are needed for cross-builds. A cross-compiler is one
216#	of them.
217# 4. world stage [WMAKE]
218#	This stage actually builds the world.
219# 5. install stage (optional) [IMAKE]
220#	This stage installs a previously built world.
221#
222
223BOOTSTRAPPING?=	0
224
225# Common environment for world related stages
226CROSSENV=	MAKEOBJDIRPREFIX=${OBJTREE} \
227		MACHINE_ARCH=${TARGET_ARCH} \
228		MACHINE=${TARGET} \
229		CPUTYPE=${TARGET_CPUTYPE}
230.if ${MK_GROFF} != "no"
231CROSSENV+=	GROFF_BIN_PATH=${WORLDTMP}/legacy/usr/bin \
232		GROFF_FONT_PATH=${WORLDTMP}/legacy/usr/share/groff_font \
233		GROFF_TMAC_PATH=${WORLDTMP}/legacy/usr/share/tmac
234.endif
235.if defined(TARGET_CFLAGS)
236CROSSENV+=	${TARGET_CFLAGS}
237.endif
238
239# bootstrap-tools stage
240BMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
241		PATH=${BPATH}:${PATH} \
242		WORLDTMP=${WORLDTMP} \
243		VERSION="${VERSION}" \
244		MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
245BMAKE=		MAKEOBJDIRPREFIX=${WORLDTMP} \
246		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
247		DESTDIR= \
248		BOOTSTRAPPING=${OSRELDATE} \
249		SSP_CFLAGS= MK_PIE=no \
250		MK_HTML=no MK_INFO=no NO_LINT=yes MK_MAN=no \
251		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
252		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
253		MK_CLANG_FULL=no MK_LLDB=no MK_TESTS=no
254
255# build-tools stage
256TMAKE=		MAKEOBJDIRPREFIX=${OBJTREE} \
257		${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
258		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
259		DESTDIR= \
260		BOOTSTRAPPING=${OSRELDATE} \
261		SSP_CFLAGS= MK_PIE=no \
262		-DNO_LINT \
263		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no MK_CLANG_FULL=no MK_LLDB=no MK_TESTS=no
264
265# cross-tools stage
266XMAKE=		TOOLS_PREFIX=${WORLDTMP} ${BMAKE} \
267		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
268		MK_GDB=no MK_TESTS=no
269
270# kernel-tools stage
271KTMAKEENV=	INSTALL="sh ${.CURDIR}/tools/install.sh" \
272		PATH=${BPATH}:${PATH} \
273		WORLDTMP=${WORLDTMP} \
274		VERSION="${VERSION}"
275KTMAKE=		TOOLS_PREFIX=${WORLDTMP} MAKEOBJDIRPREFIX=${WORLDTMP} \
276		${KTMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
277		DESTDIR= \
278		BOOTSTRAPPING=${OSRELDATE} \
279		SSP_CFLAGS= MK_PIE=no \
280		MK_HTML=no MK_INFO=no -DNO_LINT MK_MAN=no \
281		-DNO_PIC MK_PROFILE=no -DNO_SHARED \
282		-DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no
283
284# world stage
285WMAKEENV=	${CROSSENV} \
286		_SHLIBDIRPREFIX=${WORLDTMP} \
287		_LDSCRIPTROOT= \
288		VERSION="${VERSION}" \
289		INSTALL="sh ${.CURDIR}/tools/install.sh" \
290		PATH=${TMPPATH}
291
292# make hierarchy
293HMAKE=		PATH=${TMPPATH} ${MAKE} LOCAL_MTREE=${LOCAL_MTREE:Q}
294.if defined(NO_ROOT)
295HMAKE+=		PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT
296.endif
297
298.if ${MK_CDDL} == "no"
299WMAKEENV+=	MK_CTF=no
300.endif
301
302.if defined(CROSS_TOOLCHAIN_PREFIX)
303CROSS_COMPILER_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
304CROSS_BINUTILS_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
305.endif
306XCOMPILERS=	CC CXX CPP
307.for COMPILER in ${XCOMPILERS}
308.if defined(CROSS_COMPILER_PREFIX)
309X${COMPILER}?=	${CROSS_COMPILER_PREFIX}${${COMPILER}}
310.else
311X${COMPILER}?=	${${COMPILER}}
312.endif
313.endfor
314XBINUTILS=	AS AR LD NM OBJDUMP RANLIB STRINGS
315.for BINUTIL in ${XBINUTILS}
316.if defined(CROSS_BINUTILS_PREFIX)
317X${BINUTIL}?=	${CROSS_BINUTILS_PREFIX}${${BINUTIL}}
318.else
319X${BINUTIL}?=	${${BINUTIL}}
320.endif
321.endfor
322WMAKEENV+=	CC="${XCC} ${XFLAGS}" CXX="${XCXX} ${XFLAGS}" \
323		CPP="${XCPP} ${XFLAGS}" \
324		AS="${XAS}" AR="${XAR}" LD="${XLD}" NM=${XNM} \
325		OBJDUMP=${XOBJDUMP} RANLIB=${XRANLIB} STRINGS=${XSTRINGS}
326
327.if ${XCC:M/*}
328XFLAGS=		--sysroot=${WORLDTMP}
329.if defined(CROSS_BINUTILS_PREFIX)
330# In the case of xdev-build tools, CROSS_BINUTILS_PREFIX won't be a
331# directory, but the compiler will look in the right place for it's
332# tools so we don't need to tell it where to look.
333.if exists(${CROSS_BINUTILS_PREFIX})
334XFLAGS+=	-B${CROSS_BINUTILS_PREFIX}
335.endif
336.else
337XFLAGS+=	-B${WORLDTMP}/usr/bin
338.endif
339.if ${TARGET} == "arm" && ${MK_ARM_EABI} != "no"
340.if ${TARGET_ARCH:M*eb*} == ""
341TARGET_ABI=	gnueabi
342.elif ${TARGET_ARCH} == "armv6hf"
343TARGET_ABI=	gnueabihf
344.endif
345.endif
346TARGET_ABI?=	unknown
347TARGET_TRIPLE?=	${TARGET_ARCH:C/amd64/x86_64/}-${TARGET_ABI}-freebsd11.0
348XFLAGS+=	-target ${TARGET_TRIPLE}
349.endif
350
351WMAKE=		${WMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 DESTDIR=${WORLDTMP}
352
353.if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "powerpc64"
354# 32 bit world
355LIB32TMP=	${OBJTREE}${.CURDIR}/lib32
356
357.if ${TARGET_ARCH} == "amd64"
358.if empty(TARGET_CPUTYPE)
359LIB32CPUFLAGS=	-march=i686 -mmmx -msse -msse2
360.else
361LIB32CPUFLAGS=	-march=${TARGET_CPUTYPE}
362.endif
363LIB32WMAKEENV=	MACHINE=i386 MACHINE_ARCH=i386 \
364		MACHINE_CPU="i686 mmx sse sse2"
365LIB32WMAKEFLAGS=	\
366		AS="${AS} --32" \
367		LD="${LD} -m elf_i386_fbsd -Y P,${LIB32TMP}/usr/lib32"
368
369.elif ${TARGET_ARCH} == "powerpc64"
370.if empty(TARGET_CPUTYPE)
371LIB32CPUFLAGS=	-mcpu=powerpc
372.else
373LIB32CPUFLAGS=	-mcpu=${TARGET_CPUTYPE}
374.endif
375LIB32WMAKEENV=	MACHINE=powerpc MACHINE_ARCH=powerpc
376LIB32WMAKEFLAGS=	\
377		LD="${LD} -m elf32ppc_fbsd"
378.endif
379
380
381LIB32FLAGS=	-m32 ${LIB32CPUFLAGS} -DCOMPAT_32BIT \
382		-isystem ${LIB32TMP}/usr/include/ \
383		-L${LIB32TMP}/usr/lib32 \
384		-B${LIB32TMP}/usr/lib32
385.if ${XCC:M/*}
386LIB32FLAGS+=		--sysroot=${WORLDTMP}
387.endif
388
389# Yes, the flags are redundant.
390LIB32WMAKEENV+=	MAKEOBJDIRPREFIX=${OBJTREE}/lib32 \
391		_SHLIBDIRPREFIX=${LIB32TMP} \
392		_LDSCRIPTROOT=${LIB32TMP} \
393		VERSION="${VERSION}" \
394		INSTALL="sh ${.CURDIR}/tools/install.sh" \
395		PATH=${TMPPATH} \
396		LIBDIR=/usr/lib32 \
397		SHLIBDIR=/usr/lib32 \
398		LIBPRIVATEDIR=/usr/lib32/private
399LIB32WMAKEFLAGS+= CC="${XCC} ${LIB32FLAGS}" \
400		CXX="${XCXX} ${LIB32FLAGS}" \
401		DESTDIR=${LIB32TMP} \
402		-DCOMPAT_32BIT \
403		-DLIBRARIES_ONLY \
404		-DNO_CPU_CFLAGS \
405		MK_CTF=no \
406		-DNO_LINT \
407		MK_TESTS=no
408
409LIB32WMAKE=	${LIB32WMAKEENV} ${MAKE} ${LIB32WMAKEFLAGS} \
410		MK_MAN=no MK_INFO=no MK_HTML=no
411LIB32IMAKE=	${LIB32WMAKE:NINSTALL=*:NDESTDIR=*:N_LDSCRIPTROOT=*} \
412		MK_TOOLCHAIN=no ${IMAKE_INSTALL}
413.endif
414
415IMAKEENV=	${CROSSENV:N_LDSCRIPTROOT=*}
416IMAKE=		${IMAKEENV} ${MAKE} -f Makefile.inc1 \
417		${IMAKE_INSTALL} ${IMAKE_MTREE}
418.if empty(.MAKEFLAGS:M-n)
419IMAKEENV+=	PATH=${STRICTTMPPATH}:${INSTALLTMP} \
420		LD_LIBRARY_PATH=${INSTALLTMP} \
421		PATH_LOCALE=${INSTALLTMP}/locale
422IMAKE+=		__MAKE_SHELL=${INSTALLTMP}/sh
423.else
424IMAKEENV+=	PATH=${TMPPATH}:${INSTALLTMP}
425.endif
426.if defined(DB_FROM_SRC)
427INSTALLFLAGS+=	-N ${.CURDIR}/etc
428MTREEFLAGS+=	-N ${.CURDIR}/etc
429.endif
430_INSTALL_DDIR=	${DESTDIR}/${DISTDIR}
431INSTALL_DDIR=	${_INSTALL_DDIR:S://:/:g:C:/$::}
432.if defined(NO_ROOT)
433METALOG?=	${DESTDIR}/${DISTDIR}/METALOG
434IMAKE+=		-DNO_ROOT METALOG=${METALOG}
435INSTALLFLAGS+=	-U -M ${METALOG} -D ${INSTALL_DDIR}
436MTREEFLAGS+=	-W
437.endif
438.if defined(DB_FROM_SRC) || defined(NO_ROOT)
439IMAKE_INSTALL=	INSTALL="install ${INSTALLFLAGS}"
440IMAKE_MTREE=	MTREE_CMD="mtree ${MTREEFLAGS}"
441.endif
442
443# kernel stage
444KMAKEENV=	${WMAKEENV}
445KMAKE=		${KMAKEENV} ${MAKE} ${.MAKEFLAGS} ${KERNEL_FLAGS} KERNEL=${INSTKERNNAME}
446
447#
448# buildworld
449#
450# Attempt to rebuild the entire system, with reasonable chance of
451# success, regardless of how old your existing system is.
452#
453_worldtmp:
454.if ${.CURDIR:C/[^,]//g} != ""
455#	The m4 build of sendmail files doesn't like it if ',' is used
456#	anywhere in the path of it's files.
457	@echo
458	@echo "*** Error: path to source tree contains a comma ','"
459	@echo
460	false
461.endif
462	@echo
463	@echo "--------------------------------------------------------------"
464	@echo ">>> Rebuilding the temporary build tree"
465	@echo "--------------------------------------------------------------"
466.if !defined(NO_CLEAN)
467	rm -rf ${WORLDTMP}
468.if defined(LIB32TMP)
469	rm -rf ${LIB32TMP}
470.endif
471.else
472	rm -rf ${WORLDTMP}/legacy/usr/include
473#	XXX - These three can depend on any header file.
474	rm -f ${OBJTREE}${.CURDIR}/usr.bin/kdump/ioctl.c
475	rm -f ${OBJTREE}${.CURDIR}/usr.bin/kdump/kdump_subr.c
476	rm -f ${OBJTREE}${.CURDIR}/usr.bin/truss/ioctl.c
477.endif
478.for _dir in \
479    lib usr legacy/bin legacy/usr
480	mkdir -p ${WORLDTMP}/${_dir}
481.endfor
482	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
483	    -p ${WORLDTMP}/legacy/usr >/dev/null
484.if ${MK_GROFF} != "no"
485	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.groff.dist \
486	    -p ${WORLDTMP}/legacy/usr >/dev/null
487.endif
488	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
489	    -p ${WORLDTMP}/usr >/dev/null
490	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
491	    -p ${WORLDTMP}/usr/include >/dev/null
492	ln -sf ${.CURDIR}/sys ${WORLDTMP}
493.if ${MK_DEBUG_FILES} != "no"
494	# We could instead disable debug files for these build stages
495	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
496	    -p ${WORLDTMP}/legacy/usr/lib >/dev/null
497	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
498	    -p ${WORLDTMP}/usr/lib >/dev/null
499.endif
500.if ${MK_TESTS} != "no"
501	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
502	    -p ${WORLDTMP}/usr >/dev/null
503.endif
504.for _mtree in ${LOCAL_MTREE}
505	mtree -deU -f ${.CURDIR}/${_mtree} -p ${WORLDTMP} > /dev/null
506.endfor
507_legacy:
508	@echo
509	@echo "--------------------------------------------------------------"
510	@echo ">>> stage 1.1: legacy release compatibility shims"
511	@echo "--------------------------------------------------------------"
512	${_+_}cd ${.CURDIR}; ${BMAKE} legacy
513_bootstrap-tools:
514	@echo
515	@echo "--------------------------------------------------------------"
516	@echo ">>> stage 1.2: bootstrap tools"
517	@echo "--------------------------------------------------------------"
518	${_+_}cd ${.CURDIR}; ${BMAKE} bootstrap-tools
519_cleanobj:
520.if !defined(NO_CLEAN)
521	@echo
522	@echo "--------------------------------------------------------------"
523	@echo ">>> stage 2.1: cleaning up the object tree"
524	@echo "--------------------------------------------------------------"
525	${_+_}cd ${.CURDIR}; ${WMAKE} ${CLEANDIR:S/^/par-/}
526.if defined(LIB32TMP)
527	${_+_}cd ${.CURDIR}; ${LIB32WMAKE} -f Makefile.inc1 ${CLEANDIR:S/^/par-/}
528.endif
529.endif
530_obj:
531	@echo
532	@echo "--------------------------------------------------------------"
533	@echo ">>> stage 2.2: rebuilding the object tree"
534	@echo "--------------------------------------------------------------"
535	${_+_}cd ${.CURDIR}; ${WMAKE} par-obj
536_build-tools:
537	@echo
538	@echo "--------------------------------------------------------------"
539	@echo ">>> stage 2.3: build tools"
540	@echo "--------------------------------------------------------------"
541	${_+_}cd ${.CURDIR}; ${TMAKE} build-tools
542_cross-tools:
543	@echo
544	@echo "--------------------------------------------------------------"
545	@echo ">>> stage 3: cross tools"
546	@echo "--------------------------------------------------------------"
547	${_+_}cd ${.CURDIR}; ${XMAKE} cross-tools
548	${_+_}cd ${.CURDIR}; ${XMAKE} kernel-tools
549_includes:
550	@echo
551	@echo "--------------------------------------------------------------"
552	@echo ">>> stage 4.1: building includes"
553	@echo "--------------------------------------------------------------"
554	${_+_}cd ${.CURDIR}; ${WMAKE} SHARED=symlinks par-includes
555_libraries:
556	@echo
557	@echo "--------------------------------------------------------------"
558	@echo ">>> stage 4.2: building libraries"
559	@echo "--------------------------------------------------------------"
560	${_+_}cd ${.CURDIR}; \
561	    ${WMAKE} -DNO_FSCHG MK_HTML=no MK_INFO=no -DNO_LINT MK_MAN=no \
562	    MK_PROFILE=no  MK_TESTS=no libraries
563_depend:
564	@echo
565	@echo "--------------------------------------------------------------"
566	@echo ">>> stage 4.3: make dependencies"
567	@echo "--------------------------------------------------------------"
568	${_+_}cd ${.CURDIR}; ${WMAKE} par-depend
569everything:
570	@echo
571	@echo "--------------------------------------------------------------"
572	@echo ">>> stage 4.4: building everything"
573	@echo "--------------------------------------------------------------"
574	${_+_}cd ${.CURDIR}; ${WMAKE} par-all
575.if defined(LIB32TMP)
576build32:
577	@echo
578	@echo "--------------------------------------------------------------"
579	@echo ">>> stage 5.1: building 32 bit shim libraries"
580	@echo "--------------------------------------------------------------"
581	mkdir -p ${LIB32TMP}/usr/include
582	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
583	    -p ${LIB32TMP}/usr >/dev/null
584	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
585	    -p ${LIB32TMP}/usr/include >/dev/null
586.if ${MK_DEBUG_FILES} != "no"
587	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
588	    -p ${LIB32TMP}/usr/lib >/dev/null
589.endif
590	mkdir -p ${WORLDTMP}
591	ln -sf ${.CURDIR}/sys ${WORLDTMP}
592.for _t in obj includes
593	cd ${.CURDIR}/include; ${LIB32WMAKE} DIRPRFX=include/ ${_t}
594	cd ${.CURDIR}/lib; ${LIB32WMAKE} DIRPRFX=lib/ ${_t}
595.if ${MK_CDDL} != "no"
596	cd ${.CURDIR}/cddl/lib; ${LIB32WMAKE} DIRPRFX=cddl/lib/ ${_t}
597.endif
598	cd ${.CURDIR}/gnu/lib; ${LIB32WMAKE} DIRPRFX=gnu/lib/ ${_t}
599.if ${MK_CRYPT} != "no"
600	cd ${.CURDIR}/secure/lib; ${LIB32WMAKE} DIRPRFX=secure/lib/ ${_t}
601.endif
602.if ${MK_KERBEROS} != "no"
603	cd ${.CURDIR}/kerberos5/lib; ${LIB32WMAKE} DIRPRFX=kerberos5/lib ${_t}
604.endif
605.endfor
606.for _dir in usr.bin/lex/lib
607	cd ${.CURDIR}/${_dir}; ${LIB32WMAKE} DIRPRFX=${_dir}/ obj
608.endfor
609.for _dir in lib/ncurses/ncurses lib/ncurses/ncursesw lib/libmagic
610	cd ${.CURDIR}/${_dir}; \
611	    WORLDTMP=${WORLDTMP} \
612	    MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}" \
613	    MAKEOBJDIRPREFIX=${OBJTREE}/lib32 ${MAKE} SSP_CFLAGS= DESTDIR= \
614	    DIRPRFX=${_dir}/ -DNO_LINT -DNO_CPU_CFLAGS MK_WARNS=no MK_CTF=no \
615	    build-tools
616.endfor
617	cd ${.CURDIR}; \
618	    ${LIB32WMAKE} -f Makefile.inc1 libraries
619.for _t in obj depend all
620	cd ${.CURDIR}/libexec/rtld-elf; PROG=ld-elf32.so.1 ${LIB32WMAKE} \
621	    DIRPRFX=libexec/rtld-elf/ ${_t}
622	cd ${.CURDIR}/usr.bin/ldd; PROG=ldd32 ${LIB32WMAKE} \
623	    DIRPRFX=usr.bin/ldd ${_t}
624.endfor
625
626distribute32 install32:
627	cd ${.CURDIR}/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
628.if ${MK_CDDL} != "no"
629	cd ${.CURDIR}/cddl/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
630.endif
631	cd ${.CURDIR}/gnu/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
632.if ${MK_CRYPT} != "no"
633	cd ${.CURDIR}/secure/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
634.endif
635.if ${MK_KERBEROS} != "no"
636	cd ${.CURDIR}/kerberos5/lib; ${LIB32IMAKE} ${.TARGET:S/32$//}
637.endif
638	cd ${.CURDIR}/libexec/rtld-elf; \
639	    PROG=ld-elf32.so.1 ${LIB32IMAKE} ${.TARGET:S/32$//}
640	cd ${.CURDIR}/usr.bin/ldd; PROG=ldd32 ${LIB32IMAKE} ${.TARGET:S/32$//}
641.endif
642
643WMAKE_TGTS=
644.if !defined(SUBDIR_OVERRIDE)
645WMAKE_TGTS+=	_worldtmp _legacy _bootstrap-tools
646.endif
647WMAKE_TGTS+=	_cleanobj _obj _build-tools
648.if !defined(SUBDIR_OVERRIDE)
649WMAKE_TGTS+=	_cross-tools
650.endif
651WMAKE_TGTS+=	_includes _libraries _depend everything
652.if defined(LIB32TMP) && ${MK_LIB32} != "no"
653WMAKE_TGTS+=	build32
654.endif
655
656buildworld: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
657.ORDER: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
658
659buildworld_prologue:
660	@echo "--------------------------------------------------------------"
661	@echo ">>> World build started on `LC_ALL=C date`"
662	@echo "--------------------------------------------------------------"
663
664buildworld_epilogue:
665	@echo
666	@echo "--------------------------------------------------------------"
667	@echo ">>> World build completed on `LC_ALL=C date`"
668	@echo "--------------------------------------------------------------"
669
670#
671# We need to have this as a target because the indirection between Makefile
672# and Makefile.inc1 causes the correct PATH to be used, rather than a
673# modification of the current environment's PATH.  In addition, we need
674# to quote multiword values.
675#
676buildenvvars:
677	@echo ${WMAKEENV:Q}
678
679buildenv:
680	@echo Entering world for ${TARGET_ARCH}:${TARGET}
681	@cd ${.CURDIR} && env ${WMAKEENV} ${BUILDENV_SHELL} || true
682
683TOOLCHAIN_TGTS=	${WMAKE_TGTS:N_depend:Neverything:Nbuild32}
684toolchain: ${TOOLCHAIN_TGTS}
685kernel-toolchain: ${TOOLCHAIN_TGTS:N_includes:N_libraries}
686
687#
688# installcheck
689#
690# Checks to be sure system is ready for installworld/installkernel.
691#
692installcheck: _installcheck_world _installcheck_kernel
693_installcheck_world:
694_installcheck_kernel:
695
696#
697# Require DESTDIR to be set if installing for a different architecture or
698# using the user/group database in the source tree.
699#
700.if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${TARGET} != ${MACHINE} || \
701    defined(DB_FROM_SRC)
702.if !make(distributeworld)
703_installcheck_world: __installcheck_DESTDIR
704_installcheck_kernel: __installcheck_DESTDIR
705__installcheck_DESTDIR:
706.if !defined(DESTDIR) || empty(DESTDIR)
707	@echo "ERROR: Please set DESTDIR!"; \
708	false
709.endif
710.endif
711.endif
712
713.if !defined(DB_FROM_SRC)
714#
715# Check for missing UIDs/GIDs.
716#
717CHECK_UIDS=	auditdistd
718CHECK_GIDS=	audit
719.if ${MK_SENDMAIL} != "no"
720CHECK_UIDS+=	smmsp
721CHECK_GIDS+=	smmsp
722.endif
723.if ${MK_PF} != "no"
724CHECK_UIDS+=	proxy
725CHECK_GIDS+=	proxy authpf
726.endif
727.if ${MK_UNBOUND} != "no"
728CHECK_UIDS+=	unbound
729CHECK_GIDS+=	unbound
730.endif
731_installcheck_world: __installcheck_UGID
732__installcheck_UGID:
733.for uid in ${CHECK_UIDS}
734	@if ! `id -u ${uid} >/dev/null 2>&1`; then \
735		echo "ERROR: Required ${uid} user is missing, see /usr/src/UPDATING."; \
736		false; \
737	fi
738.endfor
739.for gid in ${CHECK_GIDS}
740	@if ! `find / -prune -group ${gid} >/dev/null 2>&1`; then \
741		echo "ERROR: Required ${gid} group is missing, see /usr/src/UPDATING."; \
742		false; \
743	fi
744.endfor
745.endif
746
747#
748# Required install tools to be saved in a scratch dir for safety.
749#
750.if ${MK_INFO} != "no"
751_install-info=	install-info
752.endif
753.if ${MK_ZONEINFO} != "no"
754_zoneinfo=	zic tzsetup
755.endif
756
757ITOOLS=	[ awk cap_mkdb cat chflags chmod chown \
758	date echo egrep find grep id install ${_install-info} \
759	ln lockf make mkdir mtree mv pwd_mkdb \
760	rm sed services_mkdb sh sysctl test true uname wc ${_zoneinfo} \
761	${LOCAL_ITOOLS}
762
763#
764# distributeworld
765#
766# Distributes everything compiled by a `buildworld'.
767#
768# installworld
769#
770# Installs everything compiled by a 'buildworld'.
771#
772
773# Non-base distributions produced by the base system
774EXTRA_DISTRIBUTIONS=	doc
775.if ${MK_GAMES} != "no"
776EXTRA_DISTRIBUTIONS+=	games
777.endif
778.if defined(LIB32TMP) && ${MK_LIB32} != "no"
779EXTRA_DISTRIBUTIONS+=	lib32
780.endif
781.if ${MK_TESTS} != "no"
782EXTRA_DISTRIBUTIONS+=	tests
783.endif
784
785DEBUG_DISTRIBUTIONS=
786.if ${MK_DEBUG_FILES} != "no"
787DEBUG_DISTRIBUTIONS+=	base ${EXTRA_DISTRIBUTIONS:S,doc,,}
788.endif
789
790MTREE_MAGIC?=	mtree 2.0
791
792distributeworld installworld: _installcheck_world
793	mkdir -p ${INSTALLTMP}
794	progs=$$(for prog in ${ITOOLS}; do \
795		if progpath=`which $$prog`; then \
796			echo $$progpath; \
797		else \
798			echo "Required tool $$prog not found in PATH." >&2; \
799			exit 1; \
800		fi; \
801	    done); \
802	libs=$$(ldd -f "%o %p\n" -f "%o %p\n" $$progs 2>/dev/null | sort -u | \
803	    while read line; do \
804		set -- $$line; \
805		if [ "$$2 $$3" != "not found" ]; then \
806			echo $$2; \
807		else \
808			echo "Required library $$1 not found." >&2; \
809			exit 1; \
810		fi; \
811	    done); \
812	cp $$libs $$progs ${INSTALLTMP}
813	cp -R $${PATH_LOCALE:-"/usr/share/locale"} ${INSTALLTMP}/locale
814.if defined(NO_ROOT)
815	echo "#${MTREE_MAGIC}" > ${METALOG}
816.endif
817.if make(distributeworld)
818.for dist in ${EXTRA_DISTRIBUTIONS}
819	-mkdir ${DESTDIR}/${DISTDIR}/${dist}
820	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
821	    -p ${DESTDIR}/${DISTDIR}/${dist} >/dev/null
822	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
823	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
824	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
825	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/include >/dev/null
826.if ${MK_DEBUG_FILES} != "no"
827	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
828	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib >/dev/null
829.endif
830.if ${MK_TESTS} != "no" && ${dist} == "tests"
831	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
832	    -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
833.endif
834.if defined(NO_ROOT)
835	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.root.dist | \
836	    sed -e 's#^\./#./${dist}/#' >> ${METALOG}
837	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.usr.dist | \
838	    sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
839	${IMAKEENV} mtree -C -f ${.CURDIR}/etc/mtree/BSD.include.dist | \
840	    sed -e 's#^\./#./${dist}/usr/include/#' >> ${METALOG}
841.endif
842.endfor
843	-mkdir ${DESTDIR}/${DISTDIR}/base
844	cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
845	    METALOG=${METALOG} ${IMAKE_INSTALL} ${IMAKE_MTREE} \
846	    DISTBASE=/base DESTDIR=${DESTDIR}/${DISTDIR}/base \
847	    LOCAL_MTREE=${LOCAL_MTREE:Q} distrib-dirs
848.endif
849	${_+_}cd ${.CURDIR}; ${IMAKE} re${.TARGET:S/world$//}; \
850	    ${IMAKEENV} rm -rf ${INSTALLTMP}
851.if make(distributeworld)
852.for dist in ${EXTRA_DISTRIBUTIONS}
853	find ${DESTDIR}/${DISTDIR}/${dist} -mindepth 1 -empty -delete
854.endfor
855.if defined(NO_ROOT)
856.for dist in base ${EXTRA_DISTRIBUTIONS}
857	@# For each file that exists in this dist, print the corresponding
858	@# line from the METALOG.  This relies on the fact that
859	@# a line containing only the filename will sort immediatly before
860	@# the relevant mtree line.
861	cd ${DESTDIR}/${DISTDIR}; \
862	find ./${dist} | sort -u ${METALOG} - | \
863	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
864	${DESTDIR}/${DISTDIR}/${dist}.meta
865.endfor
866.for dist in ${DEBUG_DISTRIBUTIONS}
867	@# For each file that exists in this dist, print the corresponding
868	@# line from the METALOG.  This relies on the fact that
869	@# a line containing only the filename will sort immediatly before
870	@# the relevant mtree line.
871	cd ${DESTDIR}/${DISTDIR}; \
872	find ./${dist}/usr/lib/debug | sort -u ${METALOG} - | \
873	awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
874	${DESTDIR}/${DISTDIR}/${dist}.debug.meta
875.endfor
876.endif
877.endif
878
879packageworld:
880.for dist in base ${EXTRA_DISTRIBUTIONS}
881.if defined(NO_ROOT)
882	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
883	    tar cvJf ${DESTDIR}/${DISTDIR}/${dist}.txz \
884	    --exclude usr/lib/debug \
885	    @${DESTDIR}/${DISTDIR}/${dist}.meta
886.else
887	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
888	    tar cvJf ${DESTDIR}/${DISTDIR}/${dist}.txz \
889	    --exclude usr/lib/debug .
890.endif
891.endfor
892
893.for dist in ${DEBUG_DISTRIBUTIONS}
894. if defined(NO_ROOT)
895	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
896	    tar cvJf ${DESTDIR}/${DISTDIR}/${dist}-dbg.txz \
897	    @${DESTDIR}/${DISTDIR}/${dist}.debug.meta
898. else
899	${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
900	    tar cvJfL ${DESTDIR}/${DISTDIR}/${dist}-dbg.txz \
901	    usr/lib/debug
902. endif
903.endfor
904
905#
906# reinstall
907#
908# If you have a build server, you can NFS mount the source and obj directories
909# and do a 'make reinstall' on the *client* to install new binaries from the
910# most recent server build.
911#
912reinstall:
913	@echo "--------------------------------------------------------------"
914	@echo ">>> Making hierarchy"
915	@echo "--------------------------------------------------------------"
916	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
917	    LOCAL_MTREE=${LOCAL_MTREE:Q} hierarchy
918	@echo
919	@echo "--------------------------------------------------------------"
920	@echo ">>> Installing everything"
921	@echo "--------------------------------------------------------------"
922	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install
923.if defined(LIB32TMP) && ${MK_LIB32} != "no"
924	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install32
925.endif
926
927redistribute:
928	@echo "--------------------------------------------------------------"
929	@echo ">>> Distributing everything"
930	@echo "--------------------------------------------------------------"
931	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute
932.if defined(LIB32TMP) && ${MK_LIB32} != "no"
933	${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute32 \
934	    DISTRIBUTION=lib32
935.endif
936
937distrib-dirs distribution:
938	cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
939	    ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
940
941#
942# buildkernel and installkernel
943#
944# Which kernels to build and/or install is specified by setting
945# KERNCONF. If not defined a GENERIC kernel is built/installed.
946# Only the existing (depending TARGET) config files are used
947# for building kernels and only the first of these is designated
948# as the one being installed.
949#
950# Note that we have to use TARGET instead of TARGET_ARCH when
951# we're in kernel-land. Since only TARGET_ARCH is (expected) to
952# be set to cross-build, we have to make sure TARGET is set
953# properly.
954
955.if defined(KERNFAST)
956NO_KERNELCLEAN=	t
957NO_KERNELCONFIG=	t
958NO_KERNELDEPEND=	t
959NO_KERNELOBJ=		t
960# Shortcut for KERNCONF=Blah -DKERNFAST is now KERNFAST=Blah
961.if !defined(KERNCONF) && ${KERNFAST} != "1"
962KERNCONF=${KERNFAST}
963.endif
964.endif
965.if ${TARGET_ARCH} == "powerpc64"
966KERNCONF?=	GENERIC64
967.else
968KERNCONF?=	GENERIC
969.endif
970INSTKERNNAME?=	kernel
971
972KERNSRCDIR?=	${.CURDIR}/sys
973KRNLCONFDIR=	${KERNSRCDIR}/${TARGET}/conf
974KRNLOBJDIR=	${OBJTREE}${KERNSRCDIR}
975KERNCONFDIR?=	${KRNLCONFDIR}
976
977BUILDKERNELS=
978INSTALLKERNEL=
979.for _kernel in ${KERNCONF}
980.if exists(${KERNCONFDIR}/${_kernel})
981BUILDKERNELS+=	${_kernel}
982.if empty(INSTALLKERNEL)
983INSTALLKERNEL= ${_kernel}
984.endif
985.endif
986.endfor
987
988buildkernel ${WMAKE_TGTS} ${.ALLTARGETS:M_*}: .MAKE
989
990#
991# buildkernel
992#
993# Builds all kernels defined by BUILDKERNELS.
994#
995buildkernel:
996.if empty(BUILDKERNELS)
997	@echo "ERROR: Missing kernel configuration file(s) (${KERNCONF})."; \
998	false
999.endif
1000	@echo
1001.for _kernel in ${BUILDKERNELS}
1002	@echo "--------------------------------------------------------------"
1003	@echo ">>> Kernel build for ${_kernel} started on `LC_ALL=C date`"
1004	@echo "--------------------------------------------------------------"
1005	@echo "===> ${_kernel}"
1006	mkdir -p ${KRNLOBJDIR}
1007.if !defined(NO_KERNELCONFIG)
1008	@echo
1009	@echo "--------------------------------------------------------------"
1010	@echo ">>> stage 1: configuring the kernel"
1011	@echo "--------------------------------------------------------------"
1012	cd ${KRNLCONFDIR}; \
1013		PATH=${TMPPATH} \
1014		    config ${CONFIGARGS} -d ${KRNLOBJDIR}/${_kernel} \
1015			-I '${KERNCONFDIR}' '${KERNCONFDIR}/${_kernel}'
1016.endif
1017.if !defined(NO_CLEAN) && !defined(NO_KERNELCLEAN)
1018	@echo
1019	@echo "--------------------------------------------------------------"
1020	@echo ">>> stage 2.1: cleaning up the object tree"
1021	@echo "--------------------------------------------------------------"
1022	cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} ${CLEANDIR}
1023.endif
1024.if !defined(NO_KERNELOBJ)
1025	@echo
1026	@echo "--------------------------------------------------------------"
1027	@echo ">>> stage 2.2: rebuilding the object tree"
1028	@echo "--------------------------------------------------------------"
1029	cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} obj
1030.endif
1031	@echo
1032	@echo "--------------------------------------------------------------"
1033	@echo ">>> stage 2.3: build tools"
1034	@echo "--------------------------------------------------------------"
1035	${_+_}cd ${.CURDIR}; ${KTMAKE} kernel-tools
1036.if !defined(NO_KERNELDEPEND)
1037	@echo
1038	@echo "--------------------------------------------------------------"
1039	@echo ">>> stage 3.1: making dependencies"
1040	@echo "--------------------------------------------------------------"
1041	cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} depend -DNO_MODULES_OBJ
1042.endif
1043	@echo
1044	@echo "--------------------------------------------------------------"
1045	@echo ">>> stage 3.2: building everything"
1046	@echo "--------------------------------------------------------------"
1047	cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} all -DNO_MODULES_OBJ
1048	@echo "--------------------------------------------------------------"
1049	@echo ">>> Kernel build for ${_kernel} completed on `LC_ALL=C date`"
1050	@echo "--------------------------------------------------------------"
1051.endfor
1052
1053#
1054# installkernel, etc.
1055#
1056# Install the kernel defined by INSTALLKERNEL
1057#
1058installkernel installkernel.debug \
1059reinstallkernel reinstallkernel.debug: _installcheck_kernel
1060.if empty(INSTALLKERNEL)
1061	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1062	false
1063.endif
1064	@echo "--------------------------------------------------------------"
1065	@echo ">>> Installing kernel ${INSTALLKERNEL}"
1066	@echo "--------------------------------------------------------------"
1067	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1068	    ${CROSSENV} PATH=${TMPPATH} \
1069	    ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME} ${.TARGET:S/kernel//}
1070
1071distributekernel distributekernel.debug:
1072.if empty(INSTALLKERNEL)
1073	@echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1074	false
1075.endif
1076	mkdir -p ${DESTDIR}/${DISTDIR}
1077.if defined(NO_ROOT)
1078	echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.premeta
1079.endif
1080	cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1081	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.premeta/} \
1082	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} KERNEL=${INSTKERNNAME} \
1083	    DESTDIR=${INSTALL_DDIR}/kernel \
1084	    ${.TARGET:S/distributekernel/install/}
1085.if defined(NO_ROOT)
1086	sed -e 's|^./kernel|.|' ${DESTDIR}/${DISTDIR}/kernel.premeta > \
1087	    ${DESTDIR}/${DISTDIR}/kernel.meta
1088.endif
1089.for _kernel in ${BUILDKERNELS:S/${INSTALLKERNEL}//}
1090.if defined(NO_ROOT)
1091	echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta
1092.endif
1093	cd ${KRNLOBJDIR}/${_kernel}; \
1094	    ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.${_kernel}.premeta/} \
1095	    ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} \
1096	    KERNEL=${INSTKERNNAME}.${_kernel} \
1097	    DESTDIR=${INSTALL_DDIR}/kernel.${_kernel} \
1098	    ${.TARGET:S/distributekernel/install/}
1099.if defined(NO_ROOT)
1100	sed -e 's|^./kernel|.|' \
1101	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta > \
1102	    ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1103.endif
1104.endfor
1105
1106packagekernel:
1107.if defined(NO_ROOT)
1108	cd ${DESTDIR}/${DISTDIR}/kernel; \
1109	    tar cvJf ${DESTDIR}/${DISTDIR}/kernel.txz \
1110	    @${DESTDIR}/${DISTDIR}/kernel.meta
1111.for _kernel in ${BUILDKERNELS:S/${INSTALLKERNEL}//}
1112	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1113	    tar cvJf ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.txz \
1114	    @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1115.endfor
1116.else
1117	cd ${DESTDIR}/${DISTDIR}/kernel; \
1118	    tar cvJf ${DESTDIR}/${DISTDIR}/kernel.txz .
1119.for _kernel in ${BUILDKERNELS:S/${INSTALLKERNEL}//}
1120	cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1121	    tar cvJf ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.txz .
1122.endfor
1123.endif
1124
1125#
1126# doxygen
1127#
1128# Build the API documentation with doxygen
1129#
1130doxygen:
1131	@if [ ! -x `/usr/bin/which doxygen` ]; then \
1132		echo "You need doxygen (devel/doxygen) to generate the API documentation of the kernel." | /usr/bin/fmt; \
1133		exit 1; \
1134	fi
1135	cd ${.CURDIR}/tools/kerneldoc/subsys && ${MAKE} obj all
1136
1137#
1138# update
1139#
1140# Update the source tree(s), by running svn/svnup to update to the
1141# latest copy.
1142#
1143update:
1144.if (defined(CVS_UPDATE) || defined(SUP_UPDATE)) && !defined(SVN_UPDATE)
1145	@echo "--------------------------------------------------------------"
1146	@echo "CVS_UPDATE and SUP_UPDATE are no longer supported."
1147	@echo "Please see: https://wiki.freebsd.org/CvsIsDeprecated"
1148	@echo "--------------------------------------------------------------"
1149	@exit 1
1150.endif
1151.if defined(SVN_UPDATE)
1152	@echo "--------------------------------------------------------------"
1153	@echo ">>> Updating ${.CURDIR} using Subversion"
1154	@echo "--------------------------------------------------------------"
1155	@(cd ${.CURDIR} && ${SVN} update ${SVNFLAGS})
1156.endif
1157
1158#
1159# ------------------------------------------------------------------------
1160#
1161# From here onwards are utility targets used by the 'make world' and
1162# related targets.  If your 'world' breaks, you may like to try to fix
1163# the problem and manually run the following targets to attempt to
1164# complete the build.  Beware, this is *not* guaranteed to work, you
1165# need to have a pretty good grip on the current state of the system
1166# to attempt to manually finish it.  If in doubt, 'make world' again.
1167#
1168
1169#
1170# legacy: Build compatibility shims for the next three targets
1171#
1172legacy:
1173.if ${BOOTSTRAPPING} < 800107 && ${BOOTSTRAPPING} != 0
1174	@echo "ERROR: Source upgrades from versions prior to 8.0 not supported."; \
1175	false
1176.endif
1177.for _tool in tools/build
1178	${_+_}@${ECHODIR} "===> ${_tool} (obj,includes,depend,all,install)"; \
1179	    cd ${.CURDIR}/${_tool} && \
1180	    ${MAKE} DIRPRFX=${_tool}/ obj && \
1181	    ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy includes && \
1182	    ${MAKE} DIRPRFX=${_tool}/ depend && \
1183	    ${MAKE} DIRPRFX=${_tool}/ all && \
1184	    ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy install
1185.endfor
1186
1187#
1188# bootstrap-tools: Build tools needed for compatibility
1189#
1190.if ${MK_GAMES} != "no"
1191_strfile=	games/fortune/strfile
1192.endif
1193
1194.if ${MK_CXX} != "no"
1195_gperf=		gnu/usr.bin/gperf
1196.endif
1197
1198.if ${MK_GROFF} != "no"
1199_groff=		gnu/usr.bin/groff
1200.endif
1201
1202.if ${MK_VT} != "no"
1203_vtfontcvt=	usr.bin/vtfontcvt
1204.endif
1205
1206.if ${BOOTSTRAPPING} < 900002
1207_sed=		usr.bin/sed
1208.endif
1209
1210.if ${BOOTSTRAPPING} < 900006
1211_lex=		usr.bin/lex
1212.endif
1213
1214.if ${BOOTSTRAPPING} < 1000002
1215_m4=		usr.bin/m4
1216.endif
1217
1218.if ${BOOTSTRAPPING} < 1000013
1219_yacc=		lib/liby \
1220		usr.bin/yacc
1221.endif
1222
1223.if ${BOOTSTRAPPING} < 1000014
1224_crunch=	usr.sbin/crunch
1225.endif
1226
1227.if ${BOOTSTRAPPING} < 1000026
1228_nmtree=	lib/libnetbsd \
1229		usr.sbin/nmtree
1230.endif
1231
1232.if ${BOOTSTRAPPING} < 1000027
1233_cat=		bin/cat
1234.endif
1235
1236.if ${BOOTSTRAPPING} >= 900040 && ${BOOTSTRAPPING} < 900041
1237_awk=		usr.bin/awk
1238.endif
1239
1240.if ${MK_BSNMP} != "no" && !exists(/usr/sbin/gensnmptree)
1241_gensnmptree=	usr.sbin/bsnmpd/gensnmptree
1242.endif
1243
1244# We need to build tlbgen when we're building clang either as
1245# the bootstrap compiler, or as the part of the normal build.
1246.if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no"
1247_clang_tblgen= \
1248	lib/clang/libllvmsupport \
1249	lib/clang/libllvmtablegen \
1250	usr.bin/clang/tblgen \
1251	usr.bin/clang/clang-tblgen
1252.endif
1253
1254# dtrace tools are required for older bootstrap env and cross-build
1255# pre libdwarf
1256.if ${MK_CDDL} != "no" && (${BOOTSTRAPPING} < 1100006 \
1257      || (${MACHINE} != ${TARGET} || ${MACHINE_ARCH} != ${TARGET_ARCH}))
1258_dtrace_tools= cddl/usr.bin/sgsmsg cddl/lib/libctf lib/libelf \
1259    lib/libdwarf cddl/usr.bin/ctfconvert cddl/usr.bin/ctfmerge
1260.endif
1261
1262# Default to building the GPL DTC, but build the BSDL one if users explicitly
1263# request it.
1264_dtc= usr.bin/dtc
1265.if ${MK_GPL_DTC} != "no"
1266_dtc= gnu/usr.bin/dtc
1267.endif
1268
1269.if ${MK_KERBEROS} != "no"
1270_kerberos5_bootstrap_tools= \
1271	kerberos5/tools/make-roken \
1272	kerberos5/lib/libroken \
1273	kerberos5/lib/libvers \
1274	kerberos5/tools/asn1_compile \
1275	kerberos5/tools/slc \
1276	usr.bin/compile_et
1277.endif
1278
1279#	Please document (add comment) why something is in 'bootstrap-tools'.
1280#	Try to bound the building of the bootstrap-tool to just the
1281#	FreeBSD versions that need the tool built at this stage of the build.
1282bootstrap-tools: .MAKE
1283.for _tool in \
1284    ${_clang_tblgen} \
1285    ${_kerberos5_bootstrap_tools} \
1286    ${_dtrace_tools} \
1287    ${_strfile} \
1288    ${_gperf} \
1289    ${_groff} \
1290    ${_dtc} \
1291    ${_awk} \
1292    ${_cat} \
1293    usr.bin/lorder \
1294    usr.bin/makewhatis \
1295    usr.bin/rpcgen \
1296    ${_sed} \
1297    ${_yacc} \
1298    ${_m4} \
1299    ${_lex} \
1300    lib/libmd \
1301    usr.bin/xinstall \
1302    ${_gensnmptree} \
1303    usr.sbin/config \
1304    ${_crunch} \
1305    ${_nmtree} \
1306    ${_vtfontcvt}
1307	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1308		cd ${.CURDIR}/${_tool} && \
1309		${MAKE} DIRPRFX=${_tool}/ obj && \
1310		${MAKE} DIRPRFX=${_tool}/ depend && \
1311		${MAKE} DIRPRFX=${_tool}/ all && \
1312		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX}/legacy install
1313.endfor
1314
1315#
1316# build-tools: Build special purpose build tools
1317#
1318.if !defined(NO_SHARE)
1319_share=	share/syscons/scrnmaps
1320.endif
1321
1322.if ${MK_GCC} != "no"
1323_gcc_tools= gnu/usr.bin/cc/cc_tools
1324.endif
1325
1326.if ${MK_RESCUE} != "no"
1327_rescue= rescue/rescue
1328.endif
1329
1330build-tools: .MAKE
1331.for _tool in \
1332    bin/csh \
1333    bin/sh \
1334    ${_rescue} \
1335    ${LOCAL_TOOL_DIRS} \
1336    lib/ncurses/ncurses \
1337    lib/ncurses/ncursesw \
1338    ${_share} \
1339    usr.bin/awk \
1340    lib/libmagic \
1341    usr.bin/mkesdb_static \
1342    usr.bin/mkcsmapper_static \
1343    usr.bin/vi/catalog
1344	${_+_}@${ECHODIR} "===> ${_tool} (obj,build-tools)"; \
1345		cd ${.CURDIR}/${_tool} && \
1346		${MAKE} DIRPRFX=${_tool}/ obj && \
1347		${MAKE} DIRPRFX=${_tool}/ build-tools
1348.endfor
1349.for _tool in \
1350    ${_gcc_tools}
1351	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all)"; \
1352		cd ${.CURDIR}/${_tool} && \
1353		${MAKE} DIRPRFX=${_tool}/ obj && \
1354		${MAKE} DIRPRFX=${_tool}/ depend && \
1355		${MAKE} DIRPRFX=${_tool}/ all
1356.endfor
1357
1358#
1359# kernel-tools: Build kernel-building tools
1360#
1361kernel-tools: .MAKE
1362	mkdir -p ${MAKEOBJDIRPREFIX}/usr
1363	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1364	    -p ${MAKEOBJDIRPREFIX}/usr >/dev/null
1365
1366#
1367# cross-tools: Build cross-building tools
1368#
1369.if ${TARGET_ARCH} != ${MACHINE_ARCH}
1370.if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "i386"
1371_btxld=		usr.sbin/btxld
1372.endif
1373.endif
1374.if ${TARGET_ARCH} != ${MACHINE_ARCH}
1375.if ${MK_RESCUE} != "no" || defined(RELEASEDIR)
1376_crunchide=	usr.sbin/crunch/crunchide
1377.endif
1378.if ${TARGET_ARCH} == "i386" && defined(RELEASEDIR)
1379_kgzip=		usr.sbin/kgzip
1380.endif
1381.endif
1382
1383# If we're given an XAS, don't build binutils.
1384.if ${XAS:M/*} == "" && ${MK_BINUTILS_BOOTSTRAP} != "no"
1385_binutils=	gnu/usr.bin/binutils
1386.endif
1387
1388# If an full path to an external cross compiler is given, don't build
1389# a cross compiler.
1390.if ${XCC:M/*} == "" && ${MK_CROSS_COMPILER} != "no"
1391.if ${MK_CLANG_BOOTSTRAP} != "no"
1392_clang=		usr.bin/clang
1393_clang_libs=	lib/clang
1394.endif
1395.if ${MK_GCC_BOOTSTRAP} != "no"
1396_cc=		gnu/usr.bin/cc
1397.endif
1398.endif
1399
1400cross-tools: .MAKE
1401.for _tool in \
1402    ${_clang_libs} \
1403    ${_clang} \
1404    ${_binutils} \
1405    ${_cc} \
1406    usr.bin/xlint/lint1 usr.bin/xlint/lint2 usr.bin/xlint/xlint \
1407    ${_btxld} \
1408    ${_crunchide} \
1409    ${_kgzip}
1410	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1411		cd ${.CURDIR}/${_tool} && \
1412		${MAKE} DIRPRFX=${_tool}/ obj && \
1413		${MAKE} DIRPRFX=${_tool}/ depend && \
1414		${MAKE} DIRPRFX=${_tool}/ all && \
1415		${MAKE} DIRPRFX=${_tool}/ DESTDIR=${MAKEOBJDIRPREFIX} install
1416.endfor
1417
1418#
1419# hierarchy - ensure that all the needed directories are present
1420#
1421hierarchy hier:
1422	cd ${.CURDIR}/etc && ${HMAKE} distrib-dirs
1423
1424#
1425# libraries - build all libraries, and install them under ${DESTDIR}.
1426#
1427# The list of libraries with dependents (${_prebuild_libs}) and their
1428# interdependencies (__L) are built automatically by the
1429# ${.CURDIR}/tools/make_libdeps.sh script.
1430#
1431libraries: .MAKE
1432	cd ${.CURDIR} && \
1433	    ${MAKE} -f Makefile.inc1 _prereq_libs && \
1434	    ${MAKE} -f Makefile.inc1 _startup_libs && \
1435	    ${MAKE} -f Makefile.inc1 _prebuild_libs && \
1436	    ${MAKE} -f Makefile.inc1 _generic_libs
1437
1438#
1439# static libgcc.a prerequisite for shared libc
1440#
1441_prereq_libs= gnu/lib/libssp/libssp_nonshared gnu/lib/libgcc lib/libcompiler_rt
1442
1443# These dependencies are not automatically generated:
1444#
1445# gnu/lib/csu, gnu/lib/libgcc, lib/csu and lib/libc must be built before
1446# all shared libraries for ELF.
1447#
1448_startup_libs=	gnu/lib/csu
1449.if exists(${.CURDIR}/lib/csu/${MACHINE_ARCH}-elf)
1450_startup_libs+=	lib/csu/${MACHINE_ARCH}-elf
1451.elif exists(${.CURDIR}/lib/csu/${MACHINE_ARCH})
1452_startup_libs+=	lib/csu/${MACHINE_ARCH}
1453.else
1454_startup_libs+=	lib/csu/${MACHINE_CPUARCH}
1455.endif
1456_startup_libs+=	gnu/lib/libgcc
1457_startup_libs+=	lib/libcompiler_rt
1458_startup_libs+=	lib/libc
1459_startup_libs+=	lib/libc_nonshared
1460.if ${MK_LIBCPLUSPLUS} != "no"
1461_startup_libs+=	lib/libcxxrt
1462.endif
1463
1464gnu/lib/libgcc__L: lib/libc__L
1465gnu/lib/libgcc__L: lib/libc_nonshared__L
1466.if ${MK_LIBCPLUSPLUS} != "no"
1467lib/libcxxrt__L: gnu/lib/libgcc__L
1468.endif
1469
1470_prebuild_libs=	${_kerberos5_lib_libasn1} \
1471		${_kerberos5_lib_libhdb} \
1472		${_kerberos5_lib_libheimbase} \
1473		${_kerberos5_lib_libheimntlm} \
1474		${_kerberos5_lib_libheimsqlite} \
1475		${_kerberos5_lib_libheimipcc} \
1476		${_kerberos5_lib_libhx509} ${_kerberos5_lib_libkrb5} \
1477		${_kerberos5_lib_libroken} \
1478		${_kerberos5_lib_libwind} \
1479		${_lib_atf} \
1480		lib/libbz2 ${_libcom_err} lib/libcrypt \
1481		lib/libelf lib/libexpat \
1482		${_lib_libgssapi} \
1483		lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
1484		${_lib_libcapsicum} \
1485		lib/ncurses/ncurses lib/ncurses/ncursesw \
1486		lib/libopie lib/libpam ${_lib_libthr} \
1487		lib/libradius lib/libsbuf lib/libtacplus \
1488		${_cddl_lib_libumem} ${_cddl_lib_libnvpair} \
1489		${_cddl_lib_libavl} \
1490		${_cddl_lib_libzfs_core} \
1491		lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \
1492		${_secure_lib_libcrypto} ${_lib_libldns} \
1493		${_secure_lib_libssh} ${_secure_lib_libssl}
1494.if ${MK_GNUCXX} != "no"
1495_prebuild_libs+= gnu/lib/libstdc++ gnu/lib/libsupc++
1496gnu/lib/libstdc++__L: lib/msun__L
1497gnu/lib/libsupc++__L: gnu/lib/libstdc++__L
1498.endif
1499
1500.if defined(WITH_ATF) || ${MK_TESTS} != "no"
1501.if !defined(WITH_ATF)
1502# Ensure that the ATF libraries will be built during make libraries, even
1503# though they will have WITHOUT_TESTS
1504MAKE+=		-DWITH_ATF
1505.endif
1506_lib_atf=	lib/atf
1507.endif
1508
1509.if ${MK_LIBTHR} != "no"
1510_lib_libthr=	lib/libthr
1511.endif
1512
1513.if ${MK_OFED} != "no"
1514_ofed_lib=	contrib/ofed/usr.lib/
1515.endif
1516
1517.if ${MK_CASPER} != "no"
1518_lib_libcapsicum=lib/libcapsicum
1519.endif
1520
1521lib/libcapsicum__L: lib/libnv__L
1522lib/libpjdlog__L: lib/libutil__L
1523
1524_generic_libs=	${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib} usr.bin/lex/lib ${_ofed_lib}
1525.for _DIR in ${LOCAL_LIB_DIRS}
1526.if exists(${.CURDIR}/${_DIR}/Makefile)
1527_generic_libs+= ${_DIR}
1528.endif
1529.endfor
1530
1531lib/libopie__L lib/libtacplus__L: lib/libmd__L
1532
1533.if ${MK_CDDL} != "no"
1534_cddl_lib_libumem= cddl/lib/libumem
1535_cddl_lib_libnvpair= cddl/lib/libnvpair
1536_cddl_lib_libavl= cddl/lib/libavl
1537_cddl_lib_libzfs_core= cddl/lib/libzfs_core
1538_cddl_lib= cddl/lib
1539cddl/lib/libzfs_core__L: cddl/lib/libnvpair__L
1540.endif
1541
1542.if ${MK_CRYPT} != "no"
1543.if ${MK_OPENSSL} != "no"
1544_secure_lib_libcrypto= secure/lib/libcrypto
1545_secure_lib_libssl= secure/lib/libssl
1546lib/libradius__L secure/lib/libssl__L: secure/lib/libcrypto__L
1547.if ${MK_LDNS} != "no"
1548_lib_libldns= lib/libldns
1549lib/libldns__L: secure/lib/libcrypto__L
1550.endif
1551.if ${MK_OPENSSH} != "no"
1552_secure_lib_libssh= secure/lib/libssh
1553secure/lib/libssh__L: lib/libz__L secure/lib/libcrypto__L lib/libcrypt__L
1554.if ${MK_LDNS} != "no"
1555secure/lib/libssh__L: lib/libldns__L
1556.endif
1557.if ${MK_KERBEROS_SUPPORT} != "no"
1558secure/lib/libssh__L: lib/libgssapi__L kerberos5/lib/libkrb5__L \
1559    kerberos5/lib/libhx509__L kerberos5/lib/libasn1__L lib/libcom_err__L \
1560    lib/libmd__L kerberos5/lib/libroken__L
1561.endif
1562.endif
1563.endif
1564_secure_lib=	secure/lib
1565.endif
1566
1567.if ${MK_KERBEROS} != "no"
1568kerberos5/lib/libasn1__L: lib/libcom_err__L kerberos5/lib/libroken__L
1569kerberos5/lib/libhdb__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1570    kerberos5/lib/libkrb5__L kerberos5/lib/libroken__L \
1571    kerberos5/lib/libwind__L kerberos5/lib/libheimsqlite__L
1572kerberos5/lib/libheimntlm__L: secure/lib/libcrypto__L kerberos5/lib/libkrb5__L \
1573    kerberos5/lib/libroken__L lib/libcom_err__L
1574kerberos5/lib/libhx509__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1575    secure/lib/libcrypto__L kerberos5/lib/libroken__L kerberos5/lib/libwind__L
1576kerberos5/lib/libkrb5__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
1577    lib/libcrypt__L secure/lib/libcrypto__L kerberos5/lib/libhx509__L \
1578    kerberos5/lib/libroken__L kerberos5/lib/libwind__L \
1579    kerberos5/lib/libheimbase__L kerberos5/lib/libheimipcc__L
1580kerberos5/lib/libroken__L: lib/libcrypt__L
1581kerberos5/lib/libwind__L: kerberos5/lib/libroken__L lib/libcom_err__L
1582kerberos5/lib/libheimbase__L: lib/libthr__L
1583kerberos5/lib/libheimipcc__L: kerberos5/lib/libroken__L kerberos5/lib/libheimbase__L lib/libthr__L
1584kerberos5/lib/libheimsqlite__L: lib/libthr__L
1585.endif
1586
1587.if ${MK_GSSAPI} != "no"
1588_lib_libgssapi=	lib/libgssapi
1589.endif
1590
1591.if ${MK_KERBEROS} != "no"
1592_kerberos5_lib=	kerberos5/lib
1593_kerberos5_lib_libasn1= kerberos5/lib/libasn1
1594_kerberos5_lib_libhdb= kerberos5/lib/libhdb
1595_kerberos5_lib_libheimbase= kerberos5/lib/libheimbase
1596_kerberos5_lib_libkrb5= kerberos5/lib/libkrb5
1597_kerberos5_lib_libhx509= kerberos5/lib/libhx509
1598_kerberos5_lib_libroken= kerberos5/lib/libroken
1599_kerberos5_lib_libheimntlm= kerberos5/lib/libheimntlm
1600_kerberos5_lib_libheimsqlite= kerberos5/lib/libheimsqlite
1601_kerberos5_lib_libheimipcc= kerberos5/lib/libheimipcc
1602_kerberos5_lib_libwind= kerberos5/lib/libwind
1603_libcom_err= lib/libcom_err
1604.endif
1605
1606.if ${MK_NIS} != "no"
1607_lib_libypclnt=	lib/libypclnt
1608.endif
1609
1610.if ${MK_OPENSSL} == "no"
1611lib/libradius__L: lib/libmd__L
1612.endif
1613
1614.for _lib in ${_prereq_libs}
1615${_lib}__PL: .PHONY .MAKE
1616.if exists(${.CURDIR}/${_lib})
1617	${_+_}@${ECHODIR} "===> ${_lib} (obj,depend,all,install)"; \
1618		cd ${.CURDIR}/${_lib} && \
1619		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj && \
1620		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ depend && \
1621		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
1622		    DIRPRFX=${_lib}/ all && \
1623		${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
1624		    DIRPRFX=${_lib}/ install
1625.endif
1626.endfor
1627
1628.for _lib in ${_startup_libs} ${_prebuild_libs:Nlib/libpam} ${_generic_libs}
1629${_lib}__L: .PHONY .MAKE
1630.if exists(${.CURDIR}/${_lib})
1631	${_+_}@${ECHODIR} "===> ${_lib} (obj,depend,all,install)"; \
1632		cd ${.CURDIR}/${_lib} && \
1633		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj && \
1634		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ depend && \
1635		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ all && \
1636		${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ install
1637.endif
1638.endfor
1639
1640# libpam is special: we need to build static PAM modules before
1641# static PAM library, and dynamic PAM library before dynamic PAM
1642# modules.
1643lib/libpam__L: .PHONY .MAKE
1644	${_+_}@${ECHODIR} "===> lib/libpam (obj,depend,all,install)"; \
1645		cd ${.CURDIR}/lib/libpam && \
1646		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ obj && \
1647		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ depend && \
1648		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ \
1649		    -D_NO_LIBPAM_SO_YET all && \
1650		${MAKE} MK_TESTS=no DIRPRFX=lib/libpam/ \
1651		    -D_NO_LIBPAM_SO_YET install
1652
1653_prereq_libs: ${_prereq_libs:S/$/__PL/}
1654_startup_libs: ${_startup_libs:S/$/__L/}
1655_prebuild_libs: ${_prebuild_libs:S/$/__L/}
1656_generic_libs: ${_generic_libs:S/$/__L/}
1657
1658.for __target in all clean cleandepend cleandir depend includes obj
1659.for entry in ${SUBDIR}
1660${entry}.${__target}__D: .PHONY .MAKE
1661	${_+_}@set -e; if test -d ${.CURDIR}/${entry}.${MACHINE_ARCH}; then \
1662		${ECHODIR} "===> ${DIRPRFX}${entry}.${MACHINE_ARCH} (${__target})"; \
1663		edir=${entry}.${MACHINE_ARCH}; \
1664		cd ${.CURDIR}/$${edir}; \
1665	else \
1666		${ECHODIR} "===> ${DIRPRFX}${entry} (${__target})"; \
1667		edir=${entry}; \
1668		cd ${.CURDIR}/$${edir}; \
1669	fi; \
1670	${MAKE} ${__target} DIRPRFX=${DIRPRFX}$${edir}/
1671.endfor
1672par-${__target}: ${SUBDIR:S/$/.${__target}__D/}
1673.endfor
1674
1675.include <bsd.subdir.mk>
1676
1677.if make(check-old) || make(check-old-dirs) || \
1678    make(check-old-files) || make(check-old-libs) || \
1679    make(delete-old) || make(delete-old-dirs) || \
1680    make(delete-old-files) || make(delete-old-libs)
1681
1682#
1683# check for / delete old files section
1684#
1685
1686.include "ObsoleteFiles.inc"
1687
1688OLD_LIBS_MESSAGE="Please be sure no application still uses those libraries, \
1689else you can not start such an application. Consult UPDATING for more \
1690information regarding how to cope with the removal/revision bump of a \
1691specific library."
1692
1693.if !defined(BATCH_DELETE_OLD_FILES)
1694RM_I=-i
1695.else
1696RM_I=-v
1697.endif
1698
1699delete-old-files:
1700	@echo ">>> Removing old files (only deletes safe to delete libs)"
1701# Ask for every old file if the user really wants to remove it.
1702# It's annoying, but better safe than sorry.
1703# NB: We cannot pass the list of OLD_FILES as a parameter because the
1704# argument list will get too long. Using .for/.endfor make "loops" will make
1705# the Makefile parser segfault.
1706	@exec 3<&0; \
1707	cd ${.CURDIR}; \
1708	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1709	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
1710	while read file; do \
1711		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
1712			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
1713			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
1714		fi; \
1715	done
1716# Remove catpages without corresponding manpages.
1717	@exec 3<&0; \
1718	find ${DESTDIR}/usr/share/man/cat* ! -type d | \
1719	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
1720	while read catpage; do \
1721		read manpage; \
1722		if [ ! -e "$${manpage}" ]; then \
1723			rm ${RM_I} $${catpage} <&3; \
1724	        fi; \
1725	done
1726	@echo ">>> Old files removed"
1727
1728check-old-files:
1729	@echo ">>> Checking for old files"
1730	@cd ${.CURDIR}; \
1731	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1732	    -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
1733	while read file; do \
1734		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
1735		 	echo "${DESTDIR}/$${file}"; \
1736		fi; \
1737	done
1738# Check for catpages without corresponding manpages.
1739	@find ${DESTDIR}/usr/share/man/cat* ! -type d | \
1740	sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
1741	while read catpage; do \
1742		read manpage; \
1743		if [ ! -e "$${manpage}" ]; then \
1744			echo $${catpage}; \
1745	        fi; \
1746	done
1747
1748delete-old-libs:
1749	@echo ">>> Removing old libraries"
1750	@echo "${OLD_LIBS_MESSAGE}" | fmt
1751	@exec 3<&0; \
1752	cd ${.CURDIR}; \
1753	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1754	    -V OLD_LIBS | xargs -n1 | \
1755	while read file; do \
1756		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
1757			chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
1758			rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
1759		fi; \
1760		for ext in debug symbols; do \
1761		  if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
1762		      "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
1763			  rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
1764			      <&3; \
1765		  fi; \
1766		done; \
1767	done
1768	@echo ">>> Old libraries removed"
1769
1770check-old-libs:
1771	@echo ">>> Checking for old libraries"
1772	@cd ${.CURDIR}; \
1773	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1774	    -V OLD_LIBS | xargs -n1 | \
1775	while read file; do \
1776		if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
1777			echo "${DESTDIR}/$${file}"; \
1778		fi; \
1779		for ext in debug symbols; do \
1780		  if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
1781			  echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
1782		  fi; \
1783		done; \
1784	done
1785
1786delete-old-dirs:
1787	@echo ">>> Removing old directories"
1788	@cd ${.CURDIR}; \
1789	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1790	    -V OLD_DIRS | xargs -n1 | sort -r | \
1791	while read dir; do \
1792		if [ -d "${DESTDIR}/$${dir}" ]; then \
1793			rmdir -v "${DESTDIR}/$${dir}" || true; \
1794		elif [ -L "${DESTDIR}/$${dir}" ]; then \
1795			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
1796		fi; \
1797	done
1798	@echo ">>> Old directories removed"
1799
1800check-old-dirs:
1801	@echo ">>> Checking for old directories"
1802	@cd ${.CURDIR}; \
1803	${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
1804	    -V OLD_DIRS | xargs -n1 | \
1805	while read dir; do \
1806		if [ -d "${DESTDIR}/$${dir}" ]; then \
1807			echo "${DESTDIR}/$${dir}"; \
1808		elif [ -L "${DESTDIR}/$${dir}" ]; then \
1809			echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
1810		fi; \
1811	done
1812
1813delete-old: delete-old-files delete-old-dirs
1814	@echo "To remove old libraries run '${MAKE} delete-old-libs'."
1815
1816check-old: check-old-files check-old-libs check-old-dirs
1817	@echo "To remove old files and directories run '${MAKE} delete-old'."
1818	@echo "To remove old libraries run '${MAKE} delete-old-libs'."
1819
1820.endif
1821
1822#
1823# showconfig - show build configuration.
1824#
1825showconfig:
1826	@${MAKE} -n -f src.opts.mk -V dummy -dg1 2>&1 | grep ^MK_ | sort
1827
1828.if !empty(KRNLOBJDIR) && !empty(KERNCONF)
1829DTBOUTPUTPATH= ${KRNLOBJDIR}/${KERNCONF}/
1830
1831.if !defined(FDT_DTS_FILE) || empty(FDT_DTS_FILE)
1832.if exists(${KERNCONFDIR}/${KERNCONF})
1833FDT_DTS_FILE!= awk 'BEGIN {FS="="} /^makeoptions[[:space:]]+FDT_DTS_FILE/ {print $$2}' \
1834	'${KERNCONFDIR}/${KERNCONF}' ; echo
1835.endif
1836.endif
1837
1838.endif
1839
1840.if !defined(DTBOUTPUTPATH) || !exists(${DTBOUTPUTPATH})
1841DTBOUTPUTPATH= ${.CURDIR}
1842.endif
1843
1844#
1845# Build 'standalone' Device Tree Blob
1846#
1847builddtb:
1848	@PATH=${TMPPATH} MACHINE=${TARGET} \
1849	${.CURDIR}/sys/tools/fdt/make_dtb.sh ${.CURDIR}/sys \
1850	    "${FDT_DTS_FILE}" ${DTBOUTPUTPATH}
1851
1852###############
1853
1854.if defined(TARGET) && defined(TARGET_ARCH)
1855
1856.if ${TARGET} == ${MACHINE} && ${TARGET_ARCH} == ${MACHINE_ARCH}
1857XDEV_CPUTYPE?=${CPUTYPE}
1858.else
1859XDEV_CPUTYPE?=${TARGET_CPUTYPE}
1860.endif
1861
1862NOFUN=-DNO_FSCHG MK_HTML=no MK_INFO=no -DNO_LINT \
1863	MK_MAN=no MK_NLS=no MK_PROFILE=no \
1864	MK_KERBEROS=no MK_RESCUE=no MK_TESTS=no MK_WARNS=no \
1865	TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
1866	CPUTYPE=${XDEV_CPUTYPE}
1867
1868XDDIR=${TARGET_ARCH}-freebsd
1869XDTP?=/usr/${XDDIR}
1870.if ${XDTP:N/*}
1871.error XDTP variable should be an absolute path
1872.endif
1873
1874CDBENV=MAKEOBJDIRPREFIX=${MAKEOBJDIRPREFIX}/${XDDIR} \
1875	INSTALL="sh ${.CURDIR}/tools/install.sh"
1876CDENV= ${CDBENV} \
1877	_SHLIBDIRPREFIX=${XDDESTDIR} \
1878	TOOLS_PREFIX=${XDTP}
1879CD2CFLAGS=-isystem ${XDDESTDIR}/usr/include -L${XDDESTDIR}/usr/lib \
1880	--sysroot=${XDDESTDIR}/ -B${XDDESTDIR}/usr/libexec \
1881	-B${XDDESTDIR}/usr/bin -B${XDDESTDIR}/usr/lib
1882CD2ENV=${CDENV} CC="${CC} ${CD2CFLAGS}" CXX="${CXX} ${CD2CFLAGS}" \
1883	CPP="${CPP} ${CD2CFLAGS}" \
1884	MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH}
1885
1886CDTMP=	${MAKEOBJDIRPREFIX}/${XDDIR}/${.CURDIR}/tmp
1887CDMAKE=${CDENV} PATH=${CDTMP}/usr/bin:${PATH} ${MAKE} ${NOFUN}
1888CD2MAKE=${CD2ENV} PATH=${CDTMP}/usr/bin:${XDDESTDIR}/usr/bin:${PATH} ${MAKE} ${NOFUN}
1889XDDESTDIR=${DESTDIR}/${XDTP}
1890.if !defined(OSREL)
1891OSREL!= uname -r | sed -e 's/[-(].*//'
1892.endif
1893
1894.ORDER: xdev-build xdev-install xdev-links
1895xdev: xdev-build xdev-install
1896
1897.ORDER: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
1898xdev-build: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
1899
1900_xb-worldtmp:
1901	mkdir -p ${CDTMP}/usr
1902	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1903	    -p ${CDTMP}/usr >/dev/null
1904
1905_xb-bootstrap-tools:
1906.for _tool in \
1907    ${_clang_tblgen}
1908	${_+_}@${ECHODIR} "===> ${_tool} (obj,depend,all,install)"; \
1909	cd ${.CURDIR}/${_tool} && \
1910	${CDMAKE} DIRPRFX=${_tool}/ obj && \
1911	${CDMAKE} DIRPRFX=${_tool}/ depend && \
1912	${CDMAKE} DIRPRFX=${_tool}/ all && \
1913	${CDMAKE} DIRPRFX=${_tool}/ DESTDIR=${CDTMP} install
1914.endfor
1915
1916_xb-build-tools:
1917	${_+_}@cd ${.CURDIR}; \
1918	${CDBENV} ${MAKE} -f Makefile.inc1 ${NOFUN} build-tools
1919
1920_xb-cross-tools:
1921.for _tool in \
1922    ${_binutils} \
1923    usr.bin/ar \
1924    ${_clang_libs} \
1925    ${_clang} \
1926    ${_cc}
1927	${_+_}@${ECHODIR} "===> xdev ${_tool} (obj,depend,all)"; \
1928	cd ${.CURDIR}/${_tool} && \
1929	${CDMAKE} DIRPRFX=${_tool}/ obj && \
1930	${CDMAKE} DIRPRFX=${_tool}/ depend && \
1931	${CDMAKE} DIRPRFX=${_tool}/ all
1932.endfor
1933
1934_xi-mtree:
1935	${_+_}@${ECHODIR} "mtree populating ${XDDESTDIR}"
1936	mkdir -p ${XDDESTDIR}
1937	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.root.dist \
1938	    -p ${XDDESTDIR} >/dev/null
1939	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1940	    -p ${XDDESTDIR}/usr >/dev/null
1941	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1942	    -p ${XDDESTDIR}/usr/include >/dev/null
1943.if ${MK_TESTS} != "no"
1944	mtree -deU -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1945	    -p ${XDDESTDIR}/usr >/dev/null
1946.endif
1947
1948.ORDER: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
1949xdev-install: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
1950
1951_xi-cross-tools:
1952	@echo "_xi-cross-tools"
1953.for _tool in \
1954    ${_binutils} \
1955    usr.bin/ar \
1956    ${_clang_libs} \
1957    ${_clang} \
1958    ${_cc}
1959	${_+_}@${ECHODIR} "===> xdev ${_tool} (install)"; \
1960	cd ${.CURDIR}/${_tool}; \
1961	${CDMAKE} DIRPRFX=${_tool}/ install DESTDIR=${XDDESTDIR}
1962.endfor
1963
1964_xi-includes:
1965	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 par-includes \
1966		DESTDIR=${XDDESTDIR}
1967
1968_xi-libraries:
1969	${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 libraries \
1970		DESTDIR=${XDDESTDIR}
1971
1972xdev-links:
1973	${_+_}cd ${XDDESTDIR}/usr/bin; \
1974	mkdir -p ../../../../usr/bin; \
1975		for i in *; do \
1976			ln -sf ../../${XDTP}/usr/bin/$$i \
1977			    ../../../../usr/bin/${XDDIR}-$$i; \
1978			ln -sf ../../${XDTP}/usr/bin/$$i \
1979			    ../../../../usr/bin/${XDDIR}${OSREL}-$$i; \
1980		done
1981.else
1982xdev xdev-build xdev-install xdev-links:
1983	@echo "*** Error: Both TARGET and TARGET_ARCH must be defined for \"${.TARGET}\" target"
1984.endif
1985