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 | if !enable_use_proc_macro(&target) { |
| 11 | return |
| 12 | } |
| 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 | |
| 20 | // Rust 1.30 stabilized the necessary APIs in the `proc_macro` crate |
| 21 | if minor >= 30 || cfg!(feature = "nightly") { |
| 22 | println!("cargo:rustc-cfg=wrap_proc_macro"); |
| 23 | |
| 24 | if cfg!(procmacro2_semver_exempt) { |
| 25 | println!("cargo:rustc-cfg=super_unstable"); |
| 26 | } |
| 27 | } else { |
| 28 | } |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame^] | 31 | fn enable_use_proc_macro(target: &str) -> bool { |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 32 | // wasm targets don't have the `proc_macro` crate, disable this feature. |
| 33 | if target.contains("wasm32") { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame^] | 34 | return false; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 37 | // Otherwise, only enable it if our feature is actually enabled. |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame^] | 38 | cfg!(feature = "proc-macro") |
| 39 | } |
| 40 | |
| 41 | fn rustc_minor_version() -> Option<u32> { |
| 42 | macro_rules! otry { |
| 43 | ($e:expr) => (match $e { Some(e) => e, None => return None }) |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 44 | } |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame^] | 45 | let rustc = otry!(env::var_os("RUSTC")); |
| 46 | let output = otry!(Command::new(rustc).arg("--version").output().ok()); |
| 47 | let version = otry!(str::from_utf8(&output.stdout).ok()); |
| 48 | let mut pieces = version.split('.'); |
| 49 | if pieces.next() != Some("rustc 1") { |
| 50 | return None; |
| 51 | } |
| 52 | otry!(pieces.next()).parse().ok() |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 53 | } |