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