use std::env::var;
use std::io::Write;
fn main() {
use_feature_or_nothing("windows_file_type_ext");
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");
println!("cargo:rerun-if-changed=build.rs");
}
fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
println!("cargo:rustc-check-cfg=cfg({})", feature);
}
fn use_feature(feature: &str) {
println!("cargo:rustc-cfg={}", feature);
}
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}
fn can_compile<T: AsRef<str>>(test: T) -> bool {
use std::process::Stdio;
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();
let wrapper = var("RUSTC_WRAPPER")
.ok()
.and_then(|w| if w.is_empty() { None } else { Some(w) });
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = std::process::Command::new(wrapper);
cmd.arg(rustc);
cmd
} else {
std::process::Command::new(rustc)
};
cmd.arg("--crate-type=rlib") .arg("--emit=metadata") .arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir);
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}
let mut child = cmd
.arg("-") .stdin(Stdio::piped()) .stderr(Stdio::null()) .spawn()
.unwrap();
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
child.wait().unwrap().success()
}