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