Skip to content

Commit 1a18da5

Browse files
committed
implement x run rustfmt
Signed-off-by: onur-ozkan <[email protected]>
1 parent 34c42c8 commit 1a18da5

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/bootstrap/src/core/build_steps/format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
124124
pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
125125
if build.kind == Kind::Format && build.top_stage != 0 {
126126
eprintln!("ERROR: `x fmt` only supports stage 0.");
127+
eprintln!("HELP: Use `x run rustfmt` to run in-tree rustfmt.");
127128
crate::exit!(1);
128129
}
129130

src/bootstrap/src/core/build_steps/run.rs

+53
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,56 @@ impl Step for CoverageDump {
420420
cmd.run(builder);
421421
}
422422
}
423+
424+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
425+
pub struct Rustfmt;
426+
427+
impl Step for Rustfmt {
428+
type Output = ();
429+
const ONLY_HOSTS: bool = true;
430+
431+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
432+
run.path("src/tools/rustfmt")
433+
}
434+
435+
fn make_run(run: RunConfig<'_>) {
436+
run.builder.ensure(Rustfmt);
437+
}
438+
439+
fn run(self, builder: &Builder<'_>) {
440+
let host = builder.build.build;
441+
442+
// `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
443+
// Change the stage to 1 if it's not set explicitly.
444+
let stage = if builder.config.is_explicit_stage() || builder.top_stage >= 1 {
445+
builder.top_stage
446+
} else {
447+
1
448+
};
449+
450+
if stage == 0 {
451+
eprintln!("rustfmt cannot be run at stage 0");
452+
eprintln!("HELP: Use `x fmt` to use stage 0 rustfmt.");
453+
std::process::exit(1);
454+
}
455+
456+
let compiler = builder.compiler(stage, host);
457+
let rustfmt_build = builder.ensure(tool::Rustfmt { compiler, target: host });
458+
459+
let mut rustfmt = tool::prepare_tool_cargo(
460+
builder,
461+
rustfmt_build.build_compiler,
462+
Mode::ToolRustc,
463+
host,
464+
Kind::Run,
465+
"src/tools/rustfmt",
466+
SourceType::InTree,
467+
&[],
468+
);
469+
470+
rustfmt.args(["--bin", "rustfmt", "--"]);
471+
rustfmt.args(builder.config.args());
472+
473+
rustfmt.into_cmd().run(builder);
474+
}
475+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ impl<'a> Builder<'a> {
11161116
run::FeaturesStatusDump,
11171117
run::CyclicStep,
11181118
run::CoverageDump,
1119+
run::Rustfmt,
11191120
),
11201121
Kind::Setup => {
11211122
describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)

0 commit comments

Comments
 (0)