Meeting: 22nd April 2013 – Source Code and Compiling


Source Code Compilation

This is a quick tutorial for building binaries from source code. It is aimed at a beginner and is designed as a quick getting started guide. It is aimed at people who *have* to compile code rather than people who *want* to compile code. I am not a C-programmer.

Decide on the package you are going to compile. In our case we are choosing wget, since it is easy to patch and quick to compile. XBMC and Apache are other common packages which are often compiled and follow similar steps.

Install pre-requisites

Debian has a marvellous ability which allows it to examine a package and deduce all the related packages which are required to build it from source. Pre-requisite packages:

# apt-get build-dep wget

Answer yes when prompted. This installs all the necessary packages to build wget!

Or for those of us using RPM based distros, download the src.rpm and examine it (requires yum-utils to be installed):

# yum-buildep /path/to/foo.src.rpm

Download the source using git

In the case of wget, the source code is found at git.savannah.gnu.org:

# apt-get install git

Or for the chapeau challenged / lizard loungers:

# yum install git

Download the source code using git into a directory WGET_SOURCE.

$ git clone git://git.savannah.gnu.org/wget.git WGET_SOURCE

Now to build (without SSL as we don’t want to go to the hassle or time of openSSL dependency at this stage. It’s not hard, but it’s just a quick demo!):

$ cd WGET_SOURCE
$ ./bootstrap
$ ./configure --without-ssl
$ make

Now as root

# make install

Testing

Where is the default wget installed from apt, run it and see whether we download HTML5 video:

$ which wget
$ /usr/bin/wget -m "http://127.0.0.1/test_pi.html"

Is there any HTML5 video there? No. So lets try our compiled version.

$ /usr/local/bin/wget -m "http://127.0.0.1/test_pi.html"

Is there any HTML5 video there? Oh Bobbins. Still no HTML5 videos. We need to patch wget.

Patching source code using patch.

In this case, we are going to patch wget so that it downloads HTML5 videos.

Download the patch from the mailing list, then apply it using patch

$ cd ~/WGET_SOURCE/src/

cp html-url.c html-url.c.orig
$ cat patch1.diff

--- wget.orig/src/html-url.c	2011-05-15 15:31:59.000000000 +0100
+++ wget/src/html-url.c	2012-07-11 17:08:25.986314347 +0100
@@ -77,6 +77,7 @@
   TAG_OBJECT,
   TAG_OVERLAY,
   TAG_SCRIPT,
+  TAG_SOURCE,
   TAG_TABLE,
   TAG_TD,
   TAG_TH
@@ -108,6 +109,7 @@
   { TAG_OBJECT,  "object",      tag_find_urls },
   { TAG_OVERLAY, "overlay",     tag_find_urls },
   { TAG_SCRIPT,  "script",      tag_find_urls },
+  { TAG_SOURCE,  "source",      tag_find_urls },
   { TAG_TABLE,   "table",       tag_find_urls },
   { TAG_TD,      "td",          tag_find_urls },
   { TAG_TH,      "th",          tag_find_urls }
@@ -155,6 +157,7 @@
   { TAG_OBJECT,         "data",         ATTR_INLINE },
   { TAG_OVERLAY,        "src",          ATTR_INLINE | ATTR_HTML },
   { TAG_SCRIPT,         "src",          ATTR_INLINE },
+  { TAG_SOURCE,         "src",          ATTR_INLINE },
   { TAG_TABLE,          "background",   ATTR_INLINE },
   { TAG_TD,             "background",   ATTR_INLINE },
   { TAG_TH,             "background",   ATTR_INLINE }

Note that the + indicates where files need additions, a – indicates where lines need to be removed. We could edit the source code file by hand if we wanted and make these changes, but there’s a tool to do that:

$ patch -d . <../../patch1.diff

Job done. As Make is smart regarding what has been built, we only need to re-run make and make install. It will spot that html-url.c has changed and re-compile only that part.

$ cd ..
$ make

Now as root

# make install

Have we got HTML5 goodness?

$ /usr/local/bin/wget -m "http://127.0.0.1/test_pi.html"

😎

Cross Compiling for the Raspberry Pi

Lifted from http://www.raspberrypi.org/phpBB3/viewtopic.php?f=67&t=39667

$ cd /home/tng/
$ git clone git://github.com/raspberrypi/tools.git CROSS_COMPILE_RPI

So now we have the compiler and tools, it’s time to build.

$ export ARCH=arm
$ export CROSS_COMPILE=arm-bcm2708hardfp-linux-gnueabi-
$ export CC=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-gcc
$ export NM=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-nm
$ export LD=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-ld
$ export CXX=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-g++
$ export RANLIB=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-ranlib
$ export AR=~/CROSS_COMPILE_RPI/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/
bin/arm-bcm2708hardfp-linux-gnueabi-ar
  • ARCH is the architecture we are building for.
  • CROSS_COMPILE is the tool prefix (so gcc knows how our tool files are named).
  • RANLIB is a tool which generates an Index of the archive contents and stores that index in the contents.
  • AR is a creation/extraction/modification tool (c.f. tar).
  • CC is the C Compiler.
  • CXX is the C++ (g++) compiler.
  • LD is the GNU Linker (links together compiled C/C++ programs into bigger binary).
  • NM can examine binary files.
$ ./configure --target=arm-linux --host=arm-linux --without-ssl
$ make

Don’t run a make install as this binary won’t run on our PC. We should be left with a binary in ~/WGET_SOURCE/src/ called wget. We can copy this binary to our raspberry pi and check that we have a working wget.

 scp src/wget <raspberry pi IP>:~/wget

Footnote – another useful apt utility

Also worth noting is the following command which downloads the source for a debian package.

# apt-get source <package name>

Leave a comment