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