Cross Compiling with NIF as compilation dependency

I am trying to add support for Nerves in Vix (an Image Processing library) and facing a blocker related to cross-compilation.

Background

Vix uses NIF to communicate with libvips library. It uses libvips introspection and elixir macros to generate code and documentation for all the libvips operations. Since macro expansion happen during elixir code compilation, Vix loads NIF during the code compilation.

Issue

When the compilation target and runtime target is same, it just loads the NIF elixir_make compiled. But in case of Nerves they both differ. For example in my case, I am trying to create firmware in macos targeting RPi3. During the elixir code compilation it tries to load the NIF which is created for RPi3 and fails. In other words, how to handle native dependencies during cross compilation.

Anyone faced similar issue, or have any workarounds?

Few possible workarounds I can think of

  • just create firmware on the target itself. i.e. create the firmware (compile) in the RPi3 itself.
  • slightly ugly, but we could hack around priv/* directory by replacing NIF files to match target after code compilation
  • dynamically load different NIF file (:erlang.load_nif(nif_path, 0)) based on current architecture. Again, this is ugly since I have to juggle between multiple NIF files for different targets.