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
//! RUSTC_WRAPPER shim that restores absolute paths in SBF DWARF output.
//!
//! ## Problem
//!
//! The Solana toolchain's cargo passes `-Zremap-cwd-prefix=` (empty
//! replacement) to rustc for every SBF crate. This strips `DW_AT_comp_dir`
//! from the DWARF, making all source paths relative. When multiple crates
//! share filenames like `src/lib.rs`, the debugger can't tell them apart
//! and may show source from the wrong crate.
//!
//! ## Solution
//!
//! `anchor debugger` sets `RUSTC_WRAPPER` to the `anchor` binary itself.
//! Cargo then invokes `anchor <real-rustc> <args...>` for every rustc
//! call. This module detects that invocation pattern (the env var
//! `__ANCHOR_RUSTC_WRAPPER=1` disambiguates from normal CLI usage) and
//! replaces `-Zremap-cwd-prefix=` with `-Zremap-cwd-prefix=$CWD`,
//! preserving absolute paths in the debug info.
//!
//! The sentinel env var is necessary because `RUSTC_WRAPPER` mode passes
//! a path as argv[1] (the real rustc binary), which clap would reject as
//! an unknown subcommand. The check in `main.rs` runs before clap
//! parsing so the process never hits the normal CLI dispatch.
//!
//! ## Performance
//!
//! The wrapper adds ~1ms of fork+exec overhead per rustc invocation.
//! This is negligible compared to actual compilation time.
use process;
/// Env var set by `anchor debugger` before calling `cargo build-sbf`.
/// When present, the process knows it was invoked as a RUSTC_WRAPPER
/// and should rewrite args instead of running the normal CLI.
pub const WRAPPER_SENTINEL: &str = "__ANCHOR_RUSTC_WRAPPER";
/// If we're running as a RUSTC_WRAPPER (sentinel env var is set),
/// rewrite the rustc args and exec the real compiler. Never returns.
///
/// If we're NOT in wrapper mode, returns `false` so the caller can
/// proceed with normal CLI parsing.