Skip to content

Commit 0a20864

Browse files
committed
init.c: port to FreeBSD, try to load json config from /dev/iso9660/KRUN_CONFIG
Makefile: add optional BSD build target Signed-off-by: Jan Noha <nohajc@gmail.com>
1 parent 376f256 commit 0a20864

File tree

3 files changed

+216
-17
lines changed

3 files changed

+216
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ examples/consoles
1818
examples/rootfs_fedora
1919
test-prefix
2020
/linux-sysroot
21+
/freebsd-sysroot

Makefile

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ ifeq ($(TIMESYNC),1)
8383
INIT_DEFS += -D__TIMESYNC__
8484
endif
8585

86+
CLANG = /usr/bin/clang
87+
8688
OS = $(shell uname -s)
8789
ARCH = $(shell uname -m)
8890
DEBIAN_DIST ?= bookworm
8991
ROOTFS_DIR = linux-sysroot
92+
FREEBSD_VERSION ?= 14.3-RELEASE
93+
FREEBSD_ROOTFS_DIR = freebsd-sysroot
9094

9195
KRUN_BINARY_Linux = libkrun$(VARIANT).so.$(FULL_VERSION)
9296
KRUN_SONAME_Linux = libkrun$(VARIANT).so.$(ABI_VERSION)
@@ -123,7 +127,7 @@ else
123127
SYSROOT_TARGET =
124128
endif
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
127131
else
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)
137141
endif
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+
139165
NITRO_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+
171212
clean-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)
177219
ifeq ($(SEV),1)
178220
mv target/release/libkrun.so target/release/$(KRUN_BASE_$(OS))
@@ -191,7 +233,7 @@ endif
191233
endif
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)
196238
ifeq ($(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

225267
clean:
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

Comments
 (0)