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