sccache

Cache LibraryCompiler CacheDistributed BuildRustMozilla

Cache Library

sccache

Overview

sccache is a ccache-like compiler caching tool developed by Mozilla. It caches compilation results to significantly speed up recompilation of the same code.

Details

sccache (Shared Compilation Cache) operates as a compiler wrapper for C/C++, Rust, and other languages, caching compilation results to dramatically improve development efficiency. Unlike traditional ccache, it supports distributed caching to remote storage (Redis, S3, GCS, Memcached, etc.), enabling teams to share compilation caches. It's particularly optimized for Rust projects with easy Cargo integration. Supporting both local and remote caching, it enables fast builds in CI/CD environments.

Pros and Cons

Pros

  • Fast Compilation: Dramatically speeds up recompilation of the same code
  • Distributed Cache: Share compilation cache across the entire team
  • Multi-language Support: Supports C/C++, Rust, and other languages
  • Rich Storage Options: Various backends including Redis, S3, GCS, Memcached
  • CI/CD Optimization: Fast builds in continuous integration environments
  • Flexible Configuration: Can choose between local and remote caching

Cons

  • Initial Setup: Complex configuration for distributed environments
  • Storage Dependency: Remote caching requires storage services
  • Compatibility: Some compiler options have limitations
  • Debugging: Analyzing cache miss causes can be difficult

Key Links

Usage Examples

Basic Usage

# Install sccache
brew install sccache  # macOS
scoop install sccache  # Windows
cargo install sccache --locked  # From source

# Basic compilation command
sccache gcc -o program.o -c program.c

# Start and stop sccache server
sccache --start-server
sccache --stop-server

# Check cache statistics
sccache --show-stats

Rust Project Configuration

# ~/.cargo/config.toml
[build]
rustc-wrapper = "/path/to/sccache"

# Or set via environment variable
export RUSTC_WRAPPER=/path/to/sccache
cargo build

CMake Project Configuration

# CMakeLists.txt
find_program(SCCACHE sccache REQUIRED)
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE})

# Additional settings for MSVC
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded)
cmake_policy(SET CMP0141 NEW)

Configuration File Example

# ~/.config/sccache/config
[cache.disk]
dir = "/tmp/.cache/sccache"
size = 7516192768  # 7 GiB

[cache.redis]
endpoint = "redis://127.0.0.1:6379"
expiration = 3600
key_prefix = "/sccache"

[cache.s3]
bucket = "my-build-cache"
endpoint = "s3-us-east-1.amazonaws.com"
key_prefix = "sccache"

Distributed Cache Configuration

# client config
[dist]
scheduler_url = "https://cache-scheduler.example.com"
toolchain_cache_size = 5368709120

[dist.auth]
type = "token"
token = "your-auth-token"

# server config
[builder]
type = "overlay"
build_dir = "/tmp/build"
bwrap_path = "/usr/bin/bwrap"

Environment Variable Configuration

# Set cache size
export SCCACHE_CACHE_SIZE="10G"

# Enable preprocessor cache mode
export SCCACHE_DIRECT=true

# Set local cache to read-only
export SCCACHE_LOCAL_RW_MODE=READ_ONLY

# Enable debug logging
export SCCACHE_LOG=debug
export SCCACHE_ERROR_LOG=/tmp/sccache.log

Usage in GitHub Actions

# .github/workflows/build.yml
- name: Configure sccache
  uses: actions/github-script@v7
  with:
    script: |
      core.exportVariable('ACTIONS_RESULTS_URL', process.env.ACTIONS_RESULTS_URL || '');
      core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

- name: Build with cache
  run: |
    export RUSTC_WRAPPER=sccache
    cargo build --release
  env:
    SCCACHE_GHA_ENABLED: "on"