CPM.cmake & the Source Cache
CPM.cmake ("CMake Package Manager") is not a separate tool at all: it's a single CMake script that wraps CMake's own FetchContent module. You include() it and call CPMAddPackage(), and during configure it downloads the dependency's source and adds it to your build with add_subdirectory. No Python, no extra executable, no recipes: if a library has a reasonable CMakeLists.txt, CPM can use it.
Getting CPM
The usual setup downloads a pinned CPM.cmake into the build folder on first configure (the project's README has the snippet), or you commit the file as cmake/CPM.cmake:
set(CPM_DOWNLOAD_VERSION 0.43.2)
# ... the get_cpm.cmake snippet downloads
# https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
include(cmake/CPM.cmake)Our container has version 0.43.2 at /opt/cpm/CPM.cmake and a filled source cache in $CPM_SOURCE_CACHE.
CPMAddPackage
Two syntaxes: a compact string, or named arguments when you need options:
mkdir -p app && cd app
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
include(/opt/cpm/CPM.cmake)
CPMAddPackage("gh:fmtlib/fmt#12.1.0") # GitHub repo + Git tag
CPMAddPackage(
NAME spdlog
GITHUB_REPOSITORY gabime/spdlog
VERSION 1.17.0 # tag v1.17.0 is derived from VERSION
OPTIONS "SPDLOG_FMT_EXTERNAL ON" # use the fmt target added above
)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE spdlog::spdlog)
TXT
cat > main.cpp <<'SRC'
#include <spdlog/spdlog.h>
int main() {
spdlog::set_pattern("[%l] %v");
spdlog::info("spdlog {}.{}.{} with external fmt {}", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR,
SPDLOG_VER_PATCH, FMT_VERSION);
}
SRC
echo "CPM_SOURCE_CACHE=$CPM_SOURCE_CACHE"
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release 2>&1 | grep -E 'CPM:|Configuring done'
echo "compiled $(cmake --build build | grep -c 'Building CXX') source files"
./build/appCPM_SOURCE_CACHE=/opt/cpm-cache
-- CPM: Adding package fmt@12.1.0 (12.1.0 at /opt/cpm-cache/fmt/6226)
-- CPM: Adding package spdlog@1.17.0 (v1.17.0 at /opt/cpm-cache/spdlog/7d8f)
-- Configuring done (0.5s)
compiled 9 source files
[info] spdlog 1.17.0 with external fmt 120100
| Shorthand | Means |
|---|---|
gh:user/repo#tag |
GitHub repository at a Git tag (or commit) |
gh:user/repo@1.2.3 |
version 1.2.3, tag v1.2.3 |
gl:user/repo@…, bb:… |
GitLab, Bitbucket |
https://…/archive.tar.gz |
an archive URL (add URL_HASH SHA256=… in the long form) |
Notice the last line before the run: the dependencies' sources were compiled as part of your build. fmt and spdlog are fast to build; Catch2 has around a hundred source files; Boost would take a long while. Every clean build, every CI job without a build cache, and every project on your machine compiles them again.
The source cache
Without a cache, each project and each fresh build folder downloads every dependency again. Set CPM_SOURCE_CACHE (environment or CMake variable) and CPM stores each source once, in a folder keyed by a hash of the package arguments:
find "$CPM_SOURCE_CACHE" -maxdepth 2 | sort
echo "--- a version that isn't cached"
mkdir -p other && cd other
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(other CXX)
include(/opt/cpm/CPM.cmake)
CPMAddPackage("gh:fmtlib/fmt#12.0.0")
TXT
cmake -B build -G Ninja 2>&1 | grep -E "fatal:|Build step for fmt failed|Configuring incomplete"/opt/cpm-cache
/opt/cpm-cache/catch2
/opt/cpm-cache/catch2/b894
/opt/cpm-cache/catch2/b894.hash
/opt/cpm-cache/catch2/b894.lock
/opt/cpm-cache/catch2/cmake.lock
/opt/cpm-cache/fmt
/opt/cpm-cache/fmt/6226
/opt/cpm-cache/fmt/6226.hash
/opt/cpm-cache/fmt/6226.lock
/opt/cpm-cache/fmt/cmake.lock
/opt/cpm-cache/spdlog
/opt/cpm-cache/spdlog/7d8f
/opt/cpm-cache/spdlog/7d8f.hash
/opt/cpm-cache/spdlog/7d8f.lock
/opt/cpm-cache/spdlog/cmake.lock
--- a version that isn't cached
fatal: unable to access 'https://github.com/fmtlib/fmt.git/': Could not resolve host: github.com
fatal: unable to access 'https://github.com/fmtlib/fmt.git/': Could not resolve host: github.com
fatal: unable to access 'https://github.com/fmtlib/fmt.git/': Could not resolve host: github.com
Build step for fmt failed: 1
-- Configuring incomplete, errors occurred!
A different tag means a different cache entry, and here CPM had to git clone from GitHub, which our offline container can't. The cache is keyed by the package's arguments, so editing OPTIONS doesn't create a new entry, but changing the tag or URL does.
Useful switches
| Variable / argument | Effect |
|---|---|
CPM_SOURCE_CACHE |
shared download cache (set it in your shell profile and in CI) |
CPM_USE_LOCAL_PACKAGES |
try find_package() first, download only if not found |
CPM_LOCAL_PACKAGES_ONLY |
never download; fail if find_package() fails (for distro packagers) |
CPM_DOWNLOAD_ALL |
ignore local packages, always download |
CPM_<Name>_SOURCE |
use a local checkout instead (developing a dependency side by side) |
DOWNLOAD_ONLY YES |
fetch the source but don't add_subdirectory it (header-only libraries, custom targets) |
SYSTEM YES |
treat its headers as system headers (no warnings from them) |
EXCLUDE_FROM_ALL YES |
only build its targets if something links them |
CPM_USE_LOCAL_PACKAGES is what makes a CPM project friendly to Linux distributions and to Conan or vcpkg users: if find_package(fmt) already works, CPM uses that instead of downloading.
Test yourself
What is CPM.cmake built on?
- Conan
- CMake's FetchContent module
- Git submodules
- vcpkg
What does CPM_SOURCE_CACHE save you?
- Compile time
- Repeated downloads: each source is stored once and shared between projects and build folders
- Disk space in the build folder only
- Nothing
Which variable makes CPM use an installed package (via find_package) when one exists?
- CPM_DOWNLOAD_ALL
- CPM_USE_LOCAL_PACKAGES
- CPM_SOURCE_CACHE
- FETCHCONTENT_QUIET