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