Project

General

Profile

Cross-compiling NDN projects for Raspberry Pi » History » Version 42

Wentao Shang, 10/29/2014 02:11 PM

1 1 Wentao Shang
Cross-compiling NDN projects for Raspberry Pi
2
=============================================
3
4 2 Wentao Shang
Note: before reading this document, you should already be familiar with the basic concepts of compiling and linking (especially the linking part). If not, you may be interested in reading this great book: [Linkers and Loaders](http://www.amazon.com/Linkers-Kaufmann-Software-Engineering-Programming/dp/1558604960/ref=sr_1_1?ie=UTF8&qid=1394130356&sr=8-1&keywords=linker+and+loader)
5 3 Wentao Shang
6 10 Wentao Shang
Basic idea
7 7 Wentao Shang
----------
8 1 Wentao Shang
9 7 Wentao Shang
Remember to compile a C/C++ project, we need the source code for the project, the header files for the included libraries, the binary objects of the libraries, and the compiler tools (gcc, as, ld, etc.). The combination of the last three things together is referred to as a _building environment_. Cross-compiling is no different. To cross compile a project, we first need to setup the building environment and then build the source code in that environment.
10
11
Difference between "native compiling" and "cross compiling"
12 9 Wentao Shang
--------
13 7 Wentao Shang
14 14 Wentao Shang
The biggest difference is that for native compiling, you build the binaries that will run on the same platform where you build them. For cross compiling, however, you build the binaries on one platform (called _build_ platform) and run them on another platform (called _host_ or _target_ platform). The platforms may differ in the operating systems (Windows vs. Linux) and/or the CPU architectures (x86_64 vs. arm32).
15 8 Wentao Shang
16 10 Wentao Shang
Raspberry Pi platform information
17 1 Wentao Shang
--------
18 10 Wentao Shang
19
Raspberry Pi runs on *ARMv6* CPU, which is a 32bit chip with hardware float-point support (abbreviated as *armhf*). There are many operating systems available. The one we are going to use is called *Raspbian*, which is a port of the Debian "wheezy" Linux distribution.
20
21 15 Wentao Shang
Creating compiling toolchain for Raspberry Pi
22 10 Wentao Shang
--------
23 1 Wentao Shang
24 15 Wentao Shang
To prepare a building environment, we need to get the gcc/g++ compiler toolchain that will generate binaries for the armhf platform. Raspbian already provided a set of compiling tools on their official [github](https://github.com/raspberrypi/tools). However, by the time of this writing, those tools only run on 32bit Linux systems. If we want to use 64bit Linux as the build platform, we may need to build our own gcc/g++ toolchain.
25
26
The tool we are going to use to build our gcc is [ct-ng](http://crosstool-ng.org/). It is designed to run on Linux but can be adjusted via some hacks to run on MacOSX. It is pretty easy to use and you may find this [article](http://www.kitware.com/blog/home/post/426) very helpful when building your own toolchain.
27
28
Tip: you may use the configuration file from Raspbian [github](https://github.com/raspberrypi/tools/blob/master/configs/bcm2708hardfp-ct-ng.config), which will load the "official" configurations for the platform.
29 16 Wentao Shang
30 40 Wentao Shang
We have a toolchain package available for download. It is compiled on Ubuntu 12.04 64bit platform and should also work for later versions of Ubuntu 64bit. The download link is http://irl.cs.ucla.edu/~wentao/pi/pi-tools.tar.gz
31
32 16 Wentao Shang
Getting libraries ready
33
--------
34 17 Wentao Shang
35 18 Wentao Shang
After we have the toolchain, the next step is to gather the libraries, including the header files (_.h_ files) and the binaries (_.a, .so_ files). The libraries used by the NDN projects include *openssl, Boost, sqlite3, crypto++*. Those libraries may recursively depend on other libraries and it will be a big headache to compile all of them from source code and resolve the dependencies manually. Fortunately, Raspbian has a public package repo containing the binaries for most packages available on Debian. So the easy walk-around is to _install_ those libraries directly on Raspberry Pi using _apt-get_ and then copy the relevant files down to our build machine.
36 19 Wentao Shang
37 21 Wentao Shang
On Raspbian, all the header files are in the */usr/include* folder, while the binary objects for the libraries are scattered in many places, such as */usr/lib, /lib*, etc. A simple way to find the path of a library is to run the following command:
38 19 Wentao Shang
39
    ldconfig -p | grep _library_name_
40
41 20 Wentao Shang
*ldconfig -p* will show all the dynamic linking libraries currently available on the system and their locations in the file system.
42 22 Wentao Shang
43
You may copy all the binaries into the same folder. For example, on the build machine you may have the folder _~/pi/_ and inside that folder there are two sub-folders _./include_ and _./lib_. You can copy all the header files into the include folder and the binaries into the lib folder.
44
45 33 Wentao Shang
Here is the list of library files that you need to get in order to compile the NDN projects. You MAY need other library files for your own project.
46
47
    .
48
    ├── include
49 34 Wentao Shang
    │   ├── boost/
50
    │   ├── cryptopp/
51 33 Wentao Shang
    │   ├── expat_config.h
52
    │   ├── expat_external.h
53
    │   ├── expat.h
54 34 Wentao Shang
    │   ├── openssl/
55
    │   ├── pcap/
56 33 Wentao Shang
    │   ├── pcap-bpf.h
57
    │   ├── pcap.h
58
    │   ├── pcap-namedb.h
59
    │   ├── sqlite3ext.h
60
    │   └── sqlite3.h
61
    └── lib
62
        ├── ld-linux-armhf.so.3
63
        ├── libboost_chrono.so
64
        ├── libboost_date_time.so
65
        ├── libboost_filesystem.so
66
        ├── libboost_iostreams.so
67
        ├── libboost_program_options.so
68 42 Wentao Shang
        ├── libboost_random.so
69 33 Wentao Shang
        ├── libboost_regex.so
70
        ├── libboost_system.so
71
        ├── libbz2.so.1.0
72
        ├── libcryptopp.so -> libcrypto++.so
73
        ├── libcrypto.so
74
        ├── libcrypto++.so
75
        ├── libexpat.so
76
        ├── libicudata.so.48
77
        ├── libicui18n.so.48
78
        ├── libicuuc.so.48
79
        ├── libpcap.so
80
        ├── libsqlite3.so
81
        ├── libssl.so
82
        ├── libz.so.1
83 1 Wentao Shang
        └── pkgconfig
84 41 Wentao Shang
85
We have packaged the minimum library files into a package and made it available for download. Note that the Boost library contained in this package is not complete: it only includes the minimum required libs for ndn-cxx and NFD project. The download link is http://irl.cs.ucla.edu/~wentao/pi/pi-env.tar.gz
86 33 Wentao Shang
87 22 Wentao Shang
Building the source code
88
--------
89 23 Wentao Shang
90 24 Wentao Shang
The final step is to compile the projects using the environment we created. The basic idea is to call the cross toolchain and link against the cross-compiled libraries. To do that, we need to export a set of shell variables that are used by the *make* command. For example, we can export the following variables in the shell:
91
92
    export AS=/path/to/your/toolchain/as
93
    export LD=/path/to/your/toolchain/ld
94
    export CC=/path/to/your/toolchain/cc
95
    export CXX=/path/to/your/toolchain/c++
96
97 28 Wentao Shang
This will tell *make* to use the toolchain we specify instead of the default toolchain.
98 25 Wentao Shang
99 29 Wentao Shang
In order to point the toolchain to the correct location to search libraries, we also need to export *CFLAGS/CPPFLAGS* and *LDFLAGS* variables, which will be provided to *gcc* as options:
100 1 Wentao Shang
101 26 Wentao Shang
    export CFLAGS="-I/path/to/your/include/folder -L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
102
    export LDFLAGS="-L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
103
104 28 Wentao Shang
Note that we repeat the *LDFLAGS* in the *CFLAGS*. This is because some Makefile script may combine the compiling and linking steps together and use only *CFLAGS*. (This actually happens when building the NDNx project.)
105 26 Wentao Shang
106 28 Wentao Shang
Another important option is the _rpath_ option. This specifies where the linker will search for recursive dependencies.
107 35 Wentao Shang
108 38 Wentao Shang
Here is a sample script for configuring & compiling NFD. It can be ported to compile other *waf* based projects as well.
109 35 Wentao Shang
110
    #!/bin/bash
111
112
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
113
114
    arch="arm-bcm2708hardfp-linux-gnueabi"
115
116
    export AR="${arch}-ar"
117
    export AS="${arch}-as"
118
    export LD="${arch}-ld"
119
    export CC="${arch}-cc"
120
    export CXX="${arch}-c++"
121
    export RANLIB="${arch}-ranlib"
122
123
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
124
    export CXXFLAGS="-O2 -g -I/home/wentao/pi/include -L/home/wentao/pi/lib"
125
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib -L/home/wentao/pi/lib"
126
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
127
128
    ./waf configure --prefix=/home/wentao/pi --boost-includes=/home/wentao/pi/include --boost-libs=/home/wentao/pi/lib
129
130 38 Wentao Shang
    ./waf
131 1 Wentao Shang
132 38 Wentao Shang
After running this script, you may run *./waf install* to copy all the generated binaries into the folder you specified in *--prefix* option at *./waf configure* step. In our case, it will be */home/wentao/pi*. This is helpful if you are cross-compiling your own libraries (e.g., ndn-cxx) that you want to use to further compile other projects.
133 35 Wentao Shang
134 39 Wentao Shang
Another sample script for configuring and compiling ndn-cxx:
135
136
    #!/bin/bash
137
138
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
139
140
    arch="arm-bcm2708hardfp-linux-gnueabi"
141
142
    export AR="${arch}-ar"
143
    export AS="${arch}-as"
144
    export LD="${arch}-ld"
145
    export CC="${arch}-cc"
146
    export CXX="${arch}-c++"
147
    export RANLIB="${arch}-ranlib"
148
149
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
150
    export CXXFLAGS="-O2 -g"
151
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib"
152
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
153
154
    ./waf configure --prefix=/home/wentao/pi --boost-includes=/home/wentao/pi/include --boost-libs=/home/wentao/pi/lib --with-openssl=/home/wentao/pi --with-cryptopp=/home/wentao/pi
155
156
    ./waf
157
158 38 Wentao Shang
Here is the sample script for configuring & compiling NDNx. It can be ported to compile other *automake* based projects as well.
159
160 35 Wentao Shang
    #!/bin/bash
161
162
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
163
164
    arch="arm-bcm2708hardfp-linux-gnueabi"
165
166
    export AR="${arch}-ar"
167
    export AS="${arch}-as"
168
    export LD="${arch}-ld"
169
    export CC="${arch}-cc"
170
    export CXX="${arch}-c++"
171
    export RANLIB="${arch}-ranlib"
172
173
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
174 1 Wentao Shang
    export LDFLAGS="-L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
175
176 35 Wentao Shang
    ./configure --build="x86_64-linux-gnu" --host="${arch}" --prefix="/home/wentao/pi"
177 38 Wentao Shang
178
    make