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