Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 1 | use std::env; |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 2 | use std::process::Command; |
| 3 | use std::str; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 4 | |
| 5 | fn main() { |
| 6 | println!("cargo:rerun-if-changed=build.rs"); |
| 7 | |
| 8 | let target = env::var("TARGET").unwrap(); |
| 9 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 10 | let minor = match rustc_minor_version() { |
| 11 | Some(n) => n, |
| 12 | None => return, |
| 13 | }; |
| 14 | |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 15 | 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 Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 24 | // Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate |
Olivier Goffart | 42d3019 | 2018-11-16 11:36:10 +0100 | [diff] [blame] | 25 | if (minor >= 29 && !cfg!(procmacro2_semver_exempt)) || cfg!(feature = "nightly") { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 26 | println!("cargo:rustc-cfg=wrap_proc_macro"); |
| 27 | |
| 28 | if cfg!(procmacro2_semver_exempt) { |
| 29 | println!("cargo:rustc-cfg=super_unstable"); |
David Tolnay | d7568e5 | 2018-11-11 15:23:32 -0800 | [diff] [blame] | 30 | // https://github.com/alexcrichton/proc-macro2/issues/147 |
| 31 | println!("cargo:rustc-cfg=procmacro2_semver_exempt"); |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 32 | } |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | if minor == 29 { |
| 36 | println!("cargo:rustc-cfg=slow_extend"); |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 37 | } |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 40 | fn enable_use_proc_macro(target: &str) -> bool { |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 41 | // wasm targets don't have the `proc_macro` crate, disable this feature. |
| 42 | if target.contains("wasm32") { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 43 | return false; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 46 | // Otherwise, only enable it if our feature is actually enabled. |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 47 | cfg!(feature = "proc-macro") |
| 48 | } |
| 49 | |
| 50 | fn rustc_minor_version() -> Option<u32> { |
| 51 | macro_rules! otry { |
David Tolnay | 2ff99ce | 2018-09-01 09:40:51 -0700 | [diff] [blame] | 52 | ($e:expr) => { |
| 53 | match $e { |
| 54 | Some(e) => e, |
| 55 | None => return None, |
| 56 | } |
| 57 | }; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 58 | } |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 59 | 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 Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 67 | } |