blob: cd6df2379e6f3bc1ac9b346a91bd899eab496de5 [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 let minor = match rustc_minor_version() {
11 Some(n) => n,
12 None => return,
13 };
14
Alex Crichton69385662018-11-08 06:30:04 -080015 if minor >= 26 {
16 println!("cargo:rustc-cfg=u128");
17 }
18
19 if !enable_use_proc_macro(&target) {
20 return;
21 }
22 println!("cargo:rustc-cfg=use_proc_macro");
23
David Tolnaye839e4f2018-09-06 09:36:43 -070024 // Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate
Olivier Goffart42d30192018-11-16 11:36:10 +010025 if (minor >= 29 && !cfg!(procmacro2_semver_exempt)) || cfg!(feature = "nightly") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070026 println!("cargo:rustc-cfg=wrap_proc_macro");
27
28 if cfg!(procmacro2_semver_exempt) {
29 println!("cargo:rustc-cfg=super_unstable");
David Tolnayd7568e52018-11-11 15:23:32 -080030 // https://github.com/alexcrichton/proc-macro2/issues/147
31 println!("cargo:rustc-cfg=procmacro2_semver_exempt");
Alex Crichtonce0904d2018-08-27 17:29:49 -070032 }
David Tolnaye839e4f2018-09-06 09:36:43 -070033 }
34
35 if minor == 29 {
36 println!("cargo:rustc-cfg=slow_extend");
Alex Crichtonce0904d2018-08-27 17:29:49 -070037 }
Alex Crichton53548482018-08-11 21:54:05 -070038}
39
Alex Crichtonce0904d2018-08-27 17:29:49 -070040fn enable_use_proc_macro(target: &str) -> bool {
Alex Crichton53548482018-08-11 21:54:05 -070041 // wasm targets don't have the `proc_macro` crate, disable this feature.
42 if target.contains("wasm32") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070043 return false;
Alex Crichton53548482018-08-11 21:54:05 -070044 }
45
Alex Crichton53548482018-08-11 21:54:05 -070046 // Otherwise, only enable it if our feature is actually enabled.
Alex Crichtonce0904d2018-08-27 17:29:49 -070047 cfg!(feature = "proc-macro")
48}
49
50fn rustc_minor_version() -> Option<u32> {
51 macro_rules! otry {
David Tolnay2ff99ce2018-09-01 09:40:51 -070052 ($e:expr) => {
53 match $e {
54 Some(e) => e,
55 None => return None,
56 }
57 };
Alex Crichton53548482018-08-11 21:54:05 -070058 }
Alex Crichtonce0904d2018-08-27 17:29:49 -070059 let rustc = otry!(env::var_os("RUSTC"));
60 let output = otry!(Command::new(rustc).arg("--version").output().ok());
61 let version = otry!(str::from_utf8(&output.stdout).ok());
62 let mut pieces = version.split('.');
63 if pieces.next() != Some("rustc 1") {
64 return None;
65 }
66 otry!(pieces.next()).parse().ok()
Alex Crichton53548482018-08-11 21:54:05 -070067}