Skip to content

Commit

Permalink
Merge pull request #428 from dtolnay/cargoenvvar
Browse files Browse the repository at this point in the history
Require cargo promised environment variables to be present
  • Loading branch information
dtolnay authored Dec 31, 2023
2 parents 593681b + 8ade7da commit 784ae2e
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
Expand Up @@ -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;

Expand Down Expand Up @@ -89,7 +90,7 @@ struct RustcVersion {
}

fn rustc_version() -> Option<RustcVersion> {
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");
Expand Down Expand Up @@ -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);
})
}

0 comments on commit 784ae2e

Please sign in to comment.