xref: /freebsd/tools/build/Makefile (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1# $FreeBSD$
2
3.PATH: ${.CURDIR}/../../include
4
5LIB=		egacy
6SRC=
7INCSGROUPS=	INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
8INCSGROUPS+=	MACHINESYSINCS RPCINCS
9INCS=
10
11SYSINCSDIR=	${INCLUDEDIR}/sys
12CASPERINCDIR=	${INCLUDEDIR}/casper
13# Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
14UFSINCSDIR=	${INCLUDEDIR}/ufs/ufs
15FFSINCSDIR=	${INCLUDEDIR}/ufs/ffs
16MSDOSFSINCSDIR=	${INCLUDEDIR}/fs/msdosfs
17DISKINCSDIR=	${INCLUDEDIR}/sys/disk
18MACHINESYSINCSDIR=	${INCLUDEDIR}/machine
19RPCINCSDIR=	${INCLUDEDIR}/rpc
20
21BOOTSTRAPPING?=	0
22
23
24.if ${.MAKE.OS} == "Darwin"
25_XCODE_ROOT!=xcode-select -p
26# since macOS 10.14 C headers are no longer installed in /usr but only
27# provided via the SDK
28.if ${_XCODE_ROOT} == "/Library/Developer/CommandLineTools"
29# Only command line tools installed -> host headers are in the SDKs directory
30_MACOS_SDK_DIR=${_XCODE_ROOT}/SDKs/MacOSX.sdk/
31.else
32# Full XCode installed -> host headers are below Platforms/MacOSX.platform
33_MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
34.endif
35HOST_INCLUDE_ROOT=${_MACOS_SDK_DIR}/usr/include
36.if !exists(${HOST_INCLUDE_ROOT}/stdio.h)
37.error You must install the macOS SDK (try xcode-select --install)
38.endif
39.else
40HOST_INCLUDE_ROOT=/usr/include
41.endif
42
43# Allow building libc-internal files (also on non-FreeBSD hosts)
44CFLAGS+=	-I${.CURDIR}/libc-bootstrap
45# Symbol versioning is not required for -legacy (and macOS bootstrap)
46MK_SYMVER=	no
47
48_WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
49.if ${_WITH_PWCACHEDB} == 0
50.PATH: ${.CURDIR}/../../contrib/libc-pwcache
51CFLAGS.pwcache.c+=	-I${.CURDIR}/../../contrib/libc-pwcache
52SRCS+=		pwcache.c
53.endif
54
55_WITH_STRSVIS!=	grep -c strsvis ${HOST_INCLUDE_ROOT}/vis.h 2>/dev/null || true
56.if ${_WITH_STRSVIS} == 0
57.PATH: ${.CURDIR}/../../contrib/libc-vis
58INCS+=		vis.h
59SRCS+=		vis.c unvis.c
60CFLAGS.vis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
61CFLAGS.unvis.c+=	-I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
62.endif
63
64_WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
65.if ${_WITH_REALLOCARRAY} == 0
66.PATH: ${.CURDIR}/../../lib/libc/stdlib
67INCS+=		stdlib.h
68SRCS+=		reallocarray.c
69.endif
70
71.if exists(${HOST_INCLUDE_ROOT}/sys/stat.h)
72_WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
73.else
74_WITH_UTIMENS= 0
75.endif
76.if ${_WITH_UTIMENS} == 0
77SYSINCS+=	stat.h
78SRCS+=		futimens.c utimensat.c
79.endif
80
81_WITH_EXPLICIT_BZERO!= cat ${HOST_INCLUDE_ROOT}/strings.h ${HOST_INCLUDE_ROOT}/string.h | grep -c explicit_bzero || true
82.if ${_WITH_EXPLICIT_BZERO} == 0
83# .PATH: ${SRCTOP}/sys/libkern
84# Adding sys/libkern to .PATH breaks building the cross-build compat library
85# since that attempts to build strlcpy.c from libc and adding libkern here will
86# cause it to pick the file from libkern instead (which won't compile).
87# Avoid modifying .PATH by creating a copy in the build directory instead.
88explicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
89	cp ${.ALLSRC} ${.TARGET}
90CLEANFILES+=	explicit_bzero.c
91INCS+=		strings.h
92SRCS+=		explicit_bzero.c
93.endif
94
95_WITH_FSPACECTL!= grep -c fspacectl ${HOST_INCLUDE_ROOT}/fcntl.h || true
96.if ${_WITH_FSPACECTL} == 0
97INCS+=		fcntl.h
98SRCS+=		fspacectl.c
99.endif
100
101.if exists(${HOST_INCLUDE_ROOT}/capsicum_helpers.h)
102_WITH_CAPH_ENTER!= grep -c caph_enter ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
103_WITH_CAPH_RIGHTS_LIMIT!= grep -c caph_rights_limit ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
104.endif
105.if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
106.PATH: ${SRCTOP}/lib/libcapsicum
107INCS+=		capsicum_helpers.h
108.PATH: ${SRCTOP}/lib/libcasper/libcasper
109INCS+=		libcasper.h
110.endif
111
112# rpcgen should build against the source tree rpc/types.h and not the host.
113# This is especially important on non-FreeBSD systems where the types may
114# not match.
115RPCINCS+=	${SRCTOP}/sys/rpc/types.h
116
117INCS+=	${SRCTOP}/include/mpool.h
118INCS+=	${SRCTOP}/include/ndbm.h
119INCS+=	${SRCTOP}/include/err.h
120INCS+=	${SRCTOP}/include/stringlist.h
121
122# Needed to build arc4random.c
123INCSGROUPS+=	CHACHA20INCS
124CHACHA20INCSDIR=	${INCLUDEDIR}/crypto/chacha20
125CHACHA20INCS+=	${SRCTOP}/sys/crypto/chacha20/_chacha.h \
126	${SRCTOP}/sys/crypto/chacha20/chacha.h
127
128.if ${MACHINE} == "host"
129_host_arch= ${_HOST_ARCH}
130.else
131_host_arch=${MACHINE}
132.endif
133.if ${_host_arch} == "x86_64"
134# bmake on Linux/mac often prints that instead of amd64
135_host_arch=amd64
136.elif ${_host_arch} == "aarch64"
137# Linux calls arm64, aarch64, across the board
138_host_arch=arm64
139.elif ${_host_arch:Mppc*}
140_host_arch=powerpc
141.endif
142.if ${_host_arch} == "unknown"
143# HACK: If MACHINE is unknown, assume we are building on x86
144_host_arch=amd64
145.endif
146MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/elf.h
147.if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
148INCSGROUPS+=	X86INCS
149X86INCSDIR=	${INCLUDEDIR}/x86
150X86INCS+=	${SRCTOP}/sys/x86/include/elf.h
151.endif
152
153# needed for btxld:
154MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/exec.h
155MACHINESYSINCS+=	${SRCTOP}/sys/${_host_arch}/include/reloc.h
156INCS+=	${SRCTOP}/include/a.out.h
157INCS+=	${SRCTOP}/include/nlist.h
158SYSINCS+=	${SRCTOP}/sys/sys/imgact_aout.h
159SYSINCS+=	${SRCTOP}/sys/sys/nlist_aout.h
160
161# macOS's bitstring lacks FreeBSD-specific additions used by makefs's ZFS code
162# and Linux doesn't have it at all.  Older FreeBSD versions lack recent
163# additions.
164INCS+=	${SRCTOP}/include/bitstring.h
165SYSINCS+=	${SRCTOP}/sys/sys/bitstring.h
166
167.if ${.MAKE.OS} != "FreeBSD"
168.PATH: ${.CURDIR}/cross-build
169
170# Needed by our sys/types.h wrapper
171SYSINCS+=	${SRCTOP}/sys/sys/bitcount.h
172
173# dbopen() behaves differently on Linux and FreeBSD so we ensure that we
174# bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
175# __freebsd_dbopen() so that we don't ever use the host version
176INCS+=	${SRCTOP}/include/db.h
177LIBC_SRCTOP=	${SRCTOP}/lib/libc/
178.include "${LIBC_SRCTOP}/db/Makefile.inc"
179# Do the same as we did for dbopen() for getopt() on since it's not compatible
180# on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
181.PATH: ${LIBC_SRCTOP}/stdlib
182SRCS+=	getopt.c getopt_long.c
183INCS+=	 ${SRCTOP}/include/getopt.h
184
185# getcap.c is needed for cap_mkdb:
186.PATH: ${LIBC_SRCTOP}/gen
187SRCS+=	getcap.c
188# Glibc does not provide all err*/warn* functions, and for macOS we need the
189# alias with the extra underscore.
190SRCS+=	err.c
191# Add various libbc functions that are not available in glibc:
192SRCS+=	stringlist.c setmode.c
193SRCS+=	strtonum.c merge.c heapsort.c reallocf.c
194.PATH: ${LIBC_SRCTOP}/locale
195SRCS+=	rpmatch.c
196
197.if ${.MAKE.OS} == "Linux"
198# On Linux, glibc does not provide strlcpy,strlcat or strmode.
199.PATH: ${LIBC_SRCTOP}/string
200SRCS+=	strlcpy.c strlcat.c strmode.c
201# On Linux, glibc provides ffs* but not fls*
202SRCS+=	fls.c flsl.c flsll.c
203# Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
204SRCS+=	fgetln_fallback.c fgetwln_fallback.c closefrom.c
205CFLAGS.closefrom.c+=	-DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
206	-DHAVE_DIRFD -DHAVE_SYSCONF
207# Provide getprogname/setprograme
208SRCS+=	progname.c
209# Stub implementations of fflagstostr/strtofflags
210SRCS+=	fflags.c
211.endif # ${MAKE.OS} == "Linux"
212
213.if ${.MAKE.OS} == "Darwin"
214# Standalone implementation of secure_getenv(), not available on MacOS.
215SRCS+=	secure_getenv.c
216.endif # ${MAKE.OS} == "Darwin"
217
218# Provide the same arc4random implementation on Linux/macOS
219CFLAGS.arc4random.c+=	-I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
220SRCS+=	arc4random.c arc4random_uniform.c
221
222# expand_number() is not provided by either Linux or MacOS libutil
223.PATH: ${SRCTOP}/lib/libutil
224SRCS+=	expand_number.c
225# Linux libutil also doesn't have fparseln
226SRCS+=	fparseln.c
227# A dummy sysctl for tzsetup:
228SRCS+=	fake_sysctl.c
229
230# capsicum support
231SYSINCS+=	${SRCTOP}/sys/sys/capsicum.h
232SYSINCS+=	${SRCTOP}/sys/sys/caprights.h
233SRCS+=	capsicum_stubs.c
234# XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
235# conflicts with other files. Instead copy subr_capability to the build dir.
236subr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
237	cp ${.ALLSRC} ${.TARGET}
238SRCS+=	subr_capability.c
239CLEANFILES+=	subr_capability.c
240.endif # ${MAKE.OS} != "FreeBSD"
241
242CASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
243CASPERINC+=	${SRCTOP}/lib/libcasper/services/cap_net/cap_net.h
244
245.if empty(SRCS)
246SRCS=		dummy.c
247.endif
248
249.if defined(CROSS_BUILD_TESTING)
250SUBDIR=		cross-build
251.endif
252
253# To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
254UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dinode.h
255UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dir.h
256FFSINCS+=	${SRCTOP}/sys/ufs/ffs/fs.h
257
258MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bootsect.h
259MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bpb.h
260MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/denode.h
261MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/direntry.h
262MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/fat.h
263MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
264DISKINCS+=	${SRCTOP}/sys/sys/disk/bsd.h
265
266# Needed to build config (since it uses libnv)
267SYSINCS+=	${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
268		${SRCTOP}/sys/sys/dnv.h ${SRCTOP}/sys/sys/nv_namespace.h
269
270# Needed when bootstrapping ldd (since it uses DF_1_PIE)
271SYSINCS+=	${SRCTOP}/sys/sys/elf32.h
272SYSINCS+=	${SRCTOP}/sys/sys/elf64.h
273SYSINCS+=	${SRCTOP}/sys/sys/elf_common.h
274SYSINCS+=	${SRCTOP}/sys/sys/elf_generic.h
275SYSINCS+=	${SRCTOP}/sys/sys/queue.h
276SYSINCS+=	${SRCTOP}/sys/sys/md5.h
277SYSINCS+=	${SRCTOP}/sys/sys/sbuf.h
278SYSINCS+=	${SRCTOP}/sys/sys/tree.h
279
280# vtfontcvt is using sys/font.h
281SYSINCS+=	${SRCTOP}/sys/sys/font.h
282# For mkscrfil.c:
283SYSINCS+=	${SRCTOP}/sys/sys/consio.h
284# for gencat:
285INCS+=	${SRCTOP}/include/nl_types.h
286# for vtfontcvt:
287SYSINCS+=	${SRCTOP}/sys/sys/fnv_hash.h
288# opensolaris compatibility
289INCS+=	${SRCTOP}/include/elf.h
290SYSINCS+=	${SRCTOP}/sys/sys/elf.h
291SYSINCS+=	${SRCTOP}/sys/sys/ctf.h
292
293# We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
294# accidentally run tools that are incompatible but happen to be in $PATH.
295# This is especially important when building on Linux/MacOS where many of the
296# programs used during the build accept different flags or generate different
297# output. On those platforms we only symlink the tools known to be compatible
298# (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
299# from the FreeBSD sources during the bootstrap-tools stage.
300
301# basic commands: It is fine to use the host version for all of these even on
302# Linux/MacOS since we only use flags that are supported by all of them.
303_host_tools_to_symlink=	basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
304	dirname echo env false find fmt gzip gunzip head hostname id ln ls \
305	mkdir mv nice patch rm sh sleep stat tee time touch tr true uname uniq \
306	unxz wc which xz
307
308# We also need a symlink to the absolute path to the make binary used for
309# the toplevel makefile. This is not necessarily the same as `which make`
310# since e.g. on Linux and MacOS that will be GNU make.
311_make_abs!=	which "${MAKE}"
312_host_abs_tools_to_symlink=	${_make_abs}:make ${_make_abs}:bmake
313
314.if ${.MAKE.OS} == "FreeBSD"
315# When building on FreeBSD we always copy the host tools instead of linking
316# into WORLDTMP to avoid issues with incompatible libraries (see r364030).
317# Note: we could create links if we don't intend to update the current machine.
318_COPY_HOST_TOOL=cp -pf
319.else
320# However, this is not necessary on Linux/macOS. Additionally, copying the host
321# tools to another directory with cp -p results in freezes on macOS Big Sur for
322# some unknown reason. It can also break building inside docker containers if
323# there are ACLs on shared volumes.
324_COPY_HOST_TOOL=ln -sfn
325_make_abs!=	which "${MAKE}"
326_host_abs_tools_to_symlink+=	${_make_abs}:make ${_make_abs}:bmake
327.if ${.MAKE.OS} == "Darwin"
328# /usr/bin/cpp may invoke xcrun:
329_host_tools_to_symlink+=xcrun
330.endif  # ${.MAKE.OS} == "Darwin"
331# On Ubuntu /bin/sh is dash which is totally useless. Let's just link bash
332# as the build sh since that will work fine.
333_host_abs_tools_to_symlink+=	/bin/bash:sh
334_host_tools_to_symlink:=${_host_tools_to_symlink:Nsh}
335.endif
336
337host-symlinks:
338	@echo "Linking host tools into ${DESTDIR}/bin"
339.for _tool in ${_host_tools_to_symlink}
340	@export PATH=$${PATH}:/usr/local/bin; \
341	source_path=`which ${_tool} || echo /dev/null/no/such`; \
342	if [ ! -e "$${source_path}" ] ; then \
343		echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
344	fi; \
345	rm -f "${DESTDIR}/bin/${_tool}"; \
346	${_COPY_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
347.endfor
348.for _tool in ${_host_abs_tools_to_symlink}
349	@source_path="${_tool:S/:/ /:[1]}"; \
350	target_path="${DESTDIR}/bin/${_tool:S/:/ /:[2]}"; \
351	if [ ! -e "$${source_path}" ] ; then \
352		echo "Host tool '$${source_path}' is missing"; false; \
353	fi; \
354	rm -f "$${target_path}"; \
355	${_COPY_HOST_TOOL} "$${source_path}" "$${target_path}"
356.endfor
357.if exists(/usr/libexec/flua)
358	rm -f ${DESTDIR}/usr/libexec/flua
359	${_COPY_HOST_TOOL} /usr/libexec/flua ${DESTDIR}/usr/libexec/flua
360.endif
361
362# Create all the directories that are needed during the legacy, bootstrap-tools
363# and cross-tools stages. We do this here using mkdir since mtree may not exist
364# yet (this happens if we are crossbuilding from Linux/Mac).
365INSTALLDIR_LIST= \
366	bin \
367	lib/geom \
368	usr/include/casper \
369	usr/include/private/ucl \
370	usr/include/private/zstd \
371	usr/lib \
372	usr/libdata/pkgconfig \
373	usr/libexec
374
375installdirs:
376	mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
377
378# Link usr/bin, sbin, and usr/sbin to bin so that it doesn't matter whether a
379# bootstrap tool was added to WORLTMP with a symlink or by building it in the
380# bootstrap-tools phase. We could also overrride BINDIR when building bootstrap
381# tools but adding the symlinks is easier and means all tools are also
382# in the directory that they are installed to normally.
383
384.for _dir in sbin usr/sbin usr/bin
385# delete existing directories from before r340157
386	@if [ -e ${DESTDIR}/${_dir} ] && [ ! -L ${DESTDIR}/${_dir} ]; then \
387	    echo "removing old non-symlink ${DESTDIR}/${_dir}"; \
388	    rm -rf "${DESTDIR}/${_dir}"; \
389	fi
390.endfor
391	ln -sfn bin ${DESTDIR}/sbin
392	ln -sfn ../bin ${DESTDIR}/usr/bin
393	ln -sfn ../bin ${DESTDIR}/usr/sbin
394.for _group in ${INCSGROUPS:NINCS}
395	mkdir -p "${DESTDIR}/${${_group}DIR}"
396.endfor
397
398.include <bsd.lib.mk>
399