|
| 1 | +# Persist ccache compiler cache (persist-ccache) |
| 2 | + |
| 3 | +Retain the ccache compiler cache directory across container rebuilds to speed up C/C++ compilation by reusing cached compilation results. |
| 4 | + |
| 5 | +## What this feature does |
| 6 | + |
| 7 | +This feature persists the ccache cache directory across container rebuilds: |
| 8 | + |
| 9 | +- `~/.cache/ccache/` – ccache compiler cache |
| 10 | + |
| 11 | +The cache is mounted as a persistent Docker volume that is shared across all users and workspaces, allowing cache reuse across container rebuilds. |
| 12 | + |
| 13 | +## How it works |
| 14 | + |
| 15 | +### Build phase (as root) |
| 16 | + |
| 17 | +The `install.sh` script creates a Docker volume and mount point: |
| 18 | +- `/.persist-ccache` – Volume for ccache cache |
| 19 | + |
| 20 | +The directory is created with permissions `777` so all users can access it. |
| 21 | + |
| 22 | +### User initialization phase (in user context) |
| 23 | + |
| 24 | +The `persist-ccache-init.sh` script (runs via `postCreateCommand`) performs: |
| 25 | + |
| 26 | +1. **Backup of existing data**: If `~/.cache/ccache` exists as a regular directory, it is renamed to `.bak` (preserving any existing cache). |
| 27 | +2. **Symlink creation**: Creates a symlink from `~/.cache/ccache` to the persistent volume. |
| 28 | +3. **Environment configuration**: Sets `CCACHE_DIR` and `CCACHE_MAXSIZE` environment variables. |
| 29 | +4. **Shell integration**: Adds environment variables to `~/.bashrc` and `~/.zshrc` if they exist. |
| 30 | + |
| 31 | +This approach ensures: |
| 32 | +- **No data loss**: Existing caches are backed up before being replaced |
| 33 | +- **Automatic recovery**: Symlinks persist across rebuilds; no repeated setup needed |
| 34 | +- **Multi-user support**: All users can access the shared volume |
| 35 | +- **Proper configuration**: Environment variables are set in the container so ccache knows where to store/find cache |
| 36 | + |
| 37 | +## Example Usage |
| 38 | + |
| 39 | +```json |
| 40 | +"features": { |
| 41 | + "ghcr.io/ckagerer/devcontainer-features/persist-ccache:1": { |
| 42 | + "cache_size": "5G" |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Options |
| 48 | + |
| 49 | +### `cache_size` (string, default: `"5G"`) |
| 50 | + |
| 51 | +Sets the maximum size of the ccache cache. Valid formats include: |
| 52 | +- Decimal: `kB`, `MB`, `GB`, `TB` (e.g., `10GB`) |
| 53 | +- Binary: `KiB`, `MiB`, `GiB`, `TiB` (e.g., `5GiB`) |
| 54 | + |
| 55 | +Examples: |
| 56 | +- `"5G"` (default) – 5 gigabytes |
| 57 | +- `"10GB"` – 10 gigabytes |
| 58 | +- `"100G"` – 100 gigabytes |
| 59 | +- `"50GiB"` – 50 gibibytes (binary) |
| 60 | + |
| 61 | +### `keep_going` (boolean, default: `false`) |
| 62 | + |
| 63 | +If set to `true`, the installer will not fail on errors and will attempt to continue setup. Useful for troubleshooting or environments with unusual configurations. |
| 64 | + |
| 65 | +## Performance benefits |
| 66 | + |
| 67 | +Typical ccache initialization includes: |
| 68 | + |
| 69 | +1. Creating cache directory structure |
| 70 | +2. Configuring cache limits |
| 71 | +3. Initializing cache database |
| 72 | + |
| 73 | +With persistent cache volumes, subsequent container rebuilds skip setup entirely and can immediately reuse cached compilation results. Depending on your project, this can reduce build times by 50-90% on subsequent builds. |
| 74 | + |
| 75 | +## Requirements |
| 76 | + |
| 77 | +- C/C++ build tools using ccache (optional; feature sets up infrastructure even if ccache is not yet installed) |
| 78 | +- Write permissions to `~/.cache/` (normally available in user context) |
| 79 | +- Docker volumes support (standard in dev container environments) |
| 80 | + |
| 81 | +## Environment Variables |
| 82 | + |
| 83 | +The feature automatically sets: |
| 84 | + |
| 85 | +- `CCACHE_DIR=~/.cache/ccache` – Location of the cache |
| 86 | +- `CCACHE_MAXSIZE=<cache_size>` – Maximum cache size |
| 87 | + |
| 88 | +These are set in user shell RC files (`.bashrc`, `.zshrc`) for persistence across shell sessions. |
| 89 | + |
| 90 | +## Troubleshooting |
| 91 | + |
| 92 | +### Symlinks not created |
| 93 | + |
| 94 | +Check that the `persist-ccache-init.sh` script ran without errors: |
| 95 | + |
| 96 | +```bash |
| 97 | +# View logs of the postCreateCommand |
| 98 | +# This is typically shown in the dev container build output |
| 99 | +``` |
| 100 | + |
| 101 | +### Cache still not persisting |
| 102 | + |
| 103 | +Verify the volume mount is active: |
| 104 | + |
| 105 | +```bash |
| 106 | +# Inside the container |
| 107 | +mount | grep persist-ccache |
| 108 | +# Should show: |
| 109 | +# devcontainer-persist-ccache on /.persist-ccache |
| 110 | +``` |
| 111 | + |
| 112 | +Verify environment variables are set: |
| 113 | + |
| 114 | +```bash |
| 115 | +echo $CCACHE_DIR |
| 116 | +echo $CCACHE_MAXSIZE |
| 117 | +``` |
| 118 | + |
| 119 | +### Clearing old cache data |
| 120 | + |
| 121 | +If you need to reset cached data: |
| 122 | + |
| 123 | +```bash |
| 124 | +# Inside the container (as user) |
| 125 | +rm ~/.cache/ccache.bak # Remove backups if any |
| 126 | +rm -rf ~/.cache/ccache # This will remove symlink too |
| 127 | +``` |
| 128 | + |
| 129 | +Then rebuild the dev container to recreate fresh symlinks. |
| 130 | + |
| 131 | +### Checking cache statistics |
| 132 | + |
| 133 | +```bash |
| 134 | +# Show cache statistics and current configuration |
| 135 | +ccache -s |
| 136 | + |
| 137 | +# Show compression statistics |
| 138 | +ccache -x |
| 139 | + |
| 140 | +# Clear the cache if needed |
| 141 | +ccache -C |
| 142 | +``` |
| 143 | + |
| 144 | +## Related features |
| 145 | + |
| 146 | +- [persist-shell-history](../persist-shell-history/) – Persist bash/zsh history across rebuilds |
| 147 | +- [persist-pre-commit-cache](../persist-pre-commit-cache/) – Persist pre-commit hook cache across rebuilds |
| 148 | +- [clang](../clang/) – Install clang/LLVM toolchain |
0 commit comments