Skip to content

bootstrap: Overhaul and simplify the tool_extended! macro #135014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ impl Step for Rls {
let compiler = self.compiler;
let target = self.target;

let rls = builder.ensure(tool::Rls { compiler, target, extra_features: Vec::new() });
let rls = builder.ensure(tool::Rls { compiler, target });

let mut tarball = Tarball::new(builder, "rls", &target.triple);
tarball.set_overlay(OverlayKind::Rls);
Expand Down Expand Up @@ -1239,9 +1239,8 @@ impl Step for Clippy {
// Prepare the image directory
// We expect clippy to build, because we've exited this step above if tool
// state for clippy isn't testing.
let clippy = builder.ensure(tool::Clippy { compiler, target, extra_features: Vec::new() });
let cargoclippy =
builder.ensure(tool::CargoClippy { compiler, target, extra_features: Vec::new() });
let clippy = builder.ensure(tool::Clippy { compiler, target });
let cargoclippy = builder.ensure(tool::CargoClippy { compiler, target });

let mut tarball = Tarball::new(builder, "clippy", &target.triple);
tarball.set_overlay(OverlayKind::Clippy);
Expand Down Expand Up @@ -1290,9 +1289,8 @@ impl Step for Miri {
let compiler = self.compiler;
let target = self.target;

let miri = builder.ensure(tool::Miri { compiler, target, extra_features: Vec::new() });
let cargomiri =
builder.ensure(tool::CargoMiri { compiler, target, extra_features: Vec::new() });
let miri = builder.ensure(tool::Miri { compiler, target });
let cargomiri = builder.ensure(tool::CargoMiri { compiler, target });

let mut tarball = Tarball::new(builder, "miri", &target.triple);
tarball.set_overlay(OverlayKind::Miri);
Expand Down Expand Up @@ -1423,10 +1421,8 @@ impl Step for Rustfmt {
let compiler = self.compiler;
let target = self.target;

let rustfmt =
builder.ensure(tool::Rustfmt { compiler, target, extra_features: Vec::new() });
let cargofmt =
builder.ensure(tool::Cargofmt { compiler, target, extra_features: Vec::new() });
let rustfmt = builder.ensure(tool::Rustfmt { compiler, target });
let cargofmt = builder.ensure(tool::Cargofmt { compiler, target });
let mut tarball = Tarball::new(builder, "rustfmt", &target.triple);
tarball.set_overlay(OverlayKind::Rustfmt);
tarball.is_preview(true);
Expand Down
16 changes: 4 additions & 12 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl Step for Rustfmt {
let host = self.host;
let compiler = builder.compiler(stage, host);

builder.ensure(tool::Rustfmt { compiler, target: self.host, extra_features: Vec::new() });
builder.ensure(tool::Rustfmt { compiler, target: self.host });

let mut cargo = tool::prepare_tool_cargo(
builder,
Expand Down Expand Up @@ -511,17 +511,9 @@ impl Step for Miri {
let host_compiler = builder.compiler(stage - 1, host);

// Build our tools.
let miri = builder.ensure(tool::Miri {
compiler: host_compiler,
target: host,
extra_features: Vec::new(),
});
let miri = builder.ensure(tool::Miri { compiler: host_compiler, target: host });
// the ui tests also assume cargo-miri has been built
builder.ensure(tool::CargoMiri {
compiler: host_compiler,
target: host,
extra_features: Vec::new(),
});
builder.ensure(tool::CargoMiri { compiler: host_compiler, target: host });

// We also need sysroots, for Miri and for the host (the latter for build scripts).
// This is for the tests so everything is done with the target compiler.
Expand Down Expand Up @@ -740,7 +732,7 @@ impl Step for Clippy {
let host = self.host;
let compiler = builder.compiler(stage, host);

builder.ensure(tool::Clippy { compiler, target: self.host, extra_features: Vec::new() });
builder.ensure(tool::Clippy { compiler, target: self.host });
let mut cargo = tool::prepare_tool_cargo(
builder,
compiler,
Expand Down
188 changes: 118 additions & 70 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,102 +1007,150 @@ impl Step for LibcxxVersionTool {
}

macro_rules! tool_extended {
(($sel:ident, $builder:ident),
$($name:ident,
$path:expr,
$tool_name:expr,
stable = $stable:expr
$(,tool_std = $tool_std:literal)?
$(,allow_features = $allow_features:expr)?
$(,add_bins_to_sysroot = $add_bins_to_sysroot:expr)?
;)+) => {
$(
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
(
$name:ident {
path: $path:expr,
tool_name: $tool_name:expr,
stable: $stable:expr
$( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
$( , )?
}
) => {
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct $name {
pub compiler: Compiler,
pub target: TargetSelection,
pub extra_features: Vec<String>,
}

impl Step for $name {
type Output = PathBuf;
const DEFAULT: bool = true; // Overwritten below
const DEFAULT: bool = true; // Overridden by `should_run_tool_build_step`
const ONLY_HOSTS: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path($path).default_condition(
builder.config.extended
&& builder.config.tools.as_ref().map_or(
// By default, on nightly/dev enable all tools, else only
// build stable tools.
$stable || builder.build.unstable_features(),
// If `tools` is set, search list for this tool.
|tools| {
tools.iter().any(|tool| match tool.as_ref() {
"clippy" => $tool_name == "clippy-driver",
x => $tool_name == x,
})
}),
should_run_tool_build_step(
run,
$tool_name,
$path,
$stable,
)
}

fn make_run(run: RunConfig<'_>) {
run.builder.ensure($name {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
extra_features: Vec::new(),
});
}

#[allow(unused_mut)]
fn run(mut $sel, $builder: &Builder<'_>) -> PathBuf {
let tool = $builder.ensure(ToolBuild {
compiler: $sel.compiler,
target: $sel.target,
tool: $tool_name,
mode: if false $(|| $tool_std)? { Mode::ToolStd } else { Mode::ToolRustc },
path: $path,
extra_features: $sel.extra_features,
source_type: SourceType::InTree,
allow_features: concat!($($allow_features)*),
cargo_args: vec![]
});

if (false $(|| !$add_bins_to_sysroot.is_empty())?) && $sel.compiler.stage > 0 {
let bindir = $builder.sysroot($sel.compiler).join("bin");
t!(fs::create_dir_all(&bindir));

#[allow(unused_variables)]
let tools_out = $builder
.cargo_out($sel.compiler, Mode::ToolRustc, $sel.target);
fn run(self, builder: &Builder<'_>) -> PathBuf {
let Self { compiler, target } = self;
run_tool_build_step(
builder,
compiler,
target,
$tool_name,
$path,
None $( .or(Some(&$add_bins_to_sysroot)) )?,
)
}
}
}
}

$(for add_bin in $add_bins_to_sysroot {
let bin_source = tools_out.join(exe(add_bin, $sel.target));
let bin_destination = bindir.join(exe(add_bin, $sel.compiler.host));
$builder.copy_link(&bin_source, &bin_destination);
})?
fn should_run_tool_build_step<'a>(
run: ShouldRun<'a>,
tool_name: &'static str,
path: &'static str,
stable: bool,
) -> ShouldRun<'a> {
let builder = run.builder;
run.path(path).default_condition(
builder.config.extended
&& builder.config.tools.as_ref().map_or(
// By default, on nightly/dev enable all tools, else only
// build stable tools.
stable || builder.build.unstable_features(),
// If `tools` is set, search list for this tool.
|tools| {
tools.iter().any(|tool| match tool.as_ref() {
"clippy" => tool_name == "clippy-driver",
x => tool_name == x,
})
},
),
)
}

let tool = bindir.join(exe($tool_name, $sel.compiler.host));
tool
} else {
tool
}
}
fn run_tool_build_step(
builder: &Builder<'_>,
compiler: Compiler,
target: TargetSelection,
tool_name: &'static str,
path: &'static str,
add_bins_to_sysroot: Option<&[&str]>,
) -> PathBuf {
let tool = builder.ensure(ToolBuild {
compiler,
target,
tool: tool_name,
mode: Mode::ToolRustc,
path,
extra_features: vec![],
source_type: SourceType::InTree,
allow_features: "",
cargo_args: vec![],
});

// FIXME: This should just be an if-let-chain, but those are unstable.
if let Some(add_bins_to_sysroot) =
add_bins_to_sysroot.filter(|bins| !bins.is_empty() && compiler.stage > 0)
{
let bindir = builder.sysroot(compiler).join("bin");
t!(fs::create_dir_all(&bindir));

let tools_out = builder.cargo_out(compiler, Mode::ToolRustc, target);

for add_bin in add_bins_to_sysroot {
let bin_source = tools_out.join(exe(add_bin, target));
let bin_destination = bindir.join(exe(add_bin, compiler.host));
builder.copy_link(&bin_source, &bin_destination);
}
)+

// Return a path into the bin dir.
bindir.join(exe(tool_name, compiler.host))
} else {
tool
}
}

tool_extended!((self, builder),
Cargofmt, "src/tools/rustfmt", "cargo-fmt", stable=true;
CargoClippy, "src/tools/clippy", "cargo-clippy", stable=true;
Clippy, "src/tools/clippy", "clippy-driver", stable=true, add_bins_to_sysroot = ["clippy-driver", "cargo-clippy"];
Miri, "src/tools/miri", "miri", stable=false, add_bins_to_sysroot = ["miri"];
CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", stable=false, add_bins_to_sysroot = ["cargo-miri"];
Rls, "src/tools/rls", "rls", stable=true;
Rustfmt, "src/tools/rustfmt", "rustfmt", stable=true, add_bins_to_sysroot = ["rustfmt", "cargo-fmt"];
);
tool_extended!(Cargofmt { path: "src/tools/rustfmt", tool_name: "cargo-fmt", stable: true });
tool_extended!(CargoClippy { path: "src/tools/clippy", tool_name: "cargo-clippy", stable: true });
tool_extended!(Clippy {
path: "src/tools/clippy",
tool_name: "clippy-driver",
stable: true,
add_bins_to_sysroot: ["clippy-driver", "cargo-clippy"]
});
tool_extended!(Miri {
path: "src/tools/miri",
tool_name: "miri",
stable: false,
add_bins_to_sysroot: ["miri"]
});
tool_extended!(CargoMiri {
path: "src/tools/miri/cargo-miri",
tool_name: "cargo-miri",
stable: false,
add_bins_to_sysroot: ["cargo-miri"]
});
tool_extended!(Rls { path: "src/tools/rls", tool_name: "rls", stable: true });
tool_extended!(Rustfmt {
path: "src/tools/rustfmt",
tool_name: "rustfmt",
stable: true,
add_bins_to_sysroot: ["rustfmt", "cargo-fmt"]
});

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TestFloatParse {
Expand Down
26 changes: 6 additions & 20 deletions src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,16 +1339,9 @@ impl<'a> Builder<'a> {
}

let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);
self.ensure(tool::Clippy {
compiler: build_compiler,
target: self.build.build,
extra_features: vec![],
});
let cargo_clippy = self.ensure(tool::CargoClippy {
compiler: build_compiler,
target: self.build.build,
extra_features: vec![],
});
self.ensure(tool::Clippy { compiler: build_compiler, target: self.build.build });
let cargo_clippy =
self.ensure(tool::CargoClippy { compiler: build_compiler, target: self.build.build });
let mut dylib_path = helpers::dylib_path();
dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));

Expand All @@ -1363,16 +1356,9 @@ impl<'a> Builder<'a> {
let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);

// Prepare the tools
let miri = self.ensure(tool::Miri {
compiler: build_compiler,
target: self.build.build,
extra_features: Vec::new(),
});
let cargo_miri = self.ensure(tool::CargoMiri {
compiler: build_compiler,
target: self.build.build,
extra_features: Vec::new(),
});
let miri = self.ensure(tool::Miri { compiler: build_compiler, target: self.build.build });
let cargo_miri =
self.ensure(tool::CargoMiri { compiler: build_compiler, target: self.build.build });
// Invoke cargo-miri, make sure it can find miri and cargo.
let mut cmd = command(cargo_miri);
cmd.env("MIRI", &miri);
Expand Down
Loading