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