Monday, June 23, 2014

Building LLVM 3.4.2 from source

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

NOTE : This is a update to the guide on building LLVM 3.2 already posted here. Clang is also known as Clang CFE (C Front End).

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.4.2
$wget http://llvm.org/releases/3.4.2/llvm-3.4.2.src.tar.gz
$wget http://llvm.org/releases/3.4.2/cfe-3.4.2.src.tar.gz
$wget http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz
3. Extract the downloaded sources
$tar zxvf ./llvm-3.4.2.src.tar.gz
$tar zxvf ./cfe-3.4.2.src.tar.gz
$tar zxvf ./compiler-rt-3.4.src.tar.gz
4. Move folders to correct location. We start by renaming 'llvm-3.4.2.src' to 'llvm-3.4.2' 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.4.2.src ./llvm-3.4.2
$mv ./cfe-3.4.2.src ./clang
$mv ./clang ./llvm-3.4.2/tools/
$mv ./compiler-rt-3.4 ./compiler-rt
$mv ./compiler-rt ./llvm-3.4.2/projects/
5. Once everything is in place we create a separate folder for the build process
$mkdir ./build
$cd ./build
This is how the folder structure looks like
|-- build (currently we are here)
|-- llvm-3.4.2
|   |-- projects
|   |   |-- compiler-rt
|   |-- tools
|   |   |-- clang
|-- cfe-3.4.2.src.tar.gz
|-- compiler-rt-3.4.src.tar.gz
|-- llvm-3.4.2.src.tar.gz
6. Now we start the actual configuration and compilation of LLVM inside the 'build' folder outside of the main source directory so as to keep the main source tree clean
$../llvm-3.4.2/configure --enable-shared --enable-targets=host-only
Note : There are various configuration flags for CPU architecture, optimize builds, threads, etc. Check them with the '--help' option.
$../llvm-3.4.2/configure --help
If there are any missing packages required to compile LLVM it will ask you here.
7. If everything is good we can go ahead and compile it and also keep track of the time taken. After compilation all binaries are available in the 'build/Release+Asserts/bin' folder and libraries are available in the 'build/Release+Asserts/lib' folder.
$time make -j 3
.....
llvm[0]: ***** Completed Release+Asserts Build
real 64m49.849s
user 61m49.232s
sys 2m46.721s
Great job so far, few more steps to go :)

8. Its good to run some test suites that comes along with LLVM to verify everything is working.
$make check-all
9. To start using LLVM we have to include the LLVM binaries and libraries in our path.
To add the LLVM binaries
$gedit ~/.bashrc
Add this line to the end of the file
export PATH=$PATH:$HOME/llvm/build/Release+Asserts/bin
To add the LLVM libraries
sudo gedit /etc/ld.so.conf.d/llvm.conf
Add this line to the file (replace <your_user_name> with your current username)
/home/<your_user_name>/llvm/build/Release+Asserts/lib
To apply the new settings do
$source ~/.bashrc
$sudo ldconfig
10. To check whether the LLVM binaries and libraries are correctly set
$which clang
~/llvm/build/Release+Asserts/bin/clang
$sudo ldconfig -p | grep LLVM
libLLVM-3.4.so (libc6,x86-64) => /home/prashants/llvm/build/Release+Asserts/lib/libLLVM-3.4.so
11. 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!

2 comments: