Skip to content

Commit

Permalink
Merge pull request #1563 from dtolnay/cargoenvvar
Browse files Browse the repository at this point in the history
Improve failure if cargo promised environment variable not present
  • Loading branch information
dtolnay authored Dec 30, 2023
2 parents c4d1bd6 + 867335b commit 8aa9afb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::process::{Command, Stdio};
use std::ffi::OsString;
use std::process::{self, Command, Stdio};

// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
Expand All @@ -16,7 +17,7 @@ fn main() {
}

fn unstable() -> bool {
let rustc = env::var_os("RUSTC").unwrap();
let rustc = cargo_env_var("RUSTC");

// Pick up Cargo rustc configuration.
let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
Expand Down Expand Up @@ -68,3 +69,13 @@ fn unstable() -> bool {
Err(_) => false,
}
}

fn cargo_env_var(key: &str) -> OsString {
env::var_os(key).unwrap_or_else(|| {
eprintln!(
"Environment variable ${} is not set during execution of build script",
key,
);
process::exit(1);
})
}

0 comments on commit 8aa9afb

Please sign in to comment.