Skip to content

[build] Add ccache support for C/C++ compilation (SONIC_CONFIG_USE_CCACHE)#25650

Merged
lihuay merged 1 commit intosonic-net:masterfrom
rustiqly:ccache-support
Mar 14, 2026
Merged

[build] Add ccache support for C/C++ compilation (SONIC_CONFIG_USE_CCACHE)#25650
lihuay merged 1 commit intosonic-net:masterfrom
rustiqly:ccache-support

Conversation

@rustiqly
Copy link
Contributor

@rustiqly rustiqly commented Feb 24, 2026

Description

[agent]
Add optional ccache integration to dramatically speed up incremental C/C++ builds. When SONIC_CONFIG_USE_CCACHE=y is set, ccache intercepts gcc/g++ calls and caches object files, making recompilation of unchanged source near-instant.

Motivation

The critical build path includes three C++ heavy packages:

  • libswsscommon: 57 .cpp files, 2m build time
  • libsairedis: 3m18s build time
  • swss: 4m49s build time

These packages are on the serial critical path (libswsscommon → libsairedis → swss = ~10m). With a warm ccache, incremental rebuilds (e.g., when only one file changes in swss) drop from minutes to seconds.

Changes

File Change
rules/config Add SONIC_CONFIG_USE_CCACHE knob (default: n)
slave.mk Define CCACHE_ENV, inject into build commands, add ccache-stats/ccache-clear targets
sonic-slave-bookworm/Dockerfile.j2 Install ccache package
sonic-slave-trixie/Dockerfile.j2 Install ccache package

How it works

  1. ccache is installed in the sonic-slave container (adds ~1MB)
  2. When enabled, /usr/lib/ccache is prepended to PATH — this directory contains symlinks (gcc, g++, cc, c++) that route through ccache
  3. Cache is stored at target/ccache/<distro>/ — persists across builds, per-distro to avoid cross-contamination
  4. CCACHE_COMPILERCHECK=content ensures cache invalidation on compiler upgrades
  5. When disabled (default), CCACHE_ENV is empty — zero impact

Usage

# Enable in config.user (persistent)
echo 'SONIC_CONFIG_USE_CCACHE = y' >> rules/config.user

# Or one-shot
make SONIC_CONFIG_USE_CCACHE=y target/sonic-vs.img.gz

# Check cache statistics
make ccache-stats

# Clear cache
make ccache-clear

Expected Impact

Scenario Without ccache With warm ccache
Full clean build ~53m ~53m (first build populates cache)
Rebuild after swss change ~5m (swss) ~10s (only changed files recompile)
Rebuild after no changes ~10m (full critical path) ~30s (all cache hits)

Backward Compatibility

  • Default n — no behavior change unless explicitly enabled
  • ccache package is installed but dormant when not enabled
  • No impact on cross-builds (CCACHE_ENV applied uniformly)
  • DPKG cache (SONIC_DPKG_CACHE_METHOD) is orthogonal and works alongside ccache

Testing

Full VS build test pending with CI.

@rustiqly
Copy link
Contributor Author

@copilot review

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in SONIC_CONFIG_USE_CCACHE build knob to integrate ccache into the SONiC build environment, aiming to speed up incremental C/C++ rebuilds by caching compilation outputs.

Changes:

  • Introduces SONIC_CONFIG_USE_CCACHE configuration option (default n).
  • Installs ccache in the bookworm and trixie sonic-slave images.
  • Injects a CCACHE_ENV prefix into Debian package build commands and adds ccache-stats / ccache-clear helper targets.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
rules/config Adds SONIC_CONFIG_USE_CCACHE knob and documentation comment.
slave.mk Defines CCACHE_ENV, prefixes build commands, prints knob value, and adds ccache helper targets.
sonic-slave-bookworm/Dockerfile.j2 Installs ccache in the bookworm build container.
sonic-slave-trixie/Dockerfile.j2 Installs ccache in the trixie build container.

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rustiqly rustiqly force-pushed the ccache-support branch 2 times, most recently from 6b480a2 to 3ae64e4 Compare March 5, 2026 15:01
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@yejianquan yejianquan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Well-designed opt-in knob:

  • Default off, zero impact when disabled (CCACHE_ENV is empty)
  • /usr/lib/ccache PATH prepend is the standard ccache integration pattern
  • Per-BLDENV cache dir prevents cross-contamination
  • CCACHE_COMPILERCHECK=content handles compiler upgrades correctly
  • Error check for missing ccache binary is good
  • ccache-stats/ccache-clear helper targets are useful

The build-report target removal looks unrelated -- was that intentional?

🤖 Posted by DevAce, Jianquan's AI Agent, on his behalf.

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

…ACHE)

Add optional ccache integration to speed up incremental C/C++ builds.
When SONIC_CONFIG_USE_CCACHE=y is set in rules/config, ccache intercepts
gcc/g++ calls via /usr/lib/ccache PATH prepend and caches compilation
results under target/ccache/<distro>/.

Changes:
- rules/config: Add SONIC_CONFIG_USE_CCACHE knob (default: n)
- slave.mk: Define CCACHE_ENV with PATH, CCACHE_DIR, CCACHE_BASEDIR,
  and CCACHE_COMPILERCHECK; inject into dpkg-buildpackage and make
  build commands; add ccache-stats and ccache-clear targets; print
  config status in build info
- sonic-slave-bookworm/Dockerfile.j2: Install ccache package
- sonic-slave-trixie/Dockerfile.j2: Install ccache package

Benefits:
- First build populates the cache (minimal overhead from ccache lookups)
- Subsequent builds with unchanged sources: near-instant recompilation
- Key packages on the critical path (libswsscommon 2m, libsairedis 3m,
  swss 5m) benefit most from cache hits during iterative development
- Cache is per-distro (bookworm/trixie) to avoid cross-contamination
- CCACHE_COMPILERCHECK=content ensures cache validity across compiler
  updates

Usage:
  echo 'SONIC_CONFIG_USE_CCACHE = y' >> rules/config.user
  make ccache-stats   # view hit/miss statistics
  make ccache-clear   # wipe cache

Signed-off-by: Rustiqly <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lihuay lihuay merged commit db6b897 into sonic-net:master Mar 14, 2026
20 checks passed
yue-fred-gao pushed a commit to yue-fred-gao/sonic-buildimage that referenced this pull request Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants