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