zccache 1.11.18

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
443
444
445
446
447
448
449
450
451
452
453
454
455
//! MSVC `/showIncludes` stderr parser.
//!
//! When MSVC is invoked with `/showIncludes`, it emits one line per included
//! file to stderr.  The prefix is locale-dependent:
//!
//! - English:  `Note: including file:`
//! - Japanese: `メモ: インクルード ファイル:`
//! - Chinese:  `注意: 包含文件:`
//! - German:   `Hinweis: Einlesen der Datei:`
//!
//! We auto-detect the prefix from the output rather than hardcoding a single
//! locale, producing a [`ScanResult`] with `has_computed = false`.

use std::collections::HashSet;
use std::path::Path;

use super::depfile::canonicalize_path;
use super::scanner::ScanResult;

/// Well-known English prefix — checked first as a fast path.
const ENGLISH_PREFIX: &str = "Note: including file:";

/// Parse MSVC `/showIncludes` stderr output into a [`ScanResult`].
///
/// Auto-detects the locale-specific prefix, extracts include paths,
/// deduplicates, and returns `has_computed = false` (because the compiler
/// has already resolved all macros).
///
/// Returns `(scan_result, filtered_stderr)` where `filtered_stderr` has
/// `/showIncludes` lines removed, with original line endings and empty
/// lines preserved.
pub fn parse_show_includes(stderr: &[u8], source: &Path, cwd: &Path) -> (ScanResult, Vec<u8>) {
    let source_canonical = canonicalize_path(source, cwd);
    let lines = split_lines_preserving(stderr);

    // Auto-detect the locale-specific prefix (English fast path first).
    let prefix = detect_prefix(&lines);

    let mut seen = HashSet::new();
    let mut resolved = Vec::new();
    let mut filtered = Vec::new();

    for (text, raw) in &lines {
        let mut is_include_line = false;

        if let Some(ref pfx) = prefix {
            let line_str = String::from_utf8_lossy(text);
            if let Some(path_str) = line_str.strip_prefix(pfx.as_str()) {
                let path_str = path_str.trim();
                if !path_str.is_empty() {
                    let dep_path = Path::new(path_str);
                    let abs_path = if dep_path.is_absolute() {
                        canonicalize_path(dep_path, cwd)
                    } else {
                        canonicalize_path(&cwd.join(dep_path), cwd)
                    };

                    if abs_path != source_canonical && seen.insert(abs_path.clone()) {
                        resolved.push(abs_path);
                    }
                }
                is_include_line = true;
            }
        }

        if !is_include_line {
            filtered.extend_from_slice(raw);
        }
    }

    let scan = ScanResult {
        resolved,
        unresolved: Vec::new(),
        has_computed: false,
    };
    (scan, filtered)
}

// ── Prefix detection ────────────────────────────────────────────────

/// Detect the `/showIncludes` prefix from stderr output.
///
/// First checks for the English prefix (fast path), then falls back to
/// auto-detection for non-English MSVC locales by scanning for lines
/// with the pattern `<text>:<whitespace><drive_letter>:\<path>`.
fn detect_prefix(lines: &[(&[u8], &[u8])]) -> Option<String> {
    // Fast path: check for the well-known English prefix.
    for (text, _) in lines {
        let line = String::from_utf8_lossy(text);
        if line.starts_with(ENGLISH_PREFIX) {
            return Some(ENGLISH_PREFIX.to_string());
        }
    }

    // Slow path: auto-detect from drive-letter path patterns.
    // Count candidate prefixes; the most frequent one wins.
    let mut counts: Vec<(String, usize)> = Vec::new();
    for (text, _) in lines {
        let line = String::from_utf8_lossy(text);
        if let Some(candidate) = extract_prefix_candidate(&line) {
            if let Some(entry) = counts.iter_mut().find(|(pfx, _)| *pfx == candidate) {
                entry.1 += 1;
            } else {
                counts.push((candidate, 1));
            }
        }
    }
    counts
        .into_iter()
        .max_by_key(|(_, count)| *count)
        .map(|(pfx, _)| pfx)
}

/// Extract a candidate `/showIncludes` prefix from a single line.
///
/// Matches: `<text ending with ':'><whitespace><drive_letter>:\<path>`.
/// Returns everything up to and including the colon before the path.
fn extract_prefix_candidate(line: &str) -> Option<String> {
    let bytes = line.as_bytes();
    if bytes.len() < 4 {
        return None;
    }

    for i in 0..bytes.len().saturating_sub(1) {
        if !looks_like_windows_path_start(bytes, i) {
            continue;
        }
        // Drive path at start of line is an error/warning location, not /showIncludes.
        if i == 0 {
            continue;
        }

        let before = &line[..i];
        let trimmed = before.trim_end();
        // Prefix must end with ':' and have some text before it.
        if trimmed.len() >= 2 && trimmed.ends_with(':') {
            return Some(trimmed.to_string());
        }
    }
    None
}

fn looks_like_windows_path_start(bytes: &[u8], i: usize) -> bool {
    let len = bytes.len();

    // X:\path
    if i + 2 < len
        && bytes[i].is_ascii_alphabetic()
        && bytes[i + 1] == b':'
        && bytes[i + 2] == b'\\'
    {
        return true;
    }

    // \\server\share or \\?\C:\...
    i + 1 < len && bytes[i] == b'\\' && bytes[i + 1] == b'\\'
}

// ── Line splitting ──────────────────────────────────────────────────

/// Split bytes into `(text, raw)` pairs where `text` has line terminators
/// stripped and `raw` preserves the original bytes including terminators.
fn split_lines_preserving(data: &[u8]) -> Vec<(&[u8], &[u8])> {
    let mut lines = Vec::new();
    let mut start = 0;
    for i in 0..data.len() {
        if data[i] == b'\n' {
            let text_end = if i > start && data[i - 1] == b'\r' {
                i - 1
            } else {
                i
            };
            lines.push((&data[start..text_end], &data[start..=i]));
            start = i + 1;
        }
    }
    // Trailing content without newline.
    if start < data.len() {
        lines.push((&data[start..], &data[start..]));
    }
    lines
}

#[cfg(test)]
mod tests {
    use super::super::depfile::strip_win_prefix;
    use super::*;

    #[test]
    fn parse_basic() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        // Create real files so canonicalize works.
        let h1 = cwd.join("stdio.h");
        let h2 = cwd.join("stddef.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&h2, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!(
            "Note: including file: {}\r\nNote: including file: {}\r\n",
            h1.display(),
            h2.display(),
        );

        let (scan, filtered) = parse_show_includes(stderr.as_bytes(), &source, cwd);

        assert!(!scan.has_computed);
        assert_eq!(scan.resolved.len(), 2);
        let canon_h1 = strip_win_prefix(std::fs::canonicalize(&h1).unwrap().into());
        let canon_h2 = strip_win_prefix(std::fs::canonicalize(&h2).unwrap().into());
        assert!(scan.resolved.contains(&canon_h1));
        assert!(scan.resolved.contains(&canon_h2));
        assert!(filtered.is_empty());
    }

    #[test]
    fn parse_empty_stderr() {
        let (scan, filtered) = parse_show_includes(b"", Path::new("main.cpp"), Path::new("."));
        assert!(!scan.has_computed);
        assert!(scan.resolved.is_empty());
        assert!(scan.unresolved.is_empty());
        assert!(filtered.is_empty());
    }

    #[test]
    fn parse_mixed_output() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("foo.h");
        let h2 = cwd.join("bar.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&h2, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!(
            "Note: including file: {}\r\nwarning C4996: deprecated\r\nNote: including file: {}\r\n",
            h1.display(),
            h2.display(),
        );

        let (scan, filtered) = parse_show_includes(stderr.as_bytes(), &source, cwd);

        assert_eq!(scan.resolved.len(), 2);
        let filtered_str = String::from_utf8(filtered).unwrap();
        assert!(filtered_str.contains("warning C4996"));
        assert!(!filtered_str.contains("including file"));
    }

    #[cfg(windows)]
    #[test]
    fn detect_prefix_accepts_unc_paths() {
        let line = "Hinweis: Einlesen der Datei: \\\\server\\share\\sdk\\foo.h";
        assert_eq!(
            extract_prefix_candidate(line),
            Some("Hinweis: Einlesen der Datei:".to_string())
        );
    }

    #[test]
    fn parse_deduplicates() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("dup.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!(
            "Note: including file: {}\r\nNote: including file: {}\r\n",
            h1.display(),
            h1.display(),
        );

        let (scan, _) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(scan.resolved.len(), 1);
    }

    #[test]
    fn parse_excludes_source() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let source = cwd.join("main.cpp");
        std::fs::write(&source, "").unwrap();

        let stderr = format!("Note: including file: {}\r\n", source.display());

        let (scan, _) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert!(scan.resolved.is_empty());
    }

    #[test]
    fn parse_paths_with_spaces() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let subdir = cwd.join("my headers");
        std::fs::create_dir_all(&subdir).unwrap();
        let h1 = subdir.join("spaced header.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!("Note: including file: {}\r\n", h1.display());

        let (scan, _) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(scan.resolved.len(), 1);
    }

    #[test]
    fn parse_lf_line_endings() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("unix.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        // LF only, no CR
        let stderr = format!("Note: including file: {}\n", h1.display());

        let (scan, _) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(scan.resolved.len(), 1);
    }

    #[test]
    fn parse_trims_nesting_whitespace() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("nested.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        // Deep nesting: many spaces between prefix and path
        let stderr = format!("Note: including file:               {}\r\n", h1.display());

        let (scan, _) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(scan.resolved.len(), 1);
    }

    #[test]
    fn has_computed_always_false() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();
        let source = cwd.join("main.cpp");
        std::fs::write(&source, "").unwrap();

        // Even with no /showIncludes lines, has_computed is false.
        let (scan, _) = parse_show_includes(b"some warning\r\n", &source, cwd);
        assert!(!scan.has_computed);
    }

    #[test]
    fn preserves_empty_lines() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("a.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!(
            "Note: including file: {}\r\nfirst\r\n\r\nsecond\r\n",
            h1.display(),
        );

        let (_, filtered) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(filtered, b"first\r\n\r\nsecond\r\n");
    }

    #[test]
    fn preserves_crlf_endings() {
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("a.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!("Note: including file: {}\r\nwarning\r\n", h1.display(),);

        let (_, filtered) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(filtered, b"warning\r\n");
    }

    #[cfg(windows)]
    #[test]
    fn non_english_locale_detected() {
        // Simulates Japanese MSVC locale.
        let dir = tempfile::TempDir::new().unwrap();
        let cwd = dir.path();

        let h1 = cwd.join("stdio.h");
        let h2 = cwd.join("stddef.h");
        let source = cwd.join("main.cpp");
        std::fs::write(&h1, "").unwrap();
        std::fs::write(&h2, "").unwrap();
        std::fs::write(&source, "").unwrap();

        let stderr = format!(
            "メモ: インクルード ファイル: {}\r\nメモ: インクルード ファイル: {}\r\n",
            h1.display(),
            h2.display(),
        );

        let (scan, filtered) = parse_show_includes(stderr.as_bytes(), &source, cwd);
        assert_eq!(scan.resolved.len(), 2);
        assert!(filtered.is_empty());
    }

    #[test]
    fn prefix_detection_ignores_error_paths() {
        // Error lines with paths at the start should not be detected as /showIncludes.
        let line = "C:\\src\\main.cpp(10): error C2065: 'foo': undeclared identifier";
        assert!(extract_prefix_candidate(line).is_none());
    }

    #[test]
    fn prefix_candidate_english() {
        let line = "Note: including file: C:\\Windows\\stdio.h";
        assert_eq!(
            extract_prefix_candidate(line),
            Some("Note: including file:".to_string())
        );
    }

    #[test]
    fn prefix_candidate_japanese() {
        let line = "メモ: インクルード ファイル: C:\\Windows\\stdio.h";
        assert_eq!(
            extract_prefix_candidate(line),
            Some("メモ: インクルード ファイル:".to_string())
        );
    }

    #[test]
    fn prefix_candidate_chinese() {
        let line = "注意: 包含文件: C:\\Windows\\stdio.h";
        assert_eq!(
            extract_prefix_candidate(line),
            Some("注意: 包含文件:".to_string())
        );
    }
}