It’s possible to build Clang from SVN in Ubuntu 12.04 LTS (Precise Pangolin), but you need to build GNU C++ 4.8 first, because the version that’s provided with Ubuntu 12.04 (GNU C++ 4.6) isn’t new enough to build it!

Firstly, install some build dependencies:

sudo apt-get install libmpfr-dev libgmp3-dev libmpc-dev flex bison

You might also need to do:

sudo apt-get build-dep gcc

Then build and install GNU C++ 4.8.2 to /opt/gcc-4.8.2/:

mkdir $HOME/src
cd $HOME/src

wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
tar xvf gcc-4.8.2.tar.bz2

cd gcc-4.8.2
mkdir build
cd build

../configure --disable-checking --enable-languages=c,c++ --enable-multiarch \
  --enable-shared --enable-threads=posix --program-suffix=4.8 \
  --with-gmp=/usr/local/lib --with-mpc=/usr/lib --with-mpfr=/usr/lib \
  --without-included-gettext --with-system-zlib --with-tune=generic \
  --prefix=/opt/gcc-4.8.2

make -j8

sudo make install

“make -j8” above runs a parallel build, where multiple parts of the code are compiled concurrently. This speeds up the build process on multi-core CPUs provided a few GBs of free memory is available. If you find you’re running out of memory during the build, you can change this to “make -j4” or instead simply use “make” without the -j option to disable concurrent compilation.

Once GNU C++ 4.8.2 is installed, you can optionally free up disk space with:

rm -rf $HOME/src/gcc-4.8.2/

The version of Clang in SVN also requires a newer version of CMake than what’s provided in the Ubuntu 12.04 repositories (CMake 2.8.7). We’ll build and install CMake 2.8.12.2, but to make things interesting, we’ll use CMake 2.8.7 and our newly installed GNU C++ 4.8.2 to build CMake 2.8.12.2:

sudo apt-get install cmake

mkdir $HOME/src
cd $HOME/src

wget http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
tar xvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
mkdir build
cd build

cmake -DCMAKE_INSTALL_PREFIX:PATH=/opt/cmake-2.8.12.2 \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_C_COMPILER=/opt/gcc-4.8.2/bin/gcc4.8 \
  -DCMAKE_CXX_COMPILER=/opt/gcc-4.8.2/bin/g++4.8 ..

make -j8
sudo make install

Finally we can focus on building Clang from SVN.

As per the previous blog post where we built Clang under Arch Linux, we need to install Subversion and Python 2.7 first:

sudo apt-get install subversion python

Now we do an SVN checkout of all the required components:

mkdir $HOME/src

cd $HOME/src
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm 

cd $HOME/src/llvm/tools 
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang 

cd $HOME/src/llvm/tools/clang/tools 
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra 

cd $HOME/src/llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt

Finally we can begin building Clang:

mkdir $HOME/src/llvm/build
cd $HOME/src/llvm/build

/opt/cmake-2.8.12.2/bin/cmake -DCMAKE_INSTALL_PREFIX:PATH=/opt/clang-svn \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_C_COMPILER=/opt/gcc-4.8.2/bin/gcc4.8 \
  -DCMAKE_CXX_COMPILER=/opt/gcc-4.8.2/bin/g++4.8 ..

make -j8
sudo make install

After writing all of that, it’s possible I’ve missed some steps, particularly any build prerequisites. They should be fairly self-explanatory though. For example, I don’t recall if wget is installed by default in Ubuntu, so obviously you might need to run “sudo apt-get install wget” before you can use wget.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Anti-Spam Quiz: