diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs index e85753a351232..4e20babc55a68 100644 --- a/src/bootstrap/src/core/builder.rs +++ b/src/bootstrap/src/core/builder.rs @@ -21,7 +21,7 @@ use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection}; use crate::prepare_behaviour_dump_dir; use crate::utils::cache::{Cache, Interned, INTERNER}; use crate::utils::helpers::{self, add_dylib_path, add_link_lib_path, exe, linker_args}; -use crate::utils::helpers::{libdir, linker_flags, output, t, LldThreads}; +use crate::utils::helpers::{check_cfg_arg, libdir, linker_flags, output, t, LldThreads}; use crate::EXTRA_CHECK_CFGS; use crate::{Build, CLang, Crate, DocTests, GitRepo, Mode}; @@ -1467,18 +1467,7 @@ impl<'a> Builder<'a> { rustflags.arg("-Zunstable-options"); for (restricted_mode, name, values) in EXTRA_CHECK_CFGS { if *restricted_mode == None || *restricted_mode == Some(mode) { - // Creating a string of the values by concatenating each value: - // ',"tvos","watchos"' or '' (nothing) when there are no values - let values = match values { - Some(values) => values - .iter() - .map(|val| [",", "\"", val, "\""]) - .flatten() - .collect::(), - None => String::new(), - }; - let values = values.strip_prefix(",").unwrap_or(&values); // remove the first `,` - rustflags.arg(&format!("--check-cfg=cfg({name},values({values}))")); + rustflags.arg(&check_cfg_arg(name, *values)); } } diff --git a/src/bootstrap/src/tests/helpers.rs b/src/bootstrap/src/tests/helpers.rs index afe18aebafada..163594dbb2f14 100644 --- a/src/bootstrap/src/tests/helpers.rs +++ b/src/bootstrap/src/tests/helpers.rs @@ -1,4 +1,4 @@ -use crate::utils::helpers::{extract_beta_rev, hex_encode, make}; +use crate::utils::helpers::{extract_beta_rev, hex_encode, make, check_cfg_arg}; use std::path::PathBuf; #[test] @@ -57,3 +57,16 @@ fn test_string_to_hex_encode() { let hex_string = hex_encode(input_string); assert_eq!(hex_string, "48656c6c6f2c20576f726c6421"); } + +#[test] +fn test_check_cfg_arg() { + assert_eq!(check_cfg_arg("bootstrap", None), "--check-cfg=cfg(bootstrap)"); + assert_eq!( + check_cfg_arg("target_arch", Some(&["s360"])), + "--check-cfg=cfg(target_arch,values(\"s360\"))" + ); + assert_eq!( + check_cfg_arg("target_os", Some(&["nixos", "nix2"])), + "--check-cfg=cfg(target_os,values(\"nixos\",\"nix2\"))" + ); +} diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 0c4297db6cc7b..0c917c3d57933 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -552,3 +552,22 @@ where { input.as_ref().iter().map(|x| format!("{:02x}", x)).collect() } + +/// Create a `--check-cfg` argument invocation for a given name +/// and it's values. +pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String { + // Creating a string of the values by concatenating each value: + // ',values("tvos","watchos")' or '' (nothing) when there are no values. + let next = match values { + Some(values) => { + let mut tmp = + values.iter().map(|val| [",", "\"", val, "\""]).flatten().collect::(); + + tmp.insert_str(1, "values("); + tmp.push_str(")"); + tmp + } + None => "".to_string(), + }; + format!("--check-cfg=cfg({name}{next})") +}