Skip to content

Commit

Permalink
fix(build-std): remove std unsupported check
Browse files Browse the repository at this point in the history
Will let `std:bool` to determine necessary deps for `-Zbuild-std`,
instead of erroring out.
  • Loading branch information
weihanglo committed Dec 6, 2024
1 parent 125e873 commit 1cd370c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
30 changes: 7 additions & 23 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -129,7 +113,7 @@ pub fn generate_std_roots(
profiles: &Profiles,
target_data: &RustcTargetData<'_>,
) -> CargoResult<HashMap<CompileKind, Vec<Unit>>> {
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::<CargoResult<Vec<PackageId>>>()?;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/ops/cargo_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
32 changes: 27 additions & 5 deletions tests/testsuite/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
Expand Down

0 comments on commit 1cd370c

Please sign in to comment.