Debian Codegraph @

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