Skip to content

Commit ad98471

Browse files
authored
Merge pull request #30 from dtolnay/cfg
Add rustversion::cfg! macro
2 parents 29338df + c1c5c26 commit ad98471

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

build/build.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
};
3737

3838
let version = match rustc::parse(&string) {
39-
Some(version) => format!("{:#?}\n", version),
39+
Some(version) => version,
4040
None => {
4141
eprintln!(
4242
"Error: unexpected output from `rustc --version`: {:?}\n\n\
@@ -47,6 +47,12 @@ fn main() {
4747
}
4848
};
4949

50+
if version.minor < 38 {
51+
// Prior to 1.38, a #[proc_macro] is not allowed to be named `cfg`.
52+
println!("cargo:rustc-cfg=cfg_macro_not_allowed");
53+
}
54+
55+
let version = format!("{:#?}\n", version);
5056
let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
5157
let out_file = Path::new(&out_dir).join("version.rs");
5258
fs::write(out_file, version).expect("failed to write version.rs");

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,18 @@ pub fn attr(args: TokenStream, input: TokenStream) -> TokenStream {
227227
.and_then(|args| expand::try_attr(args, input))
228228
.unwrap_or_else(Error::into_compile_error)
229229
}
230+
231+
#[cfg(not(cfg_macro_not_allowed))]
232+
#[proc_macro]
233+
pub fn cfg(input: TokenStream) -> TokenStream {
234+
use proc_macro::{Ident, Span, TokenTree};
235+
(|| {
236+
let ref mut args = iter::new(input);
237+
let expr = expr::parse(args)?;
238+
token::parse_end(args)?;
239+
let boolean = expr.eval(RUSTVERSION);
240+
let ident = Ident::new(&boolean.to_string(), Span::call_site());
241+
Ok(TokenStream::from(TokenTree::Ident(ident)))
242+
})()
243+
.unwrap_or_else(Error::into_compile_error)
244+
}

0 commit comments

Comments
 (0)