CentOS 7: Build LLVM/Clang from svn repository

For using latest LLVM/Clang, this article will describe building LLVM/Clang from svn repository.


1 Install package for building LLVM/Clang

Install package for building LLVM/Clang with yum.

sudo yum-builddep -y llvm clang

2 Install cmake

LLVM/Clang does not support configure now. You need to use cmake.

A minimum version of cmake required by LLVM/Clang is, but a version of cmake in CentOS 7 is 2.8.11.

This article will port cmake from Fedora 22 to CentOS 7.

$ wget http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/22/Everything/source/SRPMS/c/cmake-3.2.2-1.fc22.src.rpm
$ sudo yum-builddep -y cmake
$ sudo yum install jsoncpp-devel python-sphinx
$ rpmbuild --rebuild cmake-3.2.2-1.fc22.src.rpm
$ sudo yum localinstall -y rpmbuild/RPMS/x86_64/*.rpm

Now cmake 3.2.2 is installed.

$ cmake --version
cmake version 3.2.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

3 Download LLVM/Clang from svn repository

Acording to Clang page, download source code except libcxx.

$ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
$ cd llvm/tools
$ svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
$ cd clang/tools
$ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
$ cd ../../../projects
$ svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
$ cd ../..

4 Build LLVM/Clang

Create build directory, build LLVM/Clang as a release binary and install binary to /usr/local.

$ mkdir llvm.build
$ cd llvm.build
$ cmake -G "Unix Makefiles" \
-DCMAKE_INSTALL_PREFIX=/usr/local ../llvm
$ make
$ sudo make install

5 Excecution result

Compile c source code.

$ /usr/local/bin/clang --version
clang version 3.9.0 (trunk 271105)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
$ echo '#include <stdio.h>
int main(void) { printf("hello\n"); return 0; }' > hello.c
$ /usr/local/bin/clang -v hello.c -o hello
clang version 3.9.0 (trunk 271105)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/local/bin/clang-3.9" -cc1 -triple x86_64-unknown-linux-gnu
 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier
 -discard-value-names -main-file-name hello.c -mrelocation-model
 static -mthread-model posix -mdisable-fp-elim -fmath-errno
 -masm-verbose -mconstructor-aliases -munwind-tables
 -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info
 -debugger-tuning=gdb -resource-dir /usr/local/bin/../lib/clang/3.9.0
 -internal-isystem /usr/local/include -internal-isystem
 /usr/local/bin/../lib/clang/3.9.0/include -internal-externc-isystem
 /include -internal-externc-isystem /usr/include
 -fdebug-compilation-dir /home/hiroom2 -ferror-limit 19
 -fmessage-length 90 -fobjc-runtime=gcc -fdiagnostics-show-option
 -fcolor-diagnostics -o /tmp/hello-f190c1.o -x c hello.c
clang -cc1 version 3.9.0 based upon LLVM 3.9.0svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
End of search list.
 "/usr/bin/ld" --hash-style=gnu --no-add-needed --build-id
 --eh-frame-hdr -m elf_x86_64 -dynamic-linker
 /lib64/ld-linux-x86-64.so.2 -o hello
 -L/usr/local/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64
 -L/usr/local/bin/../lib -L/lib -L/usr/lib /tmp/hello-f190c1.o
 -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed
 -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o
$ ./hello