Debian Codegraph @

.cargo/vendor/proc-macro2/build.rs

raw
1 #![allow(unknown_lints)]
2 #![allow(unexpected_cfgs)]
3 #![allow(clippy::uninlined_format_args)]
4
5 use std::env;
6 use std::ffi::OsString;
7 use std::fs;
8 use std::io::ErrorKind;
9 use std::iter;
10 use std::path::Path;
11 use std::process::{self, Command, Stdio};
12 use std::str;
13
14 fn main() {
15 let rustc = rustc_minor_version().unwrap_or(u32::MAX);
16
17 if rustc >= 80 {
18 println!("cargo:rustc-check-cfg=cfg(fuzzing)");
19 println!("cargo:rustc-check-cfg=cfg(no_is_available)");
20 println!("cargo:rustc-check-cfg=cfg(no_literal_byte_character)");
21 println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)");
22 println!("cargo:rustc-check-cfg=cfg(no_source_text)");
23 println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
24 println!("cargo:rustc-check-cfg=cfg(proc_macro_span_file)");
25 println!("cargo:rustc-check-cfg=cfg(proc_macro_span_location)");
26 println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
27 println!("cargo:rustc-check-cfg=cfg(procmacro2_build_probe)");
28 println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
29 println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)");
30 println!("cargo:rustc-check-cfg=cfg(randomize_layout)");
31 println!("cargo:rustc-check-cfg=cfg(span_locations)");
32 println!("cargo:rustc-check-cfg=cfg(super_unstable)");
33 println!("cargo:rustc-check-cfg=cfg(wrap_proc_macro)");
34 }
35
36 let semver_exempt = cfg!(procmacro2_semver_exempt);
37 if semver_exempt {
38 // https://github.com/dtolnay/proc-macro2/issues/147
39 println!("cargo:rustc-cfg=procmacro2_semver_exempt");
40 }
41
42 if semver_exempt || cfg!(feature = "span-locations") {
43 // Provide methods Span::start and Span::end which give the line/column
44 // location of a token. This is behind a cfg because tracking location
45 // inside spans is a performance hit.
46 println!("cargo:rustc-cfg=span_locations");
47 }
48
49 if rustc < 57 {
50 // Do not use proc_macro::is_available() to detect whether the proc
51 // macro API is available vs needs to be polyfilled. Instead, use the
52 // proc macro API unconditionally and catch the panic that occurs if it
53 // isn't available.
54 println!("cargo:rustc-cfg=no_is_available");
55 }
56
57 if rustc < 66 {
58 // Do not call libproc_macro's Span::source_text. Always return None.
59 println!("cargo:rustc-cfg=no_source_text");
60 }
61
62 if rustc < 79 {
63 // Do not call Literal::byte_character nor Literal::c_string. They can
64 // be emulated by way of Literal::from_str.
65 println!("cargo:rustc-cfg=no_literal_byte_character");
66 println!("cargo:rustc-cfg=no_literal_c_string");
67 }
68
69 if !cfg!(feature = "proc-macro") {
70 println!("cargo:rerun-if-changed=build.rs");
71 return;
72 }
73
74 let proc_macro_span;
75 let consider_rustc_bootstrap;
76 if compile_probe_unstable("proc_macro_span", false) {
77 // This is a nightly or dev compiler, so it supports unstable features
78 // regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
79 // RUSTC_BOOTSTRAP is changed.
80 proc_macro_span = true;
81 consider_rustc_bootstrap = false;
82 } else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
83 if compile_probe_unstable("proc_macro_span", true) {
84 // This is a stable or beta compiler for which the user has set
85 // RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
86 // if they change it.
87 proc_macro_span = true;
88 consider_rustc_bootstrap = true;
89 } else if rustc_bootstrap == "1" {
90 // This compiler does not support the proc macro Span API in the
91 // form that proc-macro2 expects. No need to pay attention to
92 // RUSTC_BOOTSTRAP.
93 proc_macro_span = false;
94 consider_rustc_bootstrap = false;
95 } else {
96 // This is a stable or beta compiler for which RUSTC_BOOTSTRAP is
97 // set to restrict the use of unstable features by this crate.
98 proc_macro_span = false;
99 consider_rustc_bootstrap = true;
100 }
101 } else {
102 // Without RUSTC_BOOTSTRAP, this compiler does not support the proc
103 // macro Span API in the form that proc-macro2 expects, but try again if
104 // the user turns on unstable features.
105 proc_macro_span = false;
106 consider_rustc_bootstrap = true;
107 }
108
109 if proc_macro_span || !semver_exempt {
110 // Wrap types from libproc_macro rather than polyfilling the whole API.
111 // Enabled as long as procmacro2_semver_exempt is not set, because we
112 // can't emulate the unstable API without emulating everything else.
113 // Also enabled unconditionally on nightly, in which case the
114 // procmacro2_semver_exempt surface area is implemented by using the
115 // nightly-only proc_macro API.
116 println!("cargo:rustc-cfg=wrap_proc_macro");
117 }
118
119 if proc_macro_span {
120 // Enable non-dummy behavior of Span::byte_range and Span::join methods
121 // which requires an unstable compiler feature. Enabled when building
122 // with nightly, unless `-Z allow-feature` in RUSTFLAGS disallows
123 // unstable features.
124 println!("cargo:rustc-cfg=proc_macro_span");
125 }
126
127 if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_location")) {
128 // Enable non-dummy behavior of Span::start and Span::end methods on
129 // Rust 1.88+.
130 println!("cargo:rustc-cfg=proc_macro_span_location");
131 }
132
133 if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_file")) {
134 // Enable non-dummy behavior of Span::file and Span::local_file methods
135 // on Rust 1.88+.
136 println!("cargo:rustc-cfg=proc_macro_span_file");
137 }
138
139 if semver_exempt && proc_macro_span {
140 // Implement the semver exempt API in terms of the nightly-only
141 // proc_macro API.
142 println!("cargo:rustc-cfg=super_unstable");
143 }
144
145 if consider_rustc_bootstrap {
146 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
147 }
148 }
149
150 fn compile_probe_unstable(feature: &str, rustc_bootstrap: bool) -> bool {
151 // RUSTC_STAGE indicates that this crate is being compiled as a dependency
152 // of a multistage rustc bootstrap. This environment uses Cargo in a highly
153 // non-standard way with issues such as:
154 //
155 // https://github.com/rust-lang/cargo/issues/11138
156 // https://github.com/rust-lang/rust/issues/114839
157 //
158 env::var_os("RUSTC_STAGE").is_none() && do_compile_probe(feature, rustc_bootstrap)
159 }
160
161 fn compile_probe_stable(feature: &str) -> bool {
162 env::var_os("RUSTC_STAGE").is_some() || do_compile_probe(feature, true)
163 }
164
165 fn do_compile_probe(feature: &str, rustc_bootstrap: bool) -> bool {
166 println!("cargo:rerun-if-changed=src/probe/{}.rs", feature);
167
168 let rustc = cargo_env_var("RUSTC");
169 let out_dir = cargo_env_var("OUT_DIR");
170 let out_subdir = Path::new(&out_dir).join("probe");
171 let probefile = Path::new("src")
172 .join("probe")
173 .join(feature)
174 .with_extension("rs");
175
176 if let Err(err) = fs::create_dir(&out_subdir) {
177 if err.kind() != ErrorKind::AlreadyExists {
178 eprintln!("Failed to create {}: {}", out_subdir.display(), err);
179 process::exit(1);
180 }
181 }
182
183 let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
184 let rustc_workspace_wrapper =
185 env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty());
186 let mut rustc = rustc_wrapper
187 .into_iter()
188 .chain(rustc_workspace_wrapper)
189 .chain(iter::once(rustc));
190 let mut cmd = Command::new(rustc.next().unwrap());
191 cmd.args(rustc);
192
193 if !rustc_bootstrap {
194 cmd.env_remove("RUSTC_BOOTSTRAP");
195 }
196
197 cmd.stderr(Stdio::null())
198 .arg("--cfg=procmacro2_build_probe")
199 .arg("--edition=2021")
200 .arg("--crate-name=proc_macro2")
201 .arg("--crate-type=lib")
202 .arg("--cap-lints=allow")
203 .arg("--emit=dep-info,metadata")
204 .arg("--out-dir")
205 .arg(&out_subdir)
206 .arg(probefile);
207
208 if let Some(target) = env::var_os("TARGET") {
209 cmd.arg("--target").arg(target);
210 }
211
212 // If Cargo wants to set RUSTFLAGS, use that.
213 if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
214 if !rustflags.is_empty() {
215 for arg in rustflags.split('\x1f') {
216 cmd.arg(arg);
217 }
218 }
219 }
220
221 let success = match cmd.status() {
222 Ok(status) => status.success(),
223 Err(_) => false,
224 };
225
226 // Clean up to avoid leaving nondeterministic absolute paths in the dep-info
227 // file in OUT_DIR, which causes nonreproducible builds in build systems
228 // that treat the entire OUT_DIR as an artifact.
229 if let Err(err) = fs::remove_dir_all(&out_subdir) {
230 // libc::ENOTEMPTY
231 // Some filesystems (NFSv3) have timing issues under load where '.nfs*'
232 // dummy files can continue to get created for a short period after the
233 // probe command completes, breaking remove_dir_all.
234 // To be replaced with ErrorKind::DirectoryNotEmpty (Rust 1.83+).
235 const ENOTEMPTY: i32 = 39;
236
237 if !(err.kind() == ErrorKind::NotFound
238 || (cfg!(target_os = "linux") && err.raw_os_error() == Some(ENOTEMPTY)))
239 {
240 eprintln!("Failed to clean up {}: {}", out_subdir.display(), err);
241 process::exit(1);
242 }
243 }
244
245 success
246 }
247
248 fn rustc_minor_version() -> Option<u32> {
249 let rustc = cargo_env_var("RUSTC");
250 let output = Command::new(rustc).arg("--version").output().ok()?;
251 let version = str::from_utf8(&output.stdout).ok()?;
252 let mut pieces = version.split('.');
253 if pieces.next() != Some("rustc 1") {
254 return None;
255 }
256 pieces.next()?.parse().ok()
257 }
258
259 fn cargo_env_var(key: &str) -> OsString {
260 env::var_os(key).unwrap_or_else(|| {
261 eprintln!(
262 "Environment variable ${} is not set during execution of build script",
263 key,
264 );
265 process::exit(1);
266 })
267 }