Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use autocfg instead of our own version parser
- Loading branch information
Showing
2 changed files
with
13 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,8 @@ std = [] | |
# TODO | ||
# system_time = ["std"] | ||
|
||
[build-dependencies] | ||
autocfg = "1" | ||
|
||
[dependencies] | ||
const_fn = "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |