Project

General

Profile

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

Wentao Shang, 05/21/2014 01:56 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
Getting libraries ready
31
--------
32 17 Wentao Shang
33 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.
34 19 Wentao Shang
35 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:
36 19 Wentao Shang
37
    ldconfig -p | grep _library_name_
38
39 20 Wentao Shang
*ldconfig -p* will show all the dynamic linking libraries currently available on the system and their locations in the file system.
40 22 Wentao Shang
41
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.
42
43 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.
44
45
    .
46
    ├── include
47 34 Wentao Shang
    │   ├── boost/
48
    │   ├── cryptopp/
49 33 Wentao Shang
    │   ├── expat_config.h
50
    │   ├── expat_external.h
51
    │   ├── expat.h
52 34 Wentao Shang
    │   ├── openssl/
53
    │   ├── pcap/
54 33 Wentao Shang
    │   ├── pcap-bpf.h
55
    │   ├── pcap.h
56
    │   ├── pcap-namedb.h
57
    │   ├── sqlite3ext.h
58
    │   └── sqlite3.h
59
    └── lib
60
        ├── ld-linux-armhf.so.3
61
        ├── libboost_chrono.so
62
        ├── libboost_date_time.so
63
        ├── libboost_filesystem.so
64
        ├── libboost_iostreams.so
65
        ├── libboost_program_options.so
66
        ├── libboost_regex.so
67
        ├── libboost_system.so
68
        ├── libbz2.so.1.0
69
        ├── libcryptopp.so -> libcrypto++.so
70
        ├── libcrypto.so
71
        ├── libcrypto++.so
72
        ├── libexpat.so
73
        ├── libicudata.so.48
74
        ├── libicui18n.so.48
75
        ├── libicuuc.so.48
76
        ├── libpcap.so
77
        ├── libsqlite3.so
78
        ├── libssl.so
79
        ├── libz.so.1
80
        └── pkgconfig
81
82 22 Wentao Shang
Building the source code
83
--------
84 23 Wentao Shang
85 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:
86
87
    export AS=/path/to/your/toolchain/as
88
    export LD=/path/to/your/toolchain/ld
89
    export CC=/path/to/your/toolchain/cc
90
    export CXX=/path/to/your/toolchain/c++
91
92 28 Wentao Shang
This will tell *make* to use the toolchain we specify instead of the default toolchain.
93 25 Wentao Shang
94 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:
95 1 Wentao Shang
96 26 Wentao Shang
    export CFLAGS="-I/path/to/your/include/folder -L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
97
    export LDFLAGS="-L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
98
99 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.)
100 26 Wentao Shang
101 28 Wentao Shang
Another important option is the _rpath_ option. This specifies where the linker will search for recursive dependencies.
102 35 Wentao Shang
103 38 Wentao Shang
Here is a sample script for configuring & compiling NFD. It can be ported to compile other *waf* based projects as well.
104 35 Wentao Shang
105
    #!/bin/bash
106
107
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
108
109
    arch="arm-bcm2708hardfp-linux-gnueabi"
110
111
    export AR="${arch}-ar"
112
    export AS="${arch}-as"
113
    export LD="${arch}-ld"
114
    export CC="${arch}-cc"
115
    export CXX="${arch}-c++"
116
    export RANLIB="${arch}-ranlib"
117
118
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
119
    export CXXFLAGS="-O2 -g -I/home/wentao/pi/include -L/home/wentao/pi/lib"
120
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib -L/home/wentao/pi/lib"
121
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
122
123
    ./waf configure --prefix=/home/wentao/pi --boost-includes=/home/wentao/pi/include --boost-libs=/home/wentao/pi/lib
124
125 38 Wentao Shang
    ./waf
126 1 Wentao Shang
127 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.
128 35 Wentao Shang
129 39 Wentao Shang
Another sample script for configuring and compiling ndn-cxx:
130
131
    #!/bin/bash
132
133
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
134
135
    arch="arm-bcm2708hardfp-linux-gnueabi"
136
137
    export AR="${arch}-ar"
138
    export AS="${arch}-as"
139
    export LD="${arch}-ld"
140
    export CC="${arch}-cc"
141
    export CXX="${arch}-c++"
142
    export RANLIB="${arch}-ranlib"
143
144
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
145
    export CXXFLAGS="-O2 -g"
146
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib"
147
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
148
149
    ./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
150
151
    ./waf
152
153 38 Wentao Shang
Here is the sample script for configuring & compiling NDNx. It can be ported to compile other *automake* based projects as well.
154
155 35 Wentao Shang
    #!/bin/bash
156
157
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
158
159
    arch="arm-bcm2708hardfp-linux-gnueabi"
160
161
    export AR="${arch}-ar"
162
    export AS="${arch}-as"
163
    export LD="${arch}-ld"
164
    export CC="${arch}-cc"
165
    export CXX="${arch}-c++"
166
    export RANLIB="${arch}-ranlib"
167
168
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
169 1 Wentao Shang
    export LDFLAGS="-L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
170
171 35 Wentao Shang
    ./configure --build="x86_64-linux-gnu" --host="${arch}" --prefix="/home/wentao/pi"
172 38 Wentao Shang
173
    make