@@ -83,10 +83,14 @@ ifeq ($(TIMESYNC),1)
8383 INIT_DEFS += -D__TIMESYNC__
8484endif
8585
86+ CLANG = /usr/bin/clang
87+
8688OS = $(shell uname -s)
8789ARCH = $(shell uname -m)
8890DEBIAN_DIST ?= bookworm
8991ROOTFS_DIR = linux-sysroot
92+ FREEBSD_VERSION ?= 14.3-RELEASE
93+ FREEBSD_ROOTFS_DIR = freebsd-sysroot
9094
9195KRUN_BINARY_Linux = libkrun$(VARIANT ) .so.$(FULL_VERSION )
9296KRUN_SONAME_Linux = libkrun$(VARIANT ) .so.$(ABI_VERSION )
123127 SYSROOT_TARGET =
124128endif
125129 # Cross-compile on macOS with the LLVM linker (brew install lld)
126- CC_LINUX =/usr/bin/clang -target $(ARCH ) -linux-gnu -fuse-ld=lld -Wl,-strip-debug --sysroot $(SYSROOT_LINUX ) -Wno-c23-extensions
130+ CC_LINUX =$( CLANG ) -target $(ARCH ) -linux-gnu -fuse-ld=lld -Wl,-strip-debug --sysroot $(SYSROOT_LINUX ) -Wno-c23-extensions
127131else
128132 # Build on Linux host
129133 CC_LINUX =$(CC )
@@ -136,6 +140,28 @@ $(INIT_BINARY): $(INIT_SRC) $(SYSROOT_TARGET)
136140 $(CC_LINUX ) -O2 -static -Wall $(INIT_DEFS ) -o $@ $(INIT_SRC ) $(INIT_DEFS )
137141endif
138142
143+ ifeq ($(OS ) ,Darwin)
144+ # If SYSROOT_BSD is not set and we're on macOS, generate sysroot automatically
145+ ifeq ($(SYSROOT_BSD ) ,)
146+ SYSROOT_BSD = $(FREEBSD_ROOTFS_DIR )
147+ SYSROOT_BSD_TARGET = $(FREEBSD_ROOTFS_DIR ) /.sysroot_ready
148+ else
149+ SYSROOT_BSD_TARGET =
150+ endif
151+ # Cross-compile on macOS with the LLVM linker (brew install lld)
152+ CC_BSD =$(CLANG ) -target $(ARCH ) -unknown-freebsd -fuse-ld=lld -stdlib=libc++ -Wl,-strip-debug --sysroot $(SYSROOT_BSD )
153+ else
154+ # Build on FreeBSD host
155+ CC_BSD =$(CC )
156+ SYSROOT_BSD_TARGET =
157+ endif
158+
159+ ifeq ($(BUILD_BSD_INIT ) ,1)
160+ INIT_BINARY_BSD = init/init-freebsd
161+ $(INIT_BINARY_BSD ) : $(INIT_SRC ) $(SYSROOT_BSD_TARGET )
162+ $(CC_BSD ) -std=c23 -O2 -static -Wall $(INIT_DEFS ) -lutil -o $@ $(INIT_SRC ) $(INIT_DEFS )
163+ endif
164+
139165NITRO_INIT_BINARY = init/nitro/init
140166$(NITRO_INIT_BINARY ) : $(NITRO_INIT_SRC )
141167 $(CC ) -O2 -static -Wall $(NITRO_INIT_LD_FLAGS ) -o $@ $(NITRO_INIT_SRC ) $(NITRO_INIT_LD_FLAGS )
@@ -168,11 +194,27 @@ $(PACKAGES_FILE):
168194 @mkdir -p $(ROOTFS_TMP )
169195 @curl -fL -o $@ https://deb.debian.org/debian/dists/$(DEBIAN_DIST ) /main/binary-$(ARCH ) /Packages.xz
170196
197+ # FreeBSD sysroot preparation rules for cross-compilation on macOS
198+ FREEBSD_BASE_TXZ = $(FREEBSD_ROOTFS_DIR ) /base.txz
199+
200+ .INTERMEDIATE : $(FREEBSD_BASE_TXZ )
201+
202+ $(FREEBSD_ROOTFS_DIR ) /.sysroot_ready : $(FREEBSD_BASE_TXZ )
203+ @echo " Extracting FreeBSD base to $( FREEBSD_ROOTFS_DIR) ..."
204+ @cd $(FREEBSD_ROOTFS_DIR ) && tar xJf base.txz 2> /dev/null || true
205+ @touch $@
206+
207+ $(FREEBSD_BASE_TXZ ) :
208+ @echo " Downloading FreeBSD $( FREEBSD_VERSION) base for $( ARCH) ..."
209+ @mkdir -p $(FREEBSD_ROOTFS_DIR )
210+ @curl -fL -o $@ https://download.freebsd.org/releases/$(ARCH ) /$(FREEBSD_VERSION ) /base.txz
211+
171212clean-sysroot :
172213 rm -rf $(ROOTFS_DIR )
214+ rm -rf $(FREEBSD_ROOTFS_DIR )
173215
174216
175- $(LIBRARY_RELEASE_$(OS ) ) : $(INIT_BINARY )
217+ $(LIBRARY_RELEASE_$(OS ) ) : $(INIT_BINARY ) $( INIT_BINARY_BSD )
176218 cargo build --release $(FEATURE_FLAGS )
177219ifeq ($(SEV ) ,1)
178220 mv target/release/libkrun.so target/release/$(KRUN_BASE_$(OS))
@@ -191,7 +233,7 @@ endif
191233endif
192234 cp target/release/$(KRUN_BASE_$(OS)) $(LIBRARY_RELEASE_$(OS))
193235
194- $(LIBRARY_DEBUG_$(OS ) ) : $(INIT_BINARY )
236+ $(LIBRARY_DEBUG_$(OS ) ) : $(INIT_BINARY ) $( INIT_BINARY_BSD )
195237 cargo build $(FEATURE_FLAGS )
196238ifeq ($(SEV ) ,1)
197239 mv target/debug/libkrun.so target/debug/$(KRUN_BASE_$(OS))
@@ -223,7 +265,12 @@ install: libkrun.pc
223265 cd $(DESTDIR )$(PREFIX ) /$(LIBDIR_$(OS ) ) / ; ln -sf $(KRUN_BINARY_$(OS ) ) $(KRUN_SONAME_$(OS ) ) ; ln -sf $(KRUN_SONAME_$(OS ) ) $(KRUN_BASE_$(OS ) )
224266
225267clean :
268+ ifeq ($(BUILD_INIT ) ,1)
226269 rm -f $(INIT_BINARY)
270+ endif
271+ ifeq ($(BUILD_BSD_INIT ) ,1)
272+ rm -f $(INIT_BINARY_BSD)
273+ endif
227274 cargo clean
228275 rm -rf test-prefix
229276 cd tests; cargo clean
0 commit comments