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