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