Zigler with Nerves - not a mach-o file

I am trying to use a very simple zig function in my Nerves app, but it isn’t working.

First I get an error:

Error while loading project :zigler at /Users/herman/Projects/GratWiFi/nerves/firmware/deps/zigler

The firmware compilation continues, I can see zig building my file, but the it errors out:

10:30:00.943 [info] searching for zig in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:00.945 [info] zig expected (via cache) in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:00.945 [debug] running command: /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig fmt /var/folders/01/8bmlqt_922q399bvltpk4z7h0000gn/T/Elixir.GratWiFi.Zig.Datetime/build.zig

10:30:00.990 [debug] wrote build.zig to /var/folders/01/8bmlqt_922q399bvltpk4z7h0000gn/T/Elixir.GratWiFi.Zig.Datetime/build.zig

10:30:00.998 [info] searching for zig in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:00.998 [info] zig expected (via cache) in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:00.998 [debug] running command: /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig run -lc --dep analyte -Mmain=/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/beam/sema.zig -Merl_nif=/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/beam/stub_erl_nif.zig --dep erl_nif -Mbeam=/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/beam/beam.zig -Mattributes=/var/folders/01/8bmlqt_922q399bvltpk4z7h0000gn/T/Elixir.GratWiFi.Zig.Datetime/attributes.zig --dep erl_nif --dep beam --dep attributes -Manalyte=/Users/herman/Projects/GratWiFi/nerves/firmware/lib/gratwifi/zig/.Elixir.GratWiFi.Zig.Datetime.zig

10:30:01.797 [info] searching for zig in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:01.797 [info] zig expected (via cache) in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:01.797 [debug] running command: /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig fmt /var/folders/01/8bmlqt_922q399bvltpk4z7h0000gn/T/Elixir.GratWiFi.Zig.Datetime/module.zig

10:30:01.842 [debug] wrote module code to /var/folders/01/8bmlqt_922q399bvltpk4z7h0000gn/T/Elixir.GratWiFi.Zig.Datetime/module.zig

10:30:01.842 [info] searching for zig in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:01.842 [info] zig expected (via cache) in /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig

10:30:01.842 [debug] running command: /Users/herman/Library/Caches/zigler/zig-macos-aarch64-0.13.0/zig build -Doptimize=Debug --prefix /Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv

10:30:01.923 [debug] built library at /Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so

10:30:01.934 [error] loading module Elixir.GratWiFi.Zig.Datetime {:load_failed, ~c"Failed to load NIF library: 'dlopen(/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so, 0x0002): tried: '/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (not a mach-o file), '/System/Volumes/Preboot/Cryptexes/OS/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (no such file), '/Users/herman/Projects/GratWiFi/nerves/firmware/_build/gratwifi_rpi5_dev/lib/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (not a mach-o file), '/Users/herman/Projects/GratWiFi/nerves/firmware/deps/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (not a mach-o file), '/System/Volumes/Preboot/Cryptexes/OS/Users/herman/Projects/GratWiFi/nerves/firmware/deps/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (no such file), '/Users/herman/Projects/GratWiFi/nerves/firmware/deps/zigler/priv/lib/Elixir.GratWiFi.Zig.Datetime.so' (not a mach-o file)'"}

The firmware gets build, but when you deploy it, it goes into a reboot-loop because it cannot find the nif file.

Thinking I somehow missed something, I downloaded the nerves-examples repo, and tried the hello-zig example, but this behaves the same.

I am on a Mac Mini M4, if that makes a difference.

What I also find very strange is that zigler places the so file in its own priv-dir, instead of the app.

nvm that, I had set the :otp_app to :zigler :man_facepalming:

Still causes the same mach-o error.

I think there is somewhere an issue with target architecture or with zigler. On mac the shared object libraries are not in the .so format, but instead .dylib.

I don’t remember the details exactly, but you can look at an attempt I made to make zig builder work with elixir, I know for sure it took me some time to make it work on mac as well (tested on my mac air m1):

Here is a demo of a small app using it:

The build artifacts, namely dynamic binaries, are located in _build/zig_builder_demo/priv/.

Hey @D4no0, thanks for your feedback.

I tried zigler with a non-nerves project and that works just fine, it makes a .so and .dylib in _build/dev/lib/try_zig/priv/lib.

After fixing the :otp_app, it builds them in the “same” folder in my build tree as .so files.
So the cross-compiling works.

1 Like

It’s when it then tries to dlopen it, it is expecting it to be in MacOS format instead of the cross-compiled version.

Okay, much a do about nothing…

Correcting the :otp_app apparently also solved the reboot-loop.

There is still the error on :erlang.load_nif while building the firmware, but it works.