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