Automatically use rich API on 1.30.0+

This commit detects the rustc version in the build script of proc-macro2
to determine whether the compiler supports the necessary backend APIs.
This should hopefully allow the crate to compile on stable by default
but have a better implementation on 1.30.0+ compilers.
diff --git a/build.rs b/build.rs
index aaa92b6..cd91b4b 100644
--- a/build.rs
+++ b/build.rs
@@ -1,21 +1,53 @@
 use std::env;
+use std::process::Command;
+use std::str;
 
 fn main() {
     println!("cargo:rerun-if-changed=build.rs");
 
     let target = env::var("TARGET").unwrap();
 
-    maybe_enable_use_proc_macro(&target);
+    if !enable_use_proc_macro(&target) {
+        return
+    }
+    println!("cargo:rustc-cfg=use_proc_macro");
+
+    let minor = match rustc_minor_version() {
+        Some(n) => n,
+        None => return,
+    };
+
+    // Rust 1.30 stabilized the necessary APIs in the `proc_macro` crate
+    if minor >= 30 || cfg!(feature = "nightly") {
+        println!("cargo:rustc-cfg=wrap_proc_macro");
+
+        if cfg!(procmacro2_semver_exempt) {
+            println!("cargo:rustc-cfg=super_unstable");
+        }
+    } else {
+    }
 }
 
-fn maybe_enable_use_proc_macro(target: &str) {
+fn enable_use_proc_macro(target: &str) -> bool {
     // wasm targets don't have the `proc_macro` crate, disable this feature.
     if target.contains("wasm32") {
-        return;
+        return false;
     }
 
     // Otherwise, only enable it if our feature is actually enabled.
-    if cfg!(feature = "proc-macro") {
-        println!("cargo:rustc-cfg=use_proc_macro");
+    cfg!(feature = "proc-macro")
+}
+
+fn rustc_minor_version() -> Option<u32> {
+    macro_rules! otry {
+        ($e:expr) => (match $e { Some(e) => e, None => return None })
     }
+    let rustc = otry!(env::var_os("RUSTC"));
+    let output = otry!(Command::new(rustc).arg("--version").output().ok());
+    let version = otry!(str::from_utf8(&output.stdout).ok());
+    let mut pieces = version.split('.');
+    if pieces.next() != Some("rustc 1") {
+        return None;
+    }
+    otry!(pieces.next()).parse().ok()
 }