zccache 1.11.19

Local-first compiler cache for C/C++/Rust/Emscripten
Documentation
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
437
438
439
440
441
442
//! Clang/GCC parsing: -c, multi-file, -x header mode, header units, sticky-mode regressions.

use super::super::{parse_invocation, CompilerFamily, ParsedInvocation};
use super::args;
use crate::core::NormalizedPath;

#[test]
fn basic_cacheable_compilation() {
    let result = parse_invocation("clang++", &args(&["-c", "hello.cpp", "-o", "hello.o"]));
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("hello.cpp"));
            assert_eq!(c.output_file, NormalizedPath::new("hello.o"));
            assert_eq!(c.family, CompilerFamily::Clang);
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn assembler_sources_are_cacheable() {
    for source in ["ring.S", "plain.s"] {
        let output = source.replace('.', "_") + ".o";
        let result = parse_invocation("clang", &args(&["-c", source, "-o", output.as_str()]));
        match result {
            ParsedInvocation::Cacheable(c) => {
                assert_eq!(c.source_file, NormalizedPath::new(source));
                assert_eq!(c.output_file, NormalizedPath::new(output));
                assert_eq!(c.family, CompilerFamily::Clang);
            }
            other => panic!("expected cacheable assembler source, got: {other:?}"),
        }
    }
}

#[test]
fn no_c_flag_is_non_cacheable() {
    let result = parse_invocation("gcc", &args(&["hello.cpp", "-o", "hello"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn preprocessing_only_non_cacheable() {
    let result = parse_invocation("gcc", &args(&["-E", "hello.cpp"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn multi_file_split() {
    let result = parse_invocation("gcc", &args(&["-c", "a.cpp", "b.cpp"]));
    match result {
        ParsedInvocation::MultiFile {
            compilations,
            source_indices,
            ..
        } => {
            assert_eq!(compilations.len(), 2);
            assert_eq!(compilations[0].source_file, NormalizedPath::new("a.cpp"));
            assert_eq!(compilations[0].output_file, NormalizedPath::new("a.o"));
            assert_eq!(compilations[1].source_file, NormalizedPath::new("b.cpp"));
            assert_eq!(compilations[1].output_file, NormalizedPath::new("b.o"));
            assert_eq!(source_indices, vec![1, 2]);
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn multi_file_with_flags() {
    let result = parse_invocation(
        "g++",
        &args(&["-c", "-O2", "main.cpp", "-Wall", "util.cpp"]),
    );
    match result {
        ParsedInvocation::MultiFile {
            compilations,
            original_args,
            source_indices,
        } => {
            assert_eq!(compilations.len(), 2);
            assert_eq!(compilations[0].source_file, NormalizedPath::new("main.cpp"));
            assert_eq!(compilations[1].source_file, NormalizedPath::new("util.cpp"));
            // Flags are in original_args, not per-compilation
            assert!(original_args.contains(&"-O2".to_string()));
            assert!(original_args.contains(&"-Wall".to_string()));
            assert_eq!(source_indices, vec![2, 4]);
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn multi_file_mixed_extensions() {
    let result = parse_invocation("gcc", &args(&["-c", "file1.c", "file2.cpp"]));
    match result {
        ParsedInvocation::MultiFile { compilations, .. } => {
            assert_eq!(compilations.len(), 2);
            assert_eq!(compilations[0].source_file, NormalizedPath::new("file1.c"));
            assert_eq!(
                compilations[1].source_file,
                NormalizedPath::new("file2.cpp")
            );
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn stdin_non_cacheable() {
    let result = parse_invocation("gcc", &args(&["-c", "-"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn default_output_name() {
    let result = parse_invocation("gcc", &args(&["-c", "foo.cpp"]));
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.output_file, NormalizedPath::new("foo.o"));
        }
        _ => panic!("expected cacheable"),
    }
}

#[test]
fn original_args_preserved() {
    let input = args(&["-c", "hello.cpp", "-O2", "-std=c++17", "-DNDEBUG", "-Wall"]);
    let result = parse_invocation("clang++", &input);
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(*c.original_args, *input);
        }
        _ => panic!("expected cacheable"),
    }
}

#[test]
fn unknown_flags_preserved_in_original_args() {
    let input = args(&[
        "-c",
        "hello.cpp",
        "--deploy-dependencies",
        "--custom-flag=value",
        "-o",
        "hello.o",
    ]);
    let result = parse_invocation("clang++", &input);
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(*c.original_args, *input);
            assert_eq!(c.source_file, NormalizedPath::new("hello.cpp"));
            assert_eq!(c.output_file, NormalizedPath::new("hello.o"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn include_pch_flag_with_value() {
    let result = parse_invocation(
        "clang++",
        &args(&["-c", "foo.cpp", "-include-pch", "pch.h.pch", "-o", "foo.o"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            // PCH path is NOT treated as a source file
            assert_eq!(c.source_file, NormalizedPath::new("foo.cpp"));
            // Original args preserved
            assert!(c.original_args.contains(&"-include-pch".to_string()));
            assert!(c.original_args.contains(&"pch.h.pch".to_string()));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn pch_generation_cpp_header_is_cacheable() {
    // `clang -x c++-header -c pch.h -o pch.h.pch` should be cacheable
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c++-header", "-c", "pch.h", "-o", "pch.h.pch"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("pch.h"));
            assert_eq!(c.output_file, NormalizedPath::new("pch.h.pch"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn pch_generation_c_header_is_cacheable() {
    // `gcc -x c-header -c stdafx.h -o stdafx.h.gch` should be cacheable
    let result = parse_invocation(
        "gcc",
        &args(&["-x", "c-header", "-c", "stdafx.h", "-o", "stdafx.h.gch"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("stdafx.h"));
            assert_eq!(c.output_file, NormalizedPath::new("stdafx.h.gch"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn pch_generation_without_c_flag_is_cacheable() {
    // Meson invokes PCH generation without -c:
    // `clang++ -x c++-header header.h -o header.pch`
    // `-x c++-header` implies compilation, so -c is redundant.
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c++-header", "FastLED.h", "-o", "FastLED.h.pch"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("FastLED.h"));
            assert_eq!(c.output_file, NormalizedPath::new("FastLED.h.pch"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn pch_generation_c_header_without_c_flag_is_cacheable() {
    let result = parse_invocation(
        "gcc",
        &args(&["-x", "c-header", "stdafx.h", "-o", "stdafx.h.gch"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("stdafx.h"));
            assert_eq!(c.output_file, NormalizedPath::new("stdafx.h.gch"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn pch_generation_with_meson_flags_is_cacheable() {
    // Full Meson-style PCH invocation with extra flags
    let result = parse_invocation(
        "ctc-clang++",
        &args(&[
            "-x",
            "c++-header",
            "FastLED.h",
            "-o",
            "FastLED.h.pch",
            "-MD",
            "-MF",
            "FastLED.h.pch.d",
            "-fPIC",
            "-Iinclude",
            "-Werror=invalid-pch",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("FastLED.h"));
            assert_eq!(c.output_file, NormalizedPath::new("FastLED.h.pch"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn header_without_x_flag_is_not_source() {
    // Without `-x c++-header`, a .h file should NOT be recognized as a source
    let result = parse_invocation("clang++", &args(&["-c", "pch.h"]));
    assert!(
        matches!(result, ParsedInvocation::NonCacheable { .. }),
        "bare .h without -x header mode should be non-cacheable"
    );
}

#[test]
fn x_flag_reset_disables_header_mode() {
    // `-x c++-header pch.h -x c++ main.cpp -c -o main.o`
    // After `-x c++`, header_mode resets — main.cpp is a normal source,
    // pch.h was collected as header-mode source → multi-file.
    let result = parse_invocation(
        "clang++",
        &args(&[
            "-x",
            "c++-header",
            "pch.h",
            "-x",
            "c++",
            "main.cpp",
            "-c",
            "-o",
            "main.o",
        ]),
    );
    match result {
        ParsedInvocation::MultiFile { compilations, .. } => {
            assert_eq!(compilations.len(), 2);
            assert_eq!(compilations[0].source_file, NormalizedPath::new("pch.h"));
            assert_eq!(compilations[1].source_file, NormalizedPath::new("main.cpp"));
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn x_cpp_after_header_is_normal_compilation() {
    // `-x c++-header -x c++ main.cpp -c -o main.o`
    // No header file between the two -x flags. The second -x c++ resets
    // header_mode, so main.cpp is a normal compilation.
    let result = parse_invocation(
        "clang++",
        &args(&[
            "-x",
            "c++-header",
            "-x",
            "c++",
            "main.cpp",
            "-c",
            "-o",
            "main.o",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("main.cpp"));
            assert_eq!(c.output_file, NormalizedPath::new("main.o"));
        }
        other => panic!("expected Cacheable, got: {other:?}"),
    }
}

// ─── Regression tests: sticky header_mode bug ─────────────────────────

#[test]
fn sticky_header_mode_cpp_not_spuriously_pch() {
    // BUG: old code set header_mode=true on `-x c++-header` but never
    // reset it on `-x c++`, so main.cpp was treated as a header file
    // needing PCH generation. After the fix, `-x c++` resets header_mode,
    // and main.cpp is a normal source — not a PCH candidate.
    let result = parse_invocation(
        "clang++",
        &args(&[
            "-x",
            "c++-header",
            "pch.h",
            "-o",
            "pch.h.pch",
            "-x",
            "c++",
            "-c",
            "main.cpp",
            "-o",
            "main.o",
        ]),
    );
    // main.cpp must be recognized as a normal source via its extension,
    // NOT via header_mode. With the old bug, header_mode stayed true and
    // both pch.h and main.cpp were header-mode sources.
    match &result {
        ParsedInvocation::MultiFile { compilations, .. } => {
            assert_eq!(compilations.len(), 2);
            // pch.h picked up in header_mode
            assert_eq!(compilations[0].source_file, NormalizedPath::new("pch.h"));
            // main.cpp picked up by extension after reset
            assert_eq!(compilations[1].source_file, NormalizedPath::new("main.cpp"));
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn sticky_header_mode_non_source_not_captured_after_reset() {
    // BUG: with sticky header_mode, a positional arg like "README.txt"
    // after `-x c++` would be treated as a source file because
    // `is_source_file(arg) || header_mode` was true. After fix,
    // header_mode is false after `-x c++`, so non-source extensions
    // are correctly ignored.
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c++-header", "pch.h", "-x", "c++", "-c", "main.cpp"]),
    );
    match &result {
        ParsedInvocation::MultiFile { compilations, .. } => {
            assert_eq!(compilations.len(), 2);
            assert_eq!(compilations[0].source_file, NormalizedPath::new("pch.h"));
            assert_eq!(compilations[1].source_file, NormalizedPath::new("main.cpp"));
        }
        other => panic!("expected MultiFile, got: {other:?}"),
    }
}

#[test]
fn sticky_header_mode_no_c_flag_after_reset_is_non_cacheable() {
    // BUG: with sticky header_mode, the `!has_c_flag && !header_mode`
    // check at the end would pass (header_mode was still true),
    // making a link invocation appear cacheable. After fix, `-x c++`
    // resets header_mode so without -c it's correctly non-cacheable.
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c++-header", "-x", "c++", "main.cpp", "-o", "main"]),
    );
    assert!(
        matches!(result, ParsedInvocation::NonCacheable { .. }),
        "after -x c++ reset, no -c should be non-cacheable, got: {result:?}"
    );
}

#[test]
fn header_unit_c_is_cacheable() {
    // `-x c-header-unit` activates header-unit mode (C++20 module support).
    // Header-unit mode implies compilation, producing .pcm output.
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c-header-unit", "foo.h", "-o", "foo.pcm"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("foo.h"));
            assert_eq!(c.output_file, NormalizedPath::new("foo.pcm"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn header_unit_cpp_is_cacheable() {
    // `-x c++-header-unit` activates header-unit mode (C++20 module support).
    let result = parse_invocation(
        "clang++",
        &args(&["-x", "c++-header-unit", "foo.h", "-o", "foo.pcm"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.source_file, NormalizedPath::new("foo.h"));
            assert_eq!(c.output_file, NormalizedPath::new("foo.pcm"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}