The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Build caches

Compiling dependencies is usually the slowest part of a C++ build. cx has two caches for it.

Compiler caches

# ~/.config/cx/config.toml
[build]
compiler-cache = "auto" # "auto" (default), "sccache", "ccache", "none", or a program
  • auto: uses sccache or ccache when one is installed, so it works with no setup at all. Without either, nothing changes.
  • A name or path: that program is used as the compiler launcher (CMAKE_<LANG>_COMPILER_LAUNCHER). One that isn't installed is an error.
  • CX_COMPILER_CACHE overrides the setting for one command.

The artifact cache

With the artifact cache on, a dependency's compiled library is stored once and reused by every later build that needs exactly the same thing: in other projects, after cx clean, or in a fresh checkout.

# ~/.config/cx/config.toml
[build]
artifact-cache = true # or CX_ARTIFACT_CACHE=1
  • What is cached: the static libraries of cx packages from registries and git, with their export headers. They're stored in ~/.cache/cx/artifacts/ ($CX_HOME/artifacts/).
  • What is not: your own packages and workspace members, plain CMake dependencies, header-only libraries (nothing to compile), and anything that depends on a path or system package, whose contents cx can't vouch for.
  • On a hit, CMake prints cx: <name> comes from the artifact cache (see it with -v), and the dependency isn't compiled.

When an artifact is reused

An artifact's key is a SHA-256 of everything that can change the compiled code:

  • the package's exact source (archive checksum or git commit), its cx.toml, its enabled features and options, and cx's version
  • the same for each of its dependencies
  • the compilers (path, id and version), the build type and its flags, sanitizers, LTO, the sysroot, the toolchain file's contents, and the target platform

Correctness comes first: when any part is unknown, cx builds from source. A miss costs a compile; a wrong hit would be much worse. For the same reason, the artifact cache is off for multi-config generators (Visual Studio, Xcode), which have no single build type when CMake configures.

Delete ~/.cache/cx/artifacts/ at any time; cx builds what's missing, and cx cache gc removes artifacts nobody used for a while (see below).

A shared artifact store

The artifact cache is per machine. A store shares it, so CI runners, new containers and teammates download a dependency's compiled library instead of compiling it:

# ~/.config/cx/config.toml (or the project's .cx/config.toml)
[build]
artifact-cache = true
artifact-store = "https://cache.example.com/cx" # or file:///shared/cx, or a directory
artifact-store-write = false # CI with credentials sets true
artifact-keys = ["ed25519:…"] # optional: entries must be signed by one of these
artifact-signing-key = "/run/secrets/cx-artifacts.key" # writers: sign what they upload
  • Reading: after CMake configures, cx asks the store for every artifact it doesn't have locally, in one parallel batch, checks each one and configures again if any arrived. CMake then imports them, and cx prints Downloaded <name>-<key> (cached artifact).
  • Writing: with artifact-store-write (or CX_ARTIFACT_STORE_WRITE=1), a successful build uploads what it compiled and the store doesn't have: PUT <store>/<name>-<key>.tar.gz (and .sig when signing), or a file copy for a directory store.
  • The token for uploads is that of a registry named artifact-store: CX_TOKEN_ARTIFACT_STORE, cx login --registry artifact-store, or its credential provider. It is only sent to the store's host.
  • Integrity: an entry is a reproducible .tar.gz of the artifact with a cx-artifact.toml listing the SHA-256 of every file. It is checked before use, and with artifact-keys its signature must verify too. A bad or unsigned entry is a warning, and the dependency is built locally.
  • Network rules: a store that can't be reached is a warning, then a local build. --offline never asks an HTTP store. A fully locked build whose artifacts are all local makes no requests.
  • Servers: anything that serves files with GET and accepts PUT works (nginx with WebDAV, an object store behind a signing proxy), or a shared directory.

The key is the same one the local cache uses, plus the compiler's target and library architecture, so machines only share an artifact when the toolchains match.

Cleaning up the cache

The cache (~/.cache/cx, or CX_HOME) holds downloaded sources, git checkouts, compiled dependencies, external tools and cx install trees, and it only grows unless you clean it:

$ cx cache info # size and age per kind
$ cx cache gc # remove what wasn't used for 90 days
$ cx cache gc --max-age 30d --max-size 10GiB
$ cx cache gc --kind artifacts --dry-run
$ cx cache clean # everything (credentials and config live elsewhere)
$ cx clean --gc # in a project: stale target/ directories
  • Last use: cx records which entries a run used in <cache>/last-use.toml, written once at the end of the run; artifacts are marked by CMake when it imports them. Older entries fall back to file times.
  • What gc keeps: anything the current project's cx.lock needs, and the cached index files (--kind index removes those too). Tool releases cx no longer pins are removed whatever their age. Offline, gc only removes compiled dependencies and install trees, which can be rebuilt without the network.
  • Kinds: registry, index, git, artifacts, tools, install.
  • Automatically: cache.auto-gc = "90d" in config.toml (or CX_CACHE_AUTO_GC) runs the age-based cleanup after a successful command, at most once a day and never offline.
  • cx clean --gc removes target/<profile>/ of profiles no longer in cx.toml, and target/<triple>/ not built for 90 days.