diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index fb7b85ee0bb..d816cb1989f 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -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; diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index d157610add2..f56fe4695d1 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -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); } diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 081197bc92f..1f7cc994be9 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -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 = OnceLock::new(); diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 021763fdb37..8e881f1bf77 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -571,6 +571,25 @@ pub fn set_file_times>(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. diff --git a/src/bootstrap/src/utils/helpers/tests.rs b/src/bootstrap/src/utils/helpers/tests.rs index cfb1dde2d6e..53d56169eb0 100644 --- a/src/bootstrap/src/utils/helpers/tests.rs +++ b/src/bootstrap/src/utils/helpers/tests.rs @@ -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:?}"); + } +}