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