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