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