quarta-feira, 12 de fevereiro de 2014

How to compile a compiler? Clang! Let's do it!

On my MsC project, I wanted to use a compiler that was as powerful as GCC but not so loaded with unwanted things and efficient as well. GCC 4.8 has a very good performance and produces far-sized executables, but I needed an alternative. That's when I found CLANG. Clang is a C/C++/Objective-C front-end for LLVM Compiler (a stratified structured compiler with great performance - a really organized compiler, on simple therms).

Clang is known by its increased performance, low memory usage and is soft-spoken when talking about error (the messages are more accurate and simple for humans to understand). Compared to older version of GCC (until 4.6) Clang have an outstanding result.

This compiler is supported by a consortium, including Apple. It's a really new work in progress and worth trying.

Let's Start!

We will install Clang and LLVM on Fedora 19/20.

Installing Dependencies

I've compiled successfully Clang 3.4 (Final) on Fedora 19/20, both on x86_64 and ARM architectures (but take about 8 hours to compile on Pandaboard). I'll use on this tutorial the SVN version.

Make sure you have GCC (gcc, gcc-c++) packages and Perl (perl, perl-podlators) packages before you start the process.

# yum install gcc gcc-c++ perl perl-podlators -y

After that, create a directory named clang and a src directory inside of it

$ mkdir clang
$ mkdir clang/src

We will need libffi-devel too, so, to install it:

# yum install libffi-devel -y

And if you don't have SVN installed:

# yum install svn -y

Retrieving SVN Code

Now we'll retrieve the source code of LLVM from the official trunk:

$ cd clang/src
$ svn co http://llvm.org/svn/llvm-project/llvm/tas/RELEASE_34/final llvm


Now, the source code of Clang:

$ cd tools/

$ svn co http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_34/final clang


We will need the extra packages from Clang:

$ cd clang/tools
$ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/branches/release_34 extra

And last, compiler-rt files:

$ cd ../../../projects
$ svn co http://llvm.org/svn/llvm-project/compiler-rt/tags/RELEASE_34/final compiler-rt

Configuring Clang for the next steps

We will create a build directory and request the configuration of the packages via configure script:

$ mkdir ../build
$ cd ../build

And now, configure:

$ ../configure --enable-optimized --disable-assertions --enable-jit --enable-libffi --enable-shared


Wait a little/long time, depending on your machine, and let's start making it, literally:

$ make -j X

WARNING: You'll need to substitute X for a number in the command above! X is the number of cores you have on your machine + 1, so, if you have a dual core, X = 3.

Again, wait a little/long time, depending on your machine.

Regression Tests

Clang and LLVM comes with a regression test suite, and is necessary for assuring Clang will execute perfectly. To run this test suite do:

$ cd test
$ make
$ cd ../tools/clang/test/
$ make
$ cd ../tools/extra/test/
$ make

If a message like "UNEXPECTED FAILURE" doesn't appear, it's all ready to go.

Install it!

Now, all you need to do is install Clang:

$ ../../../../../
$ sudo make install 

Maybe you'll need to reboot. Type clang on a terminal and you'll see:

[user@localhost ~]$ clang
clang: error: no input files

DONE!


The syntax for compilation on clang is very similar to the gcc syntax, and if you are a GCC User, Clang will be a piece of cake.

Hope you enjoyed! \o

This is a modified and updated version from this tutorial.


Nenhum comentário:

Postar um comentário