Project

General

Profile

Cross-compiling NDN projects for home routers » History » Version 4

Mathias Gibbens, 09/04/2014 03:54 PM
Update OpenWRT feed information, as they've rearranged package repositories

1 1 Mathias Gibbens
Cross-compiling NDN projects for home routers
2
=============================================
3
4
Contents
5
--------
6
7
*  [Introduction](#Introduction)
8
*  [Setting up the build host](#Setting-up-the-build-host)
9
*  [OpenWRT source and toolchain](#OpenWRT-source-and-toolchain)
10
*  [DD-WRT source and toolchain](#DD-WRT-source-and-toolchain)
11
*  [Compiling ndn-cxx](#Compiling-ndn-cxx)
12
*  [Using the ndn-cxx library with other NDN projects](#Using-the-ndn-cxx-library-with-other-NDN-projects)
13
*  [Concluding remarks](#Concluding-remarks)
14
15
Introduction
16
------------
17
18 2 Mathias Gibbens
This page describes how to cross compile NDN projects to run on wireless home routers that are supported by various open source firmwares, such as [OpenWRT](https://openwrt.org/) and [DD-WRT](http://dd-wrt.com/). It's a fairly straight forward process, but it is assumed that you are familiar with basic software development in a Linux environment, have some familiarity with cross compiling in general, and aren't afraid to get your hands dirty! There are also a couple considerations that need to be addressed when targeting smaller/embedded devices, especially concerning the limited flash storage typically available on routers.
19 1 Mathias Gibbens
20
The process of setting up the build host and actually compiling the NDN project will be the same regardless of the hardware/firmware you wish to target. Follow the corresponding section that matches your firmware to get the proper cross compile environment.
21
22
Setting up the build host
23
-------------------------
24
25 3 Mathias Gibbens
Steps in this guide were tested on a system running Debian 7 (wheezy). Distribution specific commands, like installing packages, may have to be modified to suit your setup, but on the whole this guide should be applicable to any modern Linux system. All commands should be run as a normal user -- never as root!
26 1 Mathias Gibbens
27
Make sure you have the needed packages on the host system to compile the cross compile toolchain, libraries, and resulting firmware:
28
29 2 Mathias Gibbens
    sudo apt-get install build-essential subversion git-core libncurses5-dev zlib1g-dev gawk unzip pkg-config
30 1 Mathias Gibbens
31 2 Mathias Gibbens
Finally, before we actually begin the compilation process, be aware that the paths given below which reference a specific build target (like "mipsel\_mips32_uClibc-0.9.33.2") very well could be different for you. Be sure to make the appropriate adjustments to match your setup.
32 1 Mathias Gibbens
33
OpenWRT source and toolchain
34
----------------------------
35
36 2 Mathias Gibbens
Begin by fetching the current code for OpenWRT. Additional instructions are available on that project's wiki: [OpenWrt Buildroot - Installation](http://wiki.openwrt.org/doc/howto/buildroot.exigence). This guide assumes you checked out the source to your home directory.
37 1 Mathias Gibbens
38
    git clone git://git.openwrt.org/openwrt.git
39
    cd ~/openwrt/
40 4 Mathias Gibbens
41
You will need to edit the feed configuration file `feeds.conf.default` to re-enable the "oldpackages" repository. Simply find that line, and remove the leading `#`. Once done, the line should look like `src-git oldpackages http://git.openwrt.org/packages.git`. Now, fetch the current feed information and populate the package choices for the next step.
42
43 1 Mathias Gibbens
    ./scripts/feeds update -a
44
    ./scripts/feeds install -a
45
46
We need to fetch the current feed information, as that is where we get the boost, libcrypto++, and libsqlite3 libraries to run in OpenWRT.
47
48
Now, run the configuration interface and adjust the settings as you need to match your device, etc.
49
50
    cd ~/openwrt/
51
    make menuconfig
52
53 2 Mathias Gibbens
Additionally, make sure the following libraries are selected so we can compile the NDN software. (Additional libraries might be required for projects that go beyond ndn-cxx, like `libpcap` for [NFD](http://redmine.named-data.net/projects/NFD).)
54 1 Mathias Gibbens
55
    Libraries --->
56
        SSL --->
57
            libopenssl
58
        database --->
59
            libsqlite3
60
        boost-chrono
61
        boost-date_time
62
        boost-filesystem
63
        boost-iostreams
64
        boost-program_options
65
        boost-regex
66
        boost-system
67
        boost-test
68
        libcryptoxx
69
70
After you have the configuration to your liking, we need to compile the toolchain and libraries. This will take a while.
71
72
    make
73
74
Once the OpenWRT cross compile environment is compiled, we need to create two temporary symlinks so the later `./waf configure` stage will properly detect `libcrypto++`: (Again, remember that the target part of the path names may be different for you.)
75
76
    ln -s ~/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/include/crypto++ ~/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/include/cryptopp
77
    ln -s ~/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/lib/libcrypto++.so ~/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/lib/libcryptopp.so
78
79
Now the cross compile environment is ready for you to use. Proceed to [Compiling ndn-cxx](#Compiling-ndn-cxx).
80
81
DD-WRT source and toolchain
82
---------------------------
83
84 3 Mathias Gibbens
Configuring DD-WRT is a little more complicated than OpenWRT's setup. Begin by fetching the current code for DD-WRT. Additional instructions are available on that project's wiki: [Development - DD-WRT Wiki](http://www.dd-wrt.com/wiki/index.php/Development#Building_DD-WRT_from_Source). This guide assumes you checked out the source to your home directory.
85
86
    svn co svn://svn.dd-wrt.com/DD-WRT
87
    cd ~/DD-WRT/
88
89
Next, download the current cross compile toolchain for DD-WRT:
90
91
**PROBLEM** The link provided on DD-WRT's wiki page to download the toolchain is broken. I can't readily find a correct link.
92
93
    wget https://secure.dd-wrt.com/dd-wrtv2/downloads/others/sourcecode/toolchains/current-toolchains.tar.bz2
94
95
**FIXME** Need to complete section once toolchain is found.
96
97
Now the cross compile environment is ready for you to use. Proceed to [Compiling ndn-cxx](#Compiling-ndn-cxx).
98 1 Mathias Gibbens
99
Compiling ndn-cxx
100
-----------------
101
102 2 Mathias Gibbens
In this section we will compile the [ndn-cxx](http://redmine.named-data.net/projects/ndn-cxx) library. Other NDN projects should compile in a similar manner.
103 1 Mathias Gibbens
104 2 Mathias Gibbens
As mentioned in the introduction, compiling for an embedded device can present issues not normally encountered. Chief among these for us is the very limited disk space to copy files to on the router. Normal compilation of a NDN project will by default produce binaries that include debug symbols. This can add an order of magnitude to the resulting binary size (101MB vs 15MB total), making it infeasible to run on a router. Therefore we will overwrite the default compiler flags with the `CXXFLAGS` variable. Additionally, the compilation process produces a statically linked library which is several megabytes in size. Unless it is specifically needed, there's no reason to copy it to the router where it will just waste space.
105 1 Mathias Gibbens
106
Begin by fetching the current release of ndn-cxx, 0.1.0 as of this writing:
107
108
    cd
109
    wget https://github.com/named-data/ndn-cxx/archive/ndn-cxx-0.1.0.tar.gz
110
    tar xf ndn-cxx-0.1.0.tar.gz
111
    cd ndn-cxx-ndn-cxx-0.1.0/
112
113
Now, set the needed environment variables so the configuration and compilation process use the cross compile environment we have setup.
114
115 2 Mathias Gibbens
**FIXME** this is currently specific to OpenWRT
116 1 Mathias Gibbens
117
    export TOOLCHAIN_PATH=$HOME/openwrt/staging_dir/toolchain-mipsel_mips32_gcc-4.8-linaro_uClibc-0.9.33.2/bin
118
    export CROSSCOMPILE_PATH=$HOME/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr
119
    export CXX=$TOOLCHAIN_PATH/mipsel-openwrt-linux-uclibc-g++
120
    export AR=$TOOLCHAIN_PATH/mipsel-openwrt-linux-uclibc-ar
121
    export CXXFLAGS="-O2"
122
    export CFLAGS="-I$CROSSCOMPILE_PATH/include"
123
    export LDFLAGS="-L$CROSSCOMPILE_PATH/lib -lz"
124
125
Finally, configure and build the library. We will install the resulting binaries and source files into a folder within our home directory so we don't accidentally contaminate our host system with non-native binaries.
126
127
    ./waf configure --prefix=/usr --sysconfdir=/etc --with-openssl=$CROSSCOMPILE_PATH --with-cryptopp=$CROSSCOMPILE_PATH --with-sqlite3=$CROSSCOMPILE_PATH --boost-includes=$CROSSCOMPILE_PATH/include --boost-libs=$CROSSCOMPILE_PATH/lib
128
    
129
    ./waf
130
    
131
    DESTDIR=$HOME/ndn-cxx-crosscompile ./waf install
132
133
You can now find the cross compiled binaries in `~/ndn-cxx-crosscompile`.
134
135
Using the ndn-cxx library with other NDN projects
136
-------------------------------------------------
137
138 2 Mathias Gibbens
Well, now that you've got the library cross compiled, you'll likely want to use it! Because it is both cross compiled as well as installed locally in your home directory, whatever project wants to use it will have to be told where to look. As a simple example, this section will show cross compiling [NFD](http://redmine.named-data.net/projects/NFD).
139 1 Mathias Gibbens
140 2 Mathias Gibbens
NFD requires the `libpcap` library, so make sure you've selected it when compiling your firmware and cross compile environment. (If you haven't, go back and do so, then rerun `make`. The second time should be much faster, as only the parts affected by configuration changes will have to be compiled.)
141
142
Similar to ndn-cxx, we will get the current release of NFD, 0.1.0 as of this writing:
143
144
    cd
145
    wget https://github.com/named-data/NFD/archive/NFD-0.1.0.tar.gz
146
    tar xf NFD-0.1.0.tar.gz
147
    cd NFD-NFD-0.1.0/
148
149
If the environment variables from the previous section aren't still set, reset them before proceeding.
150
151
NDN projects that use ndn-cxx assume they can get the necessary include directories and linking information via pkg-config. Because ndn-cxx isn't in the standard search path, we need to add it:
152
153
    export PKG_CONFIG_PATH=$HOME/ndn-cxx-crosscompile/usr/lib/pkgconfig:$PKG_CONFIG_PATH
154
155
Additionally, we need to tweak the configuration file for ndn-cxx so it includes the correct directories, since when the pkg-config file was generated, it assumed final installation based off the supplied `--prefix` option. Edit `~/ndn-cxx-crosscompile/usr/lib/pkgconfig/libndn-cxx.pc` and change the last two lines to (make sure to change the username and target parts of the paths!):
156
157
    Libs: -L${libdir} -L/home/USER/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/lib -L/home/USER/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/lib -lndn-cxx -lboost_system -lboost_filesystem -lboost_date_time -lboost_iostreams -lboost_regex -lboost_program_options -lboost_chrono -lssl -lcrypto -lcryptopp -lsqlite3 -lrt -lpthread -L/home/USER/ndn-cxx-crosscompile/usr/lib
158
    Cflags: -I${includedir} -I/home/USER/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/include -I/home/USER/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr/include -I/home/USER/ndn-cxx-crosscompile/usr/include
159
160
Now, configure and compile NFD:
161
162
    ./waf configure --prefix=/usr --sysconfdir=/etc --with-libpcap=$CROSSCOMPILE_PATH --boost-includes=$CROSSCOMPILE_PATH/include --boost-libs=$CROSSCOMPILE_PATH/lib
163
    
164
    ./waf
165
    
166
    DESTDIR=$HOME/NFD-crosscompile ./waf install
167
168
Like before, the cross compiled binaries will be in `~/NFD-crosscompile`.
169
170
171 1 Mathias Gibbens
Concluding remarks
172
------------------
173 2 Mathias Gibbens
174
Hopefully this walk through has been helpful to you in your quest to cross compile a NDN project.
175
176
I have chosen not to talk about actually copying the resulting binaries to the router, as there are many ways to do so: package managers, tar balls, by hand, etc. There are plenty of better tutorials online that are easy to find if you need pointers doing this.