Skip to content

Commit fd3bcaa

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 6198823 commit fd3bcaa

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

config.example.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@
760760
#validate-mir-opts = 3
761761

762762
# Configure `std` features used during bootstrap
763-
# std_features = ["panic_unwind"]
763+
# std-features = ["panic_unwind", "backtrace"]
764764

765765
# =============================================================================
766766
# Options for specific targets

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,9 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
553553
.arg("--features")
554554
.arg(features);
555555
} else {
556-
features += &builder.std_features(target);
556+
let builder_std_features = format!(" {}", &builder.std_features(target));
557+
558+
features += &builder_std_features;
557559
features.push_str(compiler_builtins_c_feature);
558560

559561
cargo

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

+6-4
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

@@ -2097,7 +2097,9 @@ impl Config {
20972097
}
20982098

20992099
// std_features chosen during bootstrap
2100-
config.rust_std_features = std_features;
2100+
if let Some(std_features) = std_features {
2101+
config.rust_std_features = Some(BTreeSet::from_iter(std_features.into_iter()));
2102+
}
21012103

21022104
let default = debug == Some(true);
21032105
config.rust_debug_assertions = debug_assertions.unwrap_or(default);

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

+22
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;
@@ -326,3 +327,24 @@ fn verbose_tests_default_value() {
326327
let config = Config::parse(Flags::parse(&["build".into(), "compiler".into(), "-v".into()]));
327328
assert_eq!(config.verbose_tests, true);
328329
}
330+
331+
#[test]
332+
fn parse_rust_std_features() {
333+
let config = parse("rust.std-features = [\"panic-unwind\", \"backtrace\"]");
334+
let expected_features: BTreeSet<String> =
335+
["panic-unwind", "backtrace"].into_iter().map(|s| s.to_string()).collect();
336+
assert_eq!(config.rust_std_features, Some(expected_features));
337+
}
338+
339+
#[test]
340+
fn parse_rust_std_features_empty() {
341+
let config = parse("rust.std-features = []");
342+
let expected_features: BTreeSet<String> = BTreeSet::new();
343+
assert_eq!(config.rust_std_features, Some(expected_features));
344+
}
345+
346+
#[test]
347+
#[should_panic]
348+
fn parse_rust_std_features_invalid() {
349+
parse("rust.std-features = \"backtrace\"");
350+
}

src/bootstrap/src/lib.rs

+15-18
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};
@@ -661,38 +661,35 @@ impl Build {
661661
&self.config.rust_info
662662
}
663663

664-
/// Gets the space-separated set of activated features for the standard
665-
/// library. This can be configured with the `std_features` key in config.toml.
664+
/// Gets the space-separated set of activated features for the standard ibrary. This can be configured with the `std-features` key in config.toml.
666665
fn std_features(&self, target: TargetSelection) -> String {
667-
let mut features = if let Some(features) = &self.config.rust_std_features {
668-
features.iter().map(|s| s.as_ref()).collect::<Vec<&str>>()
666+
let mut features: BTreeSet<&str> = if let Some(features) = &self.config.rust_std_features {
667+
features.iter().map(|s| s.as_str()).collect()
669668
} else {
670-
vec![]
669+
BTreeSet::new()
671670
};
672671

673-
if !features.contains(&"panic-unwind") {
674-
features.push("panic-unwind");
675-
}
672+
features.insert("panic-unwind");
676673

677674
match self.config.llvm_libunwind(target) {
678-
LlvmLibunwind::InTree => features.push("llvm-libunwind"),
679-
LlvmLibunwind::System => features.push("system-llvm-libunwind"),
680-
LlvmLibunwind::No => {}
681-
}
675+
LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
676+
LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
677+
LlvmLibunwind::No => false,
678+
};
679+
682680
if self.config.backtrace {
683-
features.push("backtrace");
681+
features.insert("backtrace");
684682
}
685683
if self.config.profiler_enabled(target) {
686-
features.push("profiler");
684+
features.insert("profiler");
687685
}
688686
// Generate memcpy, etc. FIXME: Remove this once compiler-builtins
689687
// automatically detects this target.
690688
if target.contains("zkvm") {
691-
features.push("compiler-builtins-mem");
689+
features.insert("compiler-builtins-mem");
692690
}
693691

694-
// remove duplicates
695-
features.into_iter().collect::<HashSet<_>>().into_iter().collect::<Vec<_>>().join(" ")
692+
features.into_iter().collect::<Vec<_>>().join(" ")
696693
}
697694

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

0 commit comments

Comments
 (0)