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