blob: 3379da34dc7da07900b1243204e311861d9420ae [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
25 if minor >= 29 || 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");
30 }
David Tolnaye839e4f2018-09-06 09:36:43 -070031 }
32
33 if minor == 29 {
34 println!("cargo:rustc-cfg=slow_extend");
Alex Crichtonce0904d2018-08-27 17:29:49 -070035 }
Alex Crichton53548482018-08-11 21:54:05 -070036}
37
Alex Crichtonce0904d2018-08-27 17:29:49 -070038fn enable_use_proc_macro(target: &str) -> bool {
Alex Crichton53548482018-08-11 21:54:05 -070039 // wasm targets don't have the `proc_macro` crate, disable this feature.
40 if target.contains("wasm32") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070041 return false;
Alex Crichton53548482018-08-11 21:54:05 -070042 }
43
Alex Crichton53548482018-08-11 21:54:05 -070044 // Otherwise, only enable it if our feature is actually enabled.
Alex Crichtonce0904d2018-08-27 17:29:49 -070045 cfg!(feature = "proc-macro")
46}
47
48fn rustc_minor_version() -> Option<u32> {
49 macro_rules! otry {
David Tolnay2ff99ce2018-09-01 09:40:51 -070050 ($e:expr) => {
51 match $e {
52 Some(e) => e,
53 None => return None,
54 }
55 };
Alex Crichton53548482018-08-11 21:54:05 -070056 }
Alex Crichtonce0904d2018-08-27 17:29:49 -070057 let rustc = otry!(env::var_os("RUSTC"));
58 let output = otry!(Command::new(rustc).arg("--version").output().ok());
59 let version = otry!(str::from_utf8(&output.stdout).ok());
60 let mut pieces = version.split('.');
61 if pieces.next() != Some("rustc 1") {
62 return None;
63 }
64 otry!(pieces.next()).parse().ok()
Alex Crichton53548482018-08-11 21:54:05 -070065}