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