TExcelTAD

sccache: Cloud Backends & Rust

sccache ("shared compilation cache") is Mozilla's answer to ccache, written in Rust. It started as a ccache-like wrapper with cloud storage for Firefox's CI and now covers more ground:

ccache sccache
Languages C, C++, Objective-C, CUDA (nvcc), assembler C, C++ (GCC, Clang, MSVC, clang-cl), Rust, CUDA (nvcc, clang), HIP
Architecture runs per compile, no daemon a local server process that the wrapper talks to; it keeps state between compiles
Local cache yes (the main mode) yes (default ~/.cache/sccache, 10 GiB)
Remote storage file, HTTP, Redis (since 4.4) S3 (and S3-compatible: MinIO, R2), GCS, Azure Blob, Redis, Memcached, GitHub Actions cache, WebDAV, OSS, …
Lookup strategy direct / preprocessor / depend modes preprocessor-based hashing for C/C++; releases since 0.7 add a ccache-like "preprocessor cache mode" (direct lookups) for the local disk cache, on by default in current docs (SCCACHE_DIRECT=false turns it off)
Distributed compilation no (combine with distcc/icecream) sccache-dist: a scheduler and build servers with sandboxed toolchains
MSVC supported since 4.6 long-standing support

sccache isn't installed in this course's image, so this lesson shows configuration and sample sessions (non-run). The mechanics are the same as ccache's, which the previous lessons demonstrated for real: hash the inputs, look up the key, store the outputs.

Using it with CMake

Exactly like ccache: a compiler launcher.

bash
cmake -S . -B build -G Ninja \
  -DCMAKE_C_COMPILER_LAUNCHER=sccache \
  -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
cmake --build build
sccache --show-stats

Sample output of sccache --show-stats after a second clean build (abridged):

text
Compile requests                     128
Compile requests executed            120
Cache hits                           120
Cache hits (C/C++)                   120
Cache misses                           0
Cache timeouts                         0
Cache read errors                      0
Non-cacheable compilations             0
Non-cacheable calls                    8
Cache location                  Local disk: "/home/ada/.cache/sccache"
Max cache size                        10 GiB

Other commands: sccache --zero-stats (same idea as ccache -z), sccache --stop-server (the server also exits after an idle timeout), sccache --show-adv-stats.

Backends: configuration by environment

The backend is selected by environment variables (or ~/.config/sccache/config). A few common ones:

bash
# S3 (or any S3-compatible store)
export SCCACHE_BUCKET=acme-build-cache
export SCCACHE_REGION=eu-central-1
export SCCACHE_S3_KEY_PREFIX=linux-gcc13/        # separate namespaces per toolchain
# credentials from the usual AWS chain (env, profile, instance role)

# Google Cloud Storage
export SCCACHE_GCS_BUCKET=acme-build-cache
export SCCACHE_GCS_RW_MODE=READ_ONLY             # e.g. for pull requests
export SCCACHE_GCS_KEY_PATH=/secrets/gcs.json

# Redis
export SCCACHE_REDIS_ENDPOINT=redis://cache.internal:6379

# GitHub Actions' own cache service (in a workflow)
export SCCACHE_GHA_ENABLED=true

# Local disk (default)
export SCCACHE_DIR=$HOME/.cache/sccache
export SCCACHE_CACHE_SIZE=20G

Only one backend is active at a time. Unlike ccache's local + remote layering, a cloud backend means every lookup goes to the network, so the latency model matters more; keep the bucket in the same region as the CI runners.

Rust: RUSTC_WRAPPER

Cargo can run every rustc invocation through a wrapper. Pointing it at sccache caches compiled crates:

toml
# ~/.cargo/config.toml (or .cargo/config.toml in the project)
[build]
rustc-wrapper = "sccache"

or export RUSTC_WRAPPER=sccache. A typical CI pattern is "cache the dependencies": the dozens of crates from the registry rarely change, so they're nearly always hits, while your own crates recompile. Limitations to know:

  • Crates built incrementally (CARGO_INCREMENTAL=1, the dev-profile default) aren't cached; CI sets CARGO_INCREMENTAL=0.
  • Crates with build.rs scripts that read the environment, proc-macro crates and cdylib/binaries have restrictions, and absolute paths matter (keep the same CARGO_HOME and project path, or remap them).
  • The Cargo in Depth course covers profiles, build.rs and caching from Cargo's side.

sccache-dist: distributed compilation

sccache also ships a distributed mode: a scheduler and several build servers. Clients send compile jobs together with a packaged toolchain (the compiler and its files), and servers run them in a sandbox (an overlay-filesystem builder using bubblewrap, or Docker). It supports C/C++ and Rust, with authentication tokens between the parts. It's less common than the alternatives in the next section, and most teams that outgrow a cache move to a full remote-execution system such as Bazel's.

When to pick which

  • ccache: C/C++ on Linux and macOS, local-first workflows, NFS/HTTP/Redis sharing, the most mature C/C++ heuristics (direct/depend modes, sloppiness).
  • sccache: you also build Rust, your CI is in a cloud with object storage, or you use MSVC heavily.
  • Both: whichever you pick, measure the hit rate in CI and keep the cache key namespaces per toolchain.

Test yourself

How do you make Cargo use sccache?

  1. CMAKE_CXX_COMPILER_LAUNCHER=sccache
  2. Set RUSTC_WRAPPER=sccache (or build.rustc-wrapper in .cargo/config.toml)
  3. cargo install --cache
  4. It's automatic

Which is a real architectural difference between sccache and ccache?

  1. sccache can't cache C++
  2. sccache runs a local server process and supports cloud object stores such as S3 and GCS
  3. ccache only works on Windows
  4. sccache has no local cache

Why do CI setups for Rust often set CARGO_INCREMENTAL=0 with sccache?

  1. Incremental builds are faster
  2. Incrementally compiled crates aren't cacheable by sccache
  3. sccache needs debug info
  4. To disable build.rs