Skip to content

Commit 283c4d4

Browse files
committed
fix(build-std): make Resolve completely align to what to build
1 parent a6438c8 commit 283c4d4

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,13 @@ impl TargetInfo {
619619
.iter()
620620
.any(|sup| sup.as_str() == split.as_str())
621621
}
622+
623+
/// Checks if a target support core only.
624+
///
625+
/// If no explictly stated in target spec json, we treat it as "maybe support".
626+
pub fn support_core_only(&self) -> bool {
627+
matches!(self.supports_std, Some(false))
628+
}
622629
}
623630

624631
/// Takes rustc output (using specialized command line args), and calculates the file prefix and

src/cargo/core/compiler/standard_lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ fn std_crates<'a>(crates: &'a [String], default: &'static str, units: &[Unit]) -
4444
}
4545

4646
/// Resolve the standard library dependencies.
47+
///
48+
/// * `crates` is the arg value from `-Zbuild-std`.
4749
pub fn resolve_std<'gctx>(
4850
ws: &Workspace<'gctx>,
4951
target_data: &mut RustcTargetData<'gctx>,
5052
build_config: &BuildConfig,
53+
crates: &[String],
54+
kinds: &[CompileKind],
5155
) -> CargoResult<(PackageSet<'gctx>, Resolve, ResolvedFeatures)> {
5256
if build_config.build_plan {
5357
ws.gctx()
@@ -65,10 +69,19 @@ pub fn resolve_std<'gctx>(
6569
// `[dev-dependencies]`. No need for us to generate a `Resolve` which has
6670
// those included because we'll never use them anyway.
6771
std_ws.set_require_optional_deps(false);
68-
// `sysroot` + the default feature set below should give us a good default
69-
// Resolve, which includes `libtest` as well.
70-
let specs = Packages::Packages(vec!["sysroot".into()]);
71-
let specs = specs.to_package_id_specs(&std_ws)?;
72+
let specs = {
73+
// If there is any need std, resolve with it.
74+
// This may need a UI overhaul if `build-std` wants to fully support multi-targets.
75+
let core_only = kinds
76+
.iter()
77+
.all(|kind| target_data.info(*kind).support_core_only());
78+
let mut crates = std_crates(crates, if core_only { "core" } else { "std" }, &[]);
79+
// `sysroot` is not in the default set because it is optional, but it needs
80+
// to be part of the resolve in case we do need it or `libtest`.
81+
crates.insert("sysroot");
82+
let specs = Packages::Packages(crates.into_iter().map(Into::into).collect());
83+
specs.to_package_id_specs(&std_ws)?
84+
};
7285
let features = match &gctx.cli_unstable().build_std_features {
7386
Some(list) => list.clone(),
7487
None => vec![
@@ -115,9 +128,9 @@ pub fn generate_std_roots(
115128
) -> CargoResult<HashMap<CompileKind, Vec<Unit>>> {
116129
// Generate a map of Units for each kind requested.
117130
let mut ret = HashMap::new();
118-
let (core_only, maybe_std): (Vec<&CompileKind>, Vec<_>) = kinds.iter().partition(|kind|
119-
// Only include targets that explicitly don't support std
120-
target_data.info(**kind).supports_std == Some(false));
131+
let (core_only, maybe_std): (Vec<&CompileKind>, Vec<_>) = kinds
132+
.iter()
133+
.partition(|kind| target_data.info(**kind).support_core_only());
121134
for (default_crate, kinds) in [("core", core_only), ("std", maybe_std)] {
122135
if kinds.is_empty() {
123136
continue;

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,6 @@ pub fn create_bcx<'a, 'gctx>(
289289
resolved_features,
290290
} = resolve;
291291

292-
let std_resolve_features = if gctx.cli_unstable().build_std.is_some() {
293-
let (std_package_set, std_resolve, std_features) =
294-
standard_lib::resolve_std(ws, &mut target_data, &build_config)?;
295-
pkg_set.add_set(std_package_set);
296-
Some((std_resolve, std_features))
297-
} else {
298-
None
299-
};
300-
301292
// Find the packages in the resolver that the user wants to build (those
302293
// passed in with `-p` or the defaults from the workspace), and convert
303294
// Vec<PackageIdSpec> to a Vec<PackageId>.
@@ -398,21 +389,30 @@ pub fn create_bcx<'a, 'gctx>(
398389
Vec::new()
399390
};
400391

401-
let std_roots = if let Some(crates) = gctx.cli_unstable().build_std.as_ref() {
402-
let (std_resolve, std_features) = std_resolve_features.as_ref().unwrap();
403-
standard_lib::generate_std_roots(
392+
let (std_roots, std_resolve_features) = if let Some(crates) = &gctx.cli_unstable().build_std {
393+
let (std_package_set, std_resolve, std_features) = standard_lib::resolve_std(
394+
ws,
395+
&mut target_data,
396+
&build_config,
397+
crates,
398+
&explicit_host_kinds,
399+
)?;
400+
pkg_set.add_set(std_package_set);
401+
402+
let std_roots = standard_lib::generate_std_roots(
404403
&crates,
405404
&units,
406-
std_resolve,
407-
std_features,
405+
&std_resolve,
406+
&std_features,
408407
&explicit_host_kinds,
409408
&pkg_set,
410409
interner,
411410
&profiles,
412411
&target_data,
413-
)?
412+
)?;
413+
(std_roots, Some((std_resolve, std_features)))
414414
} else {
415-
Default::default()
415+
(Default::default(), None)
416416
};
417417

418418
let mut unit_graph = build_unit_dependencies(

src/cargo/ops/cargo_fetch.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ pub fn fetch<'a>(
6464
}
6565

6666
// If -Zbuild-std was passed, download dependencies for the standard library.
67-
if gctx.cli_unstable().build_std.is_some() {
68-
let (std_package_set, _, _) = standard_lib::resolve_std(ws, &mut data, &build_config)?;
67+
if let Some(crates) = &gctx.cli_unstable().build_std {
68+
let (std_package_set, _, _) = standard_lib::resolve_std(
69+
ws,
70+
&mut data,
71+
&build_config,
72+
crates,
73+
&build_config.requested_kinds,
74+
)?;
6975
packages.add_set(std_package_set);
7076
}
7177

tests/build-std/main.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,5 @@ fn test_panic_abort() {
413413
.build_std_arg("std,panic_abort")
414414
.env("RUSTFLAGS", "-C panic=abort")
415415
.arg("-Zbuild-std-features=panic_immediate_abort")
416-
.with_status(101)
417-
.with_stderr_data(str![[r#"
418-
[ERROR] package ID specification `panic_unwind` did not match any packages
419-
420-
"#]])
421416
.run();
422417
}

0 commit comments

Comments
 (0)