Hi. I’m wondering, has anyone heard of a project that aim to use zig to build embedded linux applications? I mean running a single application on top of a custom kernel with the bare minimum.
The reasoning is, given zig’s capabilities in terms of cross compilation and C support, zig language and toolchain could be a great match for that kind of development platform.
I don’t think you need anything special for this. Zig supports Linux natively, and has tier 2 support for many architectures. This means you might run into some features that are not yet implemented. Maybe you could be more specific about what you’re trying to build?
Zig builds all Linux binaries statically by default. It invokes syscalls directly so you’re all good here. You can take that binary and copy it to any Linux distro and run it. There are no shared object nor runtime loader dependencies by default, it will run on a bare system with nothing installed. The compiler itself is actually built this way.
@00JCIV00 this looks like microcontroller programming, not embedded linux.
I know I can easily run zig code on linux (that’s what I do everyday), but I was thinking of something akin to gokrazy. The idea here is to have a kernel + a minimal system written in zig instead of a distro, and a single app running on PID 1.
A use case would be modern electronic instruments, which are more and more built as software running on top of a raspberry pi.
In the same vein, has anyone tried to compile linux with zig? I haven’t compiled the kernel for years but I guess the build process must be quite involved …
I think the monumental challenge here is the ton of configure, make, and other scripts that run as part of compiling the kernel. I guess that much of this would have to be translated into build.zig steps which would be a huge amount of work.
In this case, the build process would be the same as a normal application. The tricky part is finding out how the kernel loads systemd and replacing it with your application.
This approach makes more sense for Go because it runs on top of Linux. Zig has no such constraint. If you’re going for the minimalist and lightweight approach, you can do better and run Zig code on top of a smaller kernel or RTOS like Zephyr. Unfortunately, Zephyr is written in C and I don’t know of any kernels written in Zig, but using @cImport and the C back-end can get you pretty far.
You wouldn’t use systemd, just boot with your application as the init process (e.g. place it as /init in the initial ram disk). If you need /sys, /proc, /dev you would just make a mount syscall for each with the corresponding filesystem type and then you are set.