2 use std::ffi::OsString;
4 use std::process::{self, Command, Stdio};
7 println!("cargo:rerun-if-changed=build/probe.rs");
9 let error_generic_member_access;
10 let consider_rustc_bootstrap;
11 if compile_probe(false) {
12 // This is a nightly or dev compiler, so it supports unstable features
13 // regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
14 // RUSTC_BOOTSTRAP is changed.
15 error_generic_member_access = true;
16 consider_rustc_bootstrap = false;
17 } else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
18 if compile_probe(true) {
19 // This is a stable or beta compiler for which the user has set
20 // RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
22 error_generic_member_access = true;
23 consider_rustc_bootstrap = true;
24 } else if rustc_bootstrap == "1" {
25 // This compiler does not support the generic member access API in
26 // the form that thiserror expects. No need to pay attention to
28 error_generic_member_access = false;
29 consider_rustc_bootstrap = false;
31 // This is a stable or beta compiler for which RUSTC_BOOTSTRAP is
32 // set to restrict the use of unstable features by this crate.
33 error_generic_member_access = false;
34 consider_rustc_bootstrap = true;
37 // Without RUSTC_BOOTSTRAP, this compiler does not support the generic
38 // member access API in the form that thiserror expects, but try again
39 // if the user turns on unstable features.
40 error_generic_member_access = false;
41 consider_rustc_bootstrap = true;
44 if error_generic_member_access {
45 println!("cargo:rustc-cfg=error_generic_member_access");
48 if consider_rustc_bootstrap {
49 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
53 fn compile_probe(rustc_bootstrap: bool) -> bool {
54 if env::var_os("RUSTC_STAGE").is_some() {
55 // We are running inside rustc bootstrap. This is a highly non-standard
56 // environment with issues such as:
58 // https://github.com/rust-lang/cargo/issues/11138
59 // https://github.com/rust-lang/rust/issues/114839
61 // Let's just not use nightly features here.
65 let rustc = cargo_env_var("RUSTC");
66 let out_dir = cargo_env_var("OUT_DIR");
67 let probefile = Path::new("build").join("probe.rs");
69 // Make sure to pick up Cargo rustc configuration.
70 let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
71 let mut cmd = Command::new(wrapper);
72 // The wrapper's first argument is supposed to be the path to rustc.
80 cmd.env_remove("RUSTC_BOOTSTRAP");
83 cmd.stderr(Stdio::null())
84 .arg("--edition=2018")
85 .arg("--crate-name=thiserror")
86 .arg("--crate-type=lib")
87 .arg("--emit=dep-info,metadata")
92 if let Some(target) = env::var_os("TARGET") {
93 cmd.arg("--target").arg(target);
96 // If Cargo wants to set RUSTFLAGS, use that.
97 if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
98 if !rustflags.is_empty() {
99 for arg in rustflags.split('\x1f') {
106 Ok(status) => status.success(),
111 fn cargo_env_var(key: &str) -> OsString {
112 env::var_os(key).unwrap_or_else(|| {
114 "Environment variable ${} is not set during execution of build script",