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