CMake Cache Flags for CI: -D, Build Type, Launchers
A handful of -D cache variables configure a CMake build for CI: build type, compiler caching, and compile-command export.
You do not need a preset for everything; a few well-chosen -D flags set the build type, wire in ccache/sccache, and emit compile_commands.json for tooling. These are the cache variables worth knowing for pipelines.
What it does
-D sets a CMake cache variable at configure time. CMAKE_BUILD_TYPE selects optimization for single-config generators like Ninja; CMAKE_<LANG>_COMPILER_LAUNCHER routes compiles through ccache or sccache; CMAKE_EXPORT_COMPILE_COMMANDS writes compile_commands.json for clang-tidy and editors. Each persists in CMakeCache.txt for later builds.
Common usage
cmake -G Ninja -B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build buildOptions
| Cache variable | What it does |
|---|---|
| CMAKE_BUILD_TYPE | Release, Debug, RelWithDebInfo, MinSizeRel (single-config) |
| CMAKE_<LANG>_COMPILER_LAUNCHER | Run ccache/sccache before the compiler |
| CMAKE_EXPORT_COMPILE_COMMANDS=ON | Emit compile_commands.json |
| CMAKE_INSTALL_PREFIX | Install location |
| CMAKE_PREFIX_PATH | Extra roots for find_package/find_library |
In CI
With Ninja, always set CMAKE_BUILD_TYPE since there is no default optimization; without it you ship unoptimized binaries. Wire the compiler launcher to ccache or sccache here so caching survives reconfigures, and turn on CMAKE_EXPORT_COMPILE_COMMANDS so static-analysis steps can read compile_commands.json. Set CMAKE_PREFIX_PATH when a dependency lives under a custom prefix.
Common errors in CI
"CMAKE_BUILD_TYPE was changed but the cache was not regenerated" hints you changed it on an existing cache; delete the build dir or reconfigure. A launcher that has no effect means the variable name is misspelled or set after the first configure cached the compiler. "Could NOT find <Pkg>" despite an install usually needs CMAKE_PREFIX_PATH pointed at its prefix.