From df375765b9df80bcde085d2fadaa0724687a3ca4 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 30 Dec 2023 15:13:04 +0100 Subject: [PATCH 1/2] Simplify bootstrap check-cfg arguments --- src/bootstrap/src/core/builder.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs index e85753a351232..9bdcb75485b94 100644 --- a/src/bootstrap/src/core/builder.rs +++ b/src/bootstrap/src/core/builder.rs @@ -1468,17 +1468,22 @@ impl<'a> Builder<'a> { 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(), + // ',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(), }; - let values = values.strip_prefix(",").unwrap_or(&values); // remove the first `,` - rustflags.arg(&format!("--check-cfg=cfg({name},values({values}))")); + rustflags.arg(&format!("--check-cfg=cfg({name}{next})")); } } From a236bdd77f958d3974cb241fa6212ef739558942 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 30 Dec 2023 18:10:28 +0100 Subject: [PATCH 2/2] bootstrap: extract --check-cfg arg generation and add unit tests for it --- src/bootstrap/src/core/builder.rs | 20 ++------------------ src/bootstrap/src/tests/helpers.rs | 15 ++++++++++++++- src/bootstrap/src/utils/helpers.rs | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs index 9bdcb75485b94..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,23 +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: - // ',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(), - }; - rustflags.arg(&format!("--check-cfg=cfg({name}{next})")); + 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})") +}