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