Skip to content

Commit 70a3189

Browse files
committed
Auto merge of rust-lang#135014 - Zalathar:tool-build-step, r=jieyouxu
bootstrap: Overhaul and simplify the `tool_extended!` macro Similar to rust-lang#134950, but for the macro that declares build steps for some tools. The main changes are: - Removing some functionality that isn't needed by any of the tools currently using the macro - Moving some code out of the macro and into ordinary helper functions - Switching to one macro invocation per tool, and struct-like syntax so that rustfmt will format them There should be no functional change.
2 parents bf6f8a4 + 5a32a35 commit 70a3189

File tree

4 files changed

+135
-113
lines changed

4 files changed

+135
-113
lines changed

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ impl Step for Rls {
11521152
let compiler = self.compiler;
11531153
let target = self.target;
11541154

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

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

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

1293-
let miri = builder.ensure(tool::Miri { compiler, target, extra_features: Vec::new() });
1294-
let cargomiri =
1295-
builder.ensure(tool::CargoMiri { compiler, target, extra_features: Vec::new() });
1292+
let miri = builder.ensure(tool::Miri { compiler, target });
1293+
let cargomiri = builder.ensure(tool::CargoMiri { compiler, target });
12961294

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

1426-
let rustfmt =
1427-
builder.ensure(tool::Rustfmt { compiler, target, extra_features: Vec::new() });
1428-
let cargofmt =
1429-
builder.ensure(tool::Cargofmt { compiler, target, extra_features: Vec::new() });
1424+
let rustfmt = builder.ensure(tool::Rustfmt { compiler, target });
1425+
let cargofmt = builder.ensure(tool::Cargofmt { compiler, target });
14301426
let mut tarball = Tarball::new(builder, "rustfmt", &target.triple);
14311427
tarball.set_overlay(OverlayKind::Rustfmt);
14321428
tarball.is_preview(true);

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

+4-12
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl Step for Rustfmt {
409409
let host = self.host;
410410
let compiler = builder.compiler(stage, host);
411411

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

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

513513
// Build our tools.
514-
let miri = builder.ensure(tool::Miri {
515-
compiler: host_compiler,
516-
target: host,
517-
extra_features: Vec::new(),
518-
});
514+
let miri = builder.ensure(tool::Miri { compiler: host_compiler, target: host });
519515
// the ui tests also assume cargo-miri has been built
520-
builder.ensure(tool::CargoMiri {
521-
compiler: host_compiler,
522-
target: host,
523-
extra_features: Vec::new(),
524-
});
516+
builder.ensure(tool::CargoMiri { compiler: host_compiler, target: host });
525517

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

743-
builder.ensure(tool::Clippy { compiler, target: self.host, extra_features: Vec::new() });
735+
builder.ensure(tool::Clippy { compiler, target: self.host });
744736
let mut cargo = tool::prepare_tool_cargo(
745737
builder,
746738
compiler,

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

+118-70
Original file line numberDiff line numberDiff line change
@@ -1007,102 +1007,150 @@ impl Step for LibcxxVersionTool {
10071007
}
10081008

10091009
macro_rules! tool_extended {
1010-
(($sel:ident, $builder:ident),
1011-
$($name:ident,
1012-
$path:expr,
1013-
$tool_name:expr,
1014-
stable = $stable:expr
1015-
$(,tool_std = $tool_std:literal)?
1016-
$(,allow_features = $allow_features:expr)?
1017-
$(,add_bins_to_sysroot = $add_bins_to_sysroot:expr)?
1018-
;)+) => {
1019-
$(
1020-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1010+
(
1011+
$name:ident {
1012+
path: $path:expr,
1013+
tool_name: $tool_name:expr,
1014+
stable: $stable:expr
1015+
$( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
1016+
$( , )?
1017+
}
1018+
) => {
1019+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
10211020
pub struct $name {
10221021
pub compiler: Compiler,
10231022
pub target: TargetSelection,
1024-
pub extra_features: Vec<String>,
10251023
}
10261024

10271025
impl Step for $name {
10281026
type Output = PathBuf;
1029-
const DEFAULT: bool = true; // Overwritten below
1027+
const DEFAULT: bool = true; // Overridden by `should_run_tool_build_step`
10301028
const ONLY_HOSTS: bool = true;
10311029

10321030
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1033-
let builder = run.builder;
1034-
run.path($path).default_condition(
1035-
builder.config.extended
1036-
&& builder.config.tools.as_ref().map_or(
1037-
// By default, on nightly/dev enable all tools, else only
1038-
// build stable tools.
1039-
$stable || builder.build.unstable_features(),
1040-
// If `tools` is set, search list for this tool.
1041-
|tools| {
1042-
tools.iter().any(|tool| match tool.as_ref() {
1043-
"clippy" => $tool_name == "clippy-driver",
1044-
x => $tool_name == x,
1045-
})
1046-
}),
1031+
should_run_tool_build_step(
1032+
run,
1033+
$tool_name,
1034+
$path,
1035+
$stable,
10471036
)
10481037
}
10491038

10501039
fn make_run(run: RunConfig<'_>) {
10511040
run.builder.ensure($name {
10521041
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
10531042
target: run.target,
1054-
extra_features: Vec::new(),
10551043
});
10561044
}
10571045

1058-
#[allow(unused_mut)]
1059-
fn run(mut $sel, $builder: &Builder<'_>) -> PathBuf {
1060-
let tool = $builder.ensure(ToolBuild {
1061-
compiler: $sel.compiler,
1062-
target: $sel.target,
1063-
tool: $tool_name,
1064-
mode: if false $(|| $tool_std)? { Mode::ToolStd } else { Mode::ToolRustc },
1065-
path: $path,
1066-
extra_features: $sel.extra_features,
1067-
source_type: SourceType::InTree,
1068-
allow_features: concat!($($allow_features)*),
1069-
cargo_args: vec![]
1070-
});
1071-
1072-
if (false $(|| !$add_bins_to_sysroot.is_empty())?) && $sel.compiler.stage > 0 {
1073-
let bindir = $builder.sysroot($sel.compiler).join("bin");
1074-
t!(fs::create_dir_all(&bindir));
1075-
1076-
#[allow(unused_variables)]
1077-
let tools_out = $builder
1078-
.cargo_out($sel.compiler, Mode::ToolRustc, $sel.target);
1046+
fn run(self, builder: &Builder<'_>) -> PathBuf {
1047+
let Self { compiler, target } = self;
1048+
run_tool_build_step(
1049+
builder,
1050+
compiler,
1051+
target,
1052+
$tool_name,
1053+
$path,
1054+
None $( .or(Some(&$add_bins_to_sysroot)) )?,
1055+
)
1056+
}
1057+
}
1058+
}
1059+
}
10791060

1080-
$(for add_bin in $add_bins_to_sysroot {
1081-
let bin_source = tools_out.join(exe(add_bin, $sel.target));
1082-
let bin_destination = bindir.join(exe(add_bin, $sel.compiler.host));
1083-
$builder.copy_link(&bin_source, &bin_destination);
1084-
})?
1061+
fn should_run_tool_build_step<'a>(
1062+
run: ShouldRun<'a>,
1063+
tool_name: &'static str,
1064+
path: &'static str,
1065+
stable: bool,
1066+
) -> ShouldRun<'a> {
1067+
let builder = run.builder;
1068+
run.path(path).default_condition(
1069+
builder.config.extended
1070+
&& builder.config.tools.as_ref().map_or(
1071+
// By default, on nightly/dev enable all tools, else only
1072+
// build stable tools.
1073+
stable || builder.build.unstable_features(),
1074+
// If `tools` is set, search list for this tool.
1075+
|tools| {
1076+
tools.iter().any(|tool| match tool.as_ref() {
1077+
"clippy" => tool_name == "clippy-driver",
1078+
x => tool_name == x,
1079+
})
1080+
},
1081+
),
1082+
)
1083+
}
10851084

1086-
let tool = bindir.join(exe($tool_name, $sel.compiler.host));
1087-
tool
1088-
} else {
1089-
tool
1090-
}
1091-
}
1085+
fn run_tool_build_step(
1086+
builder: &Builder<'_>,
1087+
compiler: Compiler,
1088+
target: TargetSelection,
1089+
tool_name: &'static str,
1090+
path: &'static str,
1091+
add_bins_to_sysroot: Option<&[&str]>,
1092+
) -> PathBuf {
1093+
let tool = builder.ensure(ToolBuild {
1094+
compiler,
1095+
target,
1096+
tool: tool_name,
1097+
mode: Mode::ToolRustc,
1098+
path,
1099+
extra_features: vec![],
1100+
source_type: SourceType::InTree,
1101+
allow_features: "",
1102+
cargo_args: vec![],
1103+
});
1104+
1105+
// FIXME: This should just be an if-let-chain, but those are unstable.
1106+
if let Some(add_bins_to_sysroot) =
1107+
add_bins_to_sysroot.filter(|bins| !bins.is_empty() && compiler.stage > 0)
1108+
{
1109+
let bindir = builder.sysroot(compiler).join("bin");
1110+
t!(fs::create_dir_all(&bindir));
1111+
1112+
let tools_out = builder.cargo_out(compiler, Mode::ToolRustc, target);
1113+
1114+
for add_bin in add_bins_to_sysroot {
1115+
let bin_source = tools_out.join(exe(add_bin, target));
1116+
let bin_destination = bindir.join(exe(add_bin, compiler.host));
1117+
builder.copy_link(&bin_source, &bin_destination);
10921118
}
1093-
)+
1119+
1120+
// Return a path into the bin dir.
1121+
bindir.join(exe(tool_name, compiler.host))
1122+
} else {
1123+
tool
10941124
}
10951125
}
10961126

1097-
tool_extended!((self, builder),
1098-
Cargofmt, "src/tools/rustfmt", "cargo-fmt", stable=true;
1099-
CargoClippy, "src/tools/clippy", "cargo-clippy", stable=true;
1100-
Clippy, "src/tools/clippy", "clippy-driver", stable=true, add_bins_to_sysroot = ["clippy-driver", "cargo-clippy"];
1101-
Miri, "src/tools/miri", "miri", stable=false, add_bins_to_sysroot = ["miri"];
1102-
CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", stable=false, add_bins_to_sysroot = ["cargo-miri"];
1103-
Rls, "src/tools/rls", "rls", stable=true;
1104-
Rustfmt, "src/tools/rustfmt", "rustfmt", stable=true, add_bins_to_sysroot = ["rustfmt", "cargo-fmt"];
1105-
);
1127+
tool_extended!(Cargofmt { path: "src/tools/rustfmt", tool_name: "cargo-fmt", stable: true });
1128+
tool_extended!(CargoClippy { path: "src/tools/clippy", tool_name: "cargo-clippy", stable: true });
1129+
tool_extended!(Clippy {
1130+
path: "src/tools/clippy",
1131+
tool_name: "clippy-driver",
1132+
stable: true,
1133+
add_bins_to_sysroot: ["clippy-driver", "cargo-clippy"]
1134+
});
1135+
tool_extended!(Miri {
1136+
path: "src/tools/miri",
1137+
tool_name: "miri",
1138+
stable: false,
1139+
add_bins_to_sysroot: ["miri"]
1140+
});
1141+
tool_extended!(CargoMiri {
1142+
path: "src/tools/miri/cargo-miri",
1143+
tool_name: "cargo-miri",
1144+
stable: false,
1145+
add_bins_to_sysroot: ["cargo-miri"]
1146+
});
1147+
tool_extended!(Rls { path: "src/tools/rls", tool_name: "rls", stable: true });
1148+
tool_extended!(Rustfmt {
1149+
path: "src/tools/rustfmt",
1150+
tool_name: "rustfmt",
1151+
stable: true,
1152+
add_bins_to_sysroot: ["rustfmt", "cargo-fmt"]
1153+
});
11061154

11071155
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11081156
pub struct TestFloatParse {

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

+6-20
Original file line numberDiff line numberDiff line change
@@ -1339,16 +1339,9 @@ impl<'a> Builder<'a> {
13391339
}
13401340

13411341
let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);
1342-
self.ensure(tool::Clippy {
1343-
compiler: build_compiler,
1344-
target: self.build.build,
1345-
extra_features: vec![],
1346-
});
1347-
let cargo_clippy = self.ensure(tool::CargoClippy {
1348-
compiler: build_compiler,
1349-
target: self.build.build,
1350-
extra_features: vec![],
1351-
});
1342+
self.ensure(tool::Clippy { compiler: build_compiler, target: self.build.build });
1343+
let cargo_clippy =
1344+
self.ensure(tool::CargoClippy { compiler: build_compiler, target: self.build.build });
13521345
let mut dylib_path = helpers::dylib_path();
13531346
dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));
13541347

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

13651358
// Prepare the tools
1366-
let miri = self.ensure(tool::Miri {
1367-
compiler: build_compiler,
1368-
target: self.build.build,
1369-
extra_features: Vec::new(),
1370-
});
1371-
let cargo_miri = self.ensure(tool::CargoMiri {
1372-
compiler: build_compiler,
1373-
target: self.build.build,
1374-
extra_features: Vec::new(),
1375-
});
1359+
let miri = self.ensure(tool::Miri { compiler: build_compiler, target: self.build.build });
1360+
let cargo_miri =
1361+
self.ensure(tool::CargoMiri { compiler: build_compiler, target: self.build.build });
13761362
// Invoke cargo-miri, make sure it can find miri and cargo.
13771363
let mut cmd = command(cargo_miri);
13781364
cmd.env("MIRI", &miri);

0 commit comments

Comments
 (0)