blob: 30b5f05facb3e5929eb98a310117348ab3c1a332 [file] [log] [blame]
Alex Crichton53548482018-08-11 21:54:05 -07001use std::env;
Alex Crichtonce0904d2018-08-27 17:29:49 -07002use std::process::Command;
3use std::str;
Alex Crichton53548482018-08-11 21:54:05 -07004
5fn main() {
6 println!("cargo:rerun-if-changed=build.rs");
7
8 let target = env::var("TARGET").unwrap();
9
Alex Crichtonce0904d2018-08-27 17:29:49 -070010 if !enable_use_proc_macro(&target) {
David Tolnay2ff99ce2018-09-01 09:40:51 -070011 return;
Alex Crichtonce0904d2018-08-27 17:29:49 -070012 }
13 println!("cargo:rustc-cfg=use_proc_macro");
14
15 let minor = match rustc_minor_version() {
16 Some(n) => n,
17 None => return,
18 };
19
David Tolnaye839e4f2018-09-06 09:36:43 -070020 // Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate
21 if minor >= 29 || cfg!(feature = "nightly") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070022 println!("cargo:rustc-cfg=wrap_proc_macro");
23
24 if cfg!(procmacro2_semver_exempt) {
25 println!("cargo:rustc-cfg=super_unstable");
26 }
David Tolnaye839e4f2018-09-06 09:36:43 -070027 }
28
29 if minor == 29 {
30 println!("cargo:rustc-cfg=slow_extend");
Alex Crichtonce0904d2018-08-27 17:29:49 -070031 }
Alex Crichton53548482018-08-11 21:54:05 -070032}
33
Alex Crichtonce0904d2018-08-27 17:29:49 -070034fn enable_use_proc_macro(target: &str) -> bool {
Alex Crichton53548482018-08-11 21:54:05 -070035 // wasm targets don't have the `proc_macro` crate, disable this feature.
36 if target.contains("wasm32") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070037 return false;
Alex Crichton53548482018-08-11 21:54:05 -070038 }
39
Alex Crichton53548482018-08-11 21:54:05 -070040 // Otherwise, only enable it if our feature is actually enabled.
Alex Crichtonce0904d2018-08-27 17:29:49 -070041 cfg!(feature = "proc-macro")
42}
43
44fn rustc_minor_version() -> Option<u32> {
45 macro_rules! otry {
David Tolnay2ff99ce2018-09-01 09:40:51 -070046 ($e:expr) => {
47 match $e {
48 Some(e) => e,
49 None => return None,
50 }
51 };
Alex Crichton53548482018-08-11 21:54:05 -070052 }
Alex Crichtonce0904d2018-08-27 17:29:49 -070053 let rustc = otry!(env::var_os("RUSTC"));
54 let output = otry!(Command::new(rustc).arg("--version").output().ok());
55 let version = otry!(str::from_utf8(&output.stdout).ok());
56 let mut pieces = version.split('.');
57 if pieces.next() != Some("rustc 1") {
58 return None;
59 }
60 otry!(pieces.next()).parse().ok()
Alex Crichton53548482018-08-11 21:54:05 -070061}