Skip to content

Commit 7e993b2

Browse files
Create run-make env_var and env_var_os helpers
1 parent 30ea1a2 commit 7e993b2

File tree

9 files changed

+51
-42
lines changed

9 files changed

+51
-42
lines changed

src/tools/run-make-support/src/cc.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::env;
21
use std::path::Path;
32
use std::process::Command;
43

5-
use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname};
4+
use crate::{
5+
bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, tmp_dir, uname,
6+
};
67

78
/// Construct a new platform-specific C compiler invocation.
89
///
@@ -27,11 +28,11 @@ impl Cc {
2728
/// WARNING: This means that what flags are accepted by the underlying C compile is
2829
/// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`.
2930
pub fn new() -> Self {
30-
let compiler = env::var("CC").unwrap();
31+
let compiler = env_var("CC");
3132

3233
let mut cmd = Command::new(compiler);
3334

34-
let default_cflags = env::var("CC_DEFAULT_FLAGS").unwrap();
35+
let default_cflags = env_var("CC_DEFAULT_FLAGS");
3536
for flag in default_cflags.split(char::is_whitespace) {
3637
cmd.arg(flag);
3738
}

src/tools/run-make-support/src/clang.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::env;
21
use std::path::Path;
32
use std::process::Command;
43

5-
use crate::{bin_name, handle_failed_output, tmp_dir};
4+
use crate::{bin_name, env_var, handle_failed_output, tmp_dir};
65

76
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
87
pub fn clang() -> Clang {
@@ -20,8 +19,7 @@ crate::impl_common_helpers!(Clang);
2019
impl Clang {
2120
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
2221
pub fn new() -> Self {
23-
let clang =
24-
env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`");
22+
let clang = env_var("CLANG");
2523
let cmd = Command::new(clang);
2624
Self { cmd }
2725
}

src/tools/run-make-support/src/lib.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod rustc;
1212
pub mod rustdoc;
1313

1414
use std::env;
15+
use std::ffi::OsString;
1516
use std::fs;
1617
use std::io;
1718
use std::path::{Path, PathBuf};
@@ -30,14 +31,28 @@ pub use run::{run, run_fail};
3031
pub use rustc::{aux_build, rustc, Rustc};
3132
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
3233

34+
pub fn env_var(name: &str) -> String {
35+
match env::var(name) {
36+
Ok(v) => v,
37+
Err(err) => panic!("failed to retrieve environment variable {name:?}: {err:?}"),
38+
}
39+
}
40+
41+
pub fn env_var_os(name: &str) -> OsString {
42+
match env::var_os(name) {
43+
Some(v) => v,
44+
None => panic!("failed to retrieve environment variable {name:?}"),
45+
}
46+
}
47+
3348
/// Path of `TMPDIR` (a temporary build directory, not under `/tmp`).
3449
pub fn tmp_dir() -> PathBuf {
35-
env::var_os("TMPDIR").unwrap().into()
50+
env_var_os("TMPDIR").into()
3651
}
3752

3853
/// `TARGET`
3954
pub fn target() -> String {
40-
env::var("TARGET").unwrap()
55+
env_var("TARGET")
4156
}
4257

4358
/// Check if target is windows-like.
@@ -62,7 +77,7 @@ pub fn static_lib(name: &str) -> PathBuf {
6277
}
6378

6479
pub fn python_command() -> Command {
65-
let python_path = std::env::var("PYTHON").expect("PYTHON environment variable does not exist");
80+
let python_path = env_var("PYTHON");
6681
Command::new(python_path)
6782
}
6883

@@ -73,7 +88,7 @@ pub fn htmldocck() -> Command {
7388
}
7489

7590
pub fn source_path() -> PathBuf {
76-
std::env::var("S").expect("S variable does not exist").into()
91+
env_var("S").into()
7792
}
7893

7994
/// Construct the static library name based on the platform.
@@ -208,12 +223,12 @@ fn handle_failed_output(cmd: &Command, output: Output, caller_line_number: u32)
208223

209224
/// Set the runtime library path as needed for running the host rustc/rustdoc/etc.
210225
pub fn set_host_rpath(cmd: &mut Command) {
211-
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
226+
let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
212227
cmd.env(&ld_lib_path_envvar, {
213228
let mut paths = vec![];
214-
paths.push(PathBuf::from(env::var("TMPDIR").unwrap()));
215-
paths.push(PathBuf::from(env::var("HOST_RPATH_DIR").unwrap()));
216-
for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) {
229+
paths.push(PathBuf::from(env_var("TMPDIR")));
230+
paths.push(PathBuf::from(env_var("HOST_RPATH_DIR")));
231+
for p in env::split_paths(&env_var(&ld_lib_path_envvar)) {
217232
paths.push(p.to_path_buf());
218233
}
219234
env::join_paths(paths.iter()).unwrap()

src/tools/run-make-support/src/llvm_readobj.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::env;
21
use std::path::{Path, PathBuf};
32
use std::process::Command;
43

5-
use crate::handle_failed_output;
4+
use crate::{env_var, handle_failed_output};
65

76
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
87
/// at `$LLVM_BIN_DIR/llvm-readobj`.
@@ -22,8 +21,7 @@ impl LlvmReadobj {
2221
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
2322
/// at `$LLVM_BIN_DIR/llvm-readobj`.
2423
pub fn new() -> Self {
25-
let llvm_bin_dir = env::var("LLVM_BIN_DIR")
26-
.expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`");
24+
let llvm_bin_dir = env_var("LLVM_BIN_DIR");
2725
let llvm_bin_dir = PathBuf::from(llvm_bin_dir);
2826
let llvm_readobj = llvm_bin_dir.join("llvm-readobj");
2927
let cmd = Command::new(llvm_readobj);

src/tools/run-make-support/src/run.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
44

5-
use crate::is_windows;
5+
use crate::{env_var, is_windows};
66

77
use super::handle_failed_output;
88

99
fn run_common(name: &str) -> (Command, Output) {
1010
let mut bin_path = PathBuf::new();
11-
bin_path.push(env::var("TMPDIR").unwrap());
11+
bin_path.push(env_var("TMPDIR"));
1212
bin_path.push(name);
13-
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
13+
let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
1414
let mut cmd = Command::new(bin_path);
1515
cmd.env(&ld_lib_path_envvar, {
1616
let mut paths = vec![];
17-
paths.push(PathBuf::from(env::var("TMPDIR").unwrap()));
18-
for p in env::split_paths(&env::var("TARGET_RPATH_ENV").unwrap()) {
17+
paths.push(PathBuf::from(env_var("TMPDIR")));
18+
for p in env::split_paths(&env_var("TARGET_RPATH_ENV")) {
1919
paths.push(p.to_path_buf());
2020
}
21-
for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) {
21+
for p in env::split_paths(&env_var(&ld_lib_path_envvar)) {
2222
paths.push(p.to_path_buf());
2323
}
2424
env::join_paths(paths.iter()).unwrap()
@@ -29,7 +29,7 @@ fn run_common(name: &str) -> (Command, Output) {
2929
for p in env::split_paths(&std::env::var("PATH").unwrap_or(String::new())) {
3030
paths.push(p.to_path_buf());
3131
}
32-
paths.push(Path::new(&std::env::var("TARGET_RPATH_DIR").unwrap()).to_path_buf());
32+
paths.push(Path::new(&env_var("TARGET_RPATH_DIR")).to_path_buf());
3333
cmd.env("PATH", env::join_paths(paths.iter()).unwrap());
3434
}
3535

src/tools/run-make-support/src/rustc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::env;
21
use std::ffi::{OsStr, OsString};
32
use std::io::Write;
43
use std::path::Path;
54
use std::process::{Command, Output, Stdio};
65

7-
use crate::{handle_failed_output, set_host_rpath, tmp_dir};
6+
use crate::{env_var, handle_failed_output, set_host_rpath, tmp_dir};
87

98
/// Construct a new `rustc` invocation.
109
pub fn rustc() -> Rustc {
@@ -26,7 +25,7 @@ pub struct Rustc {
2625
crate::impl_common_helpers!(Rustc);
2726

2827
fn setup_common() -> Command {
29-
let rustc = env::var("RUSTC").unwrap();
28+
let rustc = env_var("RUSTC");
3029
let mut cmd = Command::new(rustc);
3130
set_host_rpath(&mut cmd);
3231
cmd.arg("--out-dir").arg(tmp_dir()).arg("-L").arg(tmp_dir());

src/tools/run-make-support/src/rustdoc.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::env;
21
use std::ffi::OsStr;
32
use std::io::Write;
43
use std::path::Path;
54
use std::process::{Command, Output, Stdio};
65

7-
use crate::{handle_failed_output, set_host_rpath};
6+
use crate::{env_var, env_var_os, handle_failed_output, set_host_rpath};
87

98
/// Construct a plain `rustdoc` invocation with no flags set.
109
pub fn bare_rustdoc() -> Rustdoc {
@@ -25,7 +24,7 @@ pub struct Rustdoc {
2524
crate::impl_common_helpers!(Rustdoc);
2625

2726
fn setup_common() -> Command {
28-
let rustdoc = env::var("RUSTDOC").unwrap();
27+
let rustdoc = env_var("RUSTDOC");
2928
let mut cmd = Command::new(rustdoc);
3029
set_host_rpath(&mut cmd);
3130
cmd
@@ -41,7 +40,7 @@ impl Rustdoc {
4140
/// Construct a `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set.
4241
pub fn new() -> Self {
4342
let mut cmd = setup_common();
44-
let target_rpath_dir = env::var_os("TARGET_RPATH_DIR").unwrap();
43+
let target_rpath_dir = env_var_os("TARGET_RPATH_DIR");
4544
cmd.arg(format!("-L{}", target_rpath_dir.to_string_lossy()));
4645
Self { cmd, stdin: None }
4746
}

tests/run-make/compiler-builtins/rmake.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
#![deny(warnings)]
1616

17-
use run_make_support::object;
1817
use run_make_support::object::read::archive::ArchiveFile;
1918
use run_make_support::object::read::Object;
2019
use run_make_support::object::ObjectSection;
2120
use run_make_support::object::ObjectSymbol;
2221
use run_make_support::object::RelocationTarget;
2322
use run_make_support::set_host_rpath;
2423
use run_make_support::tmp_dir;
24+
use run_make_support::{env_var, object};
2525
use std::collections::HashSet;
2626

2727
const MANIFEST: &str = r#"
@@ -35,7 +35,7 @@ path = "lib.rs""#;
3535

3636
fn main() {
3737
let target_dir = tmp_dir().join("target");
38-
let target = std::env::var("TARGET").unwrap();
38+
let target = env_var("TARGET");
3939

4040
println!("Testing compiler_builtins for {}", target);
4141

@@ -44,9 +44,9 @@ fn main() {
4444
std::fs::write(&manifest_path, MANIFEST.as_bytes()).unwrap();
4545
std::fs::write(tmp_dir().join("lib.rs"), b"#![no_std]").unwrap();
4646

47-
let path = std::env::var("PATH").unwrap();
48-
let rustc = std::env::var("RUSTC").unwrap();
49-
let bootstrap_cargo = std::env::var("BOOTSTRAP_CARGO").unwrap();
47+
let path = env_var("PATH");
48+
let rustc = env_var("RUSTC");
49+
let bootstrap_cargo = env_var("BOOTSTRAP_CARGO");
5050
let mut cmd = std::process::Command::new(bootstrap_cargo);
5151
cmd.args([
5252
"build",

tests/run-make/windows-binary-no-external-deps/rmake.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
//! a "hello world" application by setting `PATH` to `C:\Windows\System32`.
33
//@ only-windows
44

5-
use run_make_support::{rustc, tmp_dir};
6-
use std::env;
5+
use run_make_support::{env_var, rustc, tmp_dir};
76
use std::path::PathBuf;
87
use std::process::Command;
98

109
fn main() {
1110
rustc().input("hello.rs").run();
1211

13-
let windows_dir = env::var("SystemRoot").unwrap();
12+
let windows_dir = env_var("SystemRoot");
1413
let system32: PathBuf = [&windows_dir, "System32"].iter().collect();
1514
// Note: This does not use the support wrappers so that we can precisely control the PATH
1615
let exe = tmp_dir().join("hello.exe");

0 commit comments

Comments
 (0)