Embedded linux with zig?

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.

1 Like

I think the Zig Embedded Group might be what you’re looking for.

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?

1 Like

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.

8 Likes

hey thanks for the replies. let me clarify:

@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.

1 Like

I’ve also wondered if Zig could greatly simplify the Linux From Scratch process given that many of its steps are just for getting gcc to run.

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.

1 Like

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.

1 Like

A simpler first step would be to try something like CC="zig cc" make.

1 Like

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.

1 Like

A sample repo, walkthrough blog post, or video of this… Now that I would love to see!

I found this: GitHub - ntegan/basic_initramfs: Create a busybox initramfs from scratch, and test with qemu

It’s probably a good starting point, I will check this out

2 Likes

Sounds awesome! Thanks for sharing, will take a look too.

Is this something that interests you? GitHub - lmbarros/pi3-zig-simplest-bare-metal: Close to the simplest possible Zig bare metal program targetting a Raspberry Pi 3.

3 Likes