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