Since I gave up running NetBSD on my Raspberry pi I decided it was time to try OpenWrt. And, to my surprise I also managed to cross compile Node.js!
Install OpenWrt on Raspberry Pi (v1@700MHz)
I installed OpenWrt Barrier Breaker (the currently stable release) using the standard instructions.
After you have put the image on an SD-card with dd, it is quite easy to resize the root partition:
- copy the second partition to an image file using dd
- use fdisk to delete the second partition, and create a new, bigger
- format the new partition with mkfs.ext4
- mount the image file using mount -o loop
- mount the new second partition
- copy all data from image file to second partition using cp -a
If you want to, you can edit /etc/config/network while you are anyway working with the OpenWrt root partition:
#config interface 'lan' # option ifname 'eth0' # option type 'bridge' # option proto 'static' # option ipaddr '192.168.1.1' # option netmask '255.255.255.0' # option ip6assign '60' # option gateway '?.?.?.?' # option dns '?.?.?.?' config interface 'lan' option ifname 'eth0' option proto 'dhcp' option macaddr 'XX:XX:XX:XX:XX:XX' option hostname 'rpiopenwrt'
Probably you want to disable dnsmasq, odhcpd and firewall too:
.../etc/init.d/$ chmod -x dnsmasq firewall odhcpd OR (depending on your idea of what is the right way) .../etc/rc.d$ sudo rm S60dnsmasq S35odhcpd K85odhcpd S19firewall
Also, it is a good idea to edit config.txt (on the DOS partition):
gpu_mem=1
I don’t know if 1 is really a legal value, but it worked for me, and I had much more memory available than when gpu_mem was not set.
Node.js4 added 2015-10-03
For Node.js, check Node.js 4 builds.
Building Node.js v0.12.2
I downloaded and built Node.js v0.12.2 on a Xubuntu machine with an x64 cpu. On such a machine you can download the standard OpenWrt toolchain for Raspberry Pi.
I replaced configure and cpu.cc in the standard sources with the files from This Page (they are meant for v0.12.1 but they work equally good for v0.12.2).
I then found an a gist that gave me a good start. I modified it, and ended up with:
#!/bin/sh -e export STAGING_DIR=...path to your toolchain... #Tools export CSTOOLS="$STAGING_DIR" export CSTOOLS_INC=${CSTOOLS}/include export CSTOOLS_LIB=${CSTOOLS}/lib export ARM_TARGET_LIB=$CSTOOLS_LIB export TARGET_ARCH="-march=armv6j" #Define the cross compilators on your system export AR="arm-openwrt-linux-uclibcgnueabi-ar" export CC="arm-openwrt-linux-uclibcgnueabi-gcc" export CXX="arm-openwrt-linux-uclibcgnueabi-g++" export LINK="arm-openwrt-linux-uclibcgnueabi-g++" export CPP="arm-openwrt-linux-uclibcgnueabi-gcc -E" export LD="arm-openwrt-linux-uclibcgnueabi-ld" export AS="arm-openwrt-linux-uclibcgnueabi-as" export CCLD="arm-openwrt-linux-uclibcgnueabi-gcc ${TARGET_ARCH} ${TARGET_TUNE}" export NM="arm-openwrt-linux-uclibcgnueabi-nm" export STRIP="arm-openwrt-linux-uclibcgnueabi-strip" export OBJCOPY="arm-openwrt-linux-uclibcgnueabi-objcopy" export RANLIB="arm-openwrt-linux-uclibcgnueabi-ranlib" export F77="arm-openwrt-linux-uclibcgnueabi-g77 ${TARGET_ARCH} ${TARGET_TUNE}" unset LIBC #Define flags export CXXFLAGS="-march=armv6j" export LDFLAGS="-L${CSTOOLS_LIB} -Wl,-rpath-link,${CSTOOLS_LIB} -Wl,-O1 -Wl,--hash-style=gnu" export CFLAGS="-isystem${CSTOOLS_INC} -fexpensive-optimizations -frename-registers -fomit-frame-pointer -O2" export CPPFLAGS="-isystem${CSTOOLS_INC}" export CCFLAGS="-march=armv6j" export PATH="${CSTOOLS}/bin:$PATH" ./configure --without-snapshot --dest-cpu=arm --dest-os=linux --without-npm bash --norc
Run this script in the Node.js source directory. If everything goes fine it configures the Node.js build, and leaves you with a shell where you can simply run:
$ make
If compilation is fine, you find the node binary in the out/Release folder. Copy it to your OpenWrt Raspberry Pi.
Building Node.js v0.10.35
I first successfully built Node.js v0.10.35.
The (less refined) script for configuring that I used was:
#!/bin/sh -e export STAGING_DIR=...path to your toolchain... #Tools export CSTOOLS="$STAGING_DIR" export CSTOOLS_INC=${CSTOOLS}/include export CSTOOLS_LIB=${CSTOOLS}/lib export ARM_TARGET_LIB=$CSTOOLS_LIB export GYP_DEFINES="armv7=0" #Define our target device export TARGET_ARCH="-march=armv6" export TARGET_TUNE="-mfloat-abi=hard" #Define the cross compilators on your system export AR="arm-openwrt-linux-uclibcgnueabi-ar" export CC="arm-openwrt-linux-uclibcgnueabi-gcc" export CXX="arm-openwrt-linux-uclibcgnueabi-g++" export LINK="arm-openwrt-linux-uclibcgnueabi-g++" export CPP="arm-openwrt-linux-uclibcgnueabi-gcc -E" export LD="arm-openwrt-linux-uclibcgnueabi-ld" export AS="arm-openwrt-linux-uclibcgnueabi-as" export CCLD="arm-openwrt-linux-uclibcgnueabi-gcc ${TARGET_ARCH} ${TARGET_TUNE}" export NM="arm-openwrt-linux-uclibcgnueabi-nm" export STRIP="arm-openwrt-linux-uclibcgnueabi-strip" export OBJCOPY="arm-openwrt-linux-uclibcgnueabi-objcopy" export RANLIB="arm-openwrt-linux-uclibcgnueabi-ranlib" export F77="arm-openwrt-linux-uclibcgnueabi-g77 ${TARGET_ARCH} ${TARGET_TUNE}" unset LIBC #Define flags export CXXFLAGS="-march=armv6" export LDFLAGS="-L${CSTOOLS_LIB} -Wl,-rpath-link,${CSTOOLS_LIB} -Wl,-O1 -Wl,--hash-style=gnu" export CFLAGS="-isystem${CSTOOLS_INC} -fexpensive-optimizations -frename-registers -fomit-frame-pointer -O2 -ggdb3" export CPPFLAGS="-isystem${CSTOOLS_INC}" export CCFLAGS="-march=armv6" export PATH="${CSTOOLS}/bin:$PATH" ./configure --without-snapshot --dest-cpu=arm --dest-os=linux bash --norc
Running node on the Raspberry Pi
Back on the Raspberry Pi you need to install a few packages:
# ldd ./node libdl.so.0 => /lib/libdl.so.0 (0xb6f60000) librt.so.0 => not found libstdc++.so.6 => not found libm.so.0 => /lib/libm.so.0 (0xb6f48000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb6f34000) libpthread.so.0 => not found libc.so.0 => /lib/libc.so.0 (0xb6edf000) ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0xb6f6c000) # opkg update # opkg install librt # opkg install libstdcpp
That is all! Now you should be ready to run node. The node binary is about 13Mb (the v0.10.35 was 19Mb perhaps becuase of -ggdb3), so it is not optimal to deploy it to other typical OpenWrt hardware.
Final comments
I ran a few small programs to test, and they were fine. I guess some more testing would be appropriate. The performance is very comparable to Node.js built and executed on Raspbian.
I think RaspberryPi+OpenWrt+Node.js is a very interesting and competitive combination for microservices!
Hi,
I am new to openwrt, and I want to install node on the OpenWRT platform on raspberry PI device.
The article is really nicely explained, but I am facing problem to compile the node-XXXX on my Ubuntu machine.
Can you please elaborate steps to compile node, i.e.
– where to keep that source code,
– where to keep the script and
– what is the purpose of downloaded toolchain of openwrt?
Actually, I am receiving an Error, while executing the ‘nodejs.sh’ which I kept in Node’s downloaded source code,
ERROR : ./nodejs.sh: 38: ./nodejs.sh: ./configure: not found
I am able to understand the meaning of error, but not getting where to keep those files,
So can you please explain those steps in quiet easy manner?
Thanks in advance.
Pranita,
Compiling Node.js is not very easy if you are not used to doing those things.
Do you want me to upload a working version of Node.js for OpenWrt and Raspberry Pi?
I will try to answer your questions…
– You can keep the source code anywhere you want. I suggest you create a folder named tmp or openwrt in your home directory and unpack nodejs-v0.XX.X.tar.gz there.
– You can keep the script anywhere you want, but you (your current working directory) need to be inside the nodejs source directory (where the configure script is) when you execute the script. I keep it in the same directory as the nodejs source directory and run:
– The purpose of the toolchain?
Your Ubuntu computer probably has an x86 or x64 CPU. It comes with a compiler (gcc / g++) for that machine, that produce native x86/x64 binaries. That will NOT work for OpenWrt and the RPi. You need a compiler that RUNS on Ubuntu and produces binaries for OpenWrt/RPi. That compiler is “the toolchain”. If you are new to all this, I suggest you read: http://zo0ok.com/techfindings/archives/1487 and first try to compile a very simple hello world c-program for OpenWrt/RPi.
If you are new to C: