David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 1 | // rustc-cfg emitted by the build script: |
| 2 | // |
| 3 | // "u128" |
| 4 | // Include u128 and i128 constructors for proc_macro2::Literal. Enabled on |
| 5 | // any compiler 1.26+. |
| 6 | // |
| 7 | // "use_proc_macro" |
| 8 | // Link to extern crate proc_macro. Available on any compiler and any target |
| 9 | // except wasm32. Requires "proc-macro" Cargo cfg to be enabled (default is |
| 10 | // enabled). On wasm32 we never link to proc_macro even if "proc-macro" cfg |
| 11 | // is enabled. |
| 12 | // |
| 13 | // "wrap_proc_macro" |
| 14 | // Wrap types from libproc_macro rather than polyfilling the whole API. |
| 15 | // Enabled on rustc 1.29+ as long as procmacro2_semver_exempt is not set, |
| 16 | // because we can't emulate the unstable API without emulating everything |
| 17 | // else. Also enabled unconditionally on nightly, in which case the |
| 18 | // procmacro2_semver_exempt surface area is implemented by using the |
| 19 | // nightly-only proc_macro API. |
| 20 | // |
| 21 | // "slow_extend" |
| 22 | // Fallback when `impl Extend for TokenStream` is not available. These impls |
| 23 | // were added one version later than the rest of the proc_macro token API. |
| 24 | // Enabled on rustc 1.29 only. |
| 25 | // |
| 26 | // "nightly" |
David Tolnay | 40bbb1c | 2019-01-19 19:43:55 -0800 | [diff] [blame] | 27 | // Enable the Span::unwrap method. This is to support proc_macro_span and |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 28 | // proc_macro_diagnostic use on the nightly channel without requiring the |
| 29 | // semver exemption opt-in. Enabled when building with nightly. |
| 30 | // |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 31 | // "proc_macro_span_disallowed" |
| 32 | // Don't use the proc_macro_span feature when building on the nightly |
| 33 | // channel. Detected based on `-Z allow-feature` flag passed in RUSTFLAGS. |
| 34 | // |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 35 | // "super_unstable" |
| 36 | // Implement the semver exempt API in terms of the nightly-only proc_macro |
| 37 | // API. Enabled when using procmacro2_semver_exempt on a nightly compiler. |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 38 | // |
| 39 | // "span_locations" |
| 40 | // Provide methods Span::start and Span::end which give the line/column |
| 41 | // location of a token. Enabled by procmacro2_semver_exempt or the |
| 42 | // "span-locations" Cargo cfg. This is behind a cfg because tracking |
| 43 | // location inside spans is a performance hit. |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 44 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 45 | use std::env; |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 46 | use std::process::Command; |
| 47 | use std::str; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 48 | |
| 49 | fn main() { |
| 50 | println!("cargo:rerun-if-changed=build.rs"); |
| 51 | |
| 52 | let target = env::var("TARGET").unwrap(); |
| 53 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 54 | let version = match rustc_version() { |
| 55 | Some(version) => version, |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 56 | None => return, |
| 57 | }; |
| 58 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 59 | if version.minor >= 26 { |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 60 | println!("cargo:rustc-cfg=u128"); |
| 61 | } |
| 62 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 63 | let semver_exempt = cfg!(procmacro2_semver_exempt); |
| 64 | if semver_exempt { |
| 65 | // https://github.com/alexcrichton/proc-macro2/issues/147 |
| 66 | println!("cargo:rustc-cfg=procmacro2_semver_exempt"); |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 67 | } |
| 68 | |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 69 | if semver_exempt || cfg!(feature = "span-locations") { |
| 70 | println!("cargo:rustc-cfg=span_locations"); |
| 71 | } |
| 72 | |
| 73 | if !enable_use_proc_macro(&target) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | println!("cargo:rustc-cfg=use_proc_macro"); |
| 78 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 79 | // Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate |
| 80 | if version.nightly || version.minor >= 29 && !semver_exempt { |
| 81 | println!("cargo:rustc-cfg=wrap_proc_macro"); |
| 82 | } |
| 83 | |
| 84 | if version.minor == 29 { |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 85 | println!("cargo:rustc-cfg=slow_extend"); |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 86 | } |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 87 | |
| 88 | if version.nightly { |
| 89 | println!("cargo:rustc-cfg=nightly"); |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 90 | if proc_macro_span_disallowed() { |
| 91 | println!("cargo:rustc-cfg=proc_macro_span_disallowed"); |
| 92 | } |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if semver_exempt && version.nightly { |
| 96 | println!("cargo:rustc-cfg=super_unstable"); |
| 97 | } |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 100 | fn enable_use_proc_macro(target: &str) -> bool { |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 101 | // wasm targets don't have the `proc_macro` crate, disable this feature. |
| 102 | if target.contains("wasm32") { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 103 | return false; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 106 | // Otherwise, only enable it if our feature is actually enabled. |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 107 | cfg!(feature = "proc-macro") |
| 108 | } |
| 109 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 110 | struct RustcVersion { |
| 111 | minor: u32, |
| 112 | nightly: bool, |
| 113 | } |
| 114 | |
| 115 | fn rustc_version() -> Option<RustcVersion> { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 116 | macro_rules! otry { |
David Tolnay | 2ff99ce | 2018-09-01 09:40:51 -0700 | [diff] [blame] | 117 | ($e:expr) => { |
| 118 | match $e { |
| 119 | Some(e) => e, |
| 120 | None => return None, |
| 121 | } |
| 122 | }; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 123 | } |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 124 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 125 | let rustc = otry!(env::var_os("RUSTC")); |
| 126 | let output = otry!(Command::new(rustc).arg("--version").output().ok()); |
| 127 | let version = otry!(str::from_utf8(&output.stdout).ok()); |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 128 | let nightly = version.contains("nightly"); |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 129 | let mut pieces = version.split('.'); |
| 130 | if pieces.next() != Some("rustc 1") { |
| 131 | return None; |
| 132 | } |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 133 | let minor = otry!(pieces.next()); |
| 134 | let minor = otry!(minor.parse().ok()); |
| 135 | |
| 136 | Some(RustcVersion { |
| 137 | minor: minor, |
| 138 | nightly: nightly, |
| 139 | }) |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 140 | } |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 141 | |
| 142 | fn proc_macro_span_disallowed() -> bool { |
| 143 | if let Some(rustflags) = env::var_os("RUSTFLAGS") { |
| 144 | for flag in rustflags.to_string_lossy().split(" ") { |
| 145 | if flag.contains("allow-features") && !flag.contains("proc_macro_span") { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | false |
| 151 | } |