ccgo 3.7.2

A high-performance C++ cross-platform build CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# Build Caching with ccache/sccache

## Overview

CCGO supports **compiler caching** through ccache and sccache to dramatically speed up C++ compilation by **30-50%** (or more) by caching compilation artifacts.

## Benefits

- **Faster Rebuilds** - Subsequent builds reuse cached compilation results
-**Automatic Detection** - Auto-detects and uses available cache tools
-**Zero Configuration** - Works out of the box when cache tool is installed
-**Shared Cache** - Cache is shared across all CCGO projects
-**CI/CD Friendly** - Speeds up continuous integration builds significantly

## Quick Start

### Install a Cache Tool

**Option 1: sccache (Recommended)**
```bash
# macOS
brew install sccache

# Linux
cargo install sccache

# Arch Linux
sudo pacman -S sccache

# Debian/Ubuntu
sudo apt install sccache
```

**Option 2: ccache**
```bash
# macOS
brew install ccache

# Linux
sudo apt install ccache  # Debian/Ubuntu
sudo yum install ccache  # CentOS/RHEL
sudo pacman -S ccache    # Arch Linux
```

### Build with Caching

Cache is **enabled by default** with `--cache auto`:

```bash
# Auto-detect and use available cache (default)
ccgo build linux

# Explicitly specify cache tool
ccgo build linux --cache sccache
ccgo build linux --cache ccache

# Disable caching
ccgo build linux --cache none
```

## Cache Tools Comparison

| Feature | ccache | sccache |
|---------|--------|---------|
| Language | C | Rust |
| Speed | Fast | Faster |
| Cloud Storage | No | Yes (S3, Redis, Memcached) |
| Distribution | Stable, Mature | Modern, Actively Developed |
| Platform Support | All platforms | All platforms |
| Memory Usage | Low | Medium |
| Recommendation | Good choice | Better choice |

**CCGO Preference Order**: sccache > ccache > none

## Usage

### Command-Line Options

```bash
# Auto-detect (default) - tries sccache first, then ccache
ccgo build <platform> --cache auto

# Force specific cache tool
ccgo build <platform> --cache sccache
ccgo build <platform> --cache ccache

# Disable caching
ccgo build <platform> --cache none
ccgo build <platform> --cache off
ccgo build <platform> --cache disabled
```

### Build Output

When caching is enabled, you'll see:

```bash
$ ccgo build linux

Building CCGO Library for linux...
   🚀 Using sccache for compilation caching

Configuring CMake...
-- Build files generated successfully
```

### Cache Statistics

**ccache**:
```bash
# Show cache statistics
ccache -s

# Zero statistics (reset counters)
ccache -z

# Clear cache
ccache -C
```

**sccache**:
```bash
# Show cache statistics
sccache --show-stats

# Zero statistics (reset counters)
sccache --zero-stats

# Stop server (clears in-memory cache)
sccache --stop-server
```

## Performance Impact

### First Build (Cold Cache)

```
Time: 100% (baseline)
- No cached artifacts
- Full compilation required
```

### Second Build (Warm Cache)

```
Time: 20-50% of first build (50-80% faster)
- Cached artifacts reused
- Only changed files recompiled
```

### Example Metrics

| Project Size | First Build | Cached Build | Speedup |
|-------------|-------------|--------------|---------|
| Small (5-10 files) | 10s | 3s | 3.3x |
| Medium (50-100 files) | 60s | 15s | 4x |
| Large (500+ files) | 300s | 60s | 5x |

**Note**: Speedup increases with project size and build frequency.

## How It Works

### CMake Integration

CCGO automatically configures CMake with compiler launcher variables:

```cmake
# Injected by CCGO when cache is enabled
CMAKE_C_COMPILER_LAUNCHER=/path/to/sccache
CMAKE_CXX_COMPILER_LAUNCHER=/path/to/sccache
```

This wraps your C/C++ compiler (gcc, clang, msvc) with the cache tool.

### Cache Key

Compilation artifacts are cached based on:
- Source file content
- Compiler flags
- Header dependencies
- Preprocessor definitions
- Compiler version

If any of these change, the cache is invalidated and recompilation occurs.

### Cache Location

**ccache**:
- Default: `~/.ccache/` (Linux/macOS), `%LOCALAPPDATA%\ccache` (Windows)
- Configure: `export CCACHE_DIR=/custom/path`

**sccache**:
- Default: `~/.cache/sccache/` (Linux), `~/Library/Caches/Mozilla.sccache/` (macOS)
- Configure: `export SCCACHE_DIR=/custom/path`

## Configuration

### Environment Variables

**ccache**:
```bash
# Set cache directory
export CCACHE_DIR=/path/to/cache

# Set max cache size
export CCACHE_MAXSIZE=5G

# Enable compression
export CCACHE_COMPRESS=true

# Set compression level (1-9)
export CCACHE_COMPRESSLEVEL=6
```

**sccache**:
```bash
# Set cache directory
export SCCACHE_DIR=/path/to/cache

# Set max cache size
export SCCACHE_CACHE_SIZE="5G"

# Use Redis for distributed caching
export SCCACHE_REDIS=redis://localhost:6379

# Use AWS S3 for distributed caching
export SCCACHE_BUCKET=my-sccache-bucket
export SCCACHE_REGION=us-west-2
```

### CI/CD Configuration

**GitHub Actions**:
```yaml
name: Build with Cache

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      # Install sccache
      - name: Install sccache
        run: |
          wget https://github.com/mozilla/sccache/releases/download/v0.5.4/sccache-v0.5.4-x86_64-unknown-linux-musl.tar.gz
          tar xzf sccache-v0.5.4-x86_64-unknown-linux-musl.tar.gz
          sudo mv sccache-v0.5.4-x86_64-unknown-linux-musl/sccache /usr/local/bin/

      # Cache sccache directory
      - name: Cache sccache
        uses: actions/cache@v3
        with:
          path: ~/.cache/sccache
          key: ${{ runner.os }}-sccache-${{ hashFiles('**/CCGO.toml') }}
          restore-keys: |
            ${{ runner.os }}-sccache-

      # Build with caching
      - name: Build
        run: ccgo build linux

      # Show cache statistics
      - name: Show cache stats
        run: sccache --show-stats
```

**GitLab CI**:
```yaml
build:
  image: ubuntu:latest
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
      - .cache/sccache
  before_script:
    - apt-get update && apt-get install -y sccache
    - export SCCACHE_DIR=$PWD/.cache/sccache
  script:
    - ccgo build linux
    - sccache --show-stats
```

## Troubleshooting

### Cache Not Working

**Check if cache tool is installed**:
```bash
which sccache
which ccache
```

**Check if cache is detected**:
```bash
ccgo build linux --verbose
# Should show "Using sccache for compilation caching"
```

**Verify CMake configuration**:
```bash
# Check CMake build log for COMPILER_LAUNCHER
grep COMPILER_LAUNCHER cmake_build/release/linux/CMakeCache.txt
```

### Cache Miss Rate High

**Possible causes**:
- Different compiler flags between builds
- Timestamp-based dependencies (use hash-based)
- Header files changing frequently
- Cache size too small (increase with `CCACHE_MAXSIZE` or `SCCACHE_CACHE_SIZE`)

**Solutions**:
```bash
# Increase cache size
export CCACHE_MAXSIZE=10G
export SCCACHE_CACHE_SIZE="10G"

# Enable compression to fit more in cache
export CCACHE_COMPRESS=true
```

### Permission Errors

**Fix cache directory permissions**:
```bash
# ccache
chmod -R u+w ~/.ccache

# sccache
chmod -R u+w ~/.cache/sccache
```

### Cache Corruption

**Clear and rebuild cache**:
```bash
# ccache
ccache -C  # Clear cache
ccgo build linux

# sccache
sccache --stop-server  # Stop server (clears cache)
rm -rf ~/.cache/sccache  # Remove cache directory
ccgo build linux
```

## Best Practices

### DO

✅ **Use sccache** - Faster and more feature-rich than ccache
✅ **Enable in CI/CD** - Speeds up pipeline builds significantly
✅ **Monitor cache size** - Set appropriate limits to avoid disk space issues
✅ **Share cache** - Use distributed cache (Redis/S3) for team builds
✅ **Keep cache warm** - Regular builds maintain cache effectiveness

### DON'T

❌ **Don't mix debug/release** - They have different cache keys
❌ **Don't commit cache** - Cache directory should be in `.gitignore`
❌ **Don't use tiny cache** - Set at least 5GB for medium projects
❌ **Don't disable in dev** - Caching speeds up development builds

## Advanced Usage

### Distributed Caching

**sccache with Redis**:
```bash
# Start Redis server
docker run -d -p 6379:6379 redis

# Configure sccache
export SCCACHE_REDIS=redis://localhost:6379

# Build (cache shared across team)
ccgo build linux
```

**sccache with AWS S3**:
```bash
# Configure AWS credentials
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...

# Configure S3 bucket
export SCCACHE_BUCKET=my-team-cache
export SCCACHE_REGION=us-west-2

# Build (cache shared in S3)
ccgo build linux
```

### Custom Cache Configuration

Create `.ccgo/cache_config.toml`:
```toml
[cache]
# Auto, ccache, sccache, or none
tool = "auto"

# Maximum cache size
max_size = "10G"

# Enable compression
compress = true

# Distributed cache URL (sccache only)
redis_url = "redis://localhost:6379"
```

**Note**: This feature is planned for a future release.

## See Also

- [Build System]features/build-system.md - General build system overview
- [Incremental Builds]incremental-builds.md
- [CMake Integration]reference/cmake.md

## Changelog

### v3.0.11 (2026-01-21)

- ✅ Implemented ccache/sccache integration
- ✅ Auto-detection of available cache tools
- ✅ Command-line `--cache` option
- ✅ Automatic CMake compiler launcher configuration
- ✅ Support for all platforms (Linux, macOS, Windows, iOS, Android, OHOS)

---

*Compiler caching can reduce build times by 30-50% or more, making iterative development much faster.*