Saturday, December 29, 2012

Cross-compile DTN2 for Raspberry Pi

Compiling DTN2 on the Raspberry Pi is fairly straight forward but can take some time. Now you ask yourself can I do better? Well, yes you can!

A cross compiler is essentially the act of compiling a program on a build machine that is not necessarily the same architecture as your target architecture. For example, say you have a super fast x86-based desktop which has oodles of horsepower (ie. RAM, CPU) compared to your $25/35 Raspberry Pi. Compiling programs on your super duper fast machine takes mere minutes where the Raspberry Pi can take considerably longer. What you need is to cross compile your programs on your super fast machine and then copy/transfer them over to your Raspberry Pi and it will run as if it was compiled natively on your Pi. Neat!

You will be required to cross compile a set of libraries before finally cross compiling DTN. Performing a Google search to "Cross compile DTN2" give you lots of hits. This post will give you a consolidated guide on cross compiling DTN2 for the Raspberry PI.


Build a Tool Chain

First build yourself a tool-chain for the arm processsor. Follow this excellent post from Luis Ibanez and Andrew Maclean on building yourself a tool chain for the Raspberry Pi. I won't go into any additions details since they did such a superb job on describing the steps.

http://www.kitware.com/blog/home/post/426


Cross Compile Tcl

Once you have a tool chain which includes the C and C++ compilers proceed with the following.

> wget http://sourceforge.net/projects/tcl/files/Tcl/8.6.0/tcl8.6.0-src.tar.gz
> gunzip < tcl8.6.0-src.tar.gz | tar xvf -
> cd tcl8.6.0/unix
Now, the following will depend on where you installed the tool-chain. On my system, it was installed to /home/pato/x-tools, however your setup might be different. 



Now, you will need to export the following environment variables. Remember that this will only be temporary for the life of your terminal session (ie. when you close your terminal window, you will need to do this again). 
> export LDFLAGS="-L /home/pato/x-tools/arm-unknown-linux-gnueabi/lib"
> export CFLAGS="-I /home/pato/x-tools/arm-unknown-linux-gnueabi/include"
> export CPPFLAGS="-I /home/pato/x-tools/arm-unknown-linux-gnueabi/include"
> export CC=arm-unknown-linux-gnueabi-gcc
> export AR=arm-unknown-linux-gnueabi-ar
> export RANLIB=arm-unknown-linux-gnueabi-ranlib
> export ac_cv_func_strtod=yes
> export tcl_cv_strtod_buggy=1
Whew! Now, run configure, however make sure you modify this according to your build machine. For example, if you are running another architecture you will need to modify the "--build" option for configure.

> uname -m (will list your build machine architecture)

Now, configure and compile!

> ./configure --build i686-linux --host arm-unknown-linux-gnueabi-gcc
> make

Cross Compile Berkley DB

Now, we turn our fingers to Berkley DB. Although not necessary for DTN2 to operate we will cross compile this one anyways since we can.

Open up a new terminal and get the source from Oracle. Note that the old environment variables may mess you up so I recommend starting from scratch on the environment variables.

> http://download.oracle.com/otn/berkeley-db/db-5.3.21.tar.gz

I created an account although I think you may be able to download it without one. By whatever means you get the source, eventually get it to your build machine. I used ssh to copy the source to the build machine.

Set the following environment variables

> export CC="arm-unknown-linux-gnueabi-gcc"
> export AR="arm-unknown-linux-gnueabi-ar"
> export RANLIB="arm-unknown-linux-gnueabi-ranlib"
> export STRIP="arm-unknown-linux-gnueabi-strip"

Now, run configure

> ../dist/configure host=i686-linux --build=arm-unknown-linux-gnueabi-gcc --prefix=/home/pato/DTN/berkley
> make
> make install



Cross Compile DTN2

Coming Soon!

References

The following links where used to make this post.



1 comment: