xref: /freebsd/tools/build/Makefile (revision 7b81e6c7caf23adca6a2cabdfdf29985eab9865d)
130aaff11SWarner Losh# $FreeBSD$
230aaff11SWarner Losh
330aaff11SWarner Losh.PATH: ${.CURDIR}/../../include
430aaff11SWarner Losh
51c62f923SWarner LoshLIB=		egacy
6c2774610SDavid E. O'BrienSRC=
7162ae9c8SAlex RichardsonINCSGROUPS=	INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
87ec1ec4fSAlex RichardsonINCSGROUPS+=	MACHINESYSINCS RPCINCS
930aaff11SWarner LoshINCS=
1030aaff11SWarner Losh
11d80f1dd1SJilles TjoelkerSYSINCSDIR=	${INCLUDEDIR}/sys
12d5f4dd1bSMariusz ZaborskiCASPERINCDIR=	${INCLUDEDIR}/casper
13162ae9c8SAlex Richardson# Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
14162ae9c8SAlex RichardsonUFSINCSDIR=	${INCLUDEDIR}/ufs/ufs
15162ae9c8SAlex RichardsonFFSINCSDIR=	${INCLUDEDIR}/ufs/ffs
16162ae9c8SAlex RichardsonMSDOSFSINCSDIR=	${INCLUDEDIR}/fs/msdosfs
17162ae9c8SAlex RichardsonDISKINCSDIR=	${INCLUDEDIR}/sys/disk
187ec1ec4fSAlex RichardsonMACHINESYSINCSDIR=	${INCLUDEDIR}/machine
197ec1ec4fSAlex RichardsonRPCINCSDIR=	${INCLUDEDIR}/rpc
20d80f1dd1SJilles Tjoelker
2130aaff11SWarner LoshBOOTSTRAPPING?=	0
2230aaff11SWarner Losh
237ec1ec4fSAlex Richardson
247ec1ec4fSAlex Richardson.if ${.MAKE.OS} == "Darwin"
257ec1ec4fSAlex Richardson_XCODE_ROOT!=xcode-select -p
267ec1ec4fSAlex Richardson# since macOS 10.14 C headers are no longer installed in /usr but only
277ec1ec4fSAlex Richardson# provided via the SDK
287ec1ec4fSAlex Richardson.if ${_XCODE_ROOT} == "/Library/Developer/CommandLineTools"
297ec1ec4fSAlex Richardson# Only command line tools installed -> host headers are in the SDKs directory
307ec1ec4fSAlex Richardson_MACOS_SDK_DIR=${_XCODE_ROOT}/SDKs/MacOSX.sdk/
317ec1ec4fSAlex Richardson.else
327ec1ec4fSAlex Richardson# Full XCode installed -> host headers are below Platforms/MacOSX.platform
337ec1ec4fSAlex Richardson_MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
347ec1ec4fSAlex Richardson.endif
357ec1ec4fSAlex RichardsonHOST_INCLUDE_ROOT=${_MACOS_SDK_DIR}/usr/include
367ec1ec4fSAlex Richardson.if !exists(${HOST_INCLUDE_ROOT}/stdio.h)
377ec1ec4fSAlex Richardson.error "You must install the macOS SDK (try xcode-select --install)"
387ec1ec4fSAlex Richardson.endif
397ec1ec4fSAlex Richardson.else
407ec1ec4fSAlex RichardsonHOST_INCLUDE_ROOT=/usr/include
417ec1ec4fSAlex Richardson.endif
427ec1ec4fSAlex Richardson
437ec1ec4fSAlex Richardson# Allow building libc-internal files (also on non-FreeBSD hosts)
447ec1ec4fSAlex RichardsonCFLAGS+=	-I${.CURDIR}/libc-bootstrap
457ec1ec4fSAlex Richardson# Symbol versioning is not required for -legacy (and macOS bootstrap)
467ec1ec4fSAlex RichardsonMK_SYMVER=	no
477ec1ec4fSAlex Richardson
487ec1ec4fSAlex Richardson_WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
4979626055SBrooks Davis.if ${_WITH_PWCACHEDB} == 0
5079626055SBrooks Davis.PATH: ${.CURDIR}/../../contrib/libc-pwcache
517ec1ec4fSAlex RichardsonCFLAGS.pwcache.c+=	-I${.CURDIR}/../../contrib/libc-pwcache
5279626055SBrooks DavisSRCS+=		pwcache.c
5379626055SBrooks Davis.endif
5479626055SBrooks Davis
557ec1ec4fSAlex Richardson_WITH_STRSVIS!=	grep -c strsvis ${HOST_INCLUDE_ROOT}/vis.h 2>/dev/null || true
5679626055SBrooks Davis.if ${_WITH_STRSVIS} == 0
5779626055SBrooks Davis.PATH: ${.CURDIR}/../../contrib/libc-vis
587ec1ec4fSAlex RichardsonINCS+=		vis.h
597ec1ec4fSAlex RichardsonSRCS+=		vis.c unvis.c
60a5252831SAlex RichardsonCFLAGS.vis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
61a5252831SAlex RichardsonCFLAGS.unvis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
6279626055SBrooks Davis.endif
6379626055SBrooks Davis
647ec1ec4fSAlex Richardson_WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
658e7e3163SDimitry Andric.if ${_WITH_REALLOCARRAY} == 0
668e7e3163SDimitry Andric.PATH: ${.CURDIR}/../../lib/libc/stdlib
678e7e3163SDimitry AndricINCS+=		stdlib.h
688e7e3163SDimitry AndricSRCS+=		reallocarray.c
698e7e3163SDimitry Andric.endif
708e7e3163SDimitry Andric
717ec1ec4fSAlex Richardson_WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
72d80f1dd1SJilles Tjoelker.if ${_WITH_UTIMENS} == 0
73d80f1dd1SJilles TjoelkerSYSINCS+=	stat.h
74d80f1dd1SJilles TjoelkerSRCS+=		futimens.c utimensat.c
75d80f1dd1SJilles Tjoelker.endif
76d80f1dd1SJilles Tjoelker
777ec1ec4fSAlex Richardson_WITH_EXPLICIT_BZERO!= grep -c explicit_bzero ${HOST_INCLUDE_ROOT}/strings.h || true
788d14ced6SAlex Richardson.if ${_WITH_EXPLICIT_BZERO} == 0
797ec1ec4fSAlex Richardson# .PATH: ${SRCTOP}/sys/libkern
807ec1ec4fSAlex Richardson# Adding sys/libkern to .PATH breaks building the cross-build compat library
817ec1ec4fSAlex Richardson# since that attempts to build strlcpy.c from libc and adding libkern here will
827ec1ec4fSAlex Richardson# cause it to pick the file from libkern instead (which won't compile).
837ec1ec4fSAlex Richardson# Avoid modifying .PATH by creating a copy in the build directory instead.
847ec1ec4fSAlex Richardsonexplicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
857ec1ec4fSAlex Richardson	cp ${.ALLSRC} ${.TARGET}
867ec1ec4fSAlex RichardsonCLEANFILES+=	explicit_bzero.c
878d14ced6SAlex RichardsonINCS+=		strings.h
888d14ced6SAlex RichardsonSRCS+=		explicit_bzero.c
898d14ced6SAlex Richardson.endif
908d14ced6SAlex Richardson
915425ba83SKa Ho Ng_WITH_FSPACECTL!= grep -c fspacectl ${HOST_INCLUDE_ROOT}/fcntl.h || true
925425ba83SKa Ho Ng.if ${_WITH_FSPACECTL} == 0
935425ba83SKa Ho NgINCS+=		fcntl.h
945425ba83SKa Ho NgSRCS+=		fspacectl.c
955425ba83SKa Ho Ng.endif
967ec1ec4fSAlex Richardson
977ec1ec4fSAlex Richardson.if exists(${HOST_INCLUDE_ROOT}/capsicum_helpers.h)
987ec1ec4fSAlex Richardson_WITH_CAPH_ENTER!= grep -c caph_enter ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
997ec1ec4fSAlex Richardson_WITH_CAPH_RIGHTS_LIMIT!= grep -c caph_rights_limit ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
1003b7e9bfeSAlex Richardson.endif
101fbe12f20SMark Johnston.if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
1023b7e9bfeSAlex Richardson.PATH: ${SRCTOP}/lib/libcapsicum
1033b7e9bfeSAlex RichardsonINCS+=		capsicum_helpers.h
1043b7e9bfeSAlex Richardson.PATH: ${SRCTOP}/lib/libcasper/libcasper
1053b7e9bfeSAlex RichardsonINCS+=		libcasper.h
1063b7e9bfeSAlex Richardson.endif
1073b7e9bfeSAlex Richardson
1087ec1ec4fSAlex Richardson# rpcgen should build against the source tree rpc/types.h and not the host.
1097ec1ec4fSAlex Richardson# This is especially important on non-FreeBSD systems where the types may
1107ec1ec4fSAlex Richardson# not match.
1117ec1ec4fSAlex RichardsonRPCINCS+=	${SRCTOP}/sys/rpc/types.h
1127ec1ec4fSAlex Richardson
1137ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/mpool.h
1147ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/ndbm.h
1157ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/err.h
1167ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/stringlist.h
1177ec1ec4fSAlex Richardson
1187ec1ec4fSAlex Richardson# Needed to build arc4random.c
1197ec1ec4fSAlex RichardsonINCSGROUPS+=	CHACHA20INCS
1207ec1ec4fSAlex RichardsonCHACHA20INCSDIR=	${INCLUDEDIR}/crypto/chacha20
1217ec1ec4fSAlex RichardsonCHACHA20INCS+=	${SRCTOP}/sys/crypto/chacha20/_chacha.h \
1227ec1ec4fSAlex Richardson	${SRCTOP}/sys/crypto/chacha20/chacha.h
1237ec1ec4fSAlex Richardson
1247ec1ec4fSAlex Richardson_host_arch=${MACHINE}
1257ec1ec4fSAlex Richardson.if ${_host_arch} == "x86_64"
1267ec1ec4fSAlex Richardson# bmake on Linux/mac often prints that instead of amd64
1277ec1ec4fSAlex Richardson_host_arch=amd64
128*7b81e6c7SJustin Hibbits.elif ${_host_arch:Mppc*}
129*7b81e6c7SJustin Hibbits_host_arch=powerpc
1307ec1ec4fSAlex Richardson.endif
1317ec1ec4fSAlex Richardson.if ${_host_arch} == "unknown"
1327ec1ec4fSAlex Richardson# HACK: If MACHINE is unknown, assume we are building on x86
1337ec1ec4fSAlex Richardson_host_arch=amd64
1347ec1ec4fSAlex Richardson.endif
1357ec1ec4fSAlex RichardsonMACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/elf.h
1367ec1ec4fSAlex Richardson.if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
1377ec1ec4fSAlex RichardsonINCSGROUPS+=	X86INCS
1387ec1ec4fSAlex RichardsonX86INCSDIR=	${INCLUDEDIR}/x86
1397ec1ec4fSAlex RichardsonX86INCS+=	${SRCTOP}/sys/x86/include/elf.h
1407ec1ec4fSAlex Richardson.endif
1417ec1ec4fSAlex Richardson
1427ec1ec4fSAlex Richardson# needed for btxld:
1437ec1ec4fSAlex RichardsonMACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/exec.h
1447ec1ec4fSAlex RichardsonMACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/reloc.h
1457ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/a.out.h
1467ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/nlist.h
1477ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/imgact_aout.h
1487ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/nlist_aout.h
1497ec1ec4fSAlex Richardson
150ed845580SAlex Richardson
151ed845580SAlex Richardson.if ${.MAKE.OS} != "FreeBSD"
152ed845580SAlex Richardson.PATH: ${.CURDIR}/cross-build
1537ec1ec4fSAlex Richardson# dbopen() behaves differently on Linux and FreeBSD so we ensure that we
1547ec1ec4fSAlex Richardson# bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
1557ec1ec4fSAlex Richardson# __freebsd_dbopen() so that we don't ever use the host version
1567ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/db.h
1577ec1ec4fSAlex RichardsonLIBC_SRCTOP=	${SRCTOP}/lib/libc/
1587ec1ec4fSAlex Richardson.include "${LIBC_SRCTOP}/db/Makefile.inc"
1597ec1ec4fSAlex Richardson# Do the same as we did for dbopen() for getopt() on since it's not compatible
1607ec1ec4fSAlex Richardson# on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
1617ec1ec4fSAlex Richardson.PATH: ${LIBC_SRCTOP}/stdlib
1627ec1ec4fSAlex RichardsonSRCS+=	getopt.c getopt_long.c
1637ec1ec4fSAlex RichardsonINCS+=	 ${SRCTOP}/include/getopt.h
1647ec1ec4fSAlex Richardson
1657ec1ec4fSAlex Richardson# getcap.c is needed for cap_mkdb:
1667ec1ec4fSAlex Richardson.PATH: ${LIBC_SRCTOP}/gen
1677ec1ec4fSAlex RichardsonSRCS+=	getcap.c
168962a3814SAlex Richardson# Glibc does not provide all err*/warn* functions, and for macOS we need the
169962a3814SAlex Richardson# alias with the extra underscore.
170962a3814SAlex RichardsonSRCS+=	err.c
1717ec1ec4fSAlex Richardson# Add various libbc functions that are not available in glibc:
1727ec1ec4fSAlex RichardsonSRCS+=	stringlist.c setmode.c
1737ec1ec4fSAlex RichardsonSRCS+=	strtonum.c merge.c heapsort.c reallocf.c
1747ec1ec4fSAlex Richardson.PATH: ${LIBC_SRCTOP}/locale
1757ec1ec4fSAlex RichardsonSRCS+=	rpmatch.c
1767ec1ec4fSAlex Richardson
1777ec1ec4fSAlex Richardson.if ${.MAKE.OS} == "Linux"
1787ec1ec4fSAlex Richardson# On Linux, glibc does not provide strlcpy,strlcat or strmode.
1797ec1ec4fSAlex Richardson.PATH: ${LIBC_SRCTOP}/string
1807ec1ec4fSAlex RichardsonSRCS+=	strlcpy.c strlcat.c strmode.c
1817ec1ec4fSAlex Richardson# Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
1827ec1ec4fSAlex RichardsonSRCS+=	fgetln_fallback.c fgetwln_fallback.c closefrom.c
1837ec1ec4fSAlex RichardsonCFLAGS.closefrom.c+=	-DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
1847ec1ec4fSAlex Richardson	-DHAVE_DIRFD -DHAVE_SYSCONF
185962a3814SAlex Richardson# Provide getprogname/setprograme
186962a3814SAlex RichardsonSRCS+=	progname.c
1874e64fb9fSAlex Richardson# Stub implementations of fflagstostr/strtofflags
1884e64fb9fSAlex RichardsonSRCS+=	fflags.c
1897ec1ec4fSAlex Richardson.endif
1907ec1ec4fSAlex Richardson# Provide the same arc4random implementation on Linux/macOS
1917ec1ec4fSAlex RichardsonCFLAGS.arc4random.c+=	-I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
1927ec1ec4fSAlex RichardsonSRCS+=	arc4random.c arc4random_uniform.c
1937ec1ec4fSAlex Richardson
1947ec1ec4fSAlex Richardson# expand_number() is not provided by either Linux or MacOS libutil
1957ec1ec4fSAlex Richardson.PATH: ${SRCTOP}/lib/libutil
1967ec1ec4fSAlex RichardsonSRCS+=	expand_number.c
1977ec1ec4fSAlex Richardson# Linux libutil also doesn't have fparseln
1987ec1ec4fSAlex RichardsonSRCS+=	fparseln.c
1997ec1ec4fSAlex Richardson# A dummy sysctl for tzsetup:
2007ec1ec4fSAlex RichardsonSRCS+=	fake_sysctl.c
2017ec1ec4fSAlex Richardson
2027ec1ec4fSAlex Richardson# capsicum support
2037ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/capsicum.h
2047ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/caprights.h
2057ec1ec4fSAlex RichardsonSRCS+=	capsicum_stubs.c
2067ec1ec4fSAlex Richardson# XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
2077ec1ec4fSAlex Richardson# conflicts with other files. Instead copy subr_capability to the build dir.
2087ec1ec4fSAlex Richardsonsubr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
2097ec1ec4fSAlex Richardson	cp ${.ALLSRC} ${.TARGET}
2107ec1ec4fSAlex RichardsonSRCS+=	subr_capability.c
2117ec1ec4fSAlex RichardsonCLEANFILES+=	subr_capability.c
2127ec1ec4fSAlex Richardson.endif
2137ec1ec4fSAlex Richardson
214d5f4dd1bSMariusz ZaborskiCASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
215aefe30c5SMariusz ZaborskiCASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_net/cap_net.h
216d5f4dd1bSMariusz Zaborski
21730aaff11SWarner Losh.if empty(SRCS)
21830aaff11SWarner LoshSRCS=		dummy.c
21930aaff11SWarner Losh.endif
22030aaff11SWarner Losh
221f09a3cc4SRuslan Ermilov.if defined(CROSS_BUILD_TESTING)
222f09a3cc4SRuslan ErmilovSUBDIR=		cross-build
223f09a3cc4SRuslan Ermilov.endif
224f09a3cc4SRuslan Ermilov
225162ae9c8SAlex Richardson# To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
226162ae9c8SAlex RichardsonUFSINCS+=	${SRCTOP}/sys/ufs/ufs/dinode.h
227162ae9c8SAlex RichardsonUFSINCS+=	${SRCTOP}/sys/ufs/ufs/dir.h
228162ae9c8SAlex RichardsonFFSINCS+=	${SRCTOP}/sys/ufs/ffs/fs.h
229162ae9c8SAlex Richardson
230162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bootsect.h
231162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bpb.h
232162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/denode.h
233162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/direntry.h
234162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/fat.h
235162ae9c8SAlex RichardsonMSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
236162ae9c8SAlex RichardsonDISKINCS+=	${SRCTOP}/sys/sys/disk/bsd.h
237162ae9c8SAlex Richardson
23863889bbdSAlex Richardson# Needed to build config (since it uses libnv)
239c36f4276SMarius StroblSYSINCS+=	${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
240c36f4276SMarius Strobl		${SRCTOP}/sys/sys/dnv.h
24163889bbdSAlex Richardson
242313232ddSAlex Richardson# Needed when bootstrapping ldd (since it uses DF_1_PIE)
243313232ddSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/elf32.h
244313232ddSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/elf64.h
245313232ddSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/elf_common.h
246313232ddSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/elf_generic.h
2477ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/queue.h
2487ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/md5.h
2497ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/sbuf.h
2507ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/tree.h
251313232ddSAlex Richardson
252e7fd9688SToomas Soome# vtfontcvt is using sys/font.h
253e7fd9688SToomas SoomeSYSINCS+=	${SRCTOP}/sys/sys/font.h
2547ec1ec4fSAlex Richardson# For mkscrfil.c:
2557ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/consio.h
2567ec1ec4fSAlex Richardson# for gencat:
2577ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/nl_types.h
2587ec1ec4fSAlex Richardson# for vtfontcvt:
2597ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/fnv_hash.h
2607ec1ec4fSAlex Richardson# opensolaris compatibility
2617ec1ec4fSAlex RichardsonINCS+=	${SRCTOP}/include/elf.h
2627ec1ec4fSAlex RichardsonSYSINCS+=	${SRCTOP}/sys/sys/elf.h
263e7fd9688SToomas Soome
2648f62bca4SAlex Richardson# We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
2658f62bca4SAlex Richardson# accidentally run tools that are incompatible but happen to be in $PATH.
2668f62bca4SAlex Richardson# This is especially important when building on Linux/MacOS where many of the
2678f62bca4SAlex Richardson# programs used during the build accept different flags or generate different
2688f62bca4SAlex Richardson# output. On those platforms we only symlink the tools known to be compatible
2698f62bca4SAlex Richardson# (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
2708f62bca4SAlex Richardson# from the FreeBSD sources during the bootstrap-tools stage.
2718f62bca4SAlex Richardson
2728f62bca4SAlex Richardson# basic commands: It is fine to use the host version for all of these even on
2738f62bca4SAlex Richardson# Linux/MacOS since we only use flags that are supported by all of them.
274ea6c594cSAlex Richardson_host_tools_to_symlink=	basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
2758f62bca4SAlex Richardson	dirname echo env false find fmt gzip gunzip head hostname id ln ls \
2765c9cb96aSAlex Richardson	mkdir mv nice patch rm sh sleep stat tee time touch tr true uname uniq \
2775c9cb96aSAlex Richardson	unxz wc which xz
2788f62bca4SAlex Richardson
2798f62bca4SAlex Richardson# We also need a symlink to the absolute path to the make binary used for
2808f62bca4SAlex Richardson# the toplevel makefile. This is not necessarily the same as `which make`
2818f62bca4SAlex Richardson# since e.g. on Linux and MacOS that will be GNU make.
2828f62bca4SAlex Richardson_make_abs!=	which "${MAKE}"
2838f62bca4SAlex Richardson_host_abs_tools_to_symlink=	${_make_abs}:make ${_make_abs}:bmake
2848f62bca4SAlex Richardson
285a8b20f4fSAlexander Richardson.if ${.MAKE.OS} == "FreeBSD"
286a8b20f4fSAlexander Richardson# When building on FreeBSD we always copy the host tools instead of linking
287a8b20f4fSAlexander Richardson# into WORLDTMP to avoid issues with incompatible libraries (see r364030).
288a8b20f4fSAlexander Richardson# Note: we could create links if we don't intend to update the current machine.
289a8b20f4fSAlexander Richardson_COPY_HOST_TOOL=cp -pf
290a8b20f4fSAlexander Richardson.else
291a8b20f4fSAlexander Richardson# However, this is not necessary on Linux/macOS. Additionally, copying the host
292a8b20f4fSAlexander Richardson# tools to another directory with cp -p results in freezes on macOS Big Sur for
293a8b20f4fSAlexander Richardson# some unknown reason. It can also break building inside docker containers if
294a8b20f4fSAlexander Richardson# there are ACLs on shared volumes.
295a8b20f4fSAlexander Richardson_COPY_HOST_TOOL=ln -sfn
2967ec1ec4fSAlex Richardson_make_abs!=	which "${MAKE}"
2977ec1ec4fSAlex Richardson_host_abs_tools_to_symlink+=	${_make_abs}:make ${_make_abs}:bmake
2987ec1ec4fSAlex Richardson.if ${.MAKE.OS} == "Darwin"
2997ec1ec4fSAlex Richardson# /usr/bin/cpp may invoke xcrun:
3007ec1ec4fSAlex Richardson_host_tools_to_symlink+=xcrun
3017ec1ec4fSAlex Richardson.endif  # ${.MAKE.OS} == "Darwin"
3027ec1ec4fSAlex Richardson# On Ubuntu /bin/sh is dash which is totally useless. Let's just link bash
3037ec1ec4fSAlex Richardson# as the build sh since that will work fine.
3047ec1ec4fSAlex Richardson_host_abs_tools_to_symlink+=	/bin/bash:sh
3057ec1ec4fSAlex Richardson_host_tools_to_symlink:=${_host_tools_to_symlink:Nsh}
3067ec1ec4fSAlex Richardson.endif
3077ec1ec4fSAlex Richardson
3088f62bca4SAlex Richardsonhost-symlinks:
3098f62bca4SAlex Richardson	@echo "Linking host tools into ${DESTDIR}/bin"
3108f62bca4SAlex Richardson.for _tool in ${_host_tools_to_symlink}
3118c9e4550SJessica Clarke	@export PATH=$${PATH}:/usr/local/bin; \
3128c9e4550SJessica Clarke	source_path=`which ${_tool} || echo /dev/null/no/such`; \
3138f62bca4SAlex Richardson	if [ ! -e "$${source_path}" ] ; then \
3145bb9250eSAlex Richardson		echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
3158f62bca4SAlex Richardson	fi; \
31633176cdcSWarner Losh	rm -f "${DESTDIR}/bin/${_tool}"; \
317a8b20f4fSAlexander Richardson	${_COPY_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
3188f62bca4SAlex Richardson.endfor
3198f62bca4SAlex Richardson.for _tool in ${_host_abs_tools_to_symlink}
3208f62bca4SAlex Richardson	@source_path="${_tool:S/:/ /:[1]}"; \
3218f62bca4SAlex Richardson	target_path="${DESTDIR}/bin/${_tool:S/:/ /:[2]}"; \
3228f62bca4SAlex Richardson	if [ ! -e "$${source_path}" ] ; then \
3238f62bca4SAlex Richardson		echo "Host tool '${src_path}' is missing"; false; \
3248f62bca4SAlex Richardson	fi; \
32533176cdcSWarner Losh	rm -f "$${target_path}"; \
326a8b20f4fSAlexander Richardson	${_COPY_HOST_TOOL} "$${source_path}" "$${target_path}"
3278f62bca4SAlex Richardson.endfor
3284f47920eSKyle Evans.if exists(/usr/libexec/flua)
32933176cdcSWarner Losh	rm -f ${DESTDIR}/usr/libexec/flua
330a8b20f4fSAlexander Richardson	${_COPY_HOST_TOOL} /usr/libexec/flua ${DESTDIR}/usr/libexec/flua
3314f47920eSKyle Evans.endif
3324acc8a67SAlex Richardson
3334acc8a67SAlex Richardson# Create all the directories that are needed during the legacy, bootstrap-tools
3344acc8a67SAlex Richardson# and cross-tools stages. We do this here using mkdir since mtree may not exist
3354acc8a67SAlex Richardson# yet (this happens if we are crossbuilding from Linux/Mac).
33617adf17bSSimon J. GerratyINSTALLDIR_LIST= \
33717adf17bSSimon J. Gerraty	bin \
33817adf17bSSimon J. Gerraty	lib/casper \
33917adf17bSSimon J. Gerraty	lib/geom \
34017adf17bSSimon J. Gerraty	usr/include/casper \
34194a82666SRyan Moeller	usr/include/private/ucl \
34217adf17bSSimon J. Gerraty	usr/include/private/zstd \
34317adf17bSSimon J. Gerraty	usr/lib \
344f22a5921SKyle Evans	usr/libexec
34517adf17bSSimon J. Gerraty
3464acc8a67SAlex Richardsoninstalldirs:
34717adf17bSSimon J. Gerraty	mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
34817adf17bSSimon J. Gerraty
3498f62bca4SAlex Richardson# Link usr/bin, sbin, and usr/sbin to bin so that it doesn't matter whether a
3508f62bca4SAlex Richardson# bootstrap tool was added to WORLTMP with a symlink or by building it in the
3518f62bca4SAlex Richardson# bootstrap-tools phase. We could also overrride BINDIR when building bootstrap
3528f62bca4SAlex Richardson# tools but adding the symlinks is easier and means all tools are also
3538f62bca4SAlex Richardson# in the directory that they are installed to normally.
354b25c7179SAlex Richardson
355b25c7179SAlex Richardson.for _dir in sbin usr/sbin usr/bin
356b25c7179SAlex Richardson# delete existing directories from before r340157
3575bb9250eSAlex Richardson	@if [ -e ${DESTDIR}/${_dir} ] && [ ! -L ${DESTDIR}/${_dir} ]; then \
358b25c7179SAlex Richardson	    echo "removing old non-symlink ${DESTDIR}/${_dir}"; \
359b25c7179SAlex Richardson	    rm -rf "${DESTDIR}/${_dir}"; \
360b25c7179SAlex Richardson	fi
361b25c7179SAlex Richardson.endfor
362b25c7179SAlex Richardson	ln -sfn bin ${DESTDIR}/sbin
363b25c7179SAlex Richardson	ln -sfn ../bin ${DESTDIR}/usr/bin
364b25c7179SAlex Richardson	ln -sfn ../bin ${DESTDIR}/usr/sbin
3654acc8a67SAlex Richardson.for _group in ${INCSGROUPS:NINCS}
3664acc8a67SAlex Richardson	mkdir -p "${DESTDIR}/${${_group}DIR}"
3674acc8a67SAlex Richardson.endfor
3684acc8a67SAlex Richardson
36930aaff11SWarner Losh.include <bsd.lib.mk>
370