Debian Codegraph @

third_party/rust/thiserror/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 fn main() {
11 println!("cargo:rerun-if-changed=build/probe.rs");
12
13 println!("cargo:rustc-check-cfg=cfg(error_generic_member_access)");
14 println!("cargo:rustc-check-cfg=cfg(thiserror_nightly_testing)");
15 println!("cargo:rustc-check-cfg=cfg(thiserror_no_backtrace_type)");
16
17 let error_generic_member_access;
18 let consider_rustc_bootstrap;
19 if compile_probe(false) {
20 // This is a nightly or dev compiler, so it supports unstable features
21 // regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
22 // RUSTC_BOOTSTRAP is changed.
23 error_generic_member_access = true;
24 consider_rustc_bootstrap = false;
25 } else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
26 if compile_probe(true) {
27 // This is a stable or beta compiler for which the user has set
28 // RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
29 // if they change it.
30 error_generic_member_access = true;
31 consider_rustc_bootstrap = true;
32 } else if rustc_bootstrap == "1" {
33 // This compiler does not support the generic member access API in
34 // the form that thiserror expects. No need to pay attention to
35 // RUSTC_BOOTSTRAP.
36 error_generic_member_access = false;
37 consider_rustc_bootstrap = false;
38 } else {
39 // This is a stable or beta compiler for which RUSTC_BOOTSTRAP is
40 // set to restrict the use of unstable features by this crate.
41 error_generic_member_access = false;
42 consider_rustc_bootstrap = true;
43 }
44 } else {
45 // Without RUSTC_BOOTSTRAP, this compiler does not support the generic
46 // member access API in the form that thiserror expects, but try again
47 // if the user turns on unstable features.
48 error_generic_member_access = false;
49 consider_rustc_bootstrap = true;
50 }
51
52 if error_generic_member_access {
53 println!("cargo:rustc-cfg=error_generic_member_access");
54 }
55
56 if consider_rustc_bootstrap {
57 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
58 }
59
60 // core::error::Error stabilized in Rust 1.81
61 // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror
62 let rustc = rustc_minor_version();
63 if cfg!(not(feature = "std")) && rustc.map_or(false, |rustc| rustc < 81) {
64 println!("cargo:rustc-cfg=feature=\"std\"");
65 }
66
67 let rustc = match rustc {
68 Some(rustc) => rustc,
69 None => return,
70 };
71
72 // std::backtrace::Backtrace stabilized in Rust 1.65
73 // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis
74 if rustc < 65 {
75 println!("cargo:rustc-cfg=thiserror_no_backtrace_type");
76 }
77 }
78
79 fn compile_probe(rustc_bootstrap: bool) -> bool {
80 if env::var_os("RUSTC_STAGE").is_some() {
81 // We are running inside rustc bootstrap. This is a highly non-standard
82 // environment with issues such as:
83 //
84 // https://github.com/rust-lang/cargo/issues/11138
85 // https://github.com/rust-lang/rust/issues/114839
86 //
87 // Let's just not use nightly features here.
88 return false;
89 }
90
91 let rustc = cargo_env_var("RUSTC");
92 let out_dir = cargo_env_var("OUT_DIR");
93 let out_subdir = Path::new(&out_dir).join("probe");
94 let probefile = Path::new("build").join("probe.rs");
95
96 if let Err(err) = fs::create_dir(&out_subdir) {
97 if err.kind() != ErrorKind::AlreadyExists {
98 eprintln!("Failed to create {}: {}", out_subdir.display(), err);
99 process::exit(1);
100 }
101 }
102
103 let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty());
104 let rustc_workspace_wrapper =
105 env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty());
106 let mut rustc = rustc_wrapper
107 .into_iter()
108 .chain(rustc_workspace_wrapper)
109 .chain(iter::once(rustc));
110 let mut cmd = Command::new(rustc.next().unwrap());
111 cmd.args(rustc);
112
113 if !rustc_bootstrap {
114 cmd.env_remove("RUSTC_BOOTSTRAP");
115 }
116
117 cmd.stderr(Stdio::null())
118 .arg("--edition=2018")
119 .arg("--crate-name=thiserror")
120 .arg("--crate-type=lib")
121 .arg("--cap-lints=allow")
122 .arg("--emit=dep-info,metadata")
123 .arg("--out-dir")
124 .arg(&out_subdir)
125 .arg(probefile);
126
127 if let Some(target) = env::var_os("TARGET") {
128 cmd.arg("--target").arg(target);
129 }
130
131 // If Cargo wants to set RUSTFLAGS, use that.
132 if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
133 if !rustflags.is_empty() {
134 for arg in rustflags.split('\x1f') {
135 cmd.arg(arg);
136 }
137 }
138 }
139
140 let success = match cmd.status() {
141 Ok(status) => status.success(),
142 Err(_) => false,
143 };
144
145 // Clean up to avoid leaving nondeterministic absolute paths in the dep-info
146 // file in OUT_DIR, which causes nonreproducible builds in build systems
147 // that treat the entire OUT_DIR as an artifact.
148 if let Err(err) = fs::remove_dir_all(&out_subdir) {
149 if err.kind() != ErrorKind::NotFound {
150 eprintln!("Failed to clean up {}: {}", out_subdir.display(), err);
151 process::exit(1);
152 }
153 }
154
155 success
156 }
157
158 fn rustc_minor_version() -> Option<u32> {
159 let rustc = cargo_env_var("RUSTC");
160 let output = Command::new(rustc).arg("--version").output().ok()?;
161 let version = str::from_utf8(&output.stdout).ok()?;
162 let mut pieces = version.split('.');
163 if pieces.next() != Some("rustc 1") {
164 return None;
165 }
166 pieces.next()?.parse().ok()
167 }
168
169 fn cargo_env_var(key: &str) -> OsString {
170 env::var_os(key).unwrap_or_else(|| {
171 eprintln!("Environment variable ${key} is not set during execution of build script");
172 process::exit(1);
173 })
174 }