C/C++ cross compiler

This page contains instructions on how to build a GCC toolchain, which can be used to cross-compile C and C++ applications for Barrelfish.

In general, most language constructs work (libstdc++, RTTI, virtual functions, templates, global constructors, inline assembler, thread locals) but exceptions are not supported (read here for more details).

Building (Short Version)

First, you will need to install a few packages:

$ sudo apt-get install gcc g++ make patch bison flex texinfo

Optional: to enable Graphite loop optimizations you will also need libcloog-ppl-dev.

Next, download the build script and corresponding patches to some folder in your Barrelfish tree (e.g. tools/gcc_cross_compiler):

build script

build-toolchain.sh

binutils

2.24, 2.21

GCC

4.8.2, 4.7.3, 4.5.2

The recommended versions (shown in bold) should be preferred as these have been tested and have several improvements and fixes. These are known to work with Barrelfish release2013-12-13.

Finally, modify any configuration parameters in the script, run it and a few minutes later the toolchain should be installed in the toolchain directory of your Barrelfish tree.

Hint: Each toolchain is specific to a build tree, so it can be helpful to use direnv or smartcd to automatically add/remove it from the PATH environment variable.

Usage

From this point, if the application fails to compile it is probably due to missing POSIX features in Barrelfish.

Building (Long Version)

These instructions have been tested on Ubuntu 12.10.

1. Edit $HOME/.bashrc and add these lines (modify these variables as you wish):

 export PREFIX=$HOME/arm/opt/bf-tools
 export PATH=$PATH:$PREFIX/bin
 export TARGET=x86_64-pc-barrelfish
 export TOOLS=$HOME/bsc/bf-tools
 export BF_BUILD=$HOME/arm/bf/build

We exit the shell and run it again to load these variables.

2. Download and build Barrelfish for x86_64, x86_32 or scc target (this step may have been done previously). The Barrelfish release tested is 2012-11-03:

cd $TOOLS
hg clone http://hg.barrelfish.org bf
mkdir bf/build
cd bf/build
../hake/hake.sh -a x86_64 -s ..
make

3. Then, we need to install needed packages to build the toolchain:

sudo apt-get install texinfo flex gcc libgmp-dev libmpfr-dev libmpc-dev

These steps have been done using the system compiler (gcc version 4.7.2).

4. Build and install binutils: Save file binutils-2.21-bf.patch file attached in this page to the TOOLS directory

cd $TOOLS
wget http://mirrors.usc.edu/pub/gnu/binutils/binutils-2.21.tar.bz2
tar xjvf binutils-2.21.tar.bz2
cd binutils-2.21/
patch -p1 < ../binutils-2.21-bf.patch
cd ..
mkdir binutils-2.21-build
cd binutils-2.21-build/
../binutils-2.21/configure --prefix=$PREFIX --target=$TARGET
make
make install

5. Build and install gcc and g++: Save file gcc-4.5.2-bf.patch file attached in this page to the TOOLS directory

cd $TOOLS
wget http://ftp.gnu.org/gnu/gcc/gcc-4.5.2/gcc-4.5.2.tar.bz2
tar xjvf gcc-4.5.2.tar.bz2
cd gcc-4.5.2/
patch -p1 < ../gcc-4.5.2-bf.patch

Now we need to edit the files gcc/config/i386/bf-*.h and change the first two lines:

Then we proceed with the compilation:

cd $TOOLS
mkdir gcc-4.5.2-build
cd gcc-4.5.2-build/
../gcc-4.5.2/configure --prefix=$PREFIX --target=$TARGET --disable-nls --enable-languages=c,c++ --disable-libssp
make
make install

6. We can try the C++ compiler by creating a simple program test.cc and running it in Barrelfish:

#include <iostream>

int main(int argc, char *argv[])
{
  std::cout << "Hello!" << std::endl;

  return 0;
}

We compile it with:

x86_64-pc-barrelfish-g++ test.cc -o test

Then we copy the executable to the Barrelfish build directory and add the line to menu.lst:

cp test $BF_BUILD/x86_64/sbin
cd $BF_BUILD
echo "module /x86_64/sbin/test" >> menu.lst
make sim