From 867335b6a95e4f441368f1b950b48874a6bc60f3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 30 Dec 2023 11:46:48 -0800 Subject: [PATCH] Improve failure if cargo promised environment variable not present --- build.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 893d2a927d..f14ce219dd 100644 --- a/build.rs +++ b/build.rs @@ -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 @@ -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") { @@ -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); + }) +}