zig
zig copied to clipboard
zig cc fails to compile the linux kernel
Zig Version
0.12.0-dev.163+6780a6bbf
Steps to Reproduce and Observed Behavior
- Write zigcc, zigobjcopy, zigar and zigcxx alias scripts
- Use them with the makefile
make \
-j$(nproc) \
CC="zigcc" \
LD="zigcc" \
AR="zigar" \
OBJCOPY="zigobjcopy" \
HOSTCC="zigcc" \
HOSTCXX="zigcxx" \
HOSTAR="zigar" \
HOSTLD="zigcc" \
defconfig
Errors Encountered
HOSTCC scripts/basic/fixdep
zig: warning: argument unused during compilation: '-MD' [-Wunused-command-line-argument]
scripts/basic/fixdep.c:1:1: error: unable to build C object: FileNotFound
make[1]: *** [scripts/Makefile.host:110: scripts/basic/fixdep] Error 1
make: *** [Makefile:646: scripts_basic] Error 2
@Rexicon226 seems to have gotten further but I'm not sure how... However they encountered this:
I should also mention that zig is missing readelf, objdump, and strip which LLVM has.
Expected Behavior
Successful compilation of the Linux kernel, clang/llvm/lld works fine but zig does not.
I'm trying to do the same. Current script and source here: https://github.com/rsepassi/zigroot/blob/main/kernel/build.sh
The -MD problem is this: https://github.com/ziglang/zig/issues/9948
In the linux source tree I replaced -Wp,-MD,x with -MD -MF x and same for -MMD. But I think even with that zig cc handles those flags differently than clang. Specifically it seems that zig ignores -o if -MMD is set.
I also had to replace instances of -soname with -install_name and modify some of the linker flag passing to use -Wl instead of passing the flags directly.
I also have gcc/clang-like toolchain project here intended to cross-compile nixpkgs: https://github.com/Cloudef/zig2nix/blob/master/src/cross/toolchain.nix Various wrappers are used to mangle flags and workaround annoying build systems like meson.
There's some interesting stuff like libffi actually compiles but tests fail. You can compile something from nixpkgs like this for reference:
nix build github:Cloudef/zig2nix#zigCross.x86_64-linux-gnu.<package name>
Some failing packages can be seen here: https://github.com/Cloudef/zig2nix/blob/master/src/cross/default.nix#L41-L46
@Rexicon226 seems to have hit Zig assuming .s being used for assembly but instead its being used for linker files. Probably some sort of "inclusion context" would have to be added in so Zig understands that .s being included in a .ld/.lds file means its for a linker file.