There are already some good tutorials on how to set up, build OpenWrt Linux distro and flash it into an embedded board/router, still I'm doing some work on how to cross-compile Node.js under OpenWrt on 2 embedded boards that uses AR9331 SOC and for that I will have to set up my cross-compile environment.

To start with I've installed Linux Debian 7.2 on a local VM to make things more portable and followed the OpenWrt Wiki instructions OpenWrt Buildroot Installation, also had some nice tips here Compiling OpenWRT.

Let's update Debian and install the required tools:

sudo apt-get update

sudo apt-get install build-essential
sudo apt-get install subversion
sudo apt-get install git-core

sudo apt-get install patch
sudo apt-get install bzip2
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install autoconf
sudo apt-get install gettext
sudo apt-get install unzip
sudo apt-get install libncurses5-dev
sudo apt-get install ncurses-term
sudo apt-get install zlib1g-dev
sudo apt-get install gawk
sudo apt-get install libz-dev
sudo apt-get install libssl-dev


Next step would be to download the OpenWrt source from git repo:

cd /home/developer/work/
git clone git://git.openwrt.org/openwrt.git openwrtsource
cd openwrtsource


Update OpenWrt Packages:

./scripts/feeds update -a
./scripts/feeds install -a


Prepare to build OpenWrt:

make defconfig
make prereq
make menuconfig

Since I'm building this as a cross-compile environment for the Atheros AR9330 SoC I'll select the Target System and some optional libs that will be required latter for node.js and other C++ application that I use for tests and leave all other options with the default value:

  • Target System (Atheros AR7xx/AR9xx)
  • Base System
    • <*> libc (C Library)
    • <*> libgcc (GCC Support Library)
    • {M} libpthread (POSIX Thread Library)
    • {M} librt (POSIX Realtime extension Library)
    • {M} libstdcpp (GNU Standard C++ Library)


Let's build the required packages, libs, etc (this will take a long time):

time make V=99

(Initial 'make' took around 3 hours)


OpenWrt Cross-compile tools should now be available here:

# Staging Dir
# /home/developer/work/openwrtsource/staging_dir/

# Cross Compile Tools
# /home/developer/work/openwrtsource/staging_dir/toolchain-mips_34kc_gcc-4.6-linaro_uClibc-0.9.33.2/bin/

# GCC Compiler
# /home/developer/work/openwrtsource/staging_dir/toolchain-mips_34kc_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-uclibc-gcc

# Check GCC version info
/home/developer/work/openwrtsource/staging_dir/toolchain-mips_34kc_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-uclibc-gcc --version


Now for the initial cross compile test let's compile a .c app to blink a Led from the TP-Link WR703N router: wr703n-blink.c

# get the source code
wget https://gist.github.com/revmischa/1576363/raw/828d3c2c96ade92b419ee0836c06166efcb65612/wr703n-blink.c

# export the required STAGING_DIR var
export STAGING_DIR=/home/developer/work/openwrtsource/staging_dir/

# call GCC to cross-compile the .c file
/home/developer/work/openwrtsource/staging_dir/toolchain-mips_34kc_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-uclibc-gcc wr703n-blink.c -o wr703n-blink

# verify if the binary is in a valid MIPS32 format
file wr703n-blink

# [wr703n-blink binary signature]
# ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), 
# with unknown capability 0x41000000 = 0xf676e75, with unknown capability 0x10000 = 0x70403, not stripped


At this point the cross-compile environment is now ready and fully functional. Now to test the above binary on the real hardware (TP-Link WR703N) it is required to flash the router with OpenWrt OS, copy the wr703n-blink cross-compiled binary (via SCP tool over the network) and run it from an SSH session... more details to follow on my next post!

This should also be a base tutorial for the following posts on how to cross-compile Node.js / libv8 (shared library).



comments powered by Disqus