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