2 .PATH: ${.CURDIR}/../../include
6 INCSGROUPS= INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
7 INCSGROUPS+= MACHINESYSINCS RPCINCS
10 SYSINCSDIR= ${INCLUDEDIR}/sys
11 CASPERINCDIR= ${INCLUDEDIR}/casper
12 # Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
13 UFSINCSDIR= ${INCLUDEDIR}/ufs/ufs
14 FFSINCSDIR= ${INCLUDEDIR}/ufs/ffs
15 MSDOSFSINCSDIR= ${INCLUDEDIR}/fs/msdosfs
16 DISKINCSDIR= ${INCLUDEDIR}/sys/disk
17 MACHINESYSINCSDIR= ${INCLUDEDIR}/machine
18 RPCINCSDIR= ${INCLUDEDIR}/rpc
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/
31 # Full XCode installed -> host headers are below Platforms/MacOSX.platform
32 _MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
34 HOST_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)
39 HOST_INCLUDE_ROOT=/usr/include
42 # Allow building libc-internal files (also on non-FreeBSD hosts)
43 CFLAGS+= -I${.CURDIR}/libc-bootstrap
44 # Symbol versioning is not required for -legacy (and macOS bootstrap)
47 _WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
48 .if ${_WITH_PWCACHEDB} == 0
49 .PATH: ${.CURDIR}/../../contrib/libc-pwcache
50 CFLAGS.pwcache.c+= -I${.CURDIR}/../../contrib/libc-pwcache
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
59 CFLAGS.vis.c+= -I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
60 CFLAGS.unvis.c+= -I${.CURDIR}/../../contrib/libc-vis -DHAVE_VIS=0 -DHAVE_SVIS=0
63 _WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
64 .if ${_WITH_REALLOCARRAY} == 0
65 .PATH: ${.CURDIR}/../../lib/libc/stdlib
70 .if exists(${HOST_INCLUDE_ROOT}/sys/stat.h)
71 _WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
75 .if ${_WITH_UTIMENS} == 0
77 SRCS+= futimens.c utimensat.c
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.
87 explicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
88 cp ${.ALLSRC} ${.TARGET}
89 CLEANFILES+= explicit_bzero.c
91 SRCS+= explicit_bzero.c
94 _WITH_FSPACECTL!= grep -c fspacectl ${HOST_INCLUDE_ROOT}/fcntl.h || true
95 .if ${_WITH_FSPACECTL} == 0
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
104 .if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
105 .PATH: ${SRCTOP}/lib/libcapsicum
106 INCS+= capsicum_helpers.h
107 .PATH: ${SRCTOP}/lib/libcasper/libcasper
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
114 RPCINCS+= ${SRCTOP}/sys/rpc/types.h
116 INCS+= ${SRCTOP}/include/mpool.h
117 INCS+= ${SRCTOP}/include/ndbm.h
118 INCS+= ${SRCTOP}/include/err.h
119 INCS+= ${SRCTOP}/include/stringlist.h
121 # Needed to build arc4random.c
122 INCSGROUPS+= CHACHA20INCS
123 CHACHA20INCSDIR= ${INCLUDEDIR}/crypto/chacha20
124 CHACHA20INCS+= ${SRCTOP}/sys/crypto/chacha20/_chacha.h \
125 ${SRCTOP}/sys/crypto/chacha20/chacha.h
127 .if ${MACHINE} == "host"
128 _host_arch= ${_HOST_ARCH}
130 _host_arch=${MACHINE}
132 .if ${_host_arch} == "x86_64"
133 # bmake on Linux/mac often prints that instead of amd64
135 .elif ${_host_arch} == "aarch64"
136 # Linux calls arm64, aarch64, across the board
138 .elif ${_host_arch:Mppc*}
141 .if ${_host_arch} == "unknown"
142 # HACK: If MACHINE is unknown, assume we are building on x86
145 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/elf.h
146 .if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
148 X86INCSDIR= ${INCLUDEDIR}/x86
149 X86INCS+= ${SRCTOP}/sys/x86/include/elf.h
153 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/exec.h
154 MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/reloc.h
155 INCS+= ${SRCTOP}/include/a.out.h
156 INCS+= ${SRCTOP}/include/nlist.h
157 SYSINCS+= ${SRCTOP}/sys/sys/imgact_aout.h
158 SYSINCS+= ${SRCTOP}/sys/sys/nlist_aout.h
160 # macOS's bitstring lacks FreeBSD-specific additions used by makefs's ZFS code
161 # and Linux doesn't have it at all. Older FreeBSD versions lack recent
163 INCS+= ${SRCTOP}/include/bitstring.h
164 SYSINCS+= ${SRCTOP}/sys/sys/bitstring.h
166 .if ${.MAKE.OS} != "FreeBSD"
167 .PATH: ${.CURDIR}/cross-build
169 # Needed by our sys/types.h wrapper
170 SYSINCS+= ${SRCTOP}/sys/sys/bitcount.h
172 # dbopen() behaves differently on Linux and FreeBSD so we ensure that we
173 # bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
174 # __freebsd_dbopen() so that we don't ever use the host version
175 INCS+= ${SRCTOP}/include/db.h
176 LIBC_SRCTOP= ${SRCTOP}/lib/libc/
177 .include "${LIBC_SRCTOP}/db/Makefile.inc"
178 # Do the same as we did for dbopen() for getopt() on since it's not compatible
179 # on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
180 .PATH: ${LIBC_SRCTOP}/stdlib
181 SRCS+= getopt.c getopt_long.c
182 INCS+= ${SRCTOP}/include/getopt.h
184 # getcap.c is needed for cap_mkdb:
185 .PATH: ${LIBC_SRCTOP}/gen
187 # Glibc does not provide all err*/warn* functions, and for macOS we need the
188 # alias with the extra underscore.
190 # Add various libbc functions that are not available in glibc:
191 SRCS+= stringlist.c setmode.c
192 SRCS+= strtonum.c merge.c heapsort.c reallocf.c
193 .PATH: ${LIBC_SRCTOP}/locale
196 .if ${.MAKE.OS} == "Linux"
197 # On Linux, glibc does not provide strlcpy,strlcat or strmode.
198 .PATH: ${LIBC_SRCTOP}/string
199 SRCS+= strlcpy.c strlcat.c strmode.c
200 # On Linux, glibc provides ffs* but not fls*
201 SRCS+= fls.c flsl.c flsll.c
202 # Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
203 SRCS+= fgetln_fallback.c fgetwln_fallback.c closefrom.c
204 CFLAGS.closefrom.c+= -DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
205 -DHAVE_DIRFD -DHAVE_SYSCONF
206 # Provide getprogname/setprograme
208 # Stub implementations of fflagstostr/strtofflags
211 .endif # ${MAKE.OS} == "Linux"
213 .if ${.MAKE.OS} == "Darwin"
214 # Standalone implementation of secure_getenv(), not available on MacOS.
215 SRCS+= secure_getenv.c
216 .endif # ${MAKE.OS} == "Darwin"
218 # Provide the same arc4random implementation on Linux/macOS
219 CFLAGS.arc4random.c+= -I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
220 SRCS+= arc4random.c arc4random_uniform.c
222 # expand_number() is not provided by either Linux or MacOS libutil
223 .PATH: ${SRCTOP}/lib/libutil
224 SRCS+= expand_number.c
225 # Linux libutil also doesn't have fparseln
227 # A dummy sysctl for tzsetup:
231 SYSINCS+= ${SRCTOP}/sys/sys/capsicum.h
232 SYSINCS+= ${SRCTOP}/sys/sys/caprights.h
233 SRCS+= capsicum_stubs.c
234 # XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
235 # conflicts with other files. Instead copy subr_capability to the build dir.
236 subr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
237 cp ${.ALLSRC} ${.TARGET}
238 SRCS+= subr_capability.c
239 CLEANFILES+= subr_capability.c
240 .endif # ${MAKE.OS} != "FreeBSD"
242 CASPERINC+= ${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
243 CASPERINC+= ${SRCTOP}/lib/libcasper/services/cap_net/cap_net.h
249 .if defined(CROSS_BUILD_TESTING)
253 # To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
254 UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dinode.h
255 UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dir.h
256 FFSINCS+= ${SRCTOP}/sys/ufs/ffs/fs.h
258 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bootsect.h
259 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bpb.h
260 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/denode.h
261 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/direntry.h
262 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/fat.h
263 MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
264 DISKINCS+= ${SRCTOP}/sys/sys/disk/bsd.h
266 # Needed to build config (since it uses libnv)
267 SYSINCS+= ${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
268 ${SRCTOP}/sys/sys/dnv.h ${SRCTOP}/sys/sys/nv_namespace.h
270 # Needed when bootstrapping ldd (since it uses DF_1_PIE)
271 SYSINCS+= ${SRCTOP}/sys/sys/elf32.h
272 SYSINCS+= ${SRCTOP}/sys/sys/elf64.h
273 SYSINCS+= ${SRCTOP}/sys/sys/elf_common.h
274 SYSINCS+= ${SRCTOP}/sys/sys/elf_generic.h
275 SYSINCS+= ${SRCTOP}/sys/sys/queue.h
276 SYSINCS+= ${SRCTOP}/sys/sys/md5.h
277 SYSINCS+= ${SRCTOP}/sys/sys/sbuf.h
278 SYSINCS+= ${SRCTOP}/sys/sys/tree.h
280 # vtfontcvt is using sys/font.h
281 SYSINCS+= ${SRCTOP}/sys/sys/font.h
283 SYSINCS+= ${SRCTOP}/sys/sys/consio.h
285 INCS+= ${SRCTOP}/include/nl_types.h
287 SYSINCS+= ${SRCTOP}/sys/sys/fnv_hash.h
288 # opensolaris compatibility
289 INCS+= ${SRCTOP}/include/elf.h
290 SYSINCS+= ${SRCTOP}/sys/sys/elf.h
291 SYSINCS+= ${SRCTOP}/sys/sys/ctf.h
293 SYSINCS+= ${SRCTOP}/sys/sys/kbio.h
295 # We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
296 # accidentally run tools that are incompatible but happen to be in $PATH.
297 # This is especially important when building on Linux/MacOS where many of the
298 # programs used during the build accept different flags or generate different
299 # output. On those platforms we only symlink the tools known to be compatible
300 # (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
301 # from the FreeBSD sources during the bootstrap-tools stage.
303 # basic commands: It is fine to use the host version for all of these even on
304 # Linux/MacOS since we only use flags that are supported by all of them.
305 _host_tools_to_symlink= basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
306 dirname echo env false find fmt gzip gunzip head hostname id ln ls \
307 mkdir mv nice patch rm sh sleep stat tee time touch tr true uname uniq \
310 # We also need a symlink to the absolute path to the make binary used for
311 # the toplevel makefile. This is not necessarily the same as `which make`
312 # since e.g. on Linux and MacOS that will be GNU make.
313 _make_abs!= which "${MAKE}"
314 _host_abs_tools_to_symlink= ${_make_abs}:make ${_make_abs}:bmake
316 _LINK_HOST_TOOL= ln -sfn
318 .if ${.MAKE.OS} == "FreeBSD"
319 # When building on FreeBSD we always copy the host tools instead of linking
320 # into WORLDTMP to avoid issues with incompatible libraries (see r364030).
321 # Note: we could create links if we don't intend to update the current machine.
322 _COPY_HOST_TOOL= cp -pf
324 # However, this is not necessary on Linux/macOS. Additionally, copying the host
325 # tools to another directory with cp -p results in freezes on macOS Big Sur for
326 # some unknown reason. It can also break building inside docker containers if
327 # there are ACLs on shared volumes.
328 _COPY_HOST_TOOL= ${_LINK_HOST_TOOL}
330 .if ${.MAKE.OS} == "Darwin"
331 # /usr/bin/cpp may invoke xcrun:
332 _host_tools_to_symlink+= xcrun
333 .endif # ${.MAKE.OS} == "Darwin"
335 # On Ubuntu /bin/sh is dash which is totally useless, and the same for modern
336 # macOS. Let's just link bash as the build sh since that will work fine.
337 _host_abs_tools_to_symlink+= /bin/bash:sh
338 _host_tools_to_symlink:= ${_host_tools_to_symlink:Nsh}
341 # We also need to symlink any non-absolute toolchain commands. Clang finds its
342 # resource directory relative to itself, so CC/CXX/CPP cannot be copied, and
343 # there should be no concerns about installing over the current system since we
344 # don't use the toolchain during install, so that's not an issue. However,
345 # before Clang 13 there was no symlink detection for FreeBSD so that was broken
346 # in the same way as copying (https://reviews.llvm.org/D103346), thus create a
347 # wrapper script for each to work around this and behave like a symlink.
348 # Remove this hack and just use a symlink once Clang 13 can be assumed.
349 # For consistency, we use the same strategy for LD.
350 .include <bsd.compiler.mk>
351 .if ${.MAKE.OS} == "FreeBSD" && ${COMPILER_TYPE} == "clang" && \
352 ${COMPILER_VERSION} < 130000
353 _WRAP_HOST_TOOL= sh -c "printf '\#!/bin/sh\nexec \"\%s\" \"\$$@\"\n' \"\$$0\" > \"\$$1\" && chmod +x \"\$$1\""
355 _WRAP_HOST_TOOL= ${_LINK_HOST_TOOL}
357 .for var in CC CXX CPP LD
358 .for X in $${_empty_var_} X
359 .if !empty(${X}${var}) && !${${X}${var}:M/*} && \
360 !${_toolchain_tools_to_symlink:U:M${${X}${var}}}
361 _toolchain_tools_to_symlink+= ${${X}${var}}
367 @echo "Linking host tools into ${DESTDIR}/bin"
368 .for _tool in ${_host_tools_to_symlink}
369 @export PATH=$${PATH}:/usr/local/bin; \
370 source_path=`which ${_tool} || echo /dev/null/no/such`; \
371 if [ ! -e "$${source_path}" ] ; then \
372 echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
374 rm -f "${DESTDIR}/bin/${_tool}"; \
375 ${_COPY_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
377 .for _tool in ${_host_abs_tools_to_symlink}
378 @source_path="${_tool:S/:/ /:[1]}"; \
379 target_path="${DESTDIR}/bin/${_tool:S/:/ /:[2]}"; \
380 if [ ! -e "$${source_path}" ] ; then \
381 echo "Host tool '$${source_path}' is missing"; false; \
383 rm -f "$${target_path}"; \
384 ${_COPY_HOST_TOOL} "$${source_path}" "$${target_path}"
386 .if exists(/usr/libexec/flua)
387 rm -f ${DESTDIR}/usr/libexec/flua
388 ${_COPY_HOST_TOOL} /usr/libexec/flua ${DESTDIR}/usr/libexec/flua
390 .for _tool in ${_toolchain_tools_to_symlink}
391 @export PATH=$${PATH}:/usr/local/bin; \
392 source_path=`which ${_tool} || echo /dev/null/no/such`; \
393 if [ ! -e "$${source_path}" ] ; then \
394 echo "Cannot find host tool '${_tool}' in PATH ($$PATH)." >&2; false; \
396 rm -f "${DESTDIR}/bin/${_tool}"; \
397 ${_WRAP_HOST_TOOL} "$${source_path}" "${DESTDIR}/bin/${_tool}"
400 # Create all the directories that are needed during the legacy, bootstrap-tools
401 # and cross-tools stages. We do this here using mkdir since mtree may not exist
402 # yet (this happens if we are crossbuilding from Linux/Mac).
407 usr/include/private/ucl \
408 usr/include/private/zstd \
410 usr/libdata/pkgconfig \
414 mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
416 # Link usr/bin, sbin, and usr/sbin to bin so that it doesn't matter whether a
417 # bootstrap tool was added to WORLTMP with a symlink or by building it in the
418 # bootstrap-tools phase. We could also overrride BINDIR when building bootstrap
419 # tools but adding the symlinks is easier and means all tools are also
420 # in the directory that they are installed to normally.
422 .for _dir in sbin usr/sbin usr/bin
423 # delete existing directories from before r340157
424 @if [ -e ${DESTDIR}/${_dir} ] && [ ! -L ${DESTDIR}/${_dir} ]; then \
425 echo "removing old non-symlink ${DESTDIR}/${_dir}"; \
426 rm -rf "${DESTDIR}/${_dir}"; \
429 ln -sfn bin ${DESTDIR}/sbin
430 ln -sfn ../bin ${DESTDIR}/usr/bin
431 ln -sfn ../bin ${DESTDIR}/usr/sbin
432 .for _group in ${INCSGROUPS:NINCS}
433 mkdir -p "${DESTDIR}/${${_group}DIR}"
436 .include <bsd.lib.mk>