Skip to content

Commit 0a9f2ef

Browse files
committed
Auto merge of #8490 - alexcrichton:std-customize-features, r=ehuss
Add a `-Zbuild-std-features` flag This flag is intended to pair with `-Zbuild-std` as necessary to configure the features that libstd is built with. This is highly unlikely to ever be stabilized in any form (unlike `-Zbuild-std` which we'd like to stabilize at some point), but can be useful for experimenting with the standard library. For example today it can be used to test changes to binary size by disabling backtraces. My intention is that we won't need a `--no-default-features` equivalent for libstd, where after rust-lang/rust#74377 is merged we can unconditionally specify default features are disabled but the default set of features lists `default`. That way if users want to override the list *and* include the default feature, they can just be sure to include `default`.
2 parents 8aa332d + 1faf5b9 commit 0a9f2ef

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

src/cargo/core/compiler/standard_lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,18 @@ pub fn resolve_std<'cfg>(
9999
spec_pkgs.push("test".to_string());
100100
let spec = Packages::Packages(spec_pkgs);
101101
let specs = spec.to_package_id_specs(&std_ws)?;
102-
let features = vec!["panic-unwind".to_string(), "backtrace".to_string()];
102+
let features = match &config.cli_unstable().build_std_features {
103+
Some(list) => list.clone(),
104+
None => vec![
105+
"panic-unwind".to_string(),
106+
"backtrace".to_string(),
107+
"default".to_string(),
108+
],
109+
};
103110
// dev_deps setting shouldn't really matter here.
104111
let opts = ResolveOpts::new(
105112
/*dev_deps*/ false, &features, /*all_features*/ false,
106-
/*uses_default_features*/ true,
113+
/*uses_default_features*/ false,
107114
);
108115
let resolve = ops::resolve_ws_with_opts(
109116
&std_ws,

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ pub struct CliUnstable {
350350
pub binary_dep_depinfo: bool,
351351
#[serde(deserialize_with = "deserialize_build_std")]
352352
pub build_std: Option<Vec<String>>,
353+
pub build_std_features: Option<Vec<String>>,
353354
pub timings: Option<Vec<String>>,
354355
pub doctest_xcompile: bool,
355356
pub panic_abort_tests: bool,
@@ -455,6 +456,7 @@ impl CliUnstable {
455456
"build-std" => {
456457
self.build_std = Some(crate::core::compiler::standard_lib::parse_unstable_flag(v))
457458
}
459+
"build-std-features" => self.build_std_features = Some(parse_features(v)),
458460
"timings" => self.timings = Some(parse_timings(v)),
459461
"doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?,
460462
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,

src/doc/src/reference/unstable.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ something doesn't quite work the way you'd like it to, feel free to check out
358358
the [issue tracker](https://github.com/rust-lang/wg-cargo-std-aware/issues) of
359359
the tracking repository, and if it's not there please file a new issue!
360360

361+
### build-std-features
362+
* Tracking Repository: https://github.com/rust-lang/wg-cargo-std-aware
363+
364+
This flag is a sibling to the `-Zbuild-std` feature flag. This will configure
365+
the features enabled for the standard library itself when building the standard
366+
library. The default enabled features, at this time, are `backtrace` and
367+
`panic_unwind`. This flag expects a comma-separated list and, if provided, will
368+
override the default list of features enabled.
369+
361370
### timings
362371
* Tracking Issue: [#7405](https://github.com/rust-lang/cargo/issues/7405)
363372

tests/testsuite/mock-std/src/libstd/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ path = "lib.rs"
99

1010
[dependencies]
1111
registry-dep-using-alloc = { version = "*", features = ['mockbuild'] }
12+
13+
[features]
14+
feature1 = []

tests/testsuite/mock-std/src/libstd/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
pub use std::*;
66

77
#[stable(since = "1.0.0", feature = "dummy")]
8-
pub fn custom_api() {
9-
}
8+
pub fn custom_api() {}
9+
10+
#[cfg(feature = "feature1")]
11+
#[stable(since = "1.0.0", feature = "dummy")]
12+
pub fn conditional_function() {}

tests/testsuite/mock-std/src/libtest/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ path = "lib.rs"
99

1010
[dependencies]
1111
proc_macro = { path = "../libproc_macro" }
12+
std = { path = "../libstd" }
1213
panic_unwind = { path = "../libpanic_unwind" }
1314
compiler_builtins = { path = "../libcompiler_builtins" }
1415
registry-dep-using-std = { version = "*", features = ['mockbuild'] }
1516

1617
[features]
1718
panic-unwind = []
1819
backtrace = []
20+
feature1 = ["std/feature1"]
21+
default = []

tests/testsuite/standard_lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,26 @@ fn cargo_config_injects_compiler_builtins() {
632632
.with_stderr_does_not_contain("[..]libstd[..]")
633633
.run();
634634
}
635+
636+
#[cargo_test]
637+
fn different_features() {
638+
let setup = match setup() {
639+
Some(s) => s,
640+
None => return,
641+
};
642+
let p = project()
643+
.file(
644+
"src/lib.rs",
645+
"
646+
pub fn foo() {
647+
std::conditional_function();
648+
}
649+
",
650+
)
651+
.build();
652+
p.cargo("build")
653+
.build_std(&setup)
654+
.arg("-Zbuild-std-features=feature1")
655+
.target_host()
656+
.run();
657+
}

0 commit comments

Comments
 (0)