use std::{
env,
io::Write,
iter,
process::{Command, Stdio},
};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if probe_feature("coverage_attribute").unwrap_or(false) {
println!("cargo:rustc-cfg=coverage_helper_has_coverage_attribute");
}
}
fn probe_feature(feature_name: &str) -> Option<bool> {
if env::var_os("RUSTC_STAGE").is_some() {
return None;
}
let out_dir = env::var_os("OUT_DIR")?;
let rustc = env::var_os("RUSTC")?;
let (rustc_wrapper, rustc_workspace_wrapper) =
if env::var_os("CARGO_ENCODED_RUSTFLAGS").is_some() {
(
env::var_os("RUSTC_WRAPPER").filter(|v| !v.is_empty()),
env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|v| !v.is_empty()),
)
} else {
(None, None)
};
let mut rustc =
rustc_wrapper.into_iter().chain(rustc_workspace_wrapper).chain(iter::once(rustc));
let mut cmd = Command::new(rustc.next().unwrap());
cmd.args(rustc);
cmd.stderr(Stdio::null())
.arg("--edition=2018")
.arg("--crate-name")
.arg(format!("coverage_helper_build_{}", feature_name))
.arg("--crate-type=lib")
.arg("--emit=metadata")
.arg("--cap-lints=allow")
.arg("--out-dir")
.arg(out_dir);
cmd.arg("-").stdin(Stdio::piped());
let mut child = cmd.spawn().ok()?;
let mut stdin = child.stdin.take().expect("rustc stdin");
stdin.write_all(br"#![feature(").ok()?;
stdin.write_all(feature_name.as_bytes()).ok()?;
stdin.write_all(br")]").ok()?;
drop(stdin);
let status = child.wait().ok()?;
Some(status.success())
}