akash-akya

akash-akya

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.

Most Liked

fhunleth

fhunleth

Co-author of Nerves

Looking at your project, I don’t think this is the issue, but let me start with a general statement since this comes up periodically.

For elixir_make projects, there are few important things to make NIFs work well with Nerves:

  1. Use macros like CC and CFLAGS instead of hardcoding calls to gcc. A lot of projects already do this, so sometimes this is really easy. See Environment variables — nerves v1.14.3.
  2. Put outputs in $(MIX_APP_PATH)/priv to keep host and target binaries separated. Nerves uses Mix’s MIX_TARGET feature which will create a new build directory for each target you compile for.
  3. Use Mix’s $(ERL_EI_INCLUDE_DIR) and $(ERL_EI_LIBDIR) instead of calling Erlang in the Makefile to figure out where Erlang header files and libraries are.

The standard procedure is to copy/paste a Makefile from another Elixir project to make it work with Nerves. For an example, see circuits_spi’s Makefile for a NIF example.

Nerves uses Buildroot to handle complicated C/C++ libraries. I took a look at libvips, and they made a few design decisions that I honestly don’t find enjoyable to work around. If I were required to use it, I’d do what was suggested and make a custom Nerves system to build libvips. The Buildroot package is at Buildroot libvips. Once that’s done, the Elixir Vix library would need to properly detect that it exists. That may work.

This is a long way of saying this:

  1. Give up trying to manually cross-compile libvips for Nerves
  2. If compiling with Nerves (checking $(CROSSCOMPILE) in the Makefile is probably easiest), make sure that the mode is PLATFORM_PROVIDED_LIBVIPS. The pkg-config call should look in the right place.
  3. If crosscompiling and not using a platform-provided libvips or it can’t be found, tell the user to build a custom Nerves system and BR2_PACKAGE_LIBVIPS=y to their nerves_defconfig. While at it, the user may want to also enable other libraries since libvips optionally works with so many.

Unfortunately, this will make Vix less interesting to Nerves users since it won’t work with the official Nerves systems, but it should work for anyone who needs it.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement