From 8ade7dacec5fbf122f1a360301e80dd233be4d51 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 30 Dec 2023 16:41:11 -0800 Subject: [PATCH] Require cargo promised environment variables to be present --- build.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 9f0fb51..35991ed 100644 --- a/build.rs +++ b/build.rs @@ -35,7 +35,8 @@ // 1.57+. use std::env; -use std::process::Command; +use std::ffi::OsString; +use std::process::{self, Command}; use std::str; use std::u32; @@ -89,7 +90,7 @@ struct RustcVersion { } fn rustc_version() -> Option { - let rustc = env::var_os("RUSTC")?; + let rustc = cargo_env_var("RUSTC"); let output = Command::new(rustc).arg("--version").output().ok()?; let version = str::from_utf8(&output.stdout).ok()?; let nightly = version.contains("nightly") || version.contains("dev"); @@ -131,3 +132,13 @@ fn feature_allowed(feature: &str) -> bool { // No allow-features= flag, allowed by default. true } + +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); + }) +}