1 // rustc-cfg emitted by the build script:
2 //
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 //
17 // "hygiene"
18 // Enable Span::mixed_site() and non-dummy behavior of Span::resolved_at
19 // and Span::located_at. Enabled on Rust 1.45+.
20 //
21 // "proc_macro_span"
22 // Enable non-dummy behavior of Span::start and Span::end methods which
23 // requires an unstable compiler feature. Enabled when building with
24 // nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable
25 // features.
26 //
27 // "super_unstable"
28 // Implement the semver exempt API in terms of the nightly-only proc_macro
29 // API. Enabled when using procmacro2_semver_exempt on a nightly compiler.
30 //
31 // "span_locations"
32 // Provide methods Span::start and Span::end which give the line/column
33 // location of a token. Enabled by procmacro2_semver_exempt or the
34 // "span-locations" Cargo cfg. This is behind a cfg because tracking
35 // location inside spans is a performance hit.
36 //
37 // "is_available"
38 // Use proc_macro::is_available() to detect if the proc macro API is
39 // available or needs to be polyfilled instead of trying to use the proc
40 // macro API and catching a panic if it isn't available. Enabled on Rust
41 // 1.57+.
42
43 use std::env;
44 use std::iter;
45 use std::process::{self, Command};
46 use std::str;
47
main()48 fn main() {
49 println!("cargo:rerun-if-changed=build.rs");
50
51 let version = match rustc_version() {
52 Some(version) => version,
53 None => return,
54 };
55
56 if version.minor < 31 {
57 eprintln!("Minimum supported rustc version is 1.31");
58 process::exit(1);
59 }
60
61 let docs_rs = env::var_os("DOCS_RS").is_some();
62 let semver_exempt = cfg!(procmacro2_semver_exempt) || docs_rs;
63 if semver_exempt {
64 // https://github.com/dtolnay/proc-macro2/issues/147
65 println!("cargo:rustc-cfg=procmacro2_semver_exempt");
66 }
67
68 if semver_exempt || cfg!(feature = "span-locations") {
69 println!("cargo:rustc-cfg=span_locations");
70 }
71
72 if version.minor < 32 {
73 println!("cargo:rustc-cfg=no_libprocmacro_unwind_safe");
74 }
75
76 if version.minor < 39 {
77 println!("cargo:rustc-cfg=no_bind_by_move_pattern_guard");
78 }
79
80 if version.minor < 44 {
81 println!("cargo:rustc-cfg=no_lexerror_display");
82 }
83
84 if version.minor < 45 {
85 println!("cargo:rustc-cfg=no_hygiene");
86 }
87
88 if version.minor < 54 {
89 println!("cargo:rustc-cfg=no_literal_from_str");
90 }
91
92 if version.minor < 55 {
93 println!("cargo:rustc-cfg=no_group_open_close");
94 }
95
96 if version.minor < 57 {
97 println!("cargo:rustc-cfg=no_is_available");
98 }
99
100 let target = env::var("TARGET").unwrap();
101 if !enable_use_proc_macro(&target) {
102 return;
103 }
104
105 println!("cargo:rustc-cfg=use_proc_macro");
106
107 if version.nightly || !semver_exempt {
108 println!("cargo:rustc-cfg=wrap_proc_macro");
109 }
110
111 if version.nightly && feature_allowed("proc_macro_span") {
112 println!("cargo:rustc-cfg=proc_macro_span");
113 }
114
115 if semver_exempt && version.nightly {
116 println!("cargo:rustc-cfg=super_unstable");
117 }
118 }
119
enable_use_proc_macro(target: &str) -> bool120 fn enable_use_proc_macro(target: &str) -> bool {
121 // wasm targets don't have the `proc_macro` crate, disable this feature.
122 if target.contains("wasm32") {
123 return false;
124 }
125
126 // Otherwise, only enable it if our feature is actually enabled.
127 cfg!(feature = "proc-macro")
128 }
129
130 struct RustcVersion {
131 minor: u32,
132 nightly: bool,
133 }
134
rustc_version() -> Option<RustcVersion>135 fn rustc_version() -> Option<RustcVersion> {
136 let rustc = env::var_os("RUSTC")?;
137 let output = Command::new(rustc).arg("--version").output().ok()?;
138 let version = str::from_utf8(&output.stdout).ok()?;
139 let nightly = version.contains("nightly") || version.contains("dev");
140 let mut pieces = version.split('.');
141 if pieces.next() != Some("rustc 1") {
142 return None;
143 }
144 let minor = pieces.next()?.parse().ok()?;
145 Some(RustcVersion { minor, nightly })
146 }
147
feature_allowed(feature: &str) -> bool148 fn feature_allowed(feature: &str) -> bool {
149 // Recognized formats:
150 //
151 // -Z allow-features=feature1,feature2
152 //
153 // -Zallow-features=feature1,feature2
154
155 let flags_var;
156 let flags_var_string;
157 let mut flags_var_split;
158 let mut flags_none;
159 let flags: &mut dyn Iterator<Item = &str> =
160 if let Some(encoded_rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") {
161 flags_var = encoded_rustflags;
162 flags_var_string = flags_var.to_string_lossy();
163 flags_var_split = flags_var_string.split('\x1f');
164 &mut flags_var_split
165 } else if let Some(rustflags) = env::var_os("RUSTFLAGS") {
166 flags_var = rustflags;
167 flags_var_string = flags_var.to_string_lossy();
168 flags_var_split = flags_var_string.split(' ');
169 &mut flags_var_split
170 } else {
171 flags_none = iter::empty();
172 &mut flags_none
173 };
174
175 for mut flag in flags {
176 if flag.starts_with("-Z") {
177 flag = &flag["-Z".len()..];
178 }
179 if flag.starts_with("allow-features=") {
180 flag = &flag["allow-features=".len()..];
181 return flag.split(',').any(|allowed| allowed == feature);
182 }
183 }
184
185 // No allow-features= flag, allowed by default.
186 true
187 }
188