David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 1 | // rustc-cfg emitted by the build script: |
| 2 | // |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 3 | // "use_proc_macro" |
| 4 | // Link to extern crate proc_macro. Available on any compiler and any target |
| 5 | // except wasm32. Requires "proc-macro" Cargo cfg to be enabled (default is |
| 6 | // enabled). On wasm32 we never link to proc_macro even if "proc-macro" cfg |
| 7 | // is enabled. |
| 8 | // |
| 9 | // "wrap_proc_macro" |
| 10 | // Wrap types from libproc_macro rather than polyfilling the whole API. |
| 11 | // Enabled on rustc 1.29+ as long as procmacro2_semver_exempt is not set, |
| 12 | // because we can't emulate the unstable API without emulating everything |
| 13 | // else. Also enabled unconditionally on nightly, in which case the |
| 14 | // procmacro2_semver_exempt surface area is implemented by using the |
| 15 | // nightly-only proc_macro API. |
| 16 | // |
David Tolnay | b455dd7 | 2019-04-28 13:50:51 -0700 | [diff] [blame] | 17 | // "proc_macro_span" |
| 18 | // Enable non-dummy behavior of Span::start and Span::end methods which |
| 19 | // requires an unstable compiler feature. Enabled when building with |
| 20 | // nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable |
| 21 | // features. |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 22 | // |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 23 | // "super_unstable" |
| 24 | // Implement the semver exempt API in terms of the nightly-only proc_macro |
| 25 | // API. Enabled when using procmacro2_semver_exempt on a nightly compiler. |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 26 | // |
| 27 | // "span_locations" |
| 28 | // Provide methods Span::start and Span::end which give the line/column |
| 29 | // location of a token. Enabled by procmacro2_semver_exempt or the |
| 30 | // "span-locations" Cargo cfg. This is behind a cfg because tracking |
| 31 | // location inside spans is a performance hit. |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 32 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 33 | use std::env; |
David Tolnay | 5fd7e8d | 2019-08-16 09:02:37 -0700 | [diff] [blame] | 34 | use std::process::{self, Command}; |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 35 | use std::str; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 36 | |
| 37 | fn main() { |
| 38 | println!("cargo:rerun-if-changed=build.rs"); |
| 39 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 40 | let version = match rustc_version() { |
| 41 | Some(version) => version, |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 42 | None => return, |
| 43 | }; |
| 44 | |
David Tolnay | 5fd7e8d | 2019-08-16 09:02:37 -0700 | [diff] [blame] | 45 | if version.minor < 31 { |
| 46 | eprintln!("Minimum supported rustc version is 1.31"); |
| 47 | process::exit(1); |
| 48 | } |
| 49 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 50 | let semver_exempt = cfg!(procmacro2_semver_exempt); |
| 51 | if semver_exempt { |
| 52 | // https://github.com/alexcrichton/proc-macro2/issues/147 |
| 53 | println!("cargo:rustc-cfg=procmacro2_semver_exempt"); |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 56 | if semver_exempt || cfg!(feature = "span-locations") { |
| 57 | println!("cargo:rustc-cfg=span_locations"); |
| 58 | } |
| 59 | |
David Tolnay | 5fd7e8d | 2019-08-16 09:02:37 -0700 | [diff] [blame] | 60 | let target = env::var("TARGET").unwrap(); |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 61 | if !enable_use_proc_macro(&target) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | println!("cargo:rustc-cfg=use_proc_macro"); |
| 66 | |
David Tolnay | d7dc146 | 2019-07-19 11:48:47 -0700 | [diff] [blame] | 67 | if version.nightly || !semver_exempt { |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 68 | println!("cargo:rustc-cfg=wrap_proc_macro"); |
| 69 | } |
| 70 | |
David Tolnay | ce12a48 | 2019-04-28 13:56:13 -0700 | [diff] [blame] | 71 | if version.nightly && feature_allowed("proc_macro_span") { |
David Tolnay | b455dd7 | 2019-04-28 13:50:51 -0700 | [diff] [blame] | 72 | println!("cargo:rustc-cfg=proc_macro_span"); |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if semver_exempt && version.nightly { |
| 76 | println!("cargo:rustc-cfg=super_unstable"); |
| 77 | } |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 80 | fn enable_use_proc_macro(target: &str) -> bool { |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 81 | // wasm targets don't have the `proc_macro` crate, disable this feature. |
| 82 | if target.contains("wasm32") { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 83 | return false; |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 86 | // Otherwise, only enable it if our feature is actually enabled. |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 87 | cfg!(feature = "proc-macro") |
| 88 | } |
| 89 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 90 | struct RustcVersion { |
David Tolnay | 5fd7e8d | 2019-08-16 09:02:37 -0700 | [diff] [blame] | 91 | minor: u32, |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 92 | nightly: bool, |
| 93 | } |
| 94 | |
| 95 | fn rustc_version() -> Option<RustcVersion> { |
David Tolnay | 25ae952 | 2019-07-19 11:54:35 -0700 | [diff] [blame] | 96 | let rustc = env::var_os("RUSTC")?; |
| 97 | let output = Command::new(rustc).arg("--version").output().ok()?; |
| 98 | let version = str::from_utf8(&output.stdout).ok()?; |
David Tolnay | 16d1bf5 | 2019-09-06 20:25:56 -0700 | [diff] [blame] | 99 | let nightly = version.contains("nightly") || version.contains("dev"); |
David Tolnay | 5fd7e8d | 2019-08-16 09:02:37 -0700 | [diff] [blame] | 100 | let mut pieces = version.split('.'); |
| 101 | if pieces.next() != Some("rustc 1") { |
| 102 | return None; |
| 103 | } |
| 104 | let minor = pieces.next()?.parse().ok()?; |
| 105 | Some(RustcVersion { minor, nightly }) |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 106 | } |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 107 | |
David Tolnay | ce12a48 | 2019-04-28 13:56:13 -0700 | [diff] [blame] | 108 | fn feature_allowed(feature: &str) -> bool { |
| 109 | // Recognized formats: |
| 110 | // |
| 111 | // -Z allow-features=feature1,feature2 |
| 112 | // |
| 113 | // -Zallow-features=feature1,feature2 |
| 114 | |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 115 | if let Some(rustflags) = env::var_os("RUSTFLAGS") { |
David Tolnay | ce12a48 | 2019-04-28 13:56:13 -0700 | [diff] [blame] | 116 | for mut flag in rustflags.to_string_lossy().split(' ') { |
| 117 | if flag.starts_with("-Z") { |
| 118 | flag = &flag["-Z".len()..]; |
| 119 | } |
| 120 | if flag.starts_with("allow-features=") { |
| 121 | flag = &flag["allow-features=".len()..]; |
| 122 | return flag.split(',').any(|allowed| allowed == feature); |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
David Tolnay | ce12a48 | 2019-04-28 13:56:13 -0700 | [diff] [blame] | 126 | |
| 127 | // No allow-features= flag, allowed by default. |
| 128 | true |
Wim Looman | 099db2a | 2019-04-28 11:55:09 +0200 | [diff] [blame] | 129 | } |