Skip to content

Commit c4dadf1

Browse files
committed
Linux sysroot can be generated automatically from deb repository
Signed-off-by: Jan Noha <nohajc@gmail.com>
1 parent 42a46f9 commit c4dadf1

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ examples/external_kernel
1616
examples/nitro
1717
examples/consoles
1818
test-prefix
19+
/linux-sysroot

Makefile

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ endif
8383

8484
OS = $(shell uname -s)
8585
ARCH = $(shell uname -m)
86+
DEBIAN_DIST ?= bookworm
87+
ROOTFS_DIR = linux-sysroot
8688

8789
KRUN_BINARY_Linux = libkrun$(VARIANT).so.$(FULL_VERSION)
8890
KRUN_SONAME_Linux = libkrun$(VARIANT).so.$(ABI_VERSION)
@@ -104,30 +106,69 @@ ifeq ($(PREFIX),)
104106
PREFIX := /usr/local
105107
endif
106108

107-
.PHONY: install clean test test-prefix $(LIBRARY_RELEASE_$(OS)) $(LIBRARY_DEBUG_$(OS)) libkrun.pc
109+
.PHONY: install clean test test-prefix $(LIBRARY_RELEASE_$(OS)) $(LIBRARY_DEBUG_$(OS)) libkrun.pc clean-sysroot clean-all
108110

109111
all: $(LIBRARY_RELEASE_$(OS)) libkrun.pc
110112

111113
debug: $(LIBRARY_DEBUG_$(OS)) libkrun.pc
112114

115+
ifeq ($(OS),Darwin)
116+
# If SYSROOT_LINUX is not set and we're on macOS, generate sysroot automatically
113117
ifeq ($(SYSROOT_LINUX),)
114-
# Build on Linux host
115-
CC_LINUX=$(CC)
118+
SYSROOT_LINUX = $(ROOTFS_DIR)
119+
SYSROOT_TARGET = $(ROOTFS_DIR)/.sysroot_ready
116120
else
121+
SYSROOT_TARGET =
122+
endif
117123
# Cross-compile on macOS with the LLVM linker (brew install lld)
118124
CC_LINUX=clang -target $(ARCH)-linux-gnu -fuse-ld=lld -Wl,-strip-debug --sysroot $(SYSROOT_LINUX) -Wno-c23-extensions
125+
else
126+
# Build on Linux host
127+
CC_LINUX=$(CC)
128+
SYSROOT_TARGET =
119129
endif
120130

121131
ifeq ($(BUILD_INIT),1)
122132
INIT_BINARY = init/init
123-
$(INIT_BINARY): $(INIT_SRC)
133+
$(INIT_BINARY): $(INIT_SRC) $(SYSROOT_TARGET)
124134
$(CC_LINUX) -O2 -static -Wall $(INIT_DEFS) -o $@ $(INIT_SRC) $(INIT_DEFS)
125135
endif
126136

127137
NITRO_INIT_BINARY= init/nitro/init
128138
$(NITRO_INIT_BINARY): $(NITRO_INIT_SRC)
129139
$(CC) -O2 -static -Wall $(NITRO_INIT_LD_FLAGS) -o $@ $(NITRO_INIT_SRC) $(NITRO_INIT_LD_FLAGS)
130140

141+
# Sysroot preparation rules for cross-compilation on macOS
142+
DEBIAN_PACKAGES = libc6 libc6-dev libgcc-12-dev linux-libc-dev
143+
ROOTFS_TMP = $(ROOTFS_DIR)/.tmp
144+
PACKAGES_FILE = $(ROOTFS_TMP)/Packages.xz
145+
146+
$(ROOTFS_DIR)/.sysroot_ready: $(PACKAGES_FILE)
147+
@echo "Extracting Debian packages to $(ROOTFS_DIR)..."
148+
@for pkg in $(DEBIAN_PACKAGES); do \
149+
DEB_PATH=$$(xzcat $(PACKAGES_FILE) | sed '1,/Package: '$$pkg'$$/d' | grep Filename: | sed 's/^Filename: //' | head -n1); \
150+
DEB_URL="https://deb.debian.org/debian/$$DEB_PATH"; \
151+
DEB_NAME=$$(basename "$$DEB_PATH"); \
152+
if [ ! -f "$(ROOTFS_TMP)/$$DEB_NAME" ]; then \
153+
echo "Downloading $$DEB_URL"; \
154+
curl -fL -o "$(ROOTFS_TMP)/$$DEB_NAME" "$$DEB_URL"; \
155+
fi; \
156+
cd $(ROOTFS_TMP) && ar x "$$DEB_NAME" && cd ../..; \
157+
tar xf $(ROOTFS_TMP)/data.tar.* -C $(ROOTFS_DIR); \
158+
rm -f $(ROOTFS_TMP)/data.tar.* $(ROOTFS_TMP)/control.tar.* $(ROOTFS_TMP)/debian-binary; \
159+
done
160+
@touch $@
161+
162+
$(PACKAGES_FILE):
163+
@echo "Downloading Debian package index for $(DEBIAN_DIST)/$(ARCH)..."
164+
@mkdir -p $(ROOTFS_TMP)
165+
@curl -fL -o $@ https://deb.debian.org/debian/dists/$(DEBIAN_DIST)/main/binary-$(ARCH)/Packages.xz
166+
167+
.PHONY: clean-sysroot
168+
clean-sysroot:
169+
rm -rf $(ROOTFS_DIR)
170+
171+
131172
$(LIBRARY_RELEASE_$(OS)): $(INIT_BINARY)
132173
cargo build --release $(FEATURE_FLAGS)
133174
ifeq ($(SEV),1)
@@ -184,6 +225,8 @@ clean:
184225
rm -rf test-prefix
185226
cd tests; cargo clean
186227

228+
clean-all: clean clean-sysroot
229+
187230
test-prefix/lib64/libkrun.pc: $(LIBRARY_RELEASE_$(OS))
188231
mkdir -p test-prefix
189232
PREFIX="$$(realpath test-prefix)" make install

0 commit comments

Comments
 (0)