-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect if the proc_macro_def_site feature is not disallowed by -Zallo…
…w-features
- Loading branch information
Showing
3 changed files
with
28 additions
and
7 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 |
---|---|---|
@@ -1,15 +1,36 @@ | ||
// Based on https://stackoverflow.com/a/49250753/1290530 | ||
|
||
use std::env; | ||
|
||
use rustc_version::{version_meta, Channel}; | ||
|
||
fn main() { | ||
// Set cfg flags depending on release channel | ||
match version_meta().unwrap().channel { | ||
// Enable our feature on nightly, or when using a | ||
// locally build rustc | ||
Channel::Nightly | Channel::Dev => { | ||
println!("cargo:rustc-cfg=feature=\"RUSTC_IS_NIGHTLY\""); | ||
// locally build rustc, unless `-Zallow-features` | ||
// in RUSTFLAGS disallows unstable features. | ||
Channel::Nightly | Channel::Dev if feature_allowed("proc_macro_def_site") => { | ||
println!("cargo:rustc-cfg=proc_macro_def_site"); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
// Based on https://github.com/alexcrichton/proc-macro2/pull/176 | ||
fn feature_allowed(feature: &str) -> bool { | ||
if let Some(rustflags) = env::var_os("RUSTFLAGS") { | ||
for mut flag in rustflags.to_string_lossy().split(' ') { | ||
if flag.starts_with("-Z") { | ||
flag = &flag["-Z".len()..]; | ||
} | ||
if flag.starts_with("allow-features=") { | ||
flag = &flag["allow-features=".len()..]; | ||
return flag.split(',').any(|allowed| allowed == feature); | ||
} | ||
} | ||
} | ||
|
||
// No allow-features= flag, allowed by default. | ||
true | ||
} |
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