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