-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::env; | ||
use std::ffi::OsString; | ||
use std::process::Command; | ||
use std::str; | ||
|
||
fn main() { | ||
let version = match rustc_version_info() { | ||
Some(version) => version, | ||
None => return, | ||
}; | ||
version.toolchain.set_feature(); | ||
} | ||
|
||
#[derive(PartialEq)] | ||
enum Toolchain { | ||
Stable, | ||
Beta, | ||
Nightly, | ||
} | ||
|
||
impl Toolchain { | ||
fn set_feature(self) { | ||
match self { | ||
Toolchain::Nightly => println!("cargo:rustc-cfg=nightly"), | ||
Toolchain::Beta => println!("cargo:rustc-cfg=beta"), | ||
Toolchain::Stable => println!("cargo:rustc-cfg=stable"), | ||
} | ||
} | ||
} | ||
|
||
struct VersionInfo { | ||
toolchain: Toolchain, | ||
} | ||
|
||
fn rustc_version_info() -> Option<VersionInfo> { | ||
let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("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") { | ||
return None; | ||
} | ||
let _major: u32 = pieces.next()?.parse().ok()?; | ||
let _minor: u32 = pieces.next()?.parse().ok()?; | ||
let _patch: u32 = pieces.next()?.parse().ok()?; | ||
let toolchain = match pieces.next() { | ||
Some("beta") => Toolchain::Beta, | ||
Some("nightly") => Toolchain::Nightly, | ||
_ => Toolchain::Stable, | ||
}; | ||
let version = VersionInfo { toolchain }; | ||
Some(version) | ||
} |
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
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
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
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,17 +1,34 @@ | ||
// These tests check our build script against rustversion. | ||
|
||
#[rustversion::attr(not(nightly), ignore)] | ||
//#[cfg_attr(miri, ignore)] | ||
#[test] | ||
fn nightlytest() { | ||
if !cfg!(nightly_features) { | ||
panic!("nightly feature isn't set when the toolchain is nightly"); | ||
if !cfg!(nightly) { | ||
panic!("nightly feature isn't set when the toolchain is nightly."); | ||
} | ||
if cfg!(any(beta, stable)) { | ||
panic!("beta, stable, and nightly are mutually exclusive features.") | ||
} | ||
} | ||
|
||
#[rustversion::attr(not(beta), ignore)] | ||
#[test] | ||
fn betatest() { | ||
if !cfg!(beta) { | ||
panic!("beta feature is not set when the toolchain is beta."); | ||
} | ||
if cfg!(any(nightly, stable)) { | ||
panic!("beta, stable, and nightly are mutually exclusive features.") | ||
} | ||
} | ||
|
||
#[rustversion::attr(nightly, ignore)] | ||
//#[cfg_attr(miri, ignore)] | ||
#[rustversion::attr(not(stable), ignore)] | ||
#[test] | ||
fn stabletest() { | ||
if cfg!(nightly_features) { | ||
panic!("nightly feature is set when the toolchain isn't nightly"); | ||
if !cfg!(stable) { | ||
panic!("stable feature is not set when the toolchain is stable."); | ||
} | ||
if cfg!(any(nightly, beta)) { | ||
panic!("beta, stable, and nightly are mutually exclusive features.") | ||
} | ||
} |