Skip to content

How to Cross Compile an NPM module on Omega2

Steven de Salas edited this page Jan 21, 2018 · 10 revisions

So, you are here because you want to cross-compile an NPM package for the Omega2. Am I guessing right? You've tried to npm install something on the tiny Omega2 and its fallen over with some weird node-gyp error. Bummer! Did you figure out why?

Maybe you know this already but embedded devices are designed with such small profiles that common build tools (like make, gcc and g++ compilers etc) don't work on them.

What you are supposed to do is go to an alternative more-powerful computer running a standard linux operating system (like Ubuntu), download a bunch of tools, and compile the compilers for the embedded system, then use the resulting compilers (specially built to produce binaries for the embedded system) to cross-compile your npm module (on the more powerful machine), and copy it over to the Omega2.

So some steps to follow:

  1. Read this, this and this and this for some references to fall back on if the steps here fail to go hunky-dory.

  2. Get Ubuntu 14.04 VM. I used VirtualBox and Ubuntu 14.04.5, but you can also try a pre-installed Kubuntu VM if you want to skip the O/S install.

  3. Install build tools

$ sudo apt-get install -y subversion build-essential libncurses5-dev zlib1g-dev gawk flex quilt git-core unzip libssl-dev
  1. Cross-compile the lede source - tools + toolchain (takes ~ 1hr + 10GB disk space)
$ git clone https://github.com/WereCatf/source.git werecatf
$ cd werecatf/
$ ./scripts/feeds update -a
$ make tools/install 
$ make toolchain/install 

If you have special C header requirements (libusb or libudev for example) you might also want to run make for packages.

$ ./scripts/feeds list | grep libudev
libudev-fbsd                                Small udev shim for FreeBSD/devd (and other non-systemd)
$ ./scripts/feeds install libudev-fbsd
$ make packages/compile
  1. Install npm on the Ubuntu box. I used nvm (node version manager) to get node v4.4.5 and npm 2.15.5 installed (node v4.4.5 is available via opkg on the omega).
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.26.1/install.sh | bash
$ nvm install 4.4.5 32
  1. Do a standard npm install to build from source (remember to target mipsel architecture)
$ cd ..
$ mkdir mytest && cd mytest
$ export CC="/path/to/folder/werecatf/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-gcc"
$ export CXX="/path/to/folder/werecatf/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-g++"
$ export STAGING_DIR="/path/to/folder/werecatf/staging_dir"
$ npm install --target_arch=mipsel --build-from-source [email protected] --verbose
  1. Test it out! You'll have to work out a simple test for your npm package. I put a blinkie with serial output on my Arduino Dock (connected on /dev/ttyS2) and run a simple nodeJS script as follows:
// serialtest.js
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyS2", {
  baudrate: 9600
});
serialport.on('open', function() {
 console.log('it works!');
});
serialport.on('data', function() {
  console.log(data);
})

If this is all too much for you try asking me (by adding an issue to this repo for example, with npm package and version) and if I have some spare time I'll try doing the cross-compilation in my Ubuntu VM.

Clone this wiki locally