Skip to content

Commit 16bc569

Browse files
committed
fix: use BTreeSet for std-features; add unit tests
* fix formatting of string in front of std_features * rename `std_features` to `std-features` in config.toml
1 parent 236e5b0 commit 16bc569

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

config.example.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@
757757
#validate-mir-opts = 3
758758

759759
# Configure `std` features used during bootstrap
760-
# std_features = ["panic_unwind"]
760+
# std-features = ["panic_unwind", "backtrace"]
761761

762762
# =============================================================================
763763
# Options for specific targets

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
557557
.arg("--features")
558558
.arg(features);
559559
} else {
560-
features += &builder.std_features(target);
560+
let builder_std_features = format!(" {}", &builder.std_features(target));
561+
562+
features += &builder_std_features;
561563
features.push_str(compiler_builtins_c_feature);
562564

563565
cargo

src/bootstrap/src/core/config/config.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! how the build runs.
55
66
use std::cell::{Cell, RefCell};
7-
use std::collections::{HashMap, HashSet};
7+
use std::collections::{BTreeSet, HashMap, HashSet};
88
use std::fmt::{self, Display};
99
use std::io::IsTerminal;
1010
use std::path::{absolute, Path, PathBuf};
@@ -287,7 +287,7 @@ pub struct Config {
287287
pub rust_profile_generate: Option<String>,
288288
pub rust_lto: RustcLto,
289289
pub rust_validate_mir_opts: Option<u32>,
290-
pub rust_std_features: Option<Vec<String>>,
290+
pub rust_std_features: Option<BTreeSet<String>>,
291291
pub llvm_profile_use: Option<String>,
292292
pub llvm_profile_generate: bool,
293293
pub llvm_libunwind_default: Option<LlvmLibunwind>,
@@ -1142,7 +1142,7 @@ define_config! {
11421142
download_rustc: Option<StringOrBool> = "download-rustc",
11431143
lto: Option<String> = "lto",
11441144
validate_mir_opts: Option<u32> = "validate-mir-opts",
1145-
std_features: Option<Vec<String>> = "std_features",
1145+
std_features: Option<Vec<String>> = "std-features",
11461146
}
11471147
}
11481148

@@ -2109,7 +2109,9 @@ impl Config {
21092109
}
21102110

21112111
// std_features chosen during bootstrap
2112-
config.rust_std_features = std_features;
2112+
if let Some(std_features) = std_features {
2113+
config.rust_std_features = Some(BTreeSet::from_iter(std_features.into_iter()));
2114+
}
21132115

21142116
let default = debug == Some(true);
21152117
config.rust_debug_assertions = debug_assertions.unwrap_or(default);

src/bootstrap/src/core/config/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::BTreeSet;
12
use std::env;
23
use std::fs::{remove_file, File};
34
use std::io::Write;
@@ -317,3 +318,24 @@ fn order_of_clippy_rules() {
317318

318319
assert_eq!(expected, actual);
319320
}
321+
322+
#[test]
323+
fn parse_rust_std_features() {
324+
let config = parse("rust.std-features = [\"panic-unwind\", \"backtrace\"]");
325+
let expected_features: BTreeSet<String> =
326+
["panic-unwind", "backtrace"].into_iter().map(|s| s.to_string()).collect();
327+
assert_eq!(config.rust_std_features, Some(expected_features));
328+
}
329+
330+
#[test]
331+
fn parse_rust_std_features_empty() {
332+
let config = parse("rust.std-features = []");
333+
let expected_features: BTreeSet<String> = BTreeSet::new();
334+
assert_eq!(config.rust_std_features, Some(expected_features));
335+
}
336+
337+
#[test]
338+
#[should_panic]
339+
fn parse_rust_std_features_invalid() {
340+
parse("rust.std-features = \"backtrace\"");
341+
}

src/bootstrap/src/lib.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! also check out the `src/bootstrap/README.md` file for more information.
1818
1919
use std::cell::{Cell, RefCell};
20-
use std::collections::{HashMap, HashSet};
20+
use std::collections::{BTreeSet, HashMap, HashSet};
2121
use std::fmt::Display;
2222
use std::fs::{self, File};
2323
use std::path::{Path, PathBuf};
@@ -654,38 +654,35 @@ impl Build {
654654
&self.config.rust_info
655655
}
656656

657-
/// Gets the space-separated set of activated features for the standard
658-
/// library. This can be configured with the `std_features` key in config.toml.
657+
/// Gets the space-separated set of activated features for the standard ibrary. This can be configured with the `std-features` key in config.toml.
659658
fn std_features(&self, target: TargetSelection) -> String {
660-
let mut features = if let Some(features) = &self.config.rust_std_features {
661-
features.iter().map(|s| s.as_ref()).collect::<Vec<&str>>()
659+
let mut features: BTreeSet<&str> = if let Some(features) = &self.config.rust_std_features {
660+
features.iter().map(|s| s.as_str()).collect()
662661
} else {
663-
vec![]
662+
BTreeSet::new()
664663
};
665664

666-
if !features.contains(&"panic-unwind") {
667-
features.push("panic-unwind");
668-
}
665+
features.insert("panic-unwind");
669666

670667
match self.config.llvm_libunwind(target) {
671-
LlvmLibunwind::InTree => features.push("llvm-libunwind"),
672-
LlvmLibunwind::System => features.push("system-llvm-libunwind"),
673-
LlvmLibunwind::No => {}
674-
}
668+
LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
669+
LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
670+
LlvmLibunwind::No => false,
671+
};
672+
675673
if self.config.backtrace {
676-
features.push("backtrace");
674+
features.insert("backtrace");
677675
}
678676
if self.config.profiler_enabled(target) {
679-
features.push("profiler");
677+
features.insert("profiler");
680678
}
681679
// Generate memcpy, etc. FIXME: Remove this once compiler-builtins
682680
// automatically detects this target.
683681
if target.contains("zkvm") {
684-
features.push("compiler-builtins-mem");
682+
features.insert("compiler-builtins-mem");
685683
}
686684

687-
// remove duplicates
688-
features.into_iter().collect::<HashSet<_>>().into_iter().collect::<Vec<_>>().join(" ")
685+
features.into_iter().collect::<Vec<_>>().join(" ")
689686
}
690687

691688
/// Gets the space-separated set of activated features for the compiler.

0 commit comments

Comments
 (0)