Skip to content

Commit fcfe05a

Browse files
committed
Auto merge of #119441 - Urgau:check-cfg-simplify-bootstrap-args, r=onur-ozkan
Simplify bootstrap `--check-cfg` arguments This PR simplifies the generated check-cfg arguments generated for the no-values case. For the `bootstrap` cfg: ```diff - --check-cfg=cfg(bootstrap,values()) + --check-cfg=cfg(bootstrap) ``` Those are equivalent, so there isn't any semantic difference; but the invocation is short and less distracting. `@rustbot` label +F-check-cfg
2 parents 64d5515 + a236bdd commit fcfe05a

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Diff for: src/bootstrap/src/core/builder.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection};
2121
use crate::prepare_behaviour_dump_dir;
2222
use crate::utils::cache::{Cache, Interned, INTERNER};
2323
use crate::utils::helpers::{self, add_dylib_path, add_link_lib_path, exe, linker_args};
24-
use crate::utils::helpers::{libdir, linker_flags, output, t, LldThreads};
24+
use crate::utils::helpers::{check_cfg_arg, libdir, linker_flags, output, t, LldThreads};
2525
use crate::EXTRA_CHECK_CFGS;
2626
use crate::{Build, CLang, Crate, DocTests, GitRepo, Mode};
2727

@@ -1467,18 +1467,7 @@ impl<'a> Builder<'a> {
14671467
rustflags.arg("-Zunstable-options");
14681468
for (restricted_mode, name, values) in EXTRA_CHECK_CFGS {
14691469
if *restricted_mode == None || *restricted_mode == Some(mode) {
1470-
// Creating a string of the values by concatenating each value:
1471-
// ',"tvos","watchos"' or '' (nothing) when there are no values
1472-
let values = match values {
1473-
Some(values) => values
1474-
.iter()
1475-
.map(|val| [",", "\"", val, "\""])
1476-
.flatten()
1477-
.collect::<String>(),
1478-
None => String::new(),
1479-
};
1480-
let values = values.strip_prefix(",").unwrap_or(&values); // remove the first `,`
1481-
rustflags.arg(&format!("--check-cfg=cfg({name},values({values}))"));
1470+
rustflags.arg(&check_cfg_arg(name, *values));
14821471
}
14831472
}
14841473

Diff for: src/bootstrap/src/tests/helpers.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::helpers::{extract_beta_rev, hex_encode, make};
1+
use crate::utils::helpers::{extract_beta_rev, hex_encode, make, check_cfg_arg};
22
use std::path::PathBuf;
33

44
#[test]
@@ -57,3 +57,16 @@ fn test_string_to_hex_encode() {
5757
let hex_string = hex_encode(input_string);
5858
assert_eq!(hex_string, "48656c6c6f2c20576f726c6421");
5959
}
60+
61+
#[test]
62+
fn test_check_cfg_arg() {
63+
assert_eq!(check_cfg_arg("bootstrap", None), "--check-cfg=cfg(bootstrap)");
64+
assert_eq!(
65+
check_cfg_arg("target_arch", Some(&["s360"])),
66+
"--check-cfg=cfg(target_arch,values(\"s360\"))"
67+
);
68+
assert_eq!(
69+
check_cfg_arg("target_os", Some(&["nixos", "nix2"])),
70+
"--check-cfg=cfg(target_os,values(\"nixos\",\"nix2\"))"
71+
);
72+
}

Diff for: src/bootstrap/src/utils/helpers.rs

+19
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,22 @@ where
552552
{
553553
input.as_ref().iter().map(|x| format!("{:02x}", x)).collect()
554554
}
555+
556+
/// Create a `--check-cfg` argument invocation for a given name
557+
/// and it's values.
558+
pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
559+
// Creating a string of the values by concatenating each value:
560+
// ',values("tvos","watchos")' or '' (nothing) when there are no values.
561+
let next = match values {
562+
Some(values) => {
563+
let mut tmp =
564+
values.iter().map(|val| [",", "\"", val, "\""]).flatten().collect::<String>();
565+
566+
tmp.insert_str(1, "values(");
567+
tmp.push_str(")");
568+
tmp
569+
}
570+
None => "".to_string(),
571+
};
572+
format!("--check-cfg=cfg({name}{next})")
573+
}

0 commit comments

Comments
 (0)