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(); +}