use std::env::var;
use std::io::Write;
fn main() {
use_feature_or_nothing("windows_by_handle"); use_feature_or_nothing("windows_file_type_ext");
use_feature_or_nothing("windows_change_time");
use_feature_or_nothing("io_error_more"); use_feature_or_nothing("io_error_uncategorized");
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
println!("cargo:rustc-check-cfg=cfg(emulate_second_only_system)");
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 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("-o")
.arg("-")
.stdout(Stdio::null());
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()
}