Project

General

Profile

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

Junxiao Shi, 10/08/2019 08:41 AM

1 14 Junxiao Shi
# NDN Packages for OpenWrt 18.06
2
3
Please build packages from <https://github.com/yoursunny/OpenWrt-packages> feed.
4
Original content of this page is retained for historical purpose.
5
6
---
7
8 1 Mathias Gibbens
Cross-compiling NDN projects for home routers
9
=============================================
10
11
Contents
12
--------
13
14
*  [Introduction](#Introduction)
15
*  [Setting up the build host](#Setting-up-the-build-host)
16
*  [OpenWRT source and toolchain](#OpenWRT-source-and-toolchain)
17
*  [DD-WRT source and toolchain](#DD-WRT-source-and-toolchain)
18
*  [Compiling ndn-cxx](#Compiling-ndn-cxx)
19
*  [Using the ndn-cxx library with other NDN projects](#Using-the-ndn-cxx-library-with-other-NDN-projects)
20
*  [Concluding remarks](#Concluding-remarks)
21
22
Introduction
23
------------
24
25 5 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/). Currently, it is easier to use OpenWRT. Building NDN projects to run on a router is 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.
26 1 Mathias Gibbens
27
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.
28
29
Setting up the build host
30
-------------------------
31
32 9 Mathias Gibbens
Steps in this guide were tested on a system running Debian 8 (jessie). 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!
33 1 Mathias Gibbens
34
Make sure you have the needed packages on the host system to compile the cross compile toolchain, libraries, and resulting firmware:
35
36 2 Mathias Gibbens
    sudo apt-get install build-essential subversion git-core libncurses5-dev zlib1g-dev gawk unzip pkg-config
37 1 Mathias Gibbens
38 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.
39 1 Mathias Gibbens
40
OpenWRT source and toolchain
41
----------------------------
42
43 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.
44 1 Mathias Gibbens
45
    git clone git://git.openwrt.org/openwrt.git
46
    cd ~/openwrt/
47 4 Mathias Gibbens
48 13 Mathias Gibbens
You will need to edit the feed configuration file `feeds.conf.default` to re-enable the "oldpackages" repository so we can compile the `libcrypto++` library. 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.
49 4 Mathias Gibbens
50 1 Mathias Gibbens
    ./scripts/feeds update -a
51
    ./scripts/feeds install -a
52
53
We need to fetch the current feed information, as that is where we get the boost, libcrypto++, and libsqlite3 libraries to run in OpenWRT.
54
55
Now, run the configuration interface and adjust the settings as you need to match your device, etc.
56
57
    cd ~/openwrt/
58
    make menuconfig
59
60 10 Mathias Gibbens
**IMPORTANT** As of June 2015, the OpenWRT project has [switched to a new C library](https://lists.openwrt.org/pipermail/openwrt-devel/2015-June/033702.html) called musl. If you are running OpenWRT 15.05 (Chaos Calmer) or earlier, you must change the C library that the toolchain will use back to uClib, otherwise your cross compiled code _will not run_ on the router. If you are running a development version of OpenWRT compiled from code after June 16, 2015, it will use the musl C library and you do not have to change anything.
61
62 13 Mathias Gibbens
If you are running an old version of OpenWRT, follow these options in the menuconfig to change the C library. (Note that you will have to first enable the options before actually selecting them.)
63 10 Mathias Gibbens
64
    Advanced configuration options (for developers) --->
65
        Toolchain Options --->
66
            C Library implementation (use musl) --->
67
                Use uClib
68
69 13 Mathias Gibbens
Before compiling the firmware image, 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).)
70 1 Mathias Gibbens
71
    Libraries --->
72
        database --->
73
            libsqlite3
74
        boost --->
75 13 Mathias Gibbens
            Select Boost Libraries --->
76
                Boost test package
77
                Boost atomic library
78
                Boost chrono library
79
                Boost date_time library
80
                Boost filesystem library
81
                Boost iostreams library
82
                Boost program_options library
83
                Boost random library
84
                Boost regex library
85
                Boost system library
86
                Boost thread library
87 1 Mathias Gibbens
        libcryptoxx
88
89
After you have the configuration to your liking, we need to compile the toolchain and libraries. This will take a while.
90
91
    make
92
93
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.)
94
95
    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
96
    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
97
98
Now the cross compile environment is ready for you to use. Proceed to [Compiling ndn-cxx](#Compiling-ndn-cxx).
99
100
DD-WRT source and toolchain
101 3 Mathias Gibbens
---------------------------
102
103
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.
104
105
    svn co svn://svn.dd-wrt.com/DD-WRT
106
    cd ~/DD-WRT/
107
108 5 Mathias Gibbens
Next, download the current cross compile toolchain for DD-WRT (note that the link on DD-WRT's wiki is broken, this is the correct one):
109 1 Mathias Gibbens
110 5 Mathias Gibbens
    wget ftp://ftp.dd-wrt.com/toolchains/toolchains.tar.gz
111 1 Mathias Gibbens
112 5 Mathias Gibbens
**FIXME** Need to complete this section. As of early 2015, it is easier to use the OpenWRT build system. I was able to cross-compile ndn-cxx and NFD using OpenWRT's system and then run the binaries on a DD-WRT flashed router. YMMV.
113 3 Mathias Gibbens
114
Now the cross compile environment is ready for you to use. Proceed to [Compiling ndn-cxx](#Compiling-ndn-cxx).
115 1 Mathias Gibbens
116
Compiling ndn-cxx
117
-----------------
118
119 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.
120 1 Mathias Gibbens
121 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.
122 1 Mathias Gibbens
123 12 Mathias Gibbens
Begin by fetching the current release of ndn-cxx, 0.4.1 as of this writing:
124 1 Mathias Gibbens
125
    cd
126 12 Mathias Gibbens
    wget https://github.com/named-data/ndn-cxx/archive/ndn-cxx-0.4.1.tar.gz
127 1 Mathias Gibbens
    tar xf ndn-cxx-0.4.1.tar.gz
128 12 Mathias Gibbens
    cd ndn-cxx-ndn-cxx-0.4.1/
129 11 Mathias Gibbens
130 1 Mathias Gibbens
Now, set the needed environment variables so the configuration and compilation process use the cross compile environment we have setup.
131
132 2 Mathias Gibbens
**FIXME** this is currently specific to OpenWRT
133 1 Mathias Gibbens
134
    export TOOLCHAIN_PATH=$HOME/openwrt/staging_dir/toolchain-mipsel_mips32_gcc-4.8-linaro_uClibc-0.9.33.2/bin
135
    export CROSSCOMPILE_PATH=$HOME/openwrt/staging_dir/target-mipsel_mips32_uClibc-0.9.33.2/usr
136
    export CXX=$TOOLCHAIN_PATH/mipsel-openwrt-linux-uclibc-g++
137
    export AR=$TOOLCHAIN_PATH/mipsel-openwrt-linux-uclibc-ar
138
    export CXXFLAGS="-O2"
139
    export CFLAGS="-I$CROSSCOMPILE_PATH/include"
140
    export LDFLAGS="-L$CROSSCOMPILE_PATH/lib -lz"
141
142 13 Mathias Gibbens
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. If you are using the OpenWRT buildchain, you will see warnings during compilation that the STAGING_DIR environment variable is not set. This is a harmless warning that can be ignored.
143 1 Mathias Gibbens
144 6 Mathias Gibbens
    ./waf configure --prefix=/usr --sysconfdir=/etc --with-cryptopp=$CROSSCOMPILE_PATH --with-sqlite3=$CROSSCOMPILE_PATH --boost-includes=$CROSSCOMPILE_PATH/include --boost-libs=$CROSSCOMPILE_PATH/lib
145 1 Mathias Gibbens
    
146
    ./waf
147
    
148
    DESTDIR=$HOME/ndn-cxx-crosscompile ./waf install
149
150
You can now find the cross compiled binaries in `~/ndn-cxx-crosscompile`.
151
152
Using the ndn-cxx library with other NDN projects
153
-------------------------------------------------
154
155
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).
156
157 5 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.)
158 2 Mathias Gibbens
159 12 Mathias Gibbens
Similar to ndn-cxx, we will get the current release of NFD, 0.4.1 as of this writing:
160 5 Mathias Gibbens
161
    cd
162 12 Mathias Gibbens
    wget https://github.com/named-data/NFD/archive/NFD-0.4.1.tar.gz
163
    tar xf NFD-0.4.1.tar.gz
164
    cd NFD-NFD-0.4.1/
165 7 Mathias Gibbens
166 2 Mathias Gibbens
If the environment variables from the previous section aren't still set, reset them before proceeding.
167
168
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:
169
170
    export PKG_CONFIG_PATH=$HOME/ndn-cxx-crosscompile/usr/lib/pkgconfig:$PKG_CONFIG_PATH
171 1 Mathias Gibbens
172 6 Mathias Gibbens
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 append the lib and include paths (the last arguments below) to the last two lines to (make sure to change the username part of the paths!):
173 2 Mathias Gibbens
174 6 Mathias Gibbens
    Libs: -L${libdir} [...snip...] -L/home/USER/ndn-cxx-crosscompile/usr/lib
175
    Cflags: -I${includedir} [...snip...] -I/home/USER/ndn-cxx-crosscompile/usr/include
176 2 Mathias Gibbens
177
Now, configure and compile NFD:
178
179 6 Mathias Gibbens
    ./waf configure --prefix=/usr --sysconfdir=/etc --with-libpcap=$CROSSCOMPILE_PATH --boost-includes=$CROSSCOMPILE_PATH/include --boost-libs=$CROSSCOMPILE_PATH/lib --without-websocket
180 2 Mathias Gibbens
    
181
    ./waf
182
    
183
    DESTDIR=$HOME/NFD-crosscompile ./waf install
184
185
Like before, the cross compiled binaries will be in `~/NFD-crosscompile`.
186
187
188 1 Mathias Gibbens
Concluding remarks
189
------------------
190 2 Mathias Gibbens
191
Hopefully this walk through has been helpful to you in your quest to cross compile a NDN project.
192
193 5 Mathias Gibbens
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, tarballs, by hand, etc. There are plenty of better tutorials online that are easy to find if you need pointers doing this.