Debian Codegraph @

.cargo/vendor/anyhow/build.rs

raw
1 use std::env;
2 use std::ffi::OsString;
3 use std::fs;
4 use std::io::ErrorKind;
5 use std::iter;
6 use std::path::Path;
7 use std::process::{self, Command, Stdio};
8 use std::str;
9
10 #[cfg(all(feature = "backtrace", not(feature = "std")))]
11 compile_error! {
12 "`backtrace` feature without `std` feature is not supported"
13 }
14
15 fn main() {
16 let mut error_generic_member_access = false;
17 if cfg!(feature = "std") {
18 println!("cargo:rerun-if-changed=src/nightly.rs");
19
20 let consider_rustc_bootstrap;
21 if compile_probe(false) {
22 // This is a nightly or dev compiler, so it supports unstable
23 // features regardless of RUSTC_BOOTSTRAP. No need to rerun build
24 // script if RUSTC_BOOTSTRAP is changed.
25 error_generic_member_access = true;
26 consider_rustc_bootstrap = false;
27 } else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
28 if compile_probe(true) {
29 // This is a stable or beta compiler for which the user has set
30 // RUSTC_BOOTSTRAP to turn on unstable features. Rerun build
31 // script if they change it.
32 error_generic_member_access = true;
33 consider_rustc_bootstrap = true;
34 } else if rustc_bootstrap == "1" {
35 // This compiler does not support the generic member access API
36 // in the form that anyhow expects. No need to pay attention to
37 // RUSTC_BOOTSTRAP.
38 error_generic_member_access = false;
39 consider_rustc_bootstrap = false;
40 } else {
41 // This is a stable or beta compiler for which RUSTC_BOOTSTRAP
42 // is set to restrict the use of unstable features by this
43 // crate.
44 error_generic_member_access = false;
45 consider_rustc_bootstrap = true;
46 }
47 } else {
48 // Without RUSTC_BOOTSTRAP, this compiler does not support the
49 // generic member access API in the form that anyhow expects, but
50 // try again if the user turns on unstable features.
51 error_generic_member_access = false;
52 consider_rustc_bootstrap = true;
53 }
54
55 if error_generic_member_access {
56 println!("cargo:rustc-cfg=std_backtrace");
57 println!("cargo:rustc-cfg=error_generic_member_access");
58 }
59
60 if consider_rustc_bootstrap {
61 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
62 }
63 }
64
65 let rustc = match rustc_minor_version() {
66 Some(rustc) => rustc,
67 None => return,
68 };
69
70 if rustc >= 80 {
71 println!("cargo:rustc-check-cfg=cfg(anyhow_build_probe)");
72 println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");
73 println!("cargo:rustc-check-cfg=cfg(anyhow_no_clippy_format_args)");
74 println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");
75 println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_unwind_safe)");
76 println!("cargo:rustc-check-cfg=cfg(anyhow_no_fmt_arguments_as_str)");
77 println!("cargo:rustc-check-cfg=cfg(anyhow_no_ptr_addr_of)");
78 println!("cargo:rustc-check-cfg=cfg(anyhow_no_unsafe_op_in_unsafe_fn_lint)");
79 println!("cargo:rustc-check-cfg=cfg(error_generic_member_access)");
80 println!("cargo:rustc-check-cfg=cfg(std_backtrace)");
81 }
82
83 if rustc < 51 {
84 // core::ptr::addr_of
85 // https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
86 println!("cargo:rustc-cfg=anyhow_no_ptr_addr_of");
87 }
88
89 if rustc < 52 {
90 // core::fmt::Arguments::as_str
91 // https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html#stabilized-apis
92 println!("cargo:rustc-cfg=anyhow_no_fmt_arguments_as_str");
93
94 // #![deny(unsafe_op_in_unsafe_fn)]
95 // https://github.com/rust-lang/rust/issues/71668
96 println!("cargo:rustc-cfg=anyhow_no_unsafe_op_in_unsafe_fn_lint");
97 }
98
99 if rustc < 56 {
100 // core::panic::{UnwindSafe, RefUnwindSafe}
101 // https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html#stabilized-apis
102 println!("cargo:rustc-cfg=anyhow_no_core_unwind_safe");
103 }
104
105 if !error_generic_member_access && cfg!(feature = "std") && rustc >= 65 {
106 // std::backtrace::Backtrace
107 // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis
108 println!("cargo:rustc-cfg=std_backtrace");
109 }
110
111 if rustc < 81 {
112 // core::error::Error
113 // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror
114 println!("cargo:rustc-cfg=anyhow_no_core_error");
115 }
116
117 if rustc < 85 {
118 // #[clippy::format_args]
119 // https://doc.rust-lang.org/1.85.1/clippy/attribs.html#clippyformat_args
120 println!("cargo:rustc-cfg=anyhow_no_clippy_format_args");
121 }
122 }
123
124 fn compile_probe(rustc_bootstrap: bool) -> bool {
125 if env::var_os("RUSTC_STAGE").is_some() {
126 // We are running inside rustc bootstrap. This is a highly non-standard
127 // environment with issues such as:
128 //
129 // https://github.com/rust-lang/cargo/issues/11138
130 // https://github.com/rust-lang/rust/issues/114839
131 //
132 // Let's just not use nightly features here.
133 return false;
134 }
135
136 let rustc = cargo_env_var("RUSTC");
137 let out_dir = cargo_env_var("OUT_DIR");
138 let out_subdir = Path::new(&out_dir).join("probe");
139 let probefile = Path::new("src").join("nightly.rs");
140
141 if let Err(err) = fs::create_dir(&out_subdir) {
142 if err.kind() != ErrorKind::AlreadyExists {
143 eprintln!("Failed to create {}: {}", out_subdir.display(), err);
144 process::exit(1);
145 }
146 }
147
148 let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
149 let rustc_workspace_wrapper =
150 env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty());
151 let mut rustc = rustc_wrapper
152 .into_iter()
153 .chain(rustc_workspace_wrapper)
154 .chain(iter::once(rustc));
155 let mut cmd = Command::new(rustc.next().unwrap());
156 cmd.args(rustc);
157
158 if !rustc_bootstrap {
159 cmd.env_remove("RUSTC_BOOTSTRAP");
160 }
161
162 cmd.stderr(Stdio::null())
163 .arg("--cfg=anyhow_build_probe")
164 .arg("--edition=2018")
165 .arg("--crate-name=anyhow")
166 .arg("--crate-type=lib")
167 .arg("--cap-lints=allow")
168 .arg("--emit=dep-info,metadata")
169 .arg("--out-dir")
170 .arg(&out_subdir)
171 .arg(probefile);
172
173 if let Some(target) = env::var_os("TARGET") {
174 cmd.arg("--target").arg(target);
175 }
176
177 // If Cargo wants to set RUSTFLAGS, use that.
178 if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
179 if !rustflags.is_empty() {
180 for arg in rustflags.split('\x1f') {
181 cmd.arg(arg);
182 }
183 }
184 }
185
186 let success = match cmd.status() {
187 Ok(status) => status.success(),
188 Err(_) => false,
189 };
190
191 // Clean up to avoid leaving nondeterministic absolute paths in the dep-info
192 // file in OUT_DIR, which causes nonreproducible builds in build systems
193 // that treat the entire OUT_DIR as an artifact.
194 if let Err(err) = fs::remove_dir_all(&out_subdir) {
195 // libc::ENOTEMPTY
196 // Some filesystems (NFSv3) have timing issues under load where '.nfs*'
197 // dummy files can continue to get created for a short period after the
198 // probe command completes, breaking remove_dir_all.
199 // To be replaced with ErrorKind::DirectoryNotEmpty (Rust 1.83+).
200 const ENOTEMPTY: i32 = 39;
201
202 if !(err.kind() == ErrorKind::NotFound
203 || (cfg!(target_os = "linux") && err.raw_os_error() == Some(ENOTEMPTY)))
204 {
205 eprintln!("Failed to clean up {}: {}", out_subdir.display(), err);
206 process::exit(1);
207 }
208 }
209
210 success
211 }
212
213 fn rustc_minor_version() -> Option<u32> {
214 let rustc = cargo_env_var("RUSTC");
215 let output = Command::new(rustc).arg("--version").output().ok()?;
216 let version = str::from_utf8(&output.stdout).ok()?;
217 let mut pieces = version.split('.');
218 if pieces.next() != Some("rustc 1") {
219 return None;
220 }
221 pieces.next()?.parse().ok()
222 }
223
224 fn cargo_env_var(key: &str) -> OsString {
225 env::var_os(key).unwrap_or_else(|| {
226 eprintln!(
227 "Environment variable ${} is not set during execution of build script",
228 key,
229 );
230 process::exit(1);
231 })
232 }