| 1 |
extern crate autocfg; |
| 2 |
|
| 3 |
use std::env; |
| 4 |
use std::io; |
| 5 |
|
| 6 |
mod support; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
let args: Vec<_> = env::args().skip(1).collect(); |
| 10 |
if args.is_empty() { |
| 11 |
// With no arguments, this must be a (harness-free) test run. |
| 12 |
// (we're using system binaries as wrappers, so ignore other platforms) |
| 13 |
if cfg!(unix) { |
| 14 |
test_wrappers(); |
| 15 |
} |
| 16 |
} else { |
| 17 |
// Otherwise we're acting like a no-op rustc wrapper, ignoring almost everything. |
| 18 |
for arg in args { |
| 19 |
match &*arg { |
| 20 |
// Add our own version so we can check that the wrapper is used for that. |
| 21 |
"--version" => println!("release: 12345.6789.0"), |
| 22 |
|
| 23 |
// Read all input so the writer doesn't get EPIPE when we exit. |
| 24 |
"-" => { |
| 25 |
let stdin = io::stdin(); |
| 26 |
let _ = io::copy(&mut stdin.lock(), &mut io::sink()); |
| 27 |
} |
| 28 |
|
| 29 |
_ => {} |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/// Tests that autocfg uses the RUSTC_WRAPPER and/or RUSTC_WORKSPACE_WRAPPER |
| 36 |
/// environment variables when running rustc. |
| 37 |
fn test_wrappers() { |
| 38 |
fn set(name: &str, value: Option<bool>) { |
| 39 |
match value { |
| 40 |
Some(true) => env::set_var(name, "/usr/bin/env"), |
| 41 |
Some(false) => env::set_var(name, "/bin/false"), |
| 42 |
None => env::remove_var(name), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
let out = support::out_dir(); |
| 47 |
|
| 48 |
// This is used as a heuristic to detect rust-lang/cargo#9601. |
| 49 |
env::set_var("CARGO_ENCODED_RUSTFLAGS", ""); |
| 50 |
|
| 51 |
// No wrapper, a good pass-through wrapper, and a bad wrapper. |
| 52 |
let variants = [None, Some(true), Some(false)]; |
| 53 |
|
| 54 |
for &workspace in &variants { |
| 55 |
for &rustc in &variants { |
| 56 |
set("RUSTC_WRAPPER", rustc); |
| 57 |
set("RUSTC_WORKSPACE_WRAPPER", workspace); |
| 58 |
|
| 59 |
let ac = autocfg::AutoCfg::with_dir(out.as_ref()).unwrap(); |
| 60 |
if rustc == Some(false) || workspace == Some(false) { |
| 61 |
// Everything should fail with bad wrappers. |
| 62 |
assert!(!ac.probe_type("usize")); |
| 63 |
} else { |
| 64 |
// Try known good and bad types for the wrapped rustc. |
| 65 |
assert!(ac.probe_type("usize")); |
| 66 |
assert!(!ac.probe_type("mesize")); |
| 67 |
} |
| 68 |
// Either way, we should have found the inner rustc version. |
| 69 |
assert!(ac.probe_rustc_version(1, 0)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Finally, make sure that `RUSTC_WRAPPER` is applied outermost |
| 74 |
// by using something (ourself) that doesn't pass through at all. |
| 75 |
let wrap_ignored = env::current_exe().unwrap(); |
| 76 |
env::set_var("RUSTC_WRAPPER", &wrap_ignored); |
| 77 |
env::set_var("RUSTC_WORKSPACE_WRAPPER", "/bin/false"); |
| 78 |
let ac = autocfg::AutoCfg::with_dir(out.as_ref()).unwrap(); |
| 79 |
assert!(ac.probe_type("mesize")); // anything goes! |
| 80 |
|
| 81 |
// Make sure we also got the version from that wrapper. |
| 82 |
assert!(ac.probe_rustc_version(12345, 6789)); |
| 83 |
} |