Skip to content

Commit a338439

Browse files
committed
return ToolBuildResult to utilize them from callers
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 8e011e5 commit a338439

File tree

5 files changed

+66
-85
lines changed

5 files changed

+66
-85
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ impl Step for Rls {
11801180
let mut tarball = Tarball::new(builder, "rls", &target.triple);
11811181
tarball.set_overlay(OverlayKind::Rls);
11821182
tarball.is_preview(true);
1183-
tarball.add_file(rls, "bin", 0o755);
1183+
tarball.add_file(rls.tool_path, "bin", 0o755);
11841184
tarball.add_legal_and_readme_to("share/doc/rls");
11851185
Some(tarball.generate())
11861186
}
@@ -1268,8 +1268,8 @@ impl Step for Clippy {
12681268
let mut tarball = Tarball::new(builder, "clippy", &target.triple);
12691269
tarball.set_overlay(OverlayKind::Clippy);
12701270
tarball.is_preview(true);
1271-
tarball.add_file(clippy, "bin", 0o755);
1272-
tarball.add_file(cargoclippy, "bin", 0o755);
1271+
tarball.add_file(clippy.tool_path, "bin", 0o755);
1272+
tarball.add_file(cargoclippy.tool_path, "bin", 0o755);
12731273
tarball.add_legal_and_readme_to("share/doc/clippy");
12741274
Some(tarball.generate())
12751275
}
@@ -1318,8 +1318,8 @@ impl Step for Miri {
13181318
let mut tarball = Tarball::new(builder, "miri", &target.triple);
13191319
tarball.set_overlay(OverlayKind::Miri);
13201320
tarball.is_preview(true);
1321-
tarball.add_file(miri, "bin", 0o755);
1322-
tarball.add_file(cargomiri, "bin", 0o755);
1321+
tarball.add_file(miri.tool_path, "bin", 0o755);
1322+
tarball.add_file(cargomiri.tool_path, "bin", 0o755);
13231323
tarball.add_legal_and_readme_to("share/doc/miri");
13241324
Some(tarball.generate())
13251325
}
@@ -1449,8 +1449,8 @@ impl Step for Rustfmt {
14491449
let mut tarball = Tarball::new(builder, "rustfmt", &target.triple);
14501450
tarball.set_overlay(OverlayKind::Rustfmt);
14511451
tarball.is_preview(true);
1452-
tarball.add_file(rustfmt, "bin", 0o755);
1453-
tarball.add_file(cargofmt, "bin", 0o755);
1452+
tarball.add_file(rustfmt.tool_path, "bin", 0o755);
1453+
tarball.add_file(cargofmt.tool_path, "bin", 0o755);
14541454
tarball.add_legal_and_readme_to("share/doc/rustfmt");
14551455
Some(tarball.generate())
14561456
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ impl Step for Rustfmt {
427427
let host = self.host;
428428
let compiler = builder.compiler(stage, host);
429429

430-
builder.ensure(tool::Rustfmt { compiler, target: self.host });
430+
let tool_result = builder.ensure(tool::Rustfmt { compiler, target: self.host });
431+
let compiler = tool_result.build_compiler;
431432

432433
let mut cargo = tool::prepare_tool_cargo(
433434
builder,
@@ -571,7 +572,7 @@ impl Step for Miri {
571572
// miri tests need to know about the stage sysroot
572573
cargo.env("MIRI_SYSROOT", &miri_sysroot);
573574
cargo.env("MIRI_HOST_SYSROOT", &host_sysroot);
574-
cargo.env("MIRI", &miri);
575+
cargo.env("MIRI", &miri.tool_path);
575576

576577
// Set the target.
577578
cargo.env("MIRI_TEST_TARGET", target.rustc_target_arg());
@@ -743,7 +744,8 @@ impl Step for Clippy {
743744
let host = self.host;
744745
let compiler = builder.compiler(stage, host);
745746

746-
builder.ensure(tool::Clippy { compiler, target: self.host });
747+
let tool_result = builder.ensure(tool::Clippy { compiler, target: self.host });
748+
let compiler = tool_result.build_compiler;
747749
let mut cargo = tool::prepare_tool_cargo(
748750
builder,
749751
compiler,

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

+50-68
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ impl Builder<'_> {
6565
}
6666

6767
#[derive(Clone)]
68-
struct ToolBuildResult {
69-
tool_path: PathBuf,
70-
build_compiler: Compiler,
71-
target_compiler: Compiler,
68+
pub struct ToolBuildResult {
69+
pub tool_path: PathBuf,
70+
pub build_compiler: Compiler,
71+
pub target_compiler: Compiler,
7272
}
7373

7474
impl Step for ToolBuild {
@@ -114,10 +114,28 @@ impl Step for ToolBuild {
114114
self.source_type,
115115
&self.extra_features,
116116
);
117+
118+
if path.ends_with("/rustdoc") &&
119+
// rustdoc is performance sensitive, so apply LTO to it.
120+
is_lto_stage(&self.compiler)
121+
{
122+
let lto = match builder.config.rust_lto {
123+
RustcLto::Off => Some("off"),
124+
RustcLto::Thin => Some("thin"),
125+
RustcLto::Fat => Some("fat"),
126+
RustcLto::ThinLocal => None,
127+
};
128+
if let Some(lto) = lto {
129+
cargo.env(cargo_profile_var("LTO", &builder.config), lto);
130+
}
131+
}
132+
117133
if !self.allow_features.is_empty() {
118134
cargo.allow_features(self.allow_features);
119135
}
136+
120137
cargo.args(self.cargo_args);
138+
121139
let _guard = builder.msg_tool(
122140
Kind::Build,
123141
self.mode,
@@ -163,9 +181,6 @@ pub fn prepare_tool_cargo(
163181
source_type: SourceType,
164182
extra_features: &[String],
165183
) -> CargoCommand {
166-
let compiler =
167-
if mode == Mode::ToolRustc { get_tool_rustc_compiler(builder, compiler) } else { compiler };
168-
169184
let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, cmd_kind);
170185

171186
let dir = builder.src.join(path);
@@ -667,80 +682,46 @@ impl Step for Rustdoc {
667682
}
668683
}
669684

670-
let build_compiler = get_tool_rustc_compiler(builder, target_compiler);
671-
672-
// When using `download-rustc` and a stage0 build_compiler, copying rustc doesn't actually
673-
// build stage0 libstd (because the libstd in sysroot has the wrong ABI). Explicitly build
674-
// it.
675-
builder.ensure(compile::Std::new(build_compiler, target_compiler.host));
676-
builder.ensure(compile::Rustc::new(build_compiler, target_compiler.host));
677-
678685
// The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
679686
// compiler libraries, ...) are built. Rustdoc does not require the presence of any
680687
// libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
681688
// they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
682689
// libraries here. The intuition here is that If we've built a compiler, we should be able
683690
// to build rustdoc.
684691
//
685-
let mut features = Vec::new();
692+
let mut extra_features = Vec::new();
686693
if builder.config.jemalloc(target) {
687-
features.push("jemalloc".to_string());
694+
extra_features.push("jemalloc".to_string());
688695
}
689696

690-
// NOTE: Never modify the rustflags here, it breaks the build cache for other tools!
691-
let mut cargo = prepare_tool_cargo(
692-
builder,
693-
target_compiler,
694-
Mode::ToolRustc,
695-
target,
696-
Kind::Build,
697-
"src/tools/rustdoc",
698-
SourceType::InTree,
699-
features.as_slice(),
700-
);
701-
702-
// rustdoc is performance sensitive, so apply LTO to it.
703-
if is_lto_stage(&build_compiler) {
704-
let lto = match builder.config.rust_lto {
705-
RustcLto::Off => Some("off"),
706-
RustcLto::Thin => Some("thin"),
707-
RustcLto::Fat => Some("fat"),
708-
RustcLto::ThinLocal => None,
709-
};
710-
if let Some(lto) = lto {
711-
cargo.env(cargo_profile_var("LTO", &builder.config), lto);
712-
}
713-
}
714-
715-
let _guard = builder.msg_tool(
716-
Kind::Build,
717-
Mode::ToolRustc,
718-
"rustdoc",
719-
build_compiler.stage,
720-
&self.compiler.host,
721-
&target,
722-
);
723-
cargo.into_cmd().run(builder);
724-
725-
// Cargo adds a number of paths to the dylib search path on windows, which results in
726-
// the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
727-
// rustdoc a different name.
728-
let tool_rustdoc = builder
729-
.cargo_out(build_compiler, Mode::ToolRustc, target)
730-
.join(exe("rustdoc_tool_binary", target_compiler.host));
697+
let ToolBuildResult { tool_path, build_compiler: _build_compiler, target_compiler } =
698+
builder.ensure(ToolBuild {
699+
compiler: target_compiler,
700+
target,
701+
// Cargo adds a number of paths to the dylib search path on windows, which results in
702+
// the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
703+
// rustdoc a different name.
704+
tool: "rustdoc_tool_binary",
705+
mode: Mode::ToolRustc,
706+
path: "src/tools/rustdoc",
707+
source_type: SourceType::InTree,
708+
extra_features,
709+
allow_features: "",
710+
cargo_args: Vec::new(),
711+
});
731712

732713
// don't create a stage0-sysroot/bin directory.
733714
if target_compiler.stage > 0 {
734715
if builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None {
735716
// Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
736717
// our final binaries
737-
compile::strip_debug(builder, target, &tool_rustdoc);
718+
compile::strip_debug(builder, target, &tool_path);
738719
}
739720
let bin_rustdoc = bin_rustdoc();
740-
builder.copy_link(&tool_rustdoc, &bin_rustdoc);
721+
builder.copy_link(&tool_path, &bin_rustdoc);
741722
bin_rustdoc
742723
} else {
743-
tool_rustdoc
724+
tool_path
744725
}
745726
}
746727
}
@@ -1084,7 +1065,7 @@ macro_rules! tool_extended {
10841065
}
10851066

10861067
impl Step for $name {
1087-
type Output = PathBuf;
1068+
type Output = ToolBuildResult;
10881069
const DEFAULT: bool = true; // Overridden by `should_run_tool_build_step`
10891070
const ONLY_HOSTS: bool = true;
10901071

@@ -1104,7 +1085,7 @@ macro_rules! tool_extended {
11041085
});
11051086
}
11061087

1107-
fn run(self, builder: &Builder<'_>) -> PathBuf {
1088+
fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
11081089
let Self { compiler, target } = self;
11091090
run_tool_build_step(
11101091
builder,
@@ -1150,9 +1131,9 @@ fn run_tool_build_step(
11501131
tool_name: &'static str,
11511132
path: &'static str,
11521133
add_bins_to_sysroot: Option<&[&str]>,
1153-
) -> PathBuf {
1154-
let ToolBuildResult { tool_path, build_compiler: _build_compiler, target_compiler } = builder
1155-
.ensure(ToolBuild {
1134+
) -> ToolBuildResult {
1135+
let ToolBuildResult { tool_path, build_compiler, target_compiler } =
1136+
builder.ensure(ToolBuild {
11561137
compiler,
11571138
target,
11581139
tool: tool_name,
@@ -1177,9 +1158,10 @@ fn run_tool_build_step(
11771158
}
11781159

11791160
// Return a path into the bin dir.
1180-
bindir.join(exe(tool_name, target_compiler.host))
1161+
let path = bindir.join(exe(tool_name, target_compiler.host));
1162+
ToolBuildResult { tool_path: path, build_compiler, target_compiler }
11811163
} else {
1182-
tool_path
1164+
ToolBuildResult { tool_path, build_compiler, target_compiler }
11831165
}
11841166
}
11851167

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ impl<'a> Builder<'a> {
14151415
let mut dylib_path = helpers::dylib_path();
14161416
dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));
14171417

1418-
let mut cmd = command(cargo_clippy);
1418+
let mut cmd = command(cargo_clippy.tool_path);
14191419
cmd.env(helpers::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
14201420
cmd.env("CARGO", &self.initial_cargo);
14211421
cmd
@@ -1430,8 +1430,8 @@ impl<'a> Builder<'a> {
14301430
let cargo_miri =
14311431
self.ensure(tool::CargoMiri { compiler: build_compiler, target: self.build.build });
14321432
// Invoke cargo-miri, make sure it can find miri and cargo.
1433-
let mut cmd = command(cargo_miri);
1434-
cmd.env("MIRI", &miri);
1433+
let mut cmd = command(cargo_miri.tool_path);
1434+
cmd.env("MIRI", &miri.tool_path);
14351435
cmd.env("CARGO", &self.initial_cargo);
14361436
// Need to add the `run_compiler` libs. Those are the libs produces *by* `build_compiler`,
14371437
// so they match the Miri we just built. However this means they are actually living one

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,7 @@ mod dist {
797797
// stage minus 1 if --stage is not 0. Very confusing!
798798
assert_eq!(
799799
first(builder.cache.all::<tool::Rustdoc>()),
800-
&[
801-
tool::Rustdoc { compiler: Compiler { host: a, stage: 1 } },
802-
tool::Rustdoc { compiler: Compiler { host: a, stage: 2 } },
803-
]
800+
&[tool::Rustdoc { compiler: Compiler { host: a, stage: 2 } },]
804801
);
805802
}
806803

0 commit comments

Comments
 (0)