Friday, December 8, 2017

Building LLVM 5.x from source on FreeBSD

A short guide on how to compile LLVM 5.0.0 a low level compiler infrastructure with Clang on FreeBSD.

1. Install required dependencies. You need to be logged in as root to do this.
#pkg install devel/ninja devel/swig13
2. Replace the FreeBSD default linker (ld) with the gold linker. This is important since the default linker (ld) takes up too much memory and usually crashes the building process if you don't have minimum 16GB DRAM and large swap space. You need to be logged in as root to do this.
#cp /usr/local/bin/ld /usr/local/bin/ld.ORIGINAL # backup the original ld linker
#cp /usr/local/bin/ /usr/local/bin/ld # replace with gold linker
After this step you can logout as root user.
3. Create a directory for LLVM
$mkdir ~/llvm
$cd ~/llvm
4. Download the latest version of LLVM sources including clang (C frontend) and compiler RT from
5. Extract the downloaded sources
$tar xJf ./llvm-5.0.0.src.tar.xz
$tar xJf ./cfe-5.0.0.src.tar.xz
$tar xJf ./compiler-rt-5.0.0.src.tar.xz
$tar xJf ./libcxx-5.0.0.src.tar.xz
$tar xJf ./libcxxabi-5.0.0.src.tar.xz
$tar xJf ./libunwind-5.0.0.src.tar.xz
$tar xJf ./lld-5.0.0.src.tar.xz
$tar xJf ./lldb-5.0.0.src.tar.xz
$tar xJf ./openmp-5.0.0.src.tar.xz
$tar xJf ./polly-5.0.0.src.tar.xz
$tar xJf ./clang-tools-extra-5.0.0.src.tar.xz
$tar xJf ./test-suite-5.0.0.src.tar.xz
6. Rename and move folders to correct location where LLVM expects them to be.
$mv ./llvm-5.0.0.src ./llvm-5.0.0
$mv ./cfe-5.0.0.src ./clang
$mv ./clang ./llvm-5.0.0/tools/
$mv ./compiler-rt-5.0.0.src ./compiler-rt
$mv ./compiler-rt ./llvm-5.0.0/projects/
$mv ./libcxx-5.0.0.src ./libcxx
$mv ./libcxx ./llvm-5.0.0/projects/
$mv ./libcxxabi-5.0.0.src ./libcxxabi
$mv ./libcxxabi ./llvm-5.0.0/projects/
$mv ./libunwind-5.0.0.src ./libunwind
$mv ./libunwind ./llvm-5.0.0/projects/
$mv ./lld-5.0.0.src ./lld
$mv ./lld ./llvm-5.0.0/tools/
$mv ./lldb-5.0.0.src ./lldb
$mv ./lldb ./llvm-5.0.0/tools/
$mv ./openmp-5.0.0.src ./openmp
$mv ./openmp ./llvm-5.0.0/projects/
$mv ./polly-5.0.0.src ./polly
$mv ./polly ./llvm-5.0.0/tools/
$mv ./clang-tools-extra-5.0.0.src ./extra
$mv ./extra ./llvm-5.0.0/tools/clang/tools/
$mv ./test-suite-5.0.0.src ./test-suite
$mv ./test-suite ./llvm-5.0.0/projects/
7. This is how it should look like
LLVM source code  => ./llvm
Clang source code  => ./llvm/tools/clang
compiler-rt source code  => ./llvm/projects/compiler-rt
libc++ source code  => ./lvm/projects/libcxx
libc++abi source code  => ./lvm/projects/libcxxabi
libunwind source code  => ./lvm/projects/libunwind
LLD Source code   => ./llvm/tools/lld
LLDB Source code  => ./llvm/tools/lldb
OpenMP Source code  => ./llvm/projects/openmp
Polly Source code  => ./llvm/tools/polly
clang-tools-extra  => ./llvm/tools/clang/tools/extra
LLVM Test Suite   => ./llvm/projects/test-suite
8. Once everything is in place we create a separate folder for the build and installation process
$mkdir ./llvm-5.0.0-build
$mkdir llvm-5.0.0-install
$cd ./llvm-5.0.0-build
9. Now we start the actual configuration and compilation of LLVM inside the 'llvm-5.0.0-build' folder outside of the main source directory so as to keep the main source tree clean
$cmake -G Ninja -DBUILD_SHARED_LIBS=true -DCMAKE_INSTALL_PREFIX=~/llvm/llvm-5.0.0-install -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" ~/llvm/llvm-5.0.0

10. If everything is good it will start compiling. Once its done you can install it
$ninja install
11. After everything is completed all binaries are available in the '~/llvm/llvm-5.0.0-install/bin' folder and libraries are available in the '~/llvm/llvm-5.0.0-install/lib' folder.
12. To start using LLVM we have to include the LLVM binaries and libraries in our path.
$gedit ~/.bashrc
Add this line to the end of the file
export PATH=$PATH:$HOME/llvm/llvm-5.0.0-install/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/llvm/llvm-5.0.0-install/lib
To apply the new settings do
$source ~/.bashrc
13. 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
Hello World from LLVM!
Check the version of LLVM
clang --version
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-unknown-freebsd11.0
Thread model: posix
InstalledDir: /home/USERNAME/llvm/llvm-5.0.0-install/bin

No comments:

Post a Comment