Sccache
Sccache (Shared Compilation Cache) is a ccache-like tool written in Rust that acts as a compiler wrapper to accelerate build times by caching compilation results. It supports C/C++, Rust, and CUDA, and can utilize various local and cloud storage backends like S3, Google Cloud Storage, Redis, and Memcached. The PyPI package for sccache (version 0.14.0) primarily provides access to this binary tool for Python environments. It is actively maintained by Mozilla and a community of contributors, with regular releases providing new features and bug fixes.
Warnings
- gotcha The `sccache` PyPI package primarily provides a pre-compiled Rust binary. It does not expose a direct Python API for its core caching functionality. Usage is typically via environment variables or by configuring build systems to use the `sccache` executable as a compiler wrapper.
- gotcha By default, the `sccache` server will terminate after 10 minutes of inactivity. This can lead to unexpected server shutdowns during long pauses in development or CI.
- gotcha If the `sccache` server fails to communicate (e.g., due to a crash or network issue), `sccache` will, by default, fail the build.
- gotcha If you suspect the cache contains broken build artifacts, subsequent builds might repeatedly use corrupted data.
- deprecated Older versions of the `mozilla-actions/sccache-action` GitHub Action (and other third-party actions/products using the GitHub Actions cache service) might be impacted by the decommissioning of the old cache service, potentially leading to build failures or lack of caching.
Install
-
pip install sccache -
cargo install sccache -
brew install sccache -
scoop install sccache
Quickstart
import os
import subprocess
# Ensure sccache is in PATH or set SCCACHE env var if installed elsewhere
# For simplicity, assuming 'sccache' is callable from PATH after 'pip install sccache'
# Start the sccache server in the background (optional, but recommended for efficiency)
# The server automatically stops after 10 minutes of inactivity by default.
subprocess.run(['sccache', '--start-server'])
# Example usage: Compile a Rust project with sccache
# This environment variable tells Cargo/rustc to use sccache as a wrapper
env = os.environ.copy()
env['RUSTC_WRAPPER'] = 'sccache'
print('Building Rust project with sccache...')
try:
# Simulate a Rust build command
subprocess.run(['cargo', 'build', '--release'], env=env, check=True)
print('First build (warming cache) completed.')
# Run again to demonstrate caching effect
subprocess.run(['cargo', 'clean'], check=True)
print('Cache cleaned, running second build...')
subprocess.run(['cargo', 'build', '--release'], env=env, check=True)
print('Second build (should be faster due to cache) completed.')
# Show sccache statistics
subprocess.run(['sccache', '--show-stats'], check=True)
finally:
# Stop the sccache server
subprocess.run(['sccache', '--stop-server'])
print('Sccache server stopped.')