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.10.4.
  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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement