Skip to content

Conversation

@nohajc
Copy link
Contributor

@nohajc nohajc commented Nov 11, 2025

At the moment, I'm porting init.krun to FreeBSD (hoping to open that PR soon) and I thought it would be neat to have the option to compile on macOS without spinning up a VM if one so chooses.

This relies on clang and lld (the LLVM linker). Plus, you need to provide path to a suitable sysroot. I tested the build by copying /usr/include, /usr/lib/aarch64-linux-gnu and /usr/lib/gcc/aarch64-linux-gnu from a Debian Bookworm system.

Just wasn't sure if I should strip the binary or not. Right now it's configured to only strip debug_info.

@nohajc
Copy link
Contributor Author

nohajc commented Nov 11, 2025

I suppose I should also document this in README if the change is accepted.

@nohajc nohajc force-pushed the init-cross-compilation branch from b59d444 to f62c49d Compare November 11, 2025 13:09
endif

OS = $(shell uname -s)
ARCH = $(shell uname -m)
Copy link
Contributor Author

@nohajc nohajc Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return arm64 on macOS or aarch64 on Linux but clang seems happy with either in the target triple.

@slp
Copy link
Collaborator

slp commented Nov 28, 2025

Having to gather all binaries for putting together a sysroot is a bit cumbersome. I was wondering if we could, perhaps, provide a Containerfile that would build init/init on macOS Podman/Docker. I think that would simplify a lot the process for the users.

@nohajc
Copy link
Contributor Author

nohajc commented Nov 28, 2025

My idea was to minimize build dependencies (i.e. it should be doable without any container runtime / virtualization solution). I first added this for FreeBSD on my branch where making a sysroot was as simple as downloading and unpacking base.txz.

Maybe I could write a similar script for Linux. For example, it's possible to use skopeo and umoci to download and extract files from any OCI image. But I'm sure one could find Debian tarballs or something like that as well.

@nohajc
Copy link
Contributor Author

nohajc commented Nov 29, 2025

Yep, you can just download these four deb packages, unpack with ar and tar and you have a working 40MB sysroot:

https://deb.debian.org/debian/pool/main/g/glibc/libc6_2.42-2_arm64.deb
https://deb.debian.org/debian/pool/main/g/glibc/libc6-dev_2.42-2_arm64.deb
https://deb.debian.org/debian/pool/main/g/gcc-12/libgcc-12-dev_12.5.0-6_arm64.deb
https://deb.debian.org/debian/pool/main/l/linux/linux-libc-dev_6.12.57-1_all.deb

@nohajc nohajc force-pushed the init-cross-compilation branch from 0c1863d to c4dadf1 Compare November 29, 2025 16:20
@nohajc
Copy link
Contributor Author

nohajc commented Nov 29, 2025

Ok, I updated the Makefile to generate a Debian sysroot automatically on macOS.
It downloads https://deb.debian.org/debian/dists/$(DEBIAN_DIST)/main/binary-$(ARCH)/Packages.xz and parses it to find the following packages:

libc6, libc6-dev, libgcc-12-dev, linux-libc-dev

These are downloaded and unpacked to linux-sysroot/. The only homebrew dependencies are lld and xz.

This should ultimately simplify homebrew packaging of libkrun as well.

Copy link
Collaborator

@slp slp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started using this patch with the homebrew package and works great. With just some minor changes we should be able to merge.

In addition to the inline comments, please reword the commit titles as "file: change in the file".

@nohajc nohajc force-pushed the init-cross-compilation branch from ded01df to 94a59cc Compare December 1, 2025 18:30
@nohajc
Copy link
Contributor Author

nohajc commented Dec 1, 2025

@slp Thanks for the review! I applied your suggestions.

@nohajc nohajc force-pushed the init-cross-compilation branch from 94a59cc to f658fd5 Compare December 1, 2025 18:41
…enerated sysroot

Signed-off-by: Jan Noha <nohajc@gmail.com>
…iant

Signed-off-by: Jan Noha <nohajc@gmail.com>
@nohajc nohajc force-pushed the init-cross-compilation branch from f658fd5 to c153bbf Compare December 1, 2025 18:59
@nohajc
Copy link
Contributor Author

nohajc commented Dec 1, 2025

Tbh, I don't like this kind of indentation where you're mixing spaces and tabs (and tabs have special meaning).
I just had to debug why my variables were not set properly and it was because they cannot be indented by a tab.

If you open the Makefile in VS Code, syntax highlighting actually reveals this. Also, it's already broken in couple of other places where a tab has been used by accident.

@nohajc nohajc requested a review from slp December 1, 2025 19:05
@slp
Copy link
Collaborator

slp commented Dec 2, 2025

Tbh, I don't like this kind of indentation where you're mixing spaces and tabs (and tabs have special meaning). I just had to debug why my variables were not set properly and it was because they cannot be indented by a tab.

If you open the Makefile in VS Code, syntax highlighting actually reveals this. Also, it's already broken in couple of other places where a tab has been used by accident.

Agreed, we should clean it up eventually.

Copy link
Collaborator

@slp slp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@slp slp merged commit 4146263 into containers:main Dec 2, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants