Skip to content

Commit 5391692

Browse files
nohajcslp
authored andcommitted
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 b7bf7f0 commit 5391692

3 files changed

Lines changed: 214 additions & 17 deletions

File tree

.gitignore

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

Makefile

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ ifeq ($(AWS_NITRO),1)
5555
FEATURE_FLAGS := --features aws-nitro,net
5656
endif
5757

58+
CLANG = /usr/bin/clang
59+
5860
OS = $(shell uname -s)
5961
ARCH = $(shell uname -m)
6062
DEBIAN_DIST ?= bookworm
6163
ROOTFS_DIR = linux-sysroot
6264
GCC_VERSION ?= 12
65+
FREEBSD_VERSION ?= 14.3-RELEASE
66+
FREEBSD_ROOTFS_DIR = freebsd-sysroot
6367

6468
KRUN_BINARY_Linux = libkrun$(VARIANT).so.$(FULL_VERSION)
6569
KRUN_SONAME_Linux = libkrun$(VARIANT).so.$(ABI_VERSION)
@@ -101,7 +105,7 @@ endif
101105
GCC_TRIPLET = $(subst arm64,aarch64,$(ARCH))-linux-gnu
102106
GCC_LIB_DIR = $(abspath $(SYSROOT_LINUX))/usr/lib/gcc/$(GCC_TRIPLET)/$(GCC_VERSION)
103107
# Cross-compile on macOS with the LLVM linker (brew install lld)
104-
CC_LINUX=/usr/bin/clang -target $(GCC_TRIPLET) -fuse-ld=lld -Wl,-strip-debug --sysroot $(abspath $(SYSROOT_LINUX)) -B$(GCC_LIB_DIR) -L$(GCC_LIB_DIR) -Wno-c23-extensions
108+
CC_LINUX=$(CLANG) -target $(GCC_TRIPLET) -fuse-ld=lld -Wl,-strip-debug --sysroot $(abspath $(SYSROOT_LINUX)) -B$(GCC_LIB_DIR) -L$(GCC_LIB_DIR) -Wno-c23-extensions
105109
else
106110
# Build on Linux host
107111
CC_LINUX=$(CC)
@@ -115,6 +119,28 @@ AWS_NITRO_INIT_BINARY= init/aws-nitro/init
115119
$(AWS_NITRO_INIT_BINARY): $(AWS_NITRO_INIT_SRC)
116120
$(CC) -O2 -static -s -Wall $(AWS_NITRO_INIT_LD_FLAGS) -o $@ $(AWS_NITRO_INIT_SRC) $(AWS_NITRO_INIT_LD_FLAGS)
117121

122+
ifeq ($(OS),Darwin)
123+
# If SYSROOT_BSD is not set and we're on macOS, generate sysroot automatically
124+
ifeq ($(SYSROOT_BSD),)
125+
SYSROOT_BSD = $(FREEBSD_ROOTFS_DIR)
126+
SYSROOT_BSD_TARGET = $(FREEBSD_ROOTFS_DIR)/.sysroot_ready
127+
else
128+
SYSROOT_BSD_TARGET =
129+
endif
130+
# Cross-compile on macOS with the LLVM linker (brew install lld)
131+
CC_BSD=$(CLANG) -target $(ARCH)-unknown-freebsd -fuse-ld=lld -stdlib=libc++ -Wl,-strip-debug --sysroot $(SYSROOT_BSD)
132+
else
133+
# Build on FreeBSD host
134+
CC_BSD=$(CC)
135+
SYSROOT_BSD_TARGET =
136+
endif
137+
138+
ifeq ($(BUILD_BSD_INIT),1)
139+
INIT_BINARY_BSD = init/init-freebsd
140+
$(INIT_BINARY_BSD): $(INIT_SRC) $(SYSROOT_BSD_TARGET)
141+
$(CC_BSD) -std=c23 -O2 -static -Wall $(INIT_DEFS) -lutil -o $@ $(INIT_SRC) $(INIT_DEFS)
142+
endif
143+
118144
# Sysroot preparation rules for cross-compilation on macOS
119145
DEBIAN_PACKAGES = libc6 libc6-dev libgcc-$(GCC_VERSION)-dev linux-libc-dev
120146
ROOTFS_TMP = $(ROOTFS_DIR)/.tmp
@@ -143,11 +169,27 @@ $(PACKAGES_FILE):
143169
@mkdir -p $(ROOTFS_TMP)
144170
@curl -fL -o $@ https://deb.debian.org/debian/dists/$(DEBIAN_DIST)/main/binary-$(ARCH)/Packages.xz
145171

172+
# FreeBSD sysroot preparation rules for cross-compilation on macOS
173+
FREEBSD_BASE_TXZ = $(FREEBSD_ROOTFS_DIR)/base.txz
174+
175+
.INTERMEDIATE: $(FREEBSD_BASE_TXZ)
176+
177+
$(FREEBSD_ROOTFS_DIR)/.sysroot_ready: $(FREEBSD_BASE_TXZ)
178+
@echo "Extracting FreeBSD base to $(FREEBSD_ROOTFS_DIR)..."
179+
@cd $(FREEBSD_ROOTFS_DIR) && tar xJf base.txz 2>/dev/null || true
180+
@touch $@
181+
182+
$(FREEBSD_BASE_TXZ):
183+
@echo "Downloading FreeBSD $(FREEBSD_VERSION) base for $(ARCH)..."
184+
@mkdir -p $(FREEBSD_ROOTFS_DIR)
185+
@curl -fL -o $@ https://download.freebsd.org/releases/$(ARCH)/$(FREEBSD_VERSION)/base.txz
186+
146187
clean-sysroot:
147188
rm -rf $(ROOTFS_DIR)
189+
rm -rf $(FREEBSD_ROOTFS_DIR)
148190

149191

150-
$(LIBRARY_RELEASE_$(OS)): $(SYSROOT_TARGET)
192+
$(LIBRARY_RELEASE_$(OS)): $(SYSROOT_TARGET) $(INIT_BINARY_BSD)
151193
cargo build --release $(FEATURE_FLAGS)
152194
ifeq ($(SEV),1)
153195
mv target/release/libkrun.so target/release/$(KRUN_BASE_$(OS))
@@ -166,7 +208,7 @@ endif
166208
endif
167209
cp target/release/$(KRUN_BASE_$(OS)) $(LIBRARY_RELEASE_$(OS))
168210

169-
$(LIBRARY_DEBUG_$(OS)): $(SYSROOT_TARGET)
211+
$(LIBRARY_DEBUG_$(OS)): $(SYSROOT_TARGET) $(INIT_BINARY_BSD)
170212
cargo build $(FEATURE_FLAGS)
171213
ifeq ($(SEV),1)
172214
mv target/debug/libkrun.so target/debug/$(KRUN_BASE_$(OS))
@@ -198,6 +240,9 @@ install: libkrun.pc
198240
cd $(DESTDIR)$(PREFIX)/$(LIBDIR_$(OS))/ ; ln -sf $(KRUN_BINARY_$(OS)) $(KRUN_SONAME_$(OS)) ; ln -sf $(KRUN_SONAME_$(OS)) $(KRUN_BASE_$(OS))
199241

200242
clean:
243+
ifeq ($(BUILD_BSD_INIT),1)
244+
rm -f $(INIT_BINARY_BSD)
245+
endif
201246
cargo clean
202247
rm -rf test-prefix
203248
cd tests; cargo clean

0 commit comments

Comments
 (0)