akash-akya

akash-akya

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.

Showing Posts 1 to 3

Johnny5

Johnny5

If I understand correctly then I think this might help you:
https://github.com/nerves-project/nerves_systems

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.

akash-akya

akash-akya OP

Hey @fhunleth thanks for the detailed response.
As you said, first 3 general points related to NIF are already covered. Noted your other points. I don’t know much about Nerves and Buildroot, but I thought I just have to fix the elixir code compilation part, given I can already cross-compile NIF to the target architecture (RPi3) successfully. I guess I have to learn more about buildroot.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews