Friday, September 21, 2012

Compiling LLVM on Ubuntu

A short guide on how to compile latest version of LLVM a low level compiler infrastructure with Clang on Ubuntu 12.04 (64bit).

1. Create a directory for LLVM
$mkdir ~/llvm
$cd ~/llvm
Install required dependencies (Note : my system is already setup for development so I cannot find out the exact packages required, if you know them post them as comments)
$sudo apt-get install build-essential
2. Download the latest version of LLVM sources including clang (C frontend) and compiler RT from http://llvm.org/releases/download.html#3.1
$wget http://llvm.org/releases/3.1/llvm-3.1.src.tar.gz
$wget http://llvm.org/releases/3.1/clang-3.1.src.tar.gz
$wget http://llvm.org/releases/3.1/compiler-rt-3.1.src.tar.gz
$wget http://llvm.org/releases/3.1/test-suite-3.1.src.tar.gz
3. Extract the downloaded sources
$tar zxvf ./llvm-3.1.src.tar.gz
$tar zxvf ./clang-3.1.src.tar.gz
$tar zxvf ./compiler-rt-3.1.src.tar.gz
4. Move folders to correct location. We start by renaming 'llvm-3.1.src' to 'llvm-3.1' and moving 'clang' inside the LLVM 'tools' folder and compiler-rt under the LLVM 'projects' folder. This is where LLVM expects them to be.
$mv ./llvm-3.1.src ./llvm-3.1
$mv ./clang-3.1.src ./clang
$mv ./clang ./llvm-3.1/tools/
$mv ./compiler-rt-3.1.src ./compiler-rt
$mv ./compiler-rt ./llvm-3.1/projects/
5. Once everything is in place we create a separate folder for the build process
$mkdir ./build
$cd ./build
6. Now we start the actual configuration and compilation of LLVM inside the 'build' folder
$../llvm-3.1/configure
If there are any missing packages required to compile LLVM it will ask you here. If everything is ok we can go ahead and compile it and also keep track of the time taken. Note that all binaries are available in the 'build/Release+Asserts/bin' folder including llvm, clang, clang++
$time make
.....
llvm[0]: ***** Completed Release+Asserts Build
real 44m17.244s
user 41m42.572s
sys 2m5.592s
That's it ! We are done :)

7. To start using LLVM we have to include the binaries in our path. Add the following lines to your bash profile
$gedit ~/.bashrc
Add this line to the end of the file
export PATH=$PATH:~/llvm/build/Release+Asserts/bin
To apply the new settings close the terminal and start a new one or you can do
$source ~/.bashrc
8. Test a sample program
$cd ..
$mkdir test
$cd test
$gedit test.c
Add the following lines of a simple C program and save it
#include <stdio.h>
int main(void)
{
 printf("Hello World from LLVM!\n");
 return 0;
}
Compile it using clang the C frontend to LLVM
$clang test.c -o test
Run it
$./test
Hello World from LLVM!

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I want to try to build beignet so I needed the LLVM too. I followed your steps for the 3.2 LLVM version on ubuntu 12.04 and it worked fine. The only thing that might be missing from your description is the clang compiler. Even if I renamed and copied the clang directory under the Tools, as you suggested, it seems that my system did not found it.

    sudo apt-get install clang
    solves the problem!

    Good work, thanks.

    ReplyDelete
  3. Installing clang using apt-get is not what you want. If you cant find clang the try to follow the guide again.

    ReplyDelete
  4. I don't know if it works for 3.1 but installed it on
    Ubuntu 12.10
    Everything you mentioned in step 2,3,4 but with 3.3 inspite of 3.1
    finally when i compile it using $clang test.c -o test it couldn't find clang


    I have done everything as you mentioned but it doesnot work for 3.3

    But when did sudo apt-get install clang it found it.

    ReplyDelete
  5. I am following the instruction and after time make I got 1 error make/config.mk file or directory does not exist. And similarly make/util.mk does not exist. I am trying to install clang and llvm 3.4

    ReplyDelete