zccache 1.11.22

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
//! Rustc invocation parsing: crate types, --emit, --out-dir, proc-macro/bin output naming.

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

// ─── Rustc detection tests ────────────────────────────────────────────

#[test]
fn detect_rustc_family() {
    assert_eq!(detect_family("rustc"), CompilerFamily::Rustc);
    assert_eq!(detect_family("/usr/bin/rustc"), CompilerFamily::Rustc);
    assert_eq!(detect_family("rustc.exe"), CompilerFamily::Rustc);
    assert_eq!(
        detect_family("C:\\rustup\\rustc.exe"),
        CompilerFamily::Rustc
    );
}

#[test]
fn rustc_no_depfile_support() {
    // Rustc uses --emit=dep-info, not -MD -MF
    assert!(!CompilerFamily::Rustc.supports_depfile());
}

#[test]
fn rustc_no_pch_extension() {
    assert_eq!(CompilerFamily::Rustc.pch_extension(), None);
}

// Issue #517: the system-include discovery probe spawns the compiler with
// C/C++ preprocessor flags (`-v -E -x c++ NUL`). Doing that for rustc on
// the cold path adds ~30-50 ms per first-after-clear compile while
// returning no useful data — rust has no concept of system includes.
#[test]
fn needs_system_include_discovery_truth_table() {
    assert!(CompilerFamily::Gcc.needs_system_include_discovery());
    assert!(CompilerFamily::Clang.needs_system_include_discovery());
    assert!(CompilerFamily::Msvc.needs_system_include_discovery());
    assert!(!CompilerFamily::Rustc.needs_system_include_discovery());
    assert!(!CompilerFamily::Rustfmt.needs_system_include_discovery());
}

// ─── Rustc cacheability tests ─────────────────────────────────────────

#[test]
fn rustc_lib_crate_is_cacheable() {
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--edition",
            "2021",
            "--crate-type",
            "lib",
            "--emit=dep-info,metadata,link",
            "-C",
            "opt-level=2",
            "src/lib.rs",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.family, CompilerFamily::Rustc);
            assert_eq!(c.source_file, NormalizedPath::new("src/lib.rs"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_rlib_crate_is_cacheable() {
    let result = parse_invocation("rustc", &args(&["--crate-type", "rlib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_staticlib_crate_is_cacheable() {
    let result = parse_invocation("rustc", &args(&["--crate-type", "staticlib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_bin_crate_is_cacheable() {
    // bin became cacheable in iter7 alongside a touch_mtime change
    // so cargo's fingerprint doesn't invalidate downstream when a
    // hit materializes the binary.
    let result = parse_invocation("rustc", &args(&["--crate-type", "bin", "src/main.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_bin_primary_output_uses_executable_extension() {
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--crate-name",
            "build_script_build",
            "--crate-type",
            "bin",
            "--out-dir",
            "/tmp/build/foo-abc",
            "-C",
            "extra-filename=-abc",
            "/path/to/build.rs",
        ]),
    );
    let cc = match result {
        ParsedInvocation::Cacheable(c) => c,
        other => panic!("expected cacheable, got: {other:?}"),
    };
    let out = cc.output_file.to_string_lossy();
    if cfg!(target_os = "windows") {
        assert!(
            out.ends_with("build_script_build-abc.exe"),
            "expected bin .exe, got {out}"
        );
    } else {
        assert!(
            out.ends_with("build_script_build-abc"),
            "expected bin executable, got {out}"
        );
        assert!(!out.ends_with(".rlib"), "bin must not get .rlib, got {out}");
    }
}

#[test]
fn rustc_dylib_is_non_cacheable() {
    let result = parse_invocation("rustc", &args(&["--crate-type", "dylib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn rustc_proc_macro_is_cacheable() {
    // Proc-macros are host-side dylibs whose output is deterministic for
    // a given source + dep set + rustc — caching them is the same
    // safety contract as any other rustc invocation. Targets the
    // 18× proc-macro non-cacheables on the warm-rebuild scenario.
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type", "proc-macro", "src/lib.rs"]),
    );
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_proc_macro_primary_output_uses_dylib_extension() {
    // Without this, the daemon's `collect_rustc_output_files` would
    // stat a non-existent `.rlib` path post-compile, return an empty
    // outputs vec, and take the early-return branch that skips
    // `dep_graph.update()` — leaving the context Cold forever and
    // causing every warm rebuild to recompile the proc-macro
    // (regression observed in the iter4 OODA pass).
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--crate-name",
            "serde_derive",
            "--crate-type",
            "proc-macro",
            "--out-dir",
            "/tmp/deps",
            "-C",
            "extra-filename=-abc123",
            "/path/to/src/lib.rs",
        ]),
    );
    let cc = match result {
        ParsedInvocation::Cacheable(c) => c,
        other => panic!("expected cacheable, got: {other:?}"),
    };
    let out = cc.output_file.to_string_lossy();
    if cfg!(target_os = "windows") {
        assert!(
            out.ends_with("serde_derive-abc123.dll"),
            "expected proc-macro .dll, got {out}"
        );
    } else if cfg!(target_os = "macos") {
        assert!(
            out.ends_with("libserde_derive-abc123.dylib"),
            "expected proc-macro .dylib, got {out}"
        );
    } else {
        assert!(
            out.ends_with("libserde_derive-abc123.so"),
            "expected proc-macro .so, got {out}"
        );
    }
}

#[test]
fn rustc_cdylib_is_non_cacheable() {
    let result = parse_invocation("rustc", &args(&["--crate-type", "cdylib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn rustc_no_crate_type_defaults_to_bin_cacheable() {
    // Without --crate-type, rustc defaults to bin. bin is cacheable
    // as of iter7 — see `rustc_bin_crate_is_cacheable`.
    let result = parse_invocation("rustc", &args(&["src/main.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_incremental_is_cacheable() {
    // Cargo always passes -C incremental. We allow it (ignored for cache key).
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--crate-type",
            "lib",
            "-C",
            "incremental=/tmp/incr",
            "src/lib.rs",
        ]),
    );
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

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

#[test]
fn rustc_emit_metadata_is_cacheable() {
    // cargo check uses --emit=metadata
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type", "lib", "--emit=metadata", "src/lib.rs"]),
    );
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_output_with_explicit_o() {
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type", "lib", "src/lib.rs", "-o", "libfoo.rlib"]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.output_file, NormalizedPath::new("libfoo.rlib"));
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_metadata_only_output_is_rmeta() {
    // cargo check: --emit=dep-info,metadata (no link) → primary output is .rmeta
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--crate-type",
            "lib",
            "--crate-name",
            "mylib",
            "--emit=dep-info,metadata",
            "--out-dir",
            "/target/debug/deps",
            "-C",
            "extra-filename=-abc123",
            "src/lib.rs",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(
                c.output_file,
                NormalizedPath::new("/target/debug/deps/libmylib-abc123.rmeta")
            );
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_output_from_out_dir() {
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--crate-type",
            "lib",
            "--crate-name",
            "mylib",
            "--out-dir",
            "/target/debug/deps",
            "-C",
            "extra-filename=-abc123",
            "src/lib.rs",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(
                c.output_file,
                NormalizedPath::new("/target/debug/deps/libmylib-abc123.rlib")
            );
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_full_cargo_invocation_cacheable() {
    // Realistic cargo-generated rustc command
    let result = parse_invocation(
        "rustc",
        &args(&[
            "--edition",
            "2021",
            "--crate-type",
            "lib",
            "--crate-name",
            "serde",
            "--emit=dep-info,metadata,link",
            "-C",
            "opt-level=2",
            "-C",
            "metadata=abc123def",
            "-C",
            "extra-filename=-abc123def",
            "--out-dir",
            "/target/release/deps",
            "-L",
            "dependency=/target/release/deps",
            "--extern",
            "serde_derive=/target/release/deps/libserde_derive-xyz.so",
            "--cap-lints",
            "allow",
            "--cfg",
            "feature=\"derive\"",
            "--cfg",
            "feature=\"std\"",
            "src/lib.rs",
        ]),
    );
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(c.family, CompilerFamily::Rustc);
            assert_eq!(c.source_file, NormalizedPath::new("src/lib.rs"));
            assert_eq!(
                c.output_file,
                NormalizedPath::new("/target/release/deps/libserde-abc123def.rlib")
            );
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_original_args_preserved() {
    let input = args(&["--edition", "2021", "--crate-type", "lib", "src/lib.rs"]);
    let result = parse_invocation("rustc", &input);
    match result {
        ParsedInvocation::Cacheable(c) => {
            assert_eq!(*c.original_args, *input);
        }
        other => panic!("expected cacheable, got: {other:?}"),
    }
}

#[test]
fn rustc_equal_form_crate_type() {
    let result = parse_invocation("rustc", &args(&["--crate-type=lib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_concatenated_c_incremental_is_cacheable() {
    // -Cincremental= form (no space after -C) — still cacheable
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type", "lib", "-Cincremental=/tmp", "src/lib.rs"]),
    );
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_comma_separated_crate_type_all_cacheable() {
    let result = parse_invocation("rustc", &args(&["--crate-type", "lib,rlib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_comma_separated_crate_type_mixed_non_cacheable() {
    // lib is cacheable but dylib is not
    let result = parse_invocation("rustc", &args(&["--crate-type", "lib,dylib", "src/lib.rs"]));
    assert!(matches!(result, ParsedInvocation::NonCacheable { .. }));
}

#[test]
fn rustc_comma_separated_crate_type_equals_form() {
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type=lib,staticlib", "src/lib.rs"]),
    );
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}

#[test]
fn rustc_test_flag_makes_non_cacheable() {
    // --test compiles a test harness (implicitly bin, not cacheable)
    let result = parse_invocation(
        "rustc",
        &args(&["--crate-type", "lib", "--test", "src/lib.rs"]),
    );
    // --test gets captured as unknown_flag. Since --crate-type lib is specified
    // the compilation IS cacheable. The --test flag is in unknown_flags which
    // is part of the cache key, so different --test values produce different keys.
    // This is correct: `--test` with `--crate-type lib` is a valid cacheable invocation.
    assert!(matches!(result, ParsedInvocation::Cacheable(_)));
}