run_make_support/
env.rs

1use std::ffi::OsString;
2
3#[track_caller]
4#[must_use]
5pub fn env_var(name: &str) -> String {
6    match std::env::var(name) {
7        Ok(v) => v,
8        Err(err) => panic!("failed to retrieve environment variable {name:?}: {err:?}"),
9    }
10}
11
12#[track_caller]
13#[must_use]
14pub fn env_var_os(name: &str) -> OsString {
15    match std::env::var_os(name) {
16        Some(v) => v,
17        None => panic!("failed to retrieve environment variable {name:?}"),
18    }
19}
20
21/// Check if staged `rustc`-under-test was built with debug assertions.
22#[track_caller]
23#[must_use]
24pub fn rustc_debug_assertions_enabled() -> bool {
25    // Note: we assume this env var is set when the test recipe is being executed.
26    std::env::var_os("__RUSTC_DEBUG_ASSERTIONS_ENABLED").is_some()
27}
28
29/// Check if staged `std`-under-test was built with debug assertions.
30#[track_caller]
31#[must_use]
32pub fn std_debug_assertions_enabled() -> bool {
33    // Note: we assume this env var is set when the test recipe is being executed.
34    std::env::var_os("__STD_DEBUG_ASSERTIONS_ENABLED").is_some()
35}
36
37/// Check if staged `std`-under-test was built with remapping of it's sources.
38#[track_caller]
39#[must_use]
40pub fn std_remap_debuginfo_enabled() -> bool {
41    // Note: we assume this env var is set when the test recipe is being executed.
42    std::env::var_os("__STD_REMAP_DEBUGINFO_ENABLED").is_some()
43}
44
45/// A wrapper around [`std::env::set_current_dir`] which includes the directory
46/// path in the panic message.
47#[track_caller]
48pub fn set_current_dir<P: AsRef<std::path::Path>>(dir: P) {
49    std::env::set_current_dir(dir.as_ref())
50        .expect(&format!("could not set current directory to \"{}\"", dir.as_ref().display()));
51}
52
53/// Number of parallel jobs bootstrap was configured with.
54///
55/// This may fallback to [`std::thread::available_parallelism`] when no explicit jobs count has been
56/// configured. Refer to bootstrap's jobs fallback logic.
57#[track_caller]
58pub fn jobs() -> u32 {
59    std::env::var_os("__BOOTSTRAP_JOBS")
60        .expect("`__BOOTSTRAP_JOBS` must be set by `compiletest`")
61        .to_str()
62        .expect("`__BOOTSTRAP_JOBS` must be a valid string")
63        .parse::<u32>()
64        .expect("`__BOOTSTRAP_JOBS` must be a valid `u32`")
65}