From 1faf5b9f00c5f2b660c0e305e3d51167990d6761 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 15 Jul 2020 11:44:41 -0700 Subject: [PATCH] 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`. --- src/cargo/core/compiler/standard_lib.rs | 11 +++++++-- src/cargo/core/features.rs | 2 ++ src/doc/src/reference/unstable.md | 9 ++++++++ .../testsuite/mock-std/src/libstd/Cargo.toml | 3 +++ tests/testsuite/mock-std/src/libstd/lib.rs | 7 ++++-- .../testsuite/mock-std/src/libtest/Cargo.toml | 3 +++ tests/testsuite/standard_lib.rs | 23 +++++++++++++++++++ 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index a962e93d4..a83650f33 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -99,11 +99,18 @@ pub fn resolve_std<'cfg>( spec_pkgs.push("test".to_string()); let spec = Packages::Packages(spec_pkgs); let specs = spec.to_package_id_specs(&std_ws)?; - let features = vec!["panic-unwind".to_string(), "backtrace".to_string()]; + let features = match &config.cli_unstable().build_std_features { + Some(list) => list.clone(), + None => vec![ + "panic-unwind".to_string(), + "backtrace".to_string(), + "default".to_string(), + ], + }; // dev_deps setting shouldn't really matter here. let opts = ResolveOpts::new( /*dev_deps*/ false, &features, /*all_features*/ false, - /*uses_default_features*/ true, + /*uses_default_features*/ false, ); let resolve = ops::resolve_ws_with_opts( &std_ws, diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 9bad61ad2..c8993451a 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -350,6 +350,7 @@ pub struct CliUnstable { pub binary_dep_depinfo: bool, #[serde(deserialize_with = "deserialize_build_std")] pub build_std: Option>, + pub build_std_features: Option>, pub timings: Option>, pub doctest_xcompile: bool, pub panic_abort_tests: bool, @@ -455,6 +456,7 @@ impl CliUnstable { "build-std" => { self.build_std = Some(crate::core::compiler::standard_lib::parse_unstable_flag(v)) } + "build-std-features" => self.build_std_features = Some(parse_features(v)), "timings" => self.timings = Some(parse_timings(v)), "doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?, "panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?, diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 776d55cfe..b2998dd4a 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -358,6 +358,15 @@ something doesn't quite work the way you'd like it to, feel free to check out the [issue tracker](https://github.com/rust-lang/wg-cargo-std-aware/issues) of the tracking repository, and if it's not there please file a new issue! +### build-std-features +* Tracking Repository: https://github.com/rust-lang/wg-cargo-std-aware + +This flag is a sibling to the `-Zbuild-std` feature flag. This will configure +the features enabled for the standard library itself when building the standard +library. The default enabled features, at this time, are `backtrace` and +`panic_unwind`. This flag expects a comma-separated list and, if provided, will +override the default list of features enabled. + ### timings * Tracking Issue: [#7405](https://github.com/rust-lang/cargo/issues/7405) diff --git a/tests/testsuite/mock-std/src/libstd/Cargo.toml b/tests/testsuite/mock-std/src/libstd/Cargo.toml index 55279ce94..17ecd8efc 100644 --- a/tests/testsuite/mock-std/src/libstd/Cargo.toml +++ b/tests/testsuite/mock-std/src/libstd/Cargo.toml @@ -9,3 +9,6 @@ path = "lib.rs" [dependencies] registry-dep-using-alloc = { version = "*", features = ['mockbuild'] } + +[features] +feature1 = [] diff --git a/tests/testsuite/mock-std/src/libstd/lib.rs b/tests/testsuite/mock-std/src/libstd/lib.rs index 4d1723d5e..146d4c42c 100644 --- a/tests/testsuite/mock-std/src/libstd/lib.rs +++ b/tests/testsuite/mock-std/src/libstd/lib.rs @@ -5,5 +5,8 @@ pub use std::*; #[stable(since = "1.0.0", feature = "dummy")] -pub fn custom_api() { -} +pub fn custom_api() {} + +#[cfg(feature = "feature1")] +#[stable(since = "1.0.0", feature = "dummy")] +pub fn conditional_function() {} diff --git a/tests/testsuite/mock-std/src/libtest/Cargo.toml b/tests/testsuite/mock-std/src/libtest/Cargo.toml index dc5f9da56..078e91289 100644 --- a/tests/testsuite/mock-std/src/libtest/Cargo.toml +++ b/tests/testsuite/mock-std/src/libtest/Cargo.toml @@ -9,6 +9,7 @@ path = "lib.rs" [dependencies] proc_macro = { path = "../libproc_macro" } +std = { path = "../libstd" } panic_unwind = { path = "../libpanic_unwind" } compiler_builtins = { path = "../libcompiler_builtins" } registry-dep-using-std = { version = "*", features = ['mockbuild'] } @@ -16,3 +17,5 @@ registry-dep-using-std = { version = "*", features = ['mockbuild'] } [features] panic-unwind = [] backtrace = [] +feature1 = ["std/feature1"] +default = [] diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index a06221d0e..6023beba5 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -632,3 +632,26 @@ fn cargo_config_injects_compiler_builtins() { .with_stderr_does_not_contain("[..]libstd[..]") .run(); } + +#[cargo_test] +fn different_features() { + let setup = match setup() { + Some(s) => s, + None => return, + }; + let p = project() + .file( + "src/lib.rs", + " + pub fn foo() { + std::conditional_function(); + } + ", + ) + .build(); + p.cargo("build") + .build_std(&setup) + .arg("-Zbuild-std-features=feature1") + .target_host() + .run(); +}