3 .PATH: ${.CURDIR}/../../include
7 INCSGROUPS= INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
8 INCSGROUPS+= MACHINESYSINCS RPCINCS
11 SYSINCSDIR= ${INCLUDEDIR}/sys
12 CASPERINCDIR= ${INCLUDEDIR}/casper
13 # Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
14 UFSINCSDIR= ${INCLUDEDIR}/ufs/ufs
15 FFSINCSDIR= ${INCLUDEDIR}/ufs/ffs
16 MSDOSFSINCSDIR= ${INCLUDEDIR}/fs/msdosfs
17 DISKINCSDIR= ${INCLUDEDIR}/sys/disk
18 MACHINESYSINCSDIR= ${INCLUDEDIR}/machine
19 RPCINCSDIR= ${INCLUDEDIR}/rpc
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/
32 # Full XCode installed -> host headers are below Platforms/MacOSX.platform
33 _MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
35 HOST_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)
40 HOST_INCLUDE_ROOT=/usr/include
43 # Allow building libc-internal files (also on non-FreeBSD hosts)
44 CFLAGS+= -I${.CURDIR}/libc-bootstrap
45 # Symbol versioning is not required for -legacy (and macOS bootstrap)
48 _WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
49 .if ${_WITH_PWCACHEDB} == 0
50 .PATH: ${.CURDIR}/../../contrib/libc-pwcache
51 CFLAGS.pwcache.c+= -I${.CURDIR}/../../contrib/libc-pwcache
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
60 CFLAGS.vis.c+= -I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
61 CFLAGS.unvis.c+= -I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
64 _WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
65 .if ${_WITH_REALLOCARRAY} == 0
66 .PATH: ${.CURDIR}/../../lib/libc/stdlib
71 .if exists(${HOST_INCLUDE_ROOT}/sys/stat.h)
72 _WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
76 .if ${_WITH_UTIMENS} == 0
78 SRCS+= futimens.c utimensat.c
81 _WITH_EXPLICIT_BZERO!= grep -c explicit_bzero ${HOST_INCLUDE_ROOT}/strings.h || 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.
88 explicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
89 cp ${.ALLSRC} ${.TARGET}
90 CLEANFILES+= explicit_bzero.c
92 SRCS+= explicit_bzero.c
95 _WITH_FSPACECTL!= grep -c fspacectl ${HOST_INCLUDE_ROOT}/fcntl.h || true
96 .if ${_WITH_FSPACECTL} == 0
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
105 .if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
106 .PATH: ${SRCTOP}/lib/libcapsicum
107 INCS+= capsicum_helpers.h
108 .PATH: ${SRCTOP}/lib/libcasper/libcasper
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
115 RPCINCS+= ${SRCTOP}/sys/rpc/types.h
117 INCS+= ${SRCTOP}/include/mpool.h
118 INCS+= ${SRCTOP}/include/ndbm.h
119 INCS+= ${SRCTOP}/include/err.h
120 INCS+= ${SRCTOP}/include/stringlist.h
122 # Needed to build arc4random.c
123 INCSGROUPS+= CHACHA20INCS
124 CHACHA20INCSDIR= ${INCLUDEDIR}/crypto/chacha20
125 CHACHA20INCS+= ${SRCTOP}/sys/crypto/chacha20/_chacha.h \
126 ${SRCTOP}/sys/crypto/chacha20/chacha.h
128 _host_arch=${MACHINE}
129 .if ${_host_arch} == "x86_64"
130 # bmake on Linux/mac often prints that instead of amd64
132 .elif ${_host_arch} == "aarch64"
133 # Linux calls arm64, aarch64, across the board
135 .elif ${_host_arch:Mppc*}
138 .if ${_host_arch} == "unknown"
139 # HACK: If MACHINE is unknown, assume we are building on x86
142 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/elf.h
143 .if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
145 X86INCSDIR= ${INCLUDEDIR}/x86
146 X86INCS+= ${SRCTOP}/sys/x86/include/elf.h
150 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/exec.h
151 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/reloc.h
152 INCS+= ${SRCTOP}/include/a.out.h
153 INCS+= ${SRCTOP}/include/nlist.h
154 SYSINCS+= ${SRCTOP}/sys/sys/imgact_aout.h
155 SYSINCS+= ${SRCTOP}/sys/sys/nlist_aout.h
157 # macOS's bitstring lacks FreeBSD-specific additions used by makefs's ZFS code
158 # and Linux doesn't have it at all. Older FreeBSD versions lack recent
160 INCS+= ${SRCTOP}/include/bitstring.h
161 SYSINCS+= ${SRCTOP}/sys/sys/bitstring.h
163 .if ${.MAKE.OS} != "FreeBSD"
164 .PATH: ${.CURDIR}/cross-build
166 # Needed by our sys/types.h wrapper
167 SYSINCS+= ${SRCTOP}/sys/sys/bitcount.h
169 # dbopen() behaves differently on Linux and FreeBSD so we ensure that we
170 # bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
171 # __freebsd_dbopen() so that we don't ever use the host version
172 INCS+= ${SRCTOP}/include/db.h
173 LIBC_SRCTOP= ${SRCTOP}/lib/libc/
174 .include "${LIBC_SRCTOP}/db/Makefile.inc"
175 # Do the same as we did for dbopen() for getopt() on since it's not compatible
176 # on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
177 .PATH: ${LIBC_SRCTOP}/stdlib
178 SRCS+= getopt.c getopt_long.c
179 INCS+= ${SRCTOP}/include/getopt.h
181 # getcap.c is needed for cap_mkdb:
182 .PATH: ${LIBC_SRCTOP}/gen
184 # Glibc does not provide all err*/warn* functions, and for macOS we need the
185 # alias with the extra underscore.
187 # Add various libbc functions that are not available in glibc:
188 SRCS+= stringlist.c setmode.c
189 SRCS+= strtonum.c merge.c heapsort.c reallocf.c
190 .PATH: ${LIBC_SRCTOP}/locale
193 .if ${.MAKE.OS} == "Linux"
194 # On Linux, glibc does not provide strlcpy,strlcat or strmode.
195 .PATH: ${LIBC_SRCTOP}/string
196 SRCS+= strlcpy.c strlcat.c strmode.c
197 # On Linux, glibc provides ffs* but not fls*
198 SRCS+= fls.c flsl.c flsll.c
199 # Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
200 SRCS+= fgetln_fallback.c fgetwln_fallback.c closefrom.c
201 CFLAGS.closefrom.c+= -DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
202 -DHAVE_DIRFD -DHAVE_SYSCONF
203 # Provide getprogname/setprograme
205 # Stub implementations of fflagstostr/strtofflags
208 # Provide the same arc4random implementation on Linux/macOS
209 CFLAGS.arc4random.c+= -I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
210 SRCS+= arc4random.c arc4random_uniform.c
212 # expand_number() is not provided by either Linux or MacOS libutil
213 .PATH: ${SRCTOP}/lib/libutil
214 SRCS+= expand_number.c
215 # Linux libutil also doesn't have fparseln
217 # A dummy sysctl for tzsetup:
221 SYSINCS+= ${SRCTOP}/sys/sys/capsicum.h
222 SYSINCS+= ${SRCTOP}/sys/sys/caprights.h
223 SRCS+= capsicum_stubs.c
224 # XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
225 # conflicts with other files. Instead copy subr_capability to the build dir.
226 subr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
227 cp ${.ALLSRC} ${.TARGET}
228 SRCS+= subr_capability.c
229 CLEANFILES+= subr_capability.c
232 CASPERINC+= ${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
233 CASPERINC+= ${SRCTOP}/lib/libcasper/services/cap_net/cap_net.h
239 .if defined(CROSS_BUILD_TESTING)
243 # To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
244 UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dinode.h
245 UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dir.h
246 FFSINCS+= ${SRCTOP}/sys/ufs/ffs/fs.h
248 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bootsect.h
249 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bpb.h
250 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/denode.h
251 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/direntry.h
252 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/fat.h
253 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
254 DISKINCS+= ${SRCTOP}/sys/sys/disk/bsd.h
256 # Needed to build config (since it uses libnv)
257 SYSINCS+= ${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
258 ${SRCTOP}/sys/sys/dnv.h ${SRCTOP}/sys/sys/nv_namespace.h
260 # Needed when bootstrapping ldd (since it uses DF_1_PIE)
261 SYSINCS+= ${SRCTOP}/sys/sys/elf32.h
262 SYSINCS+= ${SRCTOP}/sys/sys/elf64.h
263 SYSINCS+= ${SRCTOP}/sys/sys/elf_common.h
264 SYSINCS+= ${SRCTOP}/sys/sys/elf_generic.h
265 SYSINCS+= ${SRCTOP}/sys/sys/queue.h
266 SYSINCS+= ${SRCTOP}/sys/sys/md5.h
267 SYSINCS+= ${SRCTOP}/sys/sys/sbuf.h
268 SYSINCS+= ${SRCTOP}/sys/sys/tree.h
270 # vtfontcvt is using sys/font.h
271 SYSINCS+= ${SRCTOP}/sys/sys/font.h
273 SYSINCS+= ${SRCTOP}/sys/sys/consio.h
275 INCS+= ${SRCTOP}/include/nl_types.h
277 SYSINCS+= ${SRCTOP}/sys/sys/fnv_hash.h
278 # opensolaris compatibility
279 INCS+= ${SRCTOP}/include/elf.h
280 SYSINCS+= ${SRCTOP}/sys/sys/elf.h
281 SYSINCS+= ${SRCTOP}/sys/sys/ctf.h
283 # We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
284 # accidentally run tools that are incompatible but happen to be in $PATH.
285 # This is especially important when building on Linux/MacOS where many of the
286 # programs used during the build accept different flags or generate different
287 # output. On those platforms we only symlink the tools known to be compatible
288 # (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
289 # from the FreeBSD sources during the bootstrap-tools stage.
291 # basic commands: It is fine to use the host version for all of these even on
292 # Linux/MacOS since we only use flags that are supported by all of them.
293 _host_tools_to_symlink= basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
294 dirname echo env false find fmt gzip gunzip head hostname id ln ls \
295 mkdir mv nice patch rm sh sleep stat tee time touch tr true uname uniq \
298 # We also need a symlink to the absolute path to the make binary used for
299 # the toplevel makefile. This is not necessarily the same as `which make`
300 # since e.g. on Linux and MacOS that will be GNU make.
301 _make_abs!= which "${MAKE}"
302 _host_abs_tools_to_symlink= ${_make_abs}:make ${_make_abs}:bmake
304 .if ${.MAKE.OS} == "FreeBSD"
305 # When building on FreeBSD we always copy the host tools instead of linking
306 # into WORLDTMP to avoid issues with incompatible libraries (see r364030).
307 # Note: we could create links if we don't intend to update the current machine.
308 _COPY_HOST_TOOL=cp -pf
310 # However, this is not necessary on Linux/macOS. Additionally, copying the host
311 # tools to another directory with cp -p results in freezes on macOS Big Sur for
312 # some unknown reason. It can also break building inside docker containers if
313 # there are ACLs on shared volumes.
314 _COPY_HOST_TOOL=ln -sfn
315 _make_abs!= which "${MAKE}"
316 _host_abs_tools_to_symlink+= ${_make_abs}:make ${_make_abs}:bmake
317 .if ${.MAKE.OS} == "Darwin"
318 # /usr/bin/cpp may invoke xcrun:
319 _host_tools_to_symlink+=xcrun
320 .endif # ${.MAKE.OS} == "Darwin"
321 # On Ubuntu /bin/sh is dash which is totally useless. Let's just link bash
322 # as the build sh since that will work fine.
323 _host_abs_tools_to_symlink+= /bin/bash:sh
324 _host_tools_to_symlink:=${_host_tools_to_symlink:Nsh}
328 @echo "Linking host tools into ${DESTDIR}/bin"
329 .for _tool in ${_host_tools_to_symlink}
330 @export PATH=$${PATH}:/usr/local/bin; \
331 source_path=`which ${_tool} || echo /dev/null/no/such`; \
332 if [ ! -e "$${source_path}" ] ; then \
333 echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
335 rm -f "${DESTDIR}/bin/${_tool}"; \
336 ${_COPY_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
338 .for _tool in ${_host_abs_tools_to_symlink}
339 @source_path="${_tool:S/:/ /:[1]}"; \
340 target_path="${DESTDIR}/bin/${_tool:S/:/ /:[2]}"; \
341 if [ ! -e "$${source_path}" ] ; then \
342 echo "Host tool '$${source_path}' is missing"; false; \
344 rm -f "$${target_path}"; \
345 ${_COPY_HOST_TOOL} "$${source_path}" "$${target_path}"
347 .if exists(/usr/libexec/flua)
348 rm -f ${DESTDIR}/usr/libexec/flua
349 ${_COPY_HOST_TOOL} /usr/libexec/flua ${DESTDIR}/usr/libexec/flua
352 # Create all the directories that are needed during the legacy, bootstrap-tools
353 # and cross-tools stages. We do this here using mkdir since mtree may not exist
354 # yet (this happens if we are crossbuilding from Linux/Mac).
360 usr/include/private/ucl \
361 usr/include/private/zstd \
363 usr/libdata/pkgconfig \
367 mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
369 # Link usr/bin, sbin, and usr/sbin to bin so that it doesn't matter whether a
370 # bootstrap tool was added to WORLTMP with a symlink or by building it in the
371 # bootstrap-tools phase. We could also overrride BINDIR when building bootstrap
372 # tools but adding the symlinks is easier and means all tools are also
373 # in the directory that they are installed to normally.
375 .for _dir in sbin usr/sbin usr/bin
376 # delete existing directories from before r340157
377 @if [ -e ${DESTDIR}/${_dir} ] && [ ! -L ${DESTDIR}/${_dir} ]; then \
378 echo "removing old non-symlink ${DESTDIR}/${_dir}"; \
379 rm -rf "${DESTDIR}/${_dir}"; \
382 ln -sfn bin ${DESTDIR}/sbin
383 ln -sfn ../bin ${DESTDIR}/usr/bin
384 ln -sfn ../bin ${DESTDIR}/usr/sbin
385 .for _group in ${INCSGROUPS:NINCS}
386 mkdir -p "${DESTDIR}/${${_group}DIR}"
389 .include <bsd.lib.mk>