xref: /freebsd/tools/build/Makefile (revision 805c5004fa86e26486175cd3a0bb253dcb8ec7e0)
1.PATH: ${.CURDIR}/../../include
2
3# XXX We need to include this to avoid installing MIT KRB5 includes on
4# XXX Heimdal systems. Remove the following line when Heimdal is finally
5# XXX removed.
6.include <src.opts.mk>
7
8LIB=		egacy
9SRC=
10INCSGROUPS=	INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
11INCSGROUPS+=	MACHINESYSINCS RPCINCS
12.if ${MK_MITKRB5} != "no"
13INCSGROUPS+=	EDITINC K5GSSRPCINC GSSAPIINC
14INCSGROUPS+=	K5GSSAPIINC K52GSSAPIINC KRB5INC
15.endif
16INCS=
17
18SYSINCSDIR=	${INCLUDEDIR}/sys
19CASPERINCDIR=	${INCLUDEDIR}/casper
20# Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
21UFSINCSDIR=	${INCLUDEDIR}/ufs/ufs
22FFSINCSDIR=	${INCLUDEDIR}/ufs/ffs
23MSDOSFSINCSDIR=	${INCLUDEDIR}/fs/msdosfs
24DISKINCSDIR=	${INCLUDEDIR}/sys/disk
25MACHINESYSINCSDIR=	${INCLUDEDIR}/machine
26RPCINCSDIR=	${INCLUDEDIR}/rpc
27.if ${MK_MITKRB5} != "no"
28EDITINCDIR=	${INCLUDEDIR}/edit/readline
29K5GSSRPCINCDIR=	${INCLUDEDIR}/gssrpc
30GSSAPIINCDIR=	${INCLUDEDIR}/gssapi
31K5GSSAPIINCDIR=	${INCLUDEDIR}/gssapi_krb5
32K52GSSAPIINCDIR=${INCLUDEDIR}/gssapi_krb5/gssapi
33KRB5INCDIR=	${INCLUDEDIR}/krb5
34KDB5INCDIR=	${INCLUDEDIR}/kdb5
35KADM5INCDIR=	${INCLUDEDIR}/kadm5
36.endif
37
38BOOTSTRAPPING?=	0
39
40
41.if ${.MAKE.OS} == "Darwin"
42_XCODE_ROOT!=xcode-select -p
43# since macOS 10.14 C headers are no longer installed in /usr but only
44# provided via the SDK
45.if ${_XCODE_ROOT} == "/Library/Developer/CommandLineTools"
46# Only command line tools installed -> host headers are in the SDKs directory
47_MACOS_SDK_DIR=${_XCODE_ROOT}/SDKs/MacOSX.sdk/
48.else
49# Full XCode installed -> host headers are below Platforms/MacOSX.platform
50_MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
51.endif
52HOST_INCLUDE_ROOT=${_MACOS_SDK_DIR}/usr/include
53.if !exists(${HOST_INCLUDE_ROOT}/stdio.h)
54.error You must install the macOS SDK (try xcode-select --install)
55.endif
56.else
57HOST_INCLUDE_ROOT=/usr/include
58.endif
59
60# Allow building libc-internal files (also on non-FreeBSD hosts)
61CFLAGS+=	-I${.CURDIR}/libc-bootstrap
62# Symbol versioning is not required for -legacy (and macOS bootstrap)
63MK_SYMVER=	no
64
65_WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
66.if ${_WITH_PWCACHEDB} == 0
67.PATH: ${.CURDIR}/../../contrib/libc-pwcache
68CFLAGS.pwcache.c+=	-I${.CURDIR}/../../contrib/libc-pwcache
69SRCS+=		pwcache.c
70.endif
71
72_WITH_STRSVIS!=	grep -c strsvis ${HOST_INCLUDE_ROOT}/vis.h 2>/dev/null || true
73.if ${_WITH_STRSVIS} == 0
74.PATH: ${.CURDIR}/../../contrib/libc-vis
75INCS+=		vis.h
76SRCS+=		vis.c unvis.c
77CFLAGS.vis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
78CFLAGS.unvis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
79.endif
80
81_WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
82.if ${_WITH_REALLOCARRAY} == 0
83.PATH: ${.CURDIR}/../../lib/libc/stdlib
84INCS+=		stdlib.h
85SRCS+=		reallocarray.c
86.endif
87
88.if !exists(${HOST_INCLUDE_ROOT}/stdckdint.h)
89INCS+=		stdckdint.h
90SYSINCS+=	${SRCTOP}/sys/sys/ckdint.h
91.if ${.MAKE.OS} != "FreeBSD"
92SYSINCS+=	${SRCTOP}/sys/sys/_visible.h
93.endif
94.endif
95
96.if exists(${HOST_INCLUDE_ROOT}/sys/stat.h)
97_WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
98.else
99_WITH_UTIMENS= 0
100.endif
101.if ${_WITH_UTIMENS} == 0
102SYSINCS+=	stat.h
103SRCS+=		futimens.c utimensat.c
104.endif
105
106# Define ptraddr_t for bootstrapping
107INCS+=		stddef.h
108
109_WITH_EXPLICIT_BZERO!= cat ${HOST_INCLUDE_ROOT}/strings.h ${HOST_INCLUDE_ROOT}/string.h | grep -c explicit_bzero || true
110.if ${_WITH_EXPLICIT_BZERO} == 0
111# .PATH: ${SRCTOP}/sys/libkern
112# Adding sys/libkern to .PATH breaks building the cross-build compat library
113# since that attempts to build strlcpy.c from libc and adding libkern here will
114# cause it to pick the file from libkern instead (which won't compile).
115# Avoid modifying .PATH by creating a copy in the build directory instead.
116explicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
117	cp -f ${.ALLSRC} ${.TARGET}
118CLEANFILES+=	explicit_bzero.c
119INCS+=		strings.h
120SRCS+=		explicit_bzero.c
121.endif
122
123_WITH_FSPACECTL!= grep -c fspacectl ${HOST_INCLUDE_ROOT}/fcntl.h || true
124.if ${_WITH_FSPACECTL} == 0
125INCS+=		fcntl.h
126SRCS+=		fspacectl.c
127.endif
128
129.if exists(${HOST_INCLUDE_ROOT}/capsicum_helpers.h)
130_WITH_CAPH_ENTER!= grep -c caph_enter ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
131_WITH_CAPH_RIGHTS_LIMIT!= grep -c caph_rights_limit ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
132.endif
133.if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
134.PATH: ${SRCTOP}/lib/libcapsicum
135INCS+=		capsicum_helpers.h
136.PATH: ${SRCTOP}/lib/libcasper/libcasper
137INCS+=		libcasper.h
138.endif
139
140# rpcgen should build against the source tree rpc/types.h and not the host.
141# This is especially important on non-FreeBSD systems where the types may
142# not match.
143RPCINCS+=	${SRCTOP}/sys/rpc/types.h
144
145INCS+=	${SRCTOP}/include/mpool.h
146INCS+=	${SRCTOP}/include/ndbm.h
147INCS+=	${SRCTOP}/include/err.h
148INCS+=	${SRCTOP}/include/stringlist.h
149
150# Needed to build arc4random.c
151INCSGROUPS+=	CHACHA20INCS
152CHACHA20INCSDIR=	${INCLUDEDIR}/crypto/chacha20
153CHACHA20INCS+=	${SRCTOP}/sys/crypto/chacha20/_chacha.h \
154	${SRCTOP}/sys/crypto/chacha20/chacha.h
155
156.if ${MACHINE} == "host"
157_host_arch= ${_HOST_ARCH}
158.elif ${MACHINE} == "host32"
159_host_arch= ${_HOST_ARCH32}
160.else
161_host_arch=${MACHINE}
162.endif
163.if ${_host_arch} == "x86_64"
164# bmake on Linux/mac often prints that instead of amd64
165_host_arch=amd64
166.elif ${_host_arch} == "aarch64"
167# Linux calls arm64, aarch64, across the board
168_host_arch=arm64
169.elif ${_host_arch:Mppc*}
170_host_arch=powerpc
171.endif
172.if ${_host_arch} == "unknown"
173# HACK: If MACHINE is unknown, assume we are building on x86
174_host_arch=amd64
175.endif
176MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/elf.h
177.if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
178INCSGROUPS+=	X86INCS
179X86INCSDIR=	${INCLUDEDIR}/x86
180X86INCS+=	${SRCTOP}/sys/x86/include/elf.h
181.endif
182
183# needed for btxld:
184MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/exec.h
185MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/reloc.h
186INCS+=	${SRCTOP}/include/a.out.h
187INCS+=	${SRCTOP}/include/nlist.h
188SYSINCS+=	${SRCTOP}/sys/sys/imgact_aout.h
189SYSINCS+=	${SRCTOP}/sys/sys/nlist_aout.h
190
191# macOS's bitstring lacks FreeBSD-specific additions used by makefs's ZFS code
192# and Linux doesn't have it at all.  Older FreeBSD versions lack recent
193# additions.
194INCS+=	${SRCTOP}/include/bitstring.h
195SYSINCS+=	${SRCTOP}/sys/sys/bitstring.h
196
197.if ${.MAKE.OS} != "FreeBSD"
198.PATH: ${.CURDIR}/cross-build
199
200# Needed by our sys/types.h wrapper
201SYSINCS+=	${SRCTOP}/sys/sys/bitcount.h
202
203# dbopen() behaves differently on Linux and FreeBSD so we ensure that we
204# bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
205# __freebsd_dbopen() so that we don't ever use the host version
206INCS+=	${SRCTOP}/include/db.h
207LIBC_SRCTOP=	${SRCTOP}/lib/libc/
208.include "${LIBC_SRCTOP}/db/Makefile.inc"
209# Do the same as we did for dbopen() for getopt() on since it's not compatible
210# on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
211.PATH: ${LIBC_SRCTOP}/stdlib
212SRCS+=	getopt.c getopt_long.c
213INCS+=	 ${SRCTOP}/include/getopt.h
214
215# getcap.c is needed for cap_mkdb:
216.PATH: ${LIBC_SRCTOP}/gen
217SRCS+=	getcap.c
218# Glibc does not provide all err*/warn* functions, and for macOS we need the
219# alias with the extra underscore.
220SRCS+=	err.c
221# Add various libbc functions that are not available in glibc:
222SRCS+=	stringlist.c setmode.c
223SRCS+=	strtonum.c merge.c heapsort.c reallocf.c
224.PATH: ${LIBC_SRCTOP}/locale
225SRCS+=	rpmatch.c
226
227.if ${.MAKE.OS} == "Linux"
228# On Linux, glibc does not provide strmode. It only provides strlcpy
229# and strlcat from glibc 2.38.
230.PATH: ${LIBC_SRCTOP}/string
231SRCS+=	strmode.c
232# Assume if strlcpy exists so does strlcat
233_WITH_EXPLICIT_STRLCPY!= cat ${HOST_INCLUDE_ROOT}/strings.h ${HOST_INCLUDE_ROOT}/string.h | grep -c strlcpy || true
234.if ${_WITH_EXPLICIT_STRLCPY} == 0
235SRCS+=	strlcpy.c strlcat.c
236.endif
237# On Linux, glibc provides ffs* but not fls*
238SRCS+=	fls.c flsl.c flsll.c
239# Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
240SRCS+=	fgetln_fallback.c fgetwln_fallback.c closefrom.c
241CFLAGS.closefrom.c+=	-DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
242	-DHAVE_DIRFD -DHAVE_SYSCONF
243# Provide getprogname/setprograme
244SRCS+=	progname.c
245# Provide fflagstostr/strtofflags for mtree and makefs
246# On macOS we use the host's so conflate host and target flags, which ideally
247# we'd avoid, but in practice these align for many flags, including
248# SF_IMMUTABLE, the only flag we currently set during install.
249SRCS+=	strtofflags.c
250
251# macOS has a standalone cross-build implementation, but Linux can use the same
252# ELF one as FreeBSD
253SYSINCS+=	${SRCTOP}/sys/sys/linker_set.h
254
255.endif # ${MAKE.OS} == "Linux"
256
257.if ${.MAKE.OS} == "Darwin"
258# Standalone implementation of secure_getenv(), not available on MacOS.
259SRCS+=	secure_getenv.c
260# macOS currently lacks mempcpy
261.PATH: ${LIBC_SRCTOP}/string
262SRCS+=	mempcpy.c
263.endif # ${MAKE.OS} == "Darwin"
264
265# Provide the same arc4random implementation on Linux/macOS
266CFLAGS.arc4random.c+=	-I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
267SRCS+=	arc4random.c arc4random_uniform.c
268
269# expand_number() is not provided by either Linux or MacOS libutil
270.PATH: ${SRCTOP}/lib/libutil
271SRCS+=	expand_number.c
272# Linux libutil also doesn't have fparseln
273SRCS+=	fparseln.c
274# A dummy sysctl for tzsetup:
275SRCS+=	fake_sysctl.c
276
277# capsicum support
278SYSINCS+=	${SRCTOP}/sys/sys/capsicum.h
279SYSINCS+=	${SRCTOP}/sys/sys/caprights.h
280SRCS+=	capsicum_stubs.c
281# XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
282# conflicts with other files. Instead copy subr_capability to the build dir.
283subr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
284	cp -f ${.ALLSRC} ${.TARGET}
285SRCS+=	subr_capability.c
286CLEANFILES+=	subr_capability.c
287
288# Headers needed for msdosfs use in makefs
289SYSINCS+=	${SRCTOP}/sys/sys/_callout.h
290SYSINCS+=	${SRCTOP}/sys/sys/_lock.h
291SYSINCS+=	${SRCTOP}/sys/sys/_lockmgr.h
292SYSINCS+=	${SRCTOP}/sys/sys/_task.h
293.endif # ${MAKE.OS} != "FreeBSD"
294
295CASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
296CASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_net/cap_net.h
297
298.if ${MK_MITKRB5} != "no"
299EDITINC+=	${SRCTOP}/contrib/libedit/readline/readline.h
300K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/auth.h
301K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/auth_gss.h
302K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/auth_gssapi.h
303K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/auth_unix.h
304K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/clnt.h
305K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/netdb.h
306K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/pmap_clnt.h
307K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/pmap_prot.h
308K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/pmap_rmt.h
309K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/rpc_msg.h
310K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/rpc.h
311K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/svc_auth.h
312K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/svc.h
313K5GSSRPCINC+=	${SRCTOP}/krb5/include/gssrpc/types.h
314K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/xdr.h
315K5GSSRPCINC+=	${SRCTOP}/crypto/krb5/src/include/gssrpc/xdr.h
316GSSAPIINC+=	${SRCTOP}/crypto/krb5/src/lib/gssapi/krb5/gssapi_krb5.h
317GSSAPIINC+=	${SRCTOP}/crypto/krb5/src/lib/gssapi/generic/gssapi_alloc.h
318GSSAPIINC+=	${SRCTOP}/crypto/krb5/src/lib/gssapi/generic/gssapi_generic.h
319GSSAPIINC+=	${SRCTOP}/crypto/krb5/src/lib/gssapi/generic/gssapi_ext.h
320K5GSSAPIINC+=	${SRCTOP}/crypto/krb5/src/include/gssapi.h
321KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/ccselect_plugin.h
322KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/certauth_plugin.h
323KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/clpreauth_plugin.h
324KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/hostrealm_plugin.h
325KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/kadm5_auth_plugin.h
326KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/kadm5_hook_plugin.h
327KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/kdcpolicy_plugin.h
328KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/kdcpreauth_plugin.h
329KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/localauth_plugin.h
330KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/locate_plugin.h
331KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/plugin.h
332KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/preauth_plugin.h
333KRB5INC+=	${SRCTOP}/crypto/krb5/src/include/krb5/pwqual_plugin.h
334INCS+=		${SRCTOP}/crypto/krb5/src/include/gssapi.h
335INCS+=		${SRCTOP}/crypto/krb5/src/include/kdb.h
336INCS+=		${SRCTOP}/crypto/krb5/src/include/krb5.h
337INCS+=		${SRCTOP}/crypto/krb5/src/include/krad.h
338INCS+=		${SRCTOP}/crypto/krb5/src/util/et/com_err.h
339INCS+=		${SRCTOP}/crypto/krb5/src/util/verto/verto-module.h
340INCS+=		${SRCTOP}/crypto/krb5/src/util/verto/verto.h
341.endif
342
343.if empty(SRCS)
344SRCS=		dummy.c
345.endif
346
347.if defined(CROSS_BUILD_TESTING)
348SUBDIR=		cross-build
349.endif
350
351# To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
352UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dinode.h
353UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dir.h
354FFSINCS+=	${SRCTOP}/sys/ufs/ffs/fs.h
355
356MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bootsect.h
357MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bpb.h
358MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/denode.h
359MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/direntry.h
360MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/fat.h
361MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
362DISKINCS+=	${SRCTOP}/sys/sys/disk/bsd.h
363
364.if ${.MAKE.OS} == "FreeBSD"
365# Needed to build most of the things below, which include sys/cdefs.h either
366# directly or transitively
367SYSINCS+=	${SRCTOP}/sys/sys/_decls.h
368SYSINCS+=	${SRCTOP}/sys/sys/_visible.h
369SYSINCS+=	${SRCTOP}/sys/sys/cdefs.h
370.endif
371
372# Needed to build config (since it uses libnv)
373SYSINCS+=	${SRCTOP}/sys/sys/_nv.h
374SYSINCS+=	${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
375		${SRCTOP}/sys/sys/dnv.h ${SRCTOP}/sys/sys/nv_namespace.h
376
377# Needed when bootstrapping ldd (since it uses DF_1_PIE)
378SYSINCS+=	${SRCTOP}/sys/sys/elf32.h
379SYSINCS+=	${SRCTOP}/sys/sys/elf64.h
380SYSINCS+=	${SRCTOP}/sys/sys/elf_common.h
381SYSINCS+=	${SRCTOP}/sys/sys/elf_generic.h
382SYSINCS+=	${SRCTOP}/sys/sys/queue.h
383SYSINCS+=	${SRCTOP}/sys/sys/md5.h
384SYSINCS+=	${SRCTOP}/sys/sys/sbuf.h
385SYSINCS+=	${SRCTOP}/sys/sys/tree.h
386
387# vtfontcvt is using sys/font.h
388SYSINCS+=	${SRCTOP}/sys/sys/font.h
389# For mkscrfil.c:
390SYSINCS+=	${SRCTOP}/sys/sys/consio.h
391# for gencat:
392INCS+=	${SRCTOP}/include/nl_types.h
393# for vtfontcvt:
394SYSINCS+=	${SRCTOP}/sys/sys/fnv_hash.h
395# opensolaris compatibility
396INCS+=	${SRCTOP}/include/elf.h
397SYSINCS+=	${SRCTOP}/sys/sys/elf.h
398SYSINCS+=	${SRCTOP}/sys/sys/ctf.h
399# for kbdcontrol:
400SYSINCS+=	${SRCTOP}/sys/sys/kbio.h
401# for kldxref:
402SYSINCS+=	${SRCTOP}/sys/sys/module.h
403# for libmd:
404SYSINCS+=	${SRCTOP}/sys/sys/md4.h
405
406# We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
407# accidentally run tools that are incompatible but happen to be in $PATH.
408# This is especially important when building on Linux/MacOS where many of the
409# programs used during the build accept different flags or generate different
410# output. On those platforms we only symlink the tools known to be compatible
411# (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
412# from the FreeBSD sources during the bootstrap-tools stage.
413
414# basic commands: It is fine to use the host version for all of these even on
415# Linux/MacOS since we only use flags that are supported by all of them.
416_host_tools_to_symlink=	basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
417	dirname echo env false find fmt gzip gunzip head hostname id ln ls \
418	mkdir mv nice patch rm sh sleep stat tee time touch tr true uname uniq \
419	unxz wc which xz
420
421# We also need a symlink to the absolute path to the make binary used for
422# the toplevel makefile. This is not necessarily the same as `which make`
423# since e.g. on Linux and MacOS that will be GNU make.
424_make_abs!=	which "${MAKE}"
425_host_abs_tools_to_symlink=	${_make_abs}:make ${_make_abs}:bmake
426
427_LINK_HOST_TOOL=	ln -sfn
428
429.if ${.MAKE.OS} == "FreeBSD"
430# When building on FreeBSD we always copy the host tools instead of linking
431# into WORLDTMP to avoid issues with incompatible libraries (see r364030).
432# Note: we could create links if we don't intend to update the current machine.
433_COPY_HOST_TOOL=	install -p
434.else
435# However, this is not necessary on Linux/macOS. Additionally, copying the host
436# tools to another directory with cp -p results in freezes on macOS Big Sur for
437# some unknown reason. It can also break building inside docker containers if
438# there are ACLs on shared volumes.
439_COPY_HOST_TOOL=	${_LINK_HOST_TOOL}
440
441.if ${.MAKE.OS} == "Darwin"
442# /usr/bin/cpp may invoke xcrun:
443_host_tools_to_symlink+=	xcrun
444.endif  # ${.MAKE.OS} == "Darwin"
445
446# On Ubuntu /bin/sh is dash which is totally useless, and the same for modern
447# macOS. Let's just link bash as the build sh since that will work fine.
448_host_abs_tools_to_symlink+=	/bin/bash:sh
449_host_tools_to_symlink:=	${_host_tools_to_symlink:Nsh}
450.endif
451
452# We also need to symlink any non-absolute toolchain commands. Clang finds its
453# resource directory relative to itself, so CC/CXX/CPP cannot be copied, and
454# there should be no concerns about installing over the current system since we
455# don't use the toolchain during install, so that's not an issue. However,
456# before Clang 13 there was no symlink detection for FreeBSD so that was broken
457# in the same way as copying (https://reviews.llvm.org/D103346), thus create a
458# wrapper script for each to work around this and behave like a symlink.
459# Remove this hack and just use a symlink once Clang 13 can be assumed.
460# For consistency, we use the same strategy for LD.
461.include <bsd.compiler.mk>
462.if ${.MAKE.OS} == "FreeBSD" && ${COMPILER_TYPE} == "clang" && \
463    ${COMPILER_VERSION} < 130000
464_WRAP_HOST_TOOL=	sh -c "printf '\#!/bin/sh\nexec \"\%s\" \"\$$@\"\n' \"\$$0\" > \"\$$1\" && chmod +x \"\$$1\""
465.else
466_WRAP_HOST_TOOL=	${_LINK_HOST_TOOL}
467.endif
468.for var in CC CXX CPP LD
469.for X in $${_empty_var_} X
470.if !empty(${X}${var}) && !${${X}${var}:[1]:M/*} && \
471	!${_toolchain_tools_to_symlink:U:M${${X}${var}:[1]}}
472_toolchain_tools_to_symlink+=	${${X}${var}:[1]}
473.endif
474.endfor
475.endfor
476
477host-symlinks:
478	@echo "Linking host tools into ${DESTDIR}/bin"
479.for _tool in ${_host_tools_to_symlink}
480	@export PATH=$${PATH}:/usr/local/bin; \
481	source_path=`which ${_tool} || echo /dev/null/no/such`; \
482	if [ ! -e "$${source_path}" ] ; then \
483		echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
484	fi; \
485	rm -f "${DESTDIR}/bin/${_tool}"; \
486	${_COPY_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
487.endfor
488.for _tool in ${_host_abs_tools_to_symlink}
489	@source_path="${_tool:S/:/ /:[1]}"; \
490	target_path="${DESTDIR}/bin/${_tool:S/:/ /:[2]}"; \
491	if [ ! -e "$${source_path}" ] ; then \
492		echo "Host tool '$${source_path}' is missing"; false; \
493	fi; \
494	rm -f "$${target_path}"; \
495	${_COPY_HOST_TOOL} "$${source_path}" "$${target_path}"
496.endfor
497.if exists(/usr/libexec/flua)
498	rm -f ${DESTDIR}/usr/libexec/flua
499	${_COPY_HOST_TOOL} /usr/libexec/flua ${DESTDIR}/usr/libexec/flua
500.endif
501.for _tool in ${_toolchain_tools_to_symlink}
502	@export PATH=$${PATH}:/usr/local/bin; \
503	source_path=`which ${_tool} || echo /dev/null/no/such`; \
504	if [ ! -e "$${source_path}" ] ; then \
505		echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
506	fi; \
507	rm -f "${DESTDIR}/bin/${_tool}"; \
508	${_WRAP_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
509.endfor
510
511# Create all the directories that are needed during the legacy, bootstrap-tools
512# and cross-tools stages. We do this here using mkdir since mtree may not exist
513# yet (this happens if we are crossbuilding from Linux/Mac).
514INSTALLDIR_LIST= \
515	bin \
516	lib/geom \
517	usr/include/casper \
518	usr/include/openssl \
519	usr/include/private/ucl \
520	usr/include/private/yaml \
521	usr/include/private/zstd \
522	usr/lib \
523	usr/libdata/pkgconfig \
524	usr/libexec
525
526installdirs:
527	mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
528
529# Link usr/bin, sbin, and usr/sbin to bin so that it doesn't matter whether a
530# bootstrap tool was added to WORLTMP with a symlink or by building it in the
531# bootstrap-tools phase. We could also overrride BINDIR when building bootstrap
532# tools but adding the symlinks is easier and means all tools are also
533# in the directory that they are installed to normally.
534
535.for _dir in sbin usr/sbin usr/bin
536# delete existing directories from before r340157
537	@if [ -e ${DESTDIR}/${_dir} ] && [ ! -L ${DESTDIR}/${_dir} ]; then \
538	    echo "removing old non-symlink ${DESTDIR}/${_dir}"; \
539	    rm -rf "${DESTDIR}/${_dir}"; \
540	fi
541.endfor
542	ln -sfn bin ${DESTDIR}/sbin
543	ln -sfn ../bin ${DESTDIR}/usr/bin
544	ln -sfn ../bin ${DESTDIR}/usr/sbin
545.for _group in ${INCSGROUPS:NINCS}
546	mkdir -p "${DESTDIR}/${${_group}DIR}"
547.endfor
548
549.include <bsd.lib.mk>
550