blob: a1757114aa885a791d7b05a7ccb0596989b65c6c [file] [log] [blame]
David Tolnay17eb0702019-01-05 12:23:17 -08001// 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"
27// Enable the Span::unstable method. This is to support proc_macro_span and
28// proc_macro_diagnostic use on the nightly channel without requiring the
29// semver exemption opt-in. Enabled when building with nightly.
30//
31// "super_unstable"
32// Implement the semver exempt API in terms of the nightly-only proc_macro
33// API. Enabled when using procmacro2_semver_exempt on a nightly compiler.
34
Alex Crichton53548482018-08-11 21:54:05 -070035use std::env;
Alex Crichtonce0904d2018-08-27 17:29:49 -070036use std::process::Command;
37use std::str;
Alex Crichton53548482018-08-11 21:54:05 -070038
39fn main() {
40 println!("cargo:rerun-if-changed=build.rs");
41
42 let target = env::var("TARGET").unwrap();
43
David Tolnay17eb0702019-01-05 12:23:17 -080044 let version = match rustc_version() {
45 Some(version) => version,
Alex Crichtonce0904d2018-08-27 17:29:49 -070046 None => return,
47 };
48
David Tolnay17eb0702019-01-05 12:23:17 -080049 if version.minor >= 26 {
Alex Crichton69385662018-11-08 06:30:04 -080050 println!("cargo:rustc-cfg=u128");
51 }
52
53 if !enable_use_proc_macro(&target) {
54 return;
55 }
56 println!("cargo:rustc-cfg=use_proc_macro");
57
David Tolnay17eb0702019-01-05 12:23:17 -080058 let semver_exempt = cfg!(procmacro2_semver_exempt);
59 if semver_exempt {
60 // https://github.com/alexcrichton/proc-macro2/issues/147
61 println!("cargo:rustc-cfg=procmacro2_semver_exempt");
David Tolnaye839e4f2018-09-06 09:36:43 -070062 }
63
David Tolnay17eb0702019-01-05 12:23:17 -080064 // Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate
65 if version.nightly || version.minor >= 29 && !semver_exempt {
66 println!("cargo:rustc-cfg=wrap_proc_macro");
67 }
68
69 if version.minor == 29 {
David Tolnaye839e4f2018-09-06 09:36:43 -070070 println!("cargo:rustc-cfg=slow_extend");
Alex Crichtonce0904d2018-08-27 17:29:49 -070071 }
David Tolnay17eb0702019-01-05 12:23:17 -080072
73 if version.nightly {
74 println!("cargo:rustc-cfg=nightly");
75 }
76
77 if semver_exempt && version.nightly {
78 println!("cargo:rustc-cfg=super_unstable");
79 }
Alex Crichton53548482018-08-11 21:54:05 -070080}
81
Alex Crichtonce0904d2018-08-27 17:29:49 -070082fn enable_use_proc_macro(target: &str) -> bool {
Alex Crichton53548482018-08-11 21:54:05 -070083 // wasm targets don't have the `proc_macro` crate, disable this feature.
84 if target.contains("wasm32") {
Alex Crichtonce0904d2018-08-27 17:29:49 -070085 return false;
Alex Crichton53548482018-08-11 21:54:05 -070086 }
87
Alex Crichton53548482018-08-11 21:54:05 -070088 // Otherwise, only enable it if our feature is actually enabled.
Alex Crichtonce0904d2018-08-27 17:29:49 -070089 cfg!(feature = "proc-macro")
90}
91
David Tolnay17eb0702019-01-05 12:23:17 -080092struct RustcVersion {
93 minor: u32,
94 nightly: bool,
95}
96
97fn rustc_version() -> Option<RustcVersion> {
Alex Crichtonce0904d2018-08-27 17:29:49 -070098 macro_rules! otry {
David Tolnay2ff99ce2018-09-01 09:40:51 -070099 ($e:expr) => {
100 match $e {
101 Some(e) => e,
102 None => return None,
103 }
104 };
Alex Crichton53548482018-08-11 21:54:05 -0700105 }
David Tolnay17eb0702019-01-05 12:23:17 -0800106
Alex Crichtonce0904d2018-08-27 17:29:49 -0700107 let rustc = otry!(env::var_os("RUSTC"));
108 let output = otry!(Command::new(rustc).arg("--version").output().ok());
109 let version = otry!(str::from_utf8(&output.stdout).ok());
David Tolnay17eb0702019-01-05 12:23:17 -0800110 let nightly = version.contains("nightly");
Alex Crichtonce0904d2018-08-27 17:29:49 -0700111 let mut pieces = version.split('.');
112 if pieces.next() != Some("rustc 1") {
113 return None;
114 }
David Tolnay17eb0702019-01-05 12:23:17 -0800115 let minor = otry!(pieces.next());
116 let minor = otry!(minor.parse().ok());
117
118 Some(RustcVersion {
119 minor: minor,
120 nightly: nightly,
121 })
Alex Crichton53548482018-08-11 21:54:05 -0700122}