Skip to content

Commit 89370e0

Browse files
committed
Make WasmComponentLd a ToolTarget tool
1 parent 6511ece commit 89370e0

File tree

4 files changed

+111
-16
lines changed

4 files changed

+111
-16
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::core::build_steps::compile::{
55
};
66
use crate::core::build_steps::tool;
77
use crate::core::build_steps::tool::{
8-
COMPILETEST_ALLOW_FEATURES, SourceType, get_compiler_for_target, prepare_tool_cargo,
8+
COMPILETEST_ALLOW_FEATURES, SourceType, ToolTargetBuildMode, get_tool_target_compiler,
9+
prepare_tool_cargo,
910
};
1011
use crate::core::builder::{
1112
self, Alias, Builder, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
@@ -252,7 +253,7 @@ fn prepare_compiler_for_check(
252253

253254
match mode {
254255
Mode::ToolBootstrap => builder.compiler(0, host),
255-
Mode::ToolTarget => get_compiler_for_target(builder, target),
256+
Mode::ToolTarget => get_tool_target_compiler(builder, ToolTargetBuildMode::Build(target)),
256257
Mode::ToolStd => {
257258
// These tools require the local standard library to be checked
258259
let build_compiler = builder.compiler(builder.top_stage, host);

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,15 +2286,13 @@ impl Step for Assemble {
22862286
}
22872287

22882288
// In addition to `rust-lld` also install `wasm-component-ld` when
2289-
// LLD is enabled. This is a relatively small binary that primarily
2290-
// delegates to the `rust-lld` binary for linking and then runs
2291-
// logic to create the final binary. This is used by the
2292-
// `wasm32-wasip2` target of Rust.
2289+
// is enabled. This is used by the `wasm32-wasip2` target of Rust.
22932290
if builder.tool_enabled("wasm-component-ld") {
2294-
let wasm_component = builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
2295-
compiler: build_compiler,
2296-
target: target_compiler.host,
2297-
});
2291+
let wasm_component =
2292+
builder.ensure(crate::core::build_steps::tool::WasmComponentLd::for_compiler(
2293+
builder,
2294+
target_compiler,
2295+
));
22982296
builder.copy_link(
22992297
&wasm_component.tool_path,
23002298
&libdir_bin.join(wasm_component.tool_path.file_name().unwrap()),

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,36 @@ pub(crate) fn get_tool_rustc_compiler(
365365
builder.compiler(target_compiler.stage.saturating_sub(1), builder.config.host_target)
366366
}
367367

368-
/// Returns the smallest stage compiler that is able to compile code for the given `target`.
369-
pub(crate) fn get_compiler_for_target(builder: &Builder<'_>, target: TargetSelection) -> Compiler {
368+
/// Determines how to build a `ToolTarget`, i.e. which compiler should be used to compile it.
369+
/// The compiler stage is automatically auto-bumped if we need to cross-compile a stage 1 tool.
370+
pub enum ToolTargetBuildMode {
371+
/// Build the tool using rustc that corresponds to the selected CLI stage.
372+
Build(TargetSelection),
373+
/// Build the tool so that it can be attached to the sysroot of the passed compiler.
374+
/// Since we always dist stage 2+, the compiler that builds the tool in this case has to be
375+
/// stage 1+.
376+
Dist(Compiler),
377+
}
378+
379+
/// Returns compiler that is able to compile a `ToolTarget` tool with the given `mode`.
380+
pub(crate) fn get_tool_target_compiler(
381+
builder: &Builder<'_>,
382+
mode: ToolTargetBuildMode,
383+
) -> Compiler {
384+
let (target, min_build_compiler_stage) = match mode {
385+
ToolTargetBuildMode::Build(target) => {
386+
assert!(builder.top_stage > 0);
387+
(target, builder.top_stage - 1)
388+
}
389+
ToolTargetBuildMode::Dist(target_compiler) => {
390+
assert!(target_compiler.stage > 0);
391+
(target_compiler.host, target_compiler.stage - 1)
392+
}
393+
};
370394
let compiler = if builder.host_target == target {
371-
builder.compiler(0, builder.host_target)
395+
builder.compiler(min_build_compiler_stage, builder.host_target)
372396
} else {
373-
builder.compiler(1, builder.host_target)
397+
builder.compiler(min_build_compiler_stage.max(1), builder.host_target)
374398
};
375399
builder.std(compiler, target);
376400
compiler
@@ -533,7 +557,6 @@ bootstrap_tool!(
533557
// rustdoc-gui-test has a crate dependency on compiletest, so it needs the same unstable features.
534558
RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
535559
CoverageDump, "src/tools/coverage-dump", "coverage-dump";
536-
WasmComponentLd, "src/tools/wasm-component-ld", "wasm-component-ld", is_unstable_tool = true, allow_features = "min_specialization";
537560
UnicodeTableGenerator, "src/tools/unicode-table-generator", "unicode-table-generator";
538561
FeaturesStatusDump, "src/tools/features-status-dump", "features-status-dump";
539562
OptimizedDist, "src/tools/opt-dist", "opt-dist", submodules = &["src/tools/rustc-perf"];
@@ -661,7 +684,10 @@ impl Step for RemoteTestServer {
661684

662685
fn make_run(run: RunConfig<'_>) {
663686
run.builder.ensure(RemoteTestServer {
664-
build_compiler: get_compiler_for_target(run.builder, run.target),
687+
build_compiler: get_tool_target_compiler(
688+
run.builder,
689+
ToolTargetBuildMode::Build(run.target),
690+
),
665691
target: run.target,
666692
});
667693
}
@@ -935,6 +961,75 @@ impl Step for LldWrapper {
935961
}
936962
}
937963

964+
/// Builds the `wasm-component-ld` linker wrapper, which is shipped with rustc to be executed on the
965+
/// host platform where rustc runs.
966+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
967+
pub struct WasmComponentLd {
968+
build_compiler: Compiler,
969+
target: TargetSelection,
970+
}
971+
972+
impl WasmComponentLd {
973+
/// Returns `WasmComponentLd` that should be **used** by the passed compiler.
974+
pub fn for_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
975+
Self {
976+
build_compiler: get_tool_target_compiler(
977+
builder,
978+
ToolTargetBuildMode::Dist(target_compiler),
979+
),
980+
target: target_compiler.host,
981+
}
982+
}
983+
}
984+
985+
impl Step for WasmComponentLd {
986+
type Output = ToolBuildResult;
987+
988+
const ONLY_HOSTS: bool = true;
989+
990+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
991+
run.path("src/tools/wasm-component-ld")
992+
}
993+
994+
fn make_run(run: RunConfig<'_>) {
995+
run.builder.ensure(WasmComponentLd {
996+
build_compiler: get_tool_target_compiler(
997+
run.builder,
998+
ToolTargetBuildMode::Build(run.target),
999+
),
1000+
target: run.target,
1001+
});
1002+
}
1003+
1004+
#[cfg_attr(
1005+
feature = "tracing",
1006+
instrument(
1007+
level = "debug",
1008+
name = "WasmComponentLd::run",
1009+
skip_all,
1010+
fields(build_compiler = ?self.build_compiler),
1011+
),
1012+
)]
1013+
fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1014+
builder.ensure(ToolBuild {
1015+
build_compiler: self.build_compiler,
1016+
target: self.target,
1017+
tool: "wasm-component-ld",
1018+
mode: Mode::ToolTarget,
1019+
path: "src/tools/wasm-component-ld",
1020+
source_type: SourceType::InTree,
1021+
extra_features: vec![],
1022+
allow_features: "min-specialization",
1023+
cargo_args: vec![],
1024+
artifact_kind: ToolArtifactKind::Binary,
1025+
})
1026+
}
1027+
1028+
fn metadata(&self) -> Option<StepMetadata> {
1029+
Some(StepMetadata::build("WasmComponentLd", self.target).built_by(self.build_compiler))
1030+
}
1031+
}
1032+
9381033
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
9391034
pub struct RustAnalyzer {
9401035
pub compiler: Compiler,

src/bootstrap/src/core/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ impl<'a> Builder<'a> {
981981
tool::CoverageDump,
982982
tool::LlvmBitcodeLinker,
983983
tool::RustcPerf,
984+
tool::WasmComponentLd
984985
),
985986
Kind::Clippy => describe!(
986987
clippy::Std,

0 commit comments

Comments
 (0)