如何用CMake构建LLVM指定组件以缩短构建时间?
Absolutely, I’ve done this countless times to skip the agonizing full LLVM build—here’s a step-by-step guide tailored exactly to your needs:
Key CMake Configuration
The secret to building only what you need lies in LLVM’s CMake options that let you cherry-pick components and disable unnecessary bloat. Here’s the exact workflow to follow:
First, create a separate build directory (always keep builds isolated from source code to avoid clutter):
mkdir llvm-build && cd llvm-build
Then run the CMake configure command (adjust the ../llvm path to match your LLVM source directory location):
cmake -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_TARGETS_TO_BUILD=Native \ -DLLVM_BUILD_COMPONENTS="Analysis;Core;ExecutionEngine;InstCombine;Object;RuntimeDyld;ScalarOpts;Support;MC;MCDisassembler;MCJIT" \ -DLLVM_BUILD_TOOLS=OFF \ -DLLVM_BUILD_EXAMPLES=OFF \ -DLLVM_BUILD_TESTS=OFF \ -DLLVM_BUILD_BENCHMARKS=OFF \ ../llvm
Breakdown of Each Option
-G Ninja: Uses the Ninja build system instead of Make—it’s way faster for large projects like LLVM, so this is a must-have for cutting build time.-DCMAKE_BUILD_TYPE=Release: Builds optimized, production-ready binaries (reduces both build time and output size compared to Debug mode).-DLLVM_TARGETS_TO_BUILD=Native: Only builds support for your current machine’s architecture (no wasted time on ARM, MIPS, or other architectures you don’t need).-DLLVM_BUILD_COMPONENTS: This is where you list your target components. I mapped your requested names to LLVM’s official identifiers (e.g.,mcbecomesMC,mcdisassemblerbecomesMCDisassembler).- The final four
OFFflags: Disable tools, examples, tests, and benchmarks—all non-essential extras that add hours to full builds.
Start the Build
Once configured, kick off the build with:
ninja
If you prefer using Make instead of Ninja, replace -G Ninja with your default generator and run make -j$(nproc) to use all your CPU cores for faster compiling.
Extra Tips
- If you want to install the built libraries/headers to a specific location, add
-DCMAKE_INSTALL_PREFIX=/your/preferred/pathto the CMake command, then runninja installafter the build finishes. - LLVM’s CMake automatically handles hidden dependencies between components, so you don’t have to manually track or add required libraries.
- If you ever need to adjust your component list, just re-run the CMake command with the updated
LLVM_BUILD_COMPONENTSvalue and rebuild.
内容的提问来源于stack exchange,提问作者ClubberLang




