Skip to content

Commit 508f21e

Browse files
committed
Fix handling of std crates for no_std targets
1 parent 5105a57 commit 508f21e

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,21 +449,24 @@ fn copy_self_contained_objects(
449449
target_deps
450450
}
451451

452-
/// Resolves standard library crates for `Std::run_make` for any build kind (like check, build, clippy, etc.).
452+
/// Resolves standard library crates for `Std::run_make` for any build kind (like check, doc,
453+
/// build, clippy, etc.).
453454
pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
454-
let has_alias = run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
455+
let mut crates = run.make_run_crates(builder::Alias::Library);
456+
457+
// For no_std targets, we only want to check core and alloc
458+
// Regardless of core/alloc being selected explicitly or via the "library" default alias,
459+
// we only want to keep these two crates.
460+
// The set of no_std crates should be kept in sync with what `Builder::std_cargo` does.
461+
// Note: an alternative design would be to return an enum from this function (Default vs Subset)
462+
// of crates. However, several steps currently pass `-p <package>` even if all crates are
463+
// selected, because Cargo behaves differently in that case. To keep that behavior without
464+
// making further changes, we pre-filter the no-std crates here.
455465
let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
456-
457-
// For no_std targets, do not add any additional crates to the compilation other than what `compile::std_cargo` already adds for no_std targets.
458466
if target_is_no_std {
459-
vec![]
460-
}
461-
// If the paths include "library", build the entire standard library.
462-
else if has_alias {
463-
run.make_run_crates(builder::Alias::Library)
464-
} else {
465-
run.cargo_crates_in_set()
467+
crates.retain(|c| c == "core" || c == "alloc");
466468
}
469+
crates
467470
}
468471

469472
/// Tries to find LLVM's `compiler-rt` source directory, for building `library/profiler_builtins`.

src/bootstrap/src/core/builder/tests.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,60 @@ mod snapshot {
15211521
steps.assert_contains(StepMetadata::test("CrateLibrustc", host));
15221522
steps.assert_contains_fuzzy(StepMetadata::build("rustc", host));
15231523
}
1524+
1525+
#[test]
1526+
fn doc_library() {
1527+
let ctx = TestCtx::new();
1528+
insta::assert_snapshot!(
1529+
ctx.config("doc")
1530+
.path("library")
1531+
.targets(&["x86_64-unknown-linux-gnu"])
1532+
.get_steps()
1533+
.render_with(RenderConfig {
1534+
normalize_host: false,
1535+
}), @r"
1536+
[build] llvm <x86_64-unknown-linux-gnu>
1537+
[build] rustc 0 <x86_64-unknown-linux-gnu> -> rustc 1 <x86_64-unknown-linux-gnu>
1538+
[build] rustdoc 0 <x86_64-unknown-linux-gnu>
1539+
[doc] std 1 <x86_64-unknown-linux-gnu> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind]
1540+
");
1541+
}
1542+
1543+
#[test]
1544+
fn doc_core() {
1545+
let ctx = TestCtx::new();
1546+
insta::assert_snapshot!(
1547+
ctx.config("doc")
1548+
.path("core")
1549+
.targets(&["x86_64-unknown-linux-gnu"])
1550+
.get_steps()
1551+
.render_with(RenderConfig {
1552+
normalize_host: false,
1553+
}), @r"
1554+
[build] llvm <x86_64-unknown-linux-gnu>
1555+
[build] rustc 0 <x86_64-unknown-linux-gnu> -> rustc 1 <x86_64-unknown-linux-gnu>
1556+
[build] rustdoc 0 <x86_64-unknown-linux-gnu>
1557+
[doc] std 1 <x86_64-unknown-linux-gnu> crates=[core]
1558+
");
1559+
}
1560+
1561+
#[test]
1562+
fn doc_library_no_std_target() {
1563+
let ctx = TestCtx::new();
1564+
insta::assert_snapshot!(
1565+
ctx.config("doc")
1566+
.path("core")
1567+
.targets(&["aarch64-unknown-none"])
1568+
.get_steps()
1569+
.render_with(RenderConfig {
1570+
normalize_host: false,
1571+
}), @r"
1572+
[build] llvm <x86_64-unknown-linux-gnu>
1573+
[build] rustc 0 <x86_64-unknown-linux-gnu> -> rustc 1 <x86_64-unknown-linux-gnu>
1574+
[build] rustdoc 0 <x86_64-unknown-linux-gnu>
1575+
[doc] std 1 <aarch64-unknown-none> crates=[core]
1576+
");
1577+
}
15241578
}
15251579

15261580
struct ExecutedSteps {

0 commit comments

Comments
 (0)