Using zig as compiler

Would it be interesting to use zig as compiler/linker for the object files ?
If yes how do you configure ocaml to use zig as backend compiler ?

There was a discussion here: Benefit of using zig cc?

1 Like

I managed to use partialy zig. But there might be better ways.
This was the script i used to compile :

#!/usr/local/bin/zsh -v
rm echo *.cm* *.s *.o
ocamlopt -o echo -cc clang13 -verbose -dstartup -S -inline 20 -nodynlink
rm echo echo.o

And to link

#!/usr/local/bin/zsh -v
zig cc -v -o 'echo'  \
'echo.s' \
'echo.startup.s' \
'/usr/home/x/.opam/4.13.1+options/lib/ocaml/std_exit.o' \
'/usr/home/x/.opam/4.13.1+options/lib/ocaml/stdlib.a' \
'/usr/home/x/.opam/4.13.1+options/lib/ocaml/libasmrun.a' \
'-L/usr/home/x/.opam/4.13.1+options/lib/ocaml' \
-Wl,-E -lm

Feel free if you know better ways.

1 Like

I found a simpler solution:

ocamlopt -cc "zig cc"
1 Like

When trying a specific target i get a weird error:
E.g. zig cc --target=x86_64-freebsd-gnu “$@”

zig  build-exe -target x86_64-freebsd-gnu '-L/usr/home/x/.opam/4.13.1+options/lib/ocaml'  '/tmp/camlstartup7ae26c.o' '/usr/home/x/.opam/4.13.1+options/lib/ocaml/std_exit.o' 'echo.o' '/usr/home/x/.opam/4.13.1+options/lib/ocaml/stdlib.a' '/usr/home/x/.opam/4.13.1+options/lib/ocaml/libasmrun.a' -lm

Then i get
error: LibCInstallationNotAvailable
File “caml_startup”, line 1:
No idea what it means or what is missing.

It is more a feature request. ocamlopt should have a parameter specifying which kind of compiler/linker as gcc , clang, zig use different parameters on the command line and so ocamlopt knows just what to do.

-ccopt and -cclib will do that for you. check out ocamlopt -help or man ocamlopt
note that you’re likely to not have working cross compilation beyond at the libc level if you’re lucky… because ocaml distribution bundles object files of its own which zig cc has to link. these files are in your native machine’s format and won’t allow compilation to proceed for targets with alien formats.

1 Like

This would have been a nice use case. Bummer.