Skip to content

ensemblr/llvm-project-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

llvm-project-boilerplate

Boilerplate for LLVM based Tools

Compiling LLVM

The LLVM version provided by Ubuntu 16.04 (v3.8) has some problems that lead to errors.
We will be building LLVM v3.9.1 which is the latest as of this writing.
We assume that you have a working compiler toolchain (GCC or LLVM) and that CMake is installed (minimum version 3.4.3).

  1. Download LLVM source and unpack it in a directory of your choice which will refer to as $LLVM_SRC

  2. Create a separate build directory

    $ mkdir llvm-build
    $ cd llvm-build
  3. Instruct CMake to detect and configure your build environment:

    $ cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=X86 $LLVM_SRC

    Note that we instructed cmake to only build X86 backend. You can choose different backends if needed. Without specifying LLVM_TARGETS_TO_BUILD all supported backends will be built by default which requires more time.

  4. Now start the actual compilation within your build directory

    $ cmake --build .

    The --build option is a portable why to tell cmake to invoke the underlying build tool (make, ninja, xcodebuild, msbuild, etc.)

  5. Building takes some time to finish. After that you can install LLVM in its default directory which is /usr/local

    $ cmake --build . --target install

    Alternatively, it's possible to set a different install directory ($LLVM_HOME)

    $ cmake -DCMAKE_INSTALL_PREFIX=$LLVM_HOME -P cmake_install.cmake

    Note that $LLVM_HOME must not contain ~ (tilde) to refer to your home directory as it won't be expanded. Use absolute paths instead.

Build

$ cd llvm-project-boilerplate
$ mkdir build
$ cd build
$ LLVM_DIR="$LLVM_HOME/share/llvm/cmake" cmake ..
$ make
$ cd ..

cmake needs to find its LLVM configurations which we provide using $LLVM_DIR.

Run

$ clang-3.9 -Xclang -load -Xclang build/skeleton/libSkeletonPass.* Skeleton.cpp

Note that Clang needs to be installed separately.

Building both LLVM and Clang

  1. Run the script download.sh provided in this repo where you want to place the llvm source.

  2. Create a separate build directory

    $ mkdir llvm-build
    $ cd llvm-build
  3. Install the Ninja build system

    $ sudo apt install ninja-build

    Ninja is a preferred build system for llvm toolchain. Other options are : Unix Makefiles — for generating make-compatible parallel makefiles. Visual Studio — for generating Visual Studio projects and solutions. Xcode — for generating Xcode projects. To use these options, replace "Ninja" in next step by these strings.

  4. Instruct CMake to detect and configure your build environment:

    $ cmake -G "Ninja" -DLLVM_TARGETS_TO_BUILD=X86 $LLVM_SRC

    Note that we instructed cmake to only build X86 backend. You can choose different backends if needed. Without specifying LLVM_TARGETS_TO_BUILD all supported backends will be built by default which requires more time.

  5. Now start the actual compilation within your build directory

    $ ninja
  6. Building takes some time to finish. After that you can install LLVM in its default directory which is /usr/local

    $ ninja install

Further resources

This tutorial is based on the following resources

  • Adrian Sampson's blog entry "LLVM for Grad Students" (link)
  • LLVM documentation: Writing an LLVM pass (link)
  • LLVM documentation: Building LLVM with CMake (link)