What Is the LLVM Build System? A Complete Guide to CMake Configuration
The LLVM build system is a CMake-driven infrastructure that orchestrates compilation of core libraries, sub-projects, and runtimes through the top-level llvm/CMakeLists.txt and modular CMake modules.
The LLVM project uses a sophisticated, highly configurable build system to manage its massive monorepo containing compilers, linkers, and libraries. According to the llvm/llvm-project source code, this system centers on CMake as the primary build generator, offering granular control over which components to compile through a set of canonical variables and custom modules.
Core Architecture and CMake Integration
The LLVM build system treats CMake as its fundamental build generator, requiring version 3.20 or newer (with plans to raise the minimum to 3.31). The entry point resides at llvm/CMakeLists.txt, which defines the master project and initializes the configuration cascade.
Project and Runtime Selection
Two primary variables control what gets built:
- LLVM_ENABLE_PROJECTS – Selects sub-projects such as
clang,lld,mlir, orclang-tools-extra. These are built alongside the core LLVM libraries using the same compiler. - LLVM_ENABLE_RUNTIMES – Governs runtimes like
libcxx,libcxxabi,libunwind, andopenmp. These are built separately using the just-built compiler to ensure ABI compatibility.
In llvm/CMakeLists.txt (lines 33–54 and 73–84), these lists are processed to include the appropriate subdirectories and configure compiler flags independently for each category.
Compiler Standards and Installation Layout
LLVM enforces modern C++ standards strictly. The build system requires at least C++17 and sets this via CMake variables in llvm/CMakeLists.txt (lines 92–108). For installation, the system respects GNUInstallDirs and supports LLVM_LIBDIR_SUFFIX to enable multi-architecture library directories, as configured around lines 81–89 of the same file.
Key CMake Modules and Utilities
The LLVM build system extends standard CMake with custom modules located in cmake/Modules/. These encapsulate LLVM-specific patterns for library creation, option handling, and toolchain configuration.
LLVMVersion.cmake
Located at cmake/Modules/LLVMVersion.cmake, this module extracts version metadata (major, minor, and patch numbers) that drives preprocessor definitions and package naming throughout the build.
AddLLVM.cmake
The cmake/Modules/AddLLVM.cmake module provides helper functions such as add_llvm_library() and add_llvm_executable(). These wrappers automatically apply LLVM-specific compile flags, symbol visibility settings, and installation rules.
HandleLLVMOptions.cmake
Found in cmake/Modules/HandleLLVMOptions.cmake, this file centralizes target-level compiler and linker options. It handles warning levels, sanitizers, and platform-specific optimizations across all supported toolchains.
LLVMConfig.cmake.in
This template at cmake/Modules/LLVMConfig.cmake.in generates LLVMConfig.cmake during the build process, enabling downstream projects to locate and link against LLVM using standard CMake find_package() semantics.
Building LLVM from Source
The LLVM build system strongly recommends out-of-source builds, where you create a separate build directory rather than compiling in the source tree. This approach keeps the repository clean and allows multiple build configurations simultaneously.
Basic Build with Ninja
Here is the standard workflow for configuring and building LLVM with Clang and the C++ standard library:
# 1. Clone the repository (shallow clone for speed)
git clone --depth 1 https://github.com/llvm/llvm-project.git
cd llvm-project
# 2. Create and enter a dedicated build directory
mkdir build && cd build
# 3. Configure with CMake (enable Clang, libcxx, and libcxxabi)
cmake -G Ninja \
-DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="X86;ARM" \
-DLLVM_ENABLE_ASSERTIONS=ON \
../llvm
# 4. Compile
ninja
# 5. Install (optional)
ninja install
Minimal Core-Only Build
To build only the core LLVM libraries without any sub-projects or runtimes:
cmake -G Ninja \
-DLLVM_ENABLE_PROJECTS="" \
-DLLVM_ENABLE_RUNTIMES="" \
-DCMAKE_BUILD_TYPE=Debug \
../llvm
ninja llvm
Custom Installation Prefix
Specify a non-standard installation location using CMAKE_INSTALL_PREFIX:
cmake -G Ninja \
-DCMAKE_INSTALL_PREFIX=/opt/llvm \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
../llvm
ninja
ninja install # Installs to /opt/llvm
Summary
- The LLVM build system is a CMake-based framework requiring version 3.20 or newer, orchestrated from
llvm/CMakeLists.txt. - Use LLVM_ENABLE_PROJECTS to select sub-projects (Clang, LLD, MLIR) and LLVM_ENABLE_RUNTIMES for standard library components.
- Custom modules in
cmake/Modules/(includingAddLLVM.cmakeandHandleLLVMOptions.cmake) enforce consistent compiler flags and installation rules. - The system mandates C++17 and supports flexible installation layouts via
GNUInstallDirsandLLVM_LIBDIR_SUFFIX. - Always perform out-of-source builds using generators like Ninja for optimal compilation speed and configuration isolation.
Frequently Asked Questions
What is the minimum CMake version required for building LLVM?
LLVM requires CMake 3.20 or newer, though the project plans to increase this minimum to 3.31 in future releases. This requirement ensures support for modern CMake features used in the cross-platform configuration logic defined in llvm/CMakeLists.txt.
What is the difference between LLVM_ENABLE_PROJECTS and LLVM_ENABLE_RUNTIMES?
LLVM_ENABLE_PROJECTS controls tools built with your system compiler (like Clang, LLD, and MLIR), while LLVM_ENABLE_RUNTIMES controls libraries built with the newly compiled LLVM toolchain (like libc++, libc++abi, and OpenMP). This separation ensures that runtime libraries match the ABI and optimization settings of the compiler that uses them.
Does LLVM support in-source builds?
While technically possible, the LLVM build system strongly recommends out-of-source builds where you create a separate build directory. This practice prevents pollution of the source tree and allows maintaining multiple build configurations (Debug, Release, different target architectures) from the same source checkout.
How does LLVM handle version numbering during the build?
Version metadata is extracted by cmake/Modules/LLVMVersion.cmake, which defines major, minor, and patch version variables. These values propagate throughout the build system to generate versioned headers, package names, and the LLVMConfig.cmake file used by external projects linking against LLVM.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →