Move envify into crate::utils::helpers and add a test

This commit is contained in:
Zalathar
2026-08-16 13:43:53 +10:00
parent 57033a0033
commit af768f6825
5 changed files with 45 additions and 20 deletions

View File

@@ -43,11 +43,11 @@ use crate::utils::build_stamp::{self, BuildStamp};
use crate::utils::exec::{BootstrapCommand, command};
use crate::utils::helpers::{
self, LldThreads, TestFilterCategory, add_dylib_path, add_rustdoc_cargo_linker_args,
dylib_path, dylib_path_var, linker_args, linker_flags, t, target_supports_cranelift_backend,
up_to_date,
dylib_path, dylib_path_var, envify, linker_args, linker_flags, t,
target_supports_cranelift_backend, up_to_date,
};
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
use crate::{CLang, GitRepo, Mode, envify};
use crate::{CLang, GitRepo, Mode};
mod compiletest;
pub mod failed_tests;

View File

@@ -11,8 +11,8 @@ use crate::core::config::toml::pgo::PgoConfig;
use crate::core::config::{CompressDebuginfo, Config, DryRun, SplitDebuginfo, TargetSelection};
use crate::utils::build_stamp;
use crate::utils::exec::{BootstrapCommand, command};
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, linker_flags, t};
use crate::{CLang, GitRepo, Mode, RemapScheme, envify, prepare_behaviour_dump_dir};
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, envify, linker_flags, t};
use crate::{CLang, GitRepo, Mode, RemapScheme, prepare_behaviour_dump_dir};
/// Extra `--check-cfg` to add when building the compiler or tools
/// (Mode restriction, config name, config values (if any))
@@ -56,7 +56,7 @@ impl Rustflags {
self.env(prefix);
// ... and also handle target-specific env RUSTFLAGS if they're configured.
let target_specific = format!("CARGO_TARGET_{}_{}", crate::envify(&self.1.triple), prefix);
let target_specific = format!("CARGO_TARGET_{}_{}", envify(&self.1.triple), prefix);
self.env(&target_specific);
}

View File

@@ -1892,18 +1892,6 @@ fn chmod(path: &Path, perms: u32) {
#[cfg(windows)]
fn chmod(_path: &Path, _perms: u32) {}
fn envify(s: &str) -> String {
// Converting foo-bar to FOO_BAR is a fairly idomatic mapping to an environment variable name.
// We also convert '.' to '_' to fix https://github.com/rust-lang/rust/issues/158090
s.chars()
.map(|c| match c {
'-' | '.' => '_',
c => c,
})
.flat_map(|c| c.to_uppercase())
.collect()
}
/// Ensures that the behavior dump directory is properly initialized.
pub fn prepare_behaviour_dump_dir(build: &Build) {
static INITIALIZED: OnceLock<bool> = OnceLock::new();

View File

@@ -571,6 +571,25 @@ pub fn set_file_times<P: AsRef<Path>>(path: P, times: fs::FileTimes) -> io::Resu
f.set_times(times)
}
/// Converts a target-tuple or other string into
/// [the form expected by cargo environment variable names][cargo-env].
///
/// For example:
/// - `x86_64-unknown-linux-gnu` => `X86_64_UNKNOWN_LINUX_GNU`.
///
/// [cargo-env]: https://doc.rust-lang.org/cargo/reference/config.html#environment-variables
pub(crate) fn envify(s: &str) -> String {
// Converting foo-bar to FOO_BAR is a fairly idomatic mapping to an environment variable name.
// We also convert '.' to '_' to fix https://github.com/rust-lang/rust/issues/158090
s.chars()
.map(|c| match c {
'-' | '.' => '_',
c => c,
})
.flat_map(|c| c.to_uppercase())
.collect()
}
/// Exits the process by calling [`std::process::exit`].
///
/// In CI, extra information will be printed to make failures easier to investigate.

View File

@@ -3,8 +3,8 @@ use std::io::Write;
use std::path::PathBuf;
use crate::utils::helpers::{
check_cfg_arg, extract_beta_rev, hex_encode, make, set_file_times, submodule_path_of_paths,
symlink_dir,
check_cfg_arg, envify, extract_beta_rev, hex_encode, make, set_file_times,
submodule_path_of_paths, symlink_dir,
};
use crate::utils::tests::TestCtx;
@@ -119,3 +119,21 @@ fn test_submodule_path_of() {
// Make sure paths that only share a string prefix with a submodule are not matched.
assert_eq!(submodule_path_of_paths(&submodules, "src/tools/cargo-vendor"), None);
}
#[test]
fn test_envify() {
struct Case {
input: &'static str,
expected: &'static str,
}
let cases = &[
Case { input: "x86_64-unknown-linux-gnu", expected: "X86_64_UNKNOWN_LINUX_GNU" },
// Arbitrary target containing `.` from the tier-3 target list.
Case { input: "thumbv8m.base-none-eabi", expected: "THUMBV8M_BASE_NONE_EABI" },
];
for &Case { input, expected } in cases {
let actual = envify(input);
assert_eq!(actual, expected, "input = {input:?}");
}
}