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