How to build LLVM (with GCC as the front end) ?

  • Make sure the assembler as and linker ld are not too old. They can be found in GNU BinUtils.
  • Make sure make is not too old. It can be found at here.
  • Make sure GCC is not too old. LLVM is written in C++, so GCC must be new enough to support C++ features. See here for possible issues with specific versions of GCC.
  • Download both LLVM and LLVM-GCC Front End source code from here.

    Untar LLVM source tarball first, which will create a directory, say

        /tmp/llvm-2.x/
        
    Then untar the LLVM-GCC Front End source tarball, which will create a directory, say
        /tmp/llvm-gcc-4.y-2.x.source/
        
    It's better to use the GNU tar to avoid issues.
  • Find a scratch directory, say /scratch. Then
          cd /scratch
          /tmp/llvm-gcc-4.y-2.x.source/configure --prefix=/usr/local/ <other options>
        
    where <other options> are:

    • --enable-languages=c,c++,fortran
    • --program-prefix=llvm- (all the LLVM-GCC binaries will have this prefix)
    • --enable-llvm=/tmp/llvm-2.x/ (point to the LLVM source code directory)
    • --disable-bootstrap
    • --disable-multilib (if you only need build for 64-bit target)

    See here for other options.

  • Build and install:
        gmake -j 5
        gmake install
        
  • The following binaries will be created
        llvm-gcc
        llvm-g++
        llvm-gcov
        llvm-gfortran
        
    To test:
        llvm-gcc hello.c
        
    should create an executable binary a.out just as you would expect.
        llvm-gcc -c -emit-llvm hello.c
        
    should instead create an LLVM bitcode object hello.o. To run it (if you have LLVM compiled):
        lli hello.bc
        

How to build LLVM (with Clang as the front end)

Clang is the preferred front end for LLVM (but unlike LLVM-GCC, it does not support Fortran)
  • Make sure the assembler as and linker ld are not too old. They can be found in GNU BinUtils.
  • Make sure make is not too old. It can be found at here.
  • Make sure GCC 4.3 or 4.4 are installed. LLVM is written in C++, so GCC must be new enough to support C++ features. See here for possible issues with specific versions of GCC.

    Another reason why GCC 4.3 or 4.4 are needed is because when compiling C++ code, Clang will seek C++-specific headers (e.g. iostream) in hard-coded paths such as /usr/include/c++/4.4 or /usr/include/c++/4.3. In addition, Clang will invoke g++ to generate code (from assembly) and do the linking to libstd++.

  • Download both LLVM and Clang source code from here.

    Untar LLVM source tarball first, which will create a directory, say

        /tmp/llvm-2.x/
        
    Then enter LLVM's tool directory, untar the Clang source package here, and create a symbolic link:
        cd /tmp/llvm-2.x/tool
        tar zxvf /tmp/clang-2.x.tgz
        ln -s clang-2.x clang
        
  • In LLVM's tool directory, check (and edit) Makefile to ensure "clang" is in PARALLEL_DIRS (it is in OPTIONAL_PARALLEL_DIRS, but this is probably not enough):
        vi /tmp/llvm-2.x/tool/Makefile
        
  • Go back to LLVM's main directory and build LLVM and Clang together:
        cd /tmp/llvm-2.x/
        gmake -j 5
        
  • Make sure the C++-header files from GCC 4.3 or 4.4. can be found under
        /tmp/llvm-2.7/lib/clang/1.1/include
        
  • The binaries will be created in /tmp/llvm-2.x/Release/bin/.

    clang/clang++ are the gcc/g++ equivalent in LLVM.

    lli is the LLVM bitcode interpreter.

    To test:

        clang hello.c
        clang++ hello.cxx
        
    should create executable binary a.out just as you would expect.

    If clang++ complains about unable to find C++-specific header files, use

        clang++ -v hello.cxx
        
    to see its search paths, and copy GCC 4.3 or 4.4 C++-specific header files to the appropriate paths, or add -I... command-line options.
        clang -c -emit-llvm hello.c
        
    should instead create an LLVM bitcode object hello.o. To run it:
        lli hello.o
        
    To disassemble it (into LLVM assembly):
        llvm-dis < hello.o
        
    To convert the bitcode to native assembly code
        llc hello.o -o hello.s