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