Skip to content

Commit 7e6943d

Browse files
authored
Rollup merge of rust-lang#128182 - onur-ozkan:fix-no-std-crates, r=Mark-Simulacrum
handle no_std targets on std builds This PR unifies the `Step::run_make` logic and improves it by skipping std specific crates for no_std targets. In addition, since we now handle library crates properly, bootstrap is capable of running `x doc library` even for no_std targets as it is able to generate documentation for `alloc` crate from the standard library. Resolves rust-lang#128027 cc ``@ChrisDenton``
2 parents a5ee5cb + f56d58e commit 7e6943d

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed

src/bootstrap/src/core/build_steps/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::path::PathBuf;
44

55
use crate::core::build_steps::compile::{
6-
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo,
6+
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo, std_crates_for_run_make,
77
};
88
use crate::core::build_steps::tool::{prepare_tool_cargo, SourceType};
99
use crate::core::builder::{
@@ -49,7 +49,7 @@ impl Step for Std {
4949
}
5050

5151
fn make_run(run: RunConfig<'_>) {
52-
let crates = run.make_run_crates(Alias::Library);
52+
let crates = std_crates_for_run_make(&run);
5353
run.builder.ensure(Std { target: run.target, crates, override_build_kind: None });
5454
}
5555

src/bootstrap/src/core/build_steps/clippy.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::compile::{librustc_stamp, libstd_stamp, run_cargo, rustc_cargo, std_c
44
use super::tool::{prepare_tool_cargo, SourceType};
55
use super::{check, compile};
66
use crate::builder::{Builder, ShouldRun};
7+
use crate::core::build_steps::compile::std_crates_for_run_make;
78
use crate::core::builder;
89
use crate::core::builder::{crate_description, Alias, Kind, RunConfig, Step};
910
use crate::{Mode, Subcommand, TargetSelection};
@@ -106,7 +107,7 @@ impl Step for Std {
106107
}
107108

108109
fn make_run(run: RunConfig<'_>) {
109-
let crates = run.make_run_crates(Alias::Library);
110+
let crates = std_crates_for_run_make(&run);
110111
run.builder.ensure(Std { target: run.target, crates });
111112
}
112113

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ impl Step for Std {
123123
}
124124

125125
fn make_run(run: RunConfig<'_>) {
126-
// If the paths include "library", build the entire standard library.
127-
let has_alias =
128-
run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
129-
let crates = if has_alias { Default::default() } else { run.cargo_crates_in_set() };
130-
126+
let crates = std_crates_for_run_make(&run);
131127
run.builder.ensure(Std {
132128
compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
133129
target: run.target,
@@ -425,6 +421,28 @@ fn copy_self_contained_objects(
425421
target_deps
426422
}
427423

424+
/// Resolves standard library crates for `Std::run_make` for any build kind (like check, build, clippy, etc.).
425+
pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
426+
// FIXME: Extend builder tests to cover the `crates` field of `Std` instances.
427+
if cfg!(feature = "bootstrap-self-test") {
428+
return vec![];
429+
}
430+
431+
let has_alias = run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
432+
let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
433+
434+
// 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.
435+
if target_is_no_std {
436+
vec![]
437+
}
438+
// If the paths include "library", build the entire standard library.
439+
else if has_alias {
440+
run.make_run_crates(builder::Alias::Library)
441+
} else {
442+
run.cargo_crates_in_set()
443+
}
444+
}
445+
428446
/// Configure cargo to compile the standard library, adding appropriate env vars
429447
/// and such.
430448
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {

src/bootstrap/src/core/build_steps/dist.rs

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ impl Step for JsonDocs {
106106
builder.ensure(crate::core::build_steps::doc::Std::new(
107107
builder.top_stage,
108108
host,
109-
builder,
110109
DocumentationFormat::Json,
111110
));
112111

src/bootstrap/src/core/build_steps/doc.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,8 @@ pub struct Std {
564564
}
565565

566566
impl Std {
567-
pub(crate) fn new(
568-
stage: u32,
569-
target: TargetSelection,
570-
builder: &Builder<'_>,
571-
format: DocumentationFormat,
572-
) -> Self {
573-
let crates = builder
574-
.in_tree_crates("sysroot", Some(target))
575-
.into_iter()
576-
.map(|krate| krate.name.to_string())
577-
.collect();
578-
Std { stage, target, format, crates }
567+
pub(crate) fn new(stage: u32, target: TargetSelection, format: DocumentationFormat) -> Self {
568+
Std { stage, target, format, crates: vec![] }
579569
}
580570
}
581571

@@ -589,6 +579,7 @@ impl Step for Std {
589579
}
590580

591581
fn make_run(run: RunConfig<'_>) {
582+
let crates = compile::std_crates_for_run_make(&run);
592583
run.builder.ensure(Std {
593584
stage: run.builder.top_stage,
594585
target: run.target,
@@ -597,7 +588,7 @@ impl Step for Std {
597588
} else {
598589
DocumentationFormat::Html
599590
},
600-
crates: run.make_run_crates(Alias::Library),
591+
crates,
601592
});
602593
}
603594

@@ -695,13 +686,6 @@ fn doc_std(
695686
extra_args: &[&str],
696687
requested_crates: &[String],
697688
) {
698-
if builder.no_std(target) == Some(true) {
699-
panic!(
700-
"building std documentation for no_std target {target} is not supported\n\
701-
Set `docs = false` in the config to disable documentation, or pass `--skip library`."
702-
);
703-
}
704-
705689
let compiler = builder.compiler(stage, builder.config.build);
706690

707691
let target_doc_dir_name = if format == DocumentationFormat::Json { "json-doc" } else { "doc" };

src/bootstrap/src/core/build_steps/test.rs

-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,6 @@ impl Step for RustdocJSStd {
847847
builder.ensure(crate::core::build_steps::doc::Std::new(
848848
builder.top_stage,
849849
self.target,
850-
builder,
851850
DocumentationFormat::Html,
852851
));
853852
let _guard = builder.msg(

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

-4
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,9 @@ macro_rules! std {
7979

8080
macro_rules! doc_std {
8181
($host:ident => $target:ident, stage = $stage:literal) => {{
82-
let config = configure("doc", &["A-A"], &["A-A"]);
83-
let build = Build::new(config);
84-
let builder = Builder::new(&build);
8582
doc::Std::new(
8683
$stage,
8784
TargetSelection::from_user(concat!(stringify!($target), "-", stringify!($target))),
88-
&builder,
8985
DocumentationFormat::Html,
9086
)
9187
}};

0 commit comments

Comments
 (0)