use std::{env, process::Command, str};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rustc-check-cfg=cfg(doc_cfg)");
println!("cargo:rustc-check-cfg=cfg(path_buf_deref_mut)");
println!("cargo:rustc-check-cfg=cfg(path_buf_capacity)");
println!("cargo:rustc-check-cfg=cfg(shrink_to)");
println!("cargo:rustc-check-cfg=cfg(try_reserve_2)");
println!("cargo:rustc-check-cfg=cfg(os_str_bytes)");
println!("cargo:rustc-check-cfg=cfg(absolute_path)");
let compiler = match rustc_version() {
Some(compiler) => compiler,
None => return,
};
if compiler.minor >= 44 {
println!("cargo:rustc-cfg=path_buf_capacity");
}
if compiler.minor >= 56 {
println!("cargo:rustc-cfg=shrink_to");
}
if (compiler.minor >= 63
&& (compiler.channel == ReleaseChannel::Stable || compiler.channel == ReleaseChannel::Beta))
|| compiler.minor >= 64
{
println!("cargo:rustc-cfg=try_reserve_2");
}
if (compiler.minor >= 68
&& (compiler.channel == ReleaseChannel::Stable || compiler.channel == ReleaseChannel::Beta))
|| compiler.minor >= 69
{
println!("cargo:rustc-cfg=path_buf_deref_mut");
}
if (compiler.minor >= 74 && compiler.channel == ReleaseChannel::Stable) || compiler.minor >= 75
{
println!("cargo:rustc-cfg=os_str_bytes");
}
if (compiler.minor >= 79 && compiler.channel == ReleaseChannel::Stable) || compiler.minor >= 80
{
println!("cargo:rustc-cfg=absolute_path");
}
#[cfg(all(feature = "proptest", not(feature = "proptest1")))]
{
println!(
"cargo:warning=proptest feature is enabled, but proptest1 isn't -- this won't do anything"
);
}
#[cfg(all(feature = "serde", not(feature = "serde1")))]
{
println!(
"cargo:warning=serde feature is enabled, but serde1 isn't -- this won't do anything"
);
}
}
struct Compiler {
minor: u32,
channel: ReleaseChannel,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ReleaseChannel {
Stable,
Beta,
Nightly,
}
fn rustc_version() -> Option<Compiler> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let minor = pieces.next()?.parse().ok()?;
let channel = if version.contains("nightly") {
ReleaseChannel::Nightly
} else if version.contains("beta") {
ReleaseChannel::Beta
} else {
ReleaseChannel::Stable
};
Some(Compiler { minor, channel })
}