diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 377d5337f48..013506a3810 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -15,7 +15,7 @@ use std::path::PathBuf; use super::BuildConfig; -fn std_crates<'a>(crates: &'a [String], units: Option<&[Unit]>) -> HashSet<&'a str> { +fn std_crates<'a>(crates: &'a [String], units: &[Unit]) -> HashSet<&'a str> { let mut crates = HashSet::from_iter(crates.iter().map(|s| s.as_str())); // This is a temporary hack until there is a more principled way to // declare dependencies in Cargo.toml. @@ -30,13 +30,11 @@ fn std_crates<'a>(crates: &'a [String], units: Option<&[Unit]>) -> HashSet<&'a s crates.insert("compiler_builtins"); // Only build libtest if it looks like it is needed (libtest depends on libstd) // If we know what units we're building, we can filter for libtest depending on the jobs. - if let Some(units) = units { - if units - .iter() - .any(|unit| unit.mode.is_rustc_test() && unit.target.harness()) - { - crates.insert("test"); - } + if units + .iter() + .any(|unit| unit.mode.is_rustc_test() && unit.target.harness()) + { + crates.insert("test"); } } else if crates.contains("core") { crates.insert("compiler_builtins"); @@ -50,27 +48,13 @@ pub fn resolve_std<'gctx>( ws: &Workspace<'gctx>, target_data: &mut RustcTargetData<'gctx>, build_config: &BuildConfig, - crates: &[String], ) -> CargoResult<(PackageSet<'gctx>, Resolve, ResolvedFeatures)> { - let crates = std_crates(crates, None); - if build_config.build_plan { ws.gctx() .shell() .warn("-Zbuild-std does not currently fully support --build-plan")?; } - // check that targets support building std - if crates.contains("std") { - let unsupported_targets = target_data.get_unsupported_std_targets(); - if !unsupported_targets.is_empty() { - anyhow::bail!( - "building std is not supported on the following targets: {}", - unsupported_targets.join(", ") - ) - } - } - let src_path = detect_sysroot_src_path(target_data)?; let std_ws_manifest_path = src_path.join("Cargo.toml"); let gctx = ws.gctx(); @@ -129,7 +113,7 @@ pub fn generate_std_roots( profiles: &Profiles, target_data: &RustcTargetData<'_>, ) -> CargoResult>> { - let std_ids = std_crates(crates, Some(units)) + let std_ids = std_crates(crates, units) .iter() .map(|crate_name| std_resolve.query(crate_name)) .collect::>>()?; diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index c1bc93800f4..4e011e7ec5a 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -289,9 +289,9 @@ pub fn create_bcx<'a, 'gctx>( resolved_features, } = resolve; - let std_resolve_features = if let Some(crates) = &gctx.cli_unstable().build_std { + let std_resolve_features = if gctx.cli_unstable().build_std.is_some() { let (std_package_set, std_resolve, std_features) = - standard_lib::resolve_std(ws, &mut target_data, &build_config, crates)?; + standard_lib::resolve_std(ws, &mut target_data, &build_config)?; pkg_set.add_set(std_package_set); Some((std_resolve, std_features)) } else { diff --git a/src/cargo/ops/cargo_fetch.rs b/src/cargo/ops/cargo_fetch.rs index 589bbf47812..b1e596f6485 100644 --- a/src/cargo/ops/cargo_fetch.rs +++ b/src/cargo/ops/cargo_fetch.rs @@ -64,9 +64,8 @@ pub fn fetch<'a>( } // If -Zbuild-std was passed, download dependencies for the standard library. - if let Some(crates) = gctx.cli_unstable().build_std.as_ref() { - let (std_package_set, _, _) = - standard_lib::resolve_std(ws, &mut data, &build_config, &crates)?; + if gctx.cli_unstable().build_std.is_some() { + let (std_package_set, _, _) = standard_lib::resolve_std(ws, &mut data, &build_config)?; packages.add_set(std_package_set); } diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 214df036561..75237b16974 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -389,8 +389,29 @@ fn check_core() { .run(); } -#[cargo_test(build_std_mock)] -fn test_std_on_unsupported_target() { +#[cargo_test(build_std_mock, requires = "rustup")] +fn build_std_with_no_arg_for_core_only_target() { + let has_rustup_aarch64_unknown_none = std::process::Command::new("rustup") + .args(["target", "list", "--installed"]) + .output() + .ok() + .map(|output| { + String::from_utf8(output.stdout) + .map(|stdout| stdout.contains("aarch64-unknown-none")) + .unwrap_or_default() + }) + .unwrap_or_default(); + if !has_rustup_aarch64_unknown_none { + let msg = + "to run this test, run `rustup target add aarch64-unknown-none --toolchain nightly`"; + if cargo_util::is_ci() { + panic!("{msg}"); + } else { + eprintln!("{msg}"); + } + return; + } + let setup = setup(); let p = project() @@ -405,13 +426,14 @@ fn test_std_on_unsupported_target() { ) .build(); - p.cargo("build") + p.cargo("build -v") .arg("--target=aarch64-unknown-none") - .arg("--target=x86_64-unknown-none") .build_std(&setup) .with_status(101) .with_stderr_data(str![[r#" -[ERROR] building std is not supported on the following targets: [..] +... +error[E0463]: can't find crate for `std` +... "#]]) .run(); }