Compile LLVM with OpenMP on macOS and Linux

This post is just supposed to be my personal note. But I will be happy if this can help you in some way!

Prerequisites

# macOS
brew install libomp cmake

# Ubuntu/Debian
sudo apt-get install -y libomp-dev cmake

Download LLVM source code

# You can check the latest LLVM verison at llvm.org 
export LLVM_VER=11.0.0
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/llvm-${LLVM_VER}.src.tar.xz" -O "llvm-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/clang-${LLVM_VER}.src.tar.xz" -O "clang-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/compiler-rt-${LLVM_VER}.src.tar.xz" -O "compiler-rt-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/libcxx-${LLVM_VER}.src.tar.xz" -O "libcxx-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/libcxxabi-${LLVM_VER}.src.tar.xz" -O "libcxxabi-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/lld-${LLVM_VER}.src.tar.xz" -O "lld-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/lldb-${LLVM_VER}.src.tar.xz" -O "lldb-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/openmp-${LLVM_VER}.src.tar.xz" -O "openmp-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/polly-${LLVM_VER}.src.tar.xz" -O "polly-${LLVM_VER}.src.tar.xz"
wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/clang-tools-extra-${LLVM_VER}.src.tar.xz" -O "clang-tools-extra-${LLVM_VER}.src.tar.xz"

tar xf "llvm-${LLVM_VER}.src.tar.xz"
tar xf "clang-${LLVM_VER}.src.tar.xz" && mv "clang-${LLVM_VER}.src" clang
tar xf "compiler-rt-${LLVM_VER}.src.tar.xz" && mv "compiler-rt-${LLVM_VER}.src" compiler-rt
tar xf "libcxx-${LLVM_VER}.src.tar.xz" && mv "libcxx-${LLVM_VER}.src" libcxx
tar xf "libcxxabi-${LLVM_VER}.src.tar.xz" && mv "libcxxabi-${LLVM_VER}.src" libcxxabi
tar xf "lld-${LLVM_VER}.src.tar.xz" && mv "lld-${LLVM_VER}.src" lld
tar xf "lldb-${LLVM_VER}.src.tar.xz" && mv "lldb-${LLVM_VER}.src" lldb
tar xf "openmp-${LLVM_VER}.src.tar.xz" && mv "openmp-${LLVM_VER}.src" openmp
tar xf "polly-${LLVM_VER}.src.tar.xz" && mv "polly-${LLVM_VER}.src" polly
tar xf "clang-tools-extra-${LLVM_VER}.src.tar.xz" && mv "clang-tools-extra-${LLVM_VER}.src" clang-tools-extra

export LLVM_SRC="`pwd`/llvm-${LLVM_VER}.src"
cd "${LLVM_SRC}" && mkdir -p build && cd build

Configure and compile

# macOS WITH Xcode toolchain enabled
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_CREATE_XCODE_TOOLCHAIN=ON \
    -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;libcxx;libcxxabi;lld;lldb;openmp;polly;clang-tools-extra;" \
    ..

# macOS WITHOUT Xcode toolchain
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_CREATE_XCODE_TOOLCHAIN=OFF \
    -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;libcxx;libcxxabi;lld;lldb;openmp;polly;clang-tools-extra;" \
    ..

# Linux
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;libcxx;libcxxabi;lld;lldb;openmp;polly;clang-tools-extra;" \
    ..

# Compile
ninja

Install

# macOS WITH Xcode toolchain enabled
sudo mkdir -p /usr/local/Toolchains
sudo chmod a+w /usr/local/Toolchains
ninja install-xcode-toolchain
sudo ln -s /usr/local/Toolchains/LLVM${LLVM_VER}.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains
# copy arc related libraries from default toolchain to ours
sudo cp -rf /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc /Applications/Xcode.app/Contents/Developer/Toolchains/LLVM${LLVM_VER}.xctoolchain/usr/lib/

# macOS WITHOUT Xcode toolchain
# and Linux
sudo ninja install

Test

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + 2 =