()
| 13 | use serde_json::{json, Value}; |
| 14 | |
| 15 | fn main() { |
| 16 | // 1) replay 模式:用于 IDE 单步调试 |
| 17 | { |
| 18 | let mut it = env::args().skip(1); |
| 19 | if let Some(a1) = it.next() { |
| 20 | if a1 == "--replay" { |
| 21 | let path = it.next().expect("usage: mir_wrapper --replay <invocation.json>"); |
| 22 | let text = fs::read_to_string(&path).expect("failed to read replay file"); |
| 23 | let v: Value = serde_json::from_str(&text).expect("invalid json replay file"); |
| 24 | |
| 25 | // cwd |
| 26 | if let Some(cwd) = v.get("cwd").and_then(|x| x.as_str()) { |
| 27 | env::set_current_dir(cwd).expect("failed to set cwd from replay"); |
| 28 | } |
| 29 | |
| 30 | // env(只恢复 dump 里保存的那些) |
| 31 | if let Some(obj) = v.get("env").and_then(|x| x.as_object()) { |
| 32 | for (k, val) in obj { |
| 33 | if let Some(s) = val.as_str() { |
| 34 | env::set_var(k, s); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // argv |
| 40 | let argv = v.get("argv") |
| 41 | .and_then(|x| x.as_array()) |
| 42 | .expect("replay file missing argv") |
| 43 | .iter() |
| 44 | .map(|x| x.as_str().expect("argv must be string").to_string()) |
| 45 | .collect::<Vec<_>>(); |
| 46 | |
| 47 | let code = rust_api_bypass::driver::run_with_rustc_args(argv); |
| 48 | std::process::exit(code); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // 2) wrapper 模式:Cargo 调用形式: mir_wrapper <rustc-path> <rustc-args...> |
| 54 | let argv: Vec<String> = env::args().collect(); |
| 55 | if argv.len() < 2 { |
| 56 | eprintln!("usage: mir_wrapper <rustc-path> <rustc-args...>"); |
| 57 | std::process::exit(2); |
| 58 | } |
| 59 | let rustc_path = argv[1].clone(); |
| 60 | let rustc_args = argv[2..].to_vec(); |
| 61 | |
| 62 | // 非主包:直接透传 rustc,保证依赖/脚本/宏等正常编译 |
| 63 | let is_primary = env::var("CARGO_PRIMARY_PACKAGE").ok().as_deref() == Some("1"); |
| 64 | if !is_primary { |
| 65 | // 用同一套 rustc_driver 编译依赖,避免 metadata 版本不一致 |
| 66 | let mut full = Vec::with_capacity(1 + rustc_args.len()); |
| 67 | full.push(rustc_path.clone()); |
| 68 | full.extend(rustc_args); |
| 69 | |
| 70 | std::env::set_var("BYPASSER_BE_RUSTC", "1"); |
| 71 | std::env::set_var("MIR_CHECKER_BE_RUSTC", "1"); |
| 72 | let code = rust_api_bypass::driver::run_with_rustc_args(full); |
nothing calls this directly
no test coverage detected