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