Skip to content

Commit

Permalink
Merge pull request #273 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 30, 2023
2 parents facaaf6 + 9b7356f commit c1c003f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::needless_raw_string_hashes)]

use std::env;
use std::ffi::OsString;
use std::path::Path;
use std::process::{Command, ExitStatus, Stdio};
use std::process::{self, Command, ExitStatus, Stdio};

fn main() {
println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
Expand All @@ -25,8 +26,8 @@ fn compile_probe() -> Option<ExitStatus> {
return None;
}

let rustc = env::var_os("RUSTC")?;
let out_dir = env::var_os("OUT_DIR")?;
let rustc = cargo_env_var("RUSTC");
let out_dir = cargo_env_var("OUT_DIR");
let probefile = Path::new("build").join("probe.rs");

// Make sure to pick up Cargo rustc configuration.
Expand Down Expand Up @@ -63,3 +64,13 @@ fn compile_probe() -> Option<ExitStatus> {

cmd.status().ok()
}

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 c1c003f

Please sign in to comment.