Skip to content

Commit

Permalink
Use autocfg instead of our own version parser
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 30, 2020
1 parent ce0ea97 commit f6c3b78
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -26,5 +26,8 @@ std = []
# TODO
# system_time = ["std"]

[build-dependencies]
autocfg = "1"

[dependencies]
const_fn = "0.4"
46 changes: 10 additions & 36 deletions build.rs
@@ -1,51 +1,25 @@
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, single_use_lifetimes)]

use std::{env, process::Command, str};
use autocfg::AutoCfg;

// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => {
println!("cargo:warning={}: unable to determine rustc version", env!("CARGO_PKG_NAME"));
let cfg = match AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
println!(
"cargo:warning={}: unable to determine rustc version: {}",
env!("CARGO_PKG_NAME"),
e
);
return;
}
};

if minor >= 39 {
if cfg.probe_rustc_version(1, 39) {
println!("cargo:rustc-cfg=stable_1_39");
}
}

fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).args(&["--version", "--verbose"]).output().ok()?;
if !output.status.success() {
return None;
}
let output = str::from_utf8(&output.stdout).ok()?;

// Find the release line in the verbose version output.
let release = output
.lines()
.find(|line| line.starts_with("release: "))
.map(|line| &line["release: ".len()..])?;

// Split the version and channel info.
let mut version_channel = release.split('-');
let version = version_channel.next().unwrap();
let _channel = version_channel.next();

// Split the version into semver components.
let mut digits = version.splitn(3, '.');
let major = digits.next()?;
if major != "1" {
return None;
}
let minor = digits.next()?.parse().ok()?;
let _patch = digits.next()?;
Some(minor)
}

0 comments on commit f6c3b78

Please sign in to comment.