no bug - Correct some typos in the comments. a=typo-fix
[gecko.git] / third_party / rust / thiserror / build.rs
blob0b995d8ea0882d4c24d8e8a30deb5aaaeae3fc59
1 use std::env;
2 use std::ffi::OsString;
3 use std::path::Path;
4 use std::process::{self, Command, Stdio};
6 fn main() {
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
21             // if they change it.
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
27             // RUSTC_BOOTSTRAP.
28             error_generic_member_access = false;
29             consider_rustc_bootstrap = false;
30         } else {
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;
35         }
36     } else {
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;
42     }
44     if error_generic_member_access {
45         println!("cargo:rustc-cfg=error_generic_member_access");
46     }
48     if consider_rustc_bootstrap {
49         println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
50     }
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:
57         //
58         //     https://github.com/rust-lang/cargo/issues/11138
59         //     https://github.com/rust-lang/rust/issues/114839
60         //
61         // Let's just not use nightly features here.
62         return false;
63     }
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.
73         cmd.arg(rustc);
74         cmd
75     } else {
76         Command::new(rustc)
77     };
79     if !rustc_bootstrap {
80         cmd.env_remove("RUSTC_BOOTSTRAP");
81     }
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")
88         .arg("--out-dir")
89         .arg(out_dir)
90         .arg(probefile);
92     if let Some(target) = env::var_os("TARGET") {
93         cmd.arg("--target").arg(target);
94     }
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') {
100                 cmd.arg(arg);
101             }
102         }
103     }
105     match cmd.status() {
106         Ok(status) => status.success(),
107         Err(_) => false,
108     }
111 fn cargo_env_var(key: &str) -> OsString {
112     env::var_os(key).unwrap_or_else(|| {
113         eprintln!(
114             "Environment variable ${} is not set during execution of build script",
115             key,
116         );
117         process::exit(1);
118     })