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