Build Acceleration

mohitmishra786/low-level-dev-skills/skills/build-systems/build-acceleration

作者 mohitmishra786bdc58472fa9f无许可证253 个星标收录于 2026年10月9日更新于 2026年10月9日仓库3个月前更新

Build acceleration skill for C/C++ projects. Use when reducing compilation times with ccache, sccache, distcc, unity builds, precompiled headers, split DWARF, or IWYU. Covers caching strategies, distributed compilation, link time reduction, and diagnosing build bottlenecks. Activates on queries about slow builds, ccache, sccache, precompiled headers, unity builds, split-DWARF, or reducing C++ compile times.

AI 生成的概览

指导智能体使用 ccache、sccache、distcc、unity 构建、预编译头、split-DWARF 和 IWYU 加速 C/C++ 构建。

功能
该技能提供诊断 C/C++ 构建瓶颈并应用加速方法的说明。内容涵盖使用 ccache 和 sccache 的编译器缓存、使用 distcc 的分布式编译、unity/jumbo 构建、预编译头、用于加快链接的 split-DWARF,以及使用 IWYU 精简头文件包含。它产出面向 CMake、Make 和 Ninja 的配置片段与命令序列,而非脚本。
适用场景
当 C 或 C++ 构建过慢、需要缩短编译或链接时间时使用。也适用于设置 ccache、sccache、distcc、预编译头、unity 构建、split-DWARF,或查找拖慢编译的头文件等问题。
运行要求
不附带脚本,仅为说明。按指引操作需要 C/C++ 工具链和构建系统(如 CMake、Make 或 Ninja),以及可选的 ccache、sccache、distcc、IWYU 和 dwp 等工具;这些工具可能需要安装,分布式或云端缓存还需要网络访问和凭据。

Build Acceleration

Purpose

Guide agents through reducing C/C++ build times using caching (ccache/sccache), distributed compilation (distcc), unity/jumbo builds, precompiled headers, split-DWARF for faster linking, and include pruning with IWYU.

Triggers

  • "My C++ build is too slow — how do I speed it up?"
  • "How do I set up ccache / sccache?"
  • "How do precompiled headers work with CMake?"
  • "How do I set up distributed compilation with distcc?"
  • "How do I reduce link times with split-DWARF?"
  • "How do I find which headers are slowing down compilation?"

Workflow

1. Diagnose the bottleneck first

bash
# Time the full buildtime cmake --build build -j$(nproc)
# Find the slowest TUs (CMake ≥3.16 with --profiling-output)cmake -S . -B build -DCMAKE_CXX_FLAGS="-ftime-report"cmake --build build 2>&1 | grep "Total" | sort -t: -k2 -rn | head -20
# Ninja build timings (use ninja -j1 for serial timing)ninja -C build -j1 2>&1 | grep "^\[" | sort -t" " -k2 -rn | head -20

2. ccache — compiler cache

bash
# Installapt-get install ccache   # Ubuntu/Debianbrew install ccache      # macOS
# Check hit rateccache -s
# Configure cache size (default 5GB)ccache -M 20G
# Invalidate cache if neededccache -C

CMake integration (recommended over prefix hacks):

cmake
# CMakeLists.txtfind_program(CCACHE_PROGRAM ccache)if(CCACHE_PROGRAM)    set(CMAKE_C_COMPILER_LAUNCHER   ${CCACHE_PROGRAM})    set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})endif()

Key ~/.config/ccache/ccache.conf options:

ini
max_size = 20Gcompression = truecompression_level = 6# For CI: share cache across jobscache_dir = /shared/ccache

3. sccache — cloud-compatible cache (Rust, C/C++)

bash
cargo install sccache# Or: brew install sccache
# Set as compiler launcherexport RUSTC_WRAPPER=sccache          # for Rustexport CMAKE_C_COMPILER_LAUNCHER=sccache    # for CMake
# With S3 backendexport SCCACHE_BUCKET=my-build-cacheexport SCCACHE_REGION=us-east-1sccache --start-server
sccache --show-stats

4. Precompiled headers (PCH)

PCH compiles a large header once and reuses the binary form.

cmake
# CMake ≥3.16 native PCH supporttarget_precompile_headers(mylib PRIVATE    <vector>    <string>    <unordered_map>    "myproject/common.h")
# Share PCH across targets (avoids recompilation)target_precompile_headers(myapp REUSE_FROM mylib)
c
// Traditional: stdafx.h / pch.h approach// All TUs include pch.h as the very first include// pch.h includes heavy system headers#pragma once#include <stdio.h>#include <stdlib.h>#include <string.h>

PCH is most effective when headers are large and stable (STL, Boost, Qt). Avoid PCH for frequently-changing project headers.

5. Unity / jumbo builds

Combine multiple .cpp files into one TU to reduce header parsing overhead and improve inlining.

cmake
# CMake ≥3.16 unity buildset_target_properties(mylib PROPERTIES UNITY_BUILD ON)# Control batch size (default 8 files per unity TU)set_target_properties(mylib PROPERTIES UNITY_BUILD_BATCH_SIZE 16)
# Exclude specific files from unity (e.g., if they have ODR issues)set_source_files_properties(problem.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)

Manual unity file:

cpp
// unity_build.cpp#include "module_a.cpp"#include "module_b.cpp"#include "module_c.cpp"

Watch out for: anonymous namespaces (each TU has its own), using namespace in headers, duplicate static variables.

6. split-DWARF — reduce link time

Split DWARF puts debug info in .dwo sidecar files, dramatically reducing what the linker must process.

bash
# GCC / Clanggcc -g -gsplit-dwarf -o prog main.c
# CMake globaladd_compile_options(-gsplit-dwarf)
# Combine .dwo files for distribution (optional)dwp -o prog.dwp prog  # GNU dwp tool

Pair with --gdb-index for faster GDB startup:

bash
gcc -g -gsplit-dwarf -Wl,--gdb-index -o prog main.c

Link time comparison (large project, typical): -g full DWARF ~4×–6× longer link vs -gsplit-dwarf.

7. distcc — distributed compilation

bash
# Install on all machinesapt-get install distcc
# Start daemon on worker machinesdistccd --daemon --allow 192.168.1.0/24 --jobs 8
# Client: set DISTCC_HOSTSexport DISTCC_HOSTS="localhost/4 worker1/8 worker2/8"make -j20 CC="distcc gcc"
# CMake integrationset(CMAKE_C_COMPILER_LAUNCHER distcc)set(CMAKE_CXX_COMPILER_LAUNCHER distcc)

Stack with ccache: CC="ccache distcc gcc" — ccache checks local cache first, falls back to distcc.

8. Include pruning with IWYU

bash
# Installapt-get install iwyu
# Run via CMakecmake -S . -B build -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=iwyucmake --build build 2>&1 | tee iwyu.log
# Apply fixes automaticallyfix_include < iwyu.log --nosafe_headers

See skills/build-systems/include-what-you-use for full IWYU workflow.

For ccache configuration options, see references/ccache-config.md [blocked].

Related skills

  • Use skills/build-systems/cmake for CMake project structure
  • Use skills/build-systems/include-what-you-use for IWYU header pruning
  • Use skills/rust/rust-build-times for Rust-specific build acceleration
  • Use skills/debuggers/dwarf-debug-format for split-DWARF internals

来源与署名

来源:mohitmishra786/low-level-dev-skills位于skills/build-systems/build-acceleration提交bdc5847

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架