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