Skip to content

Commit c0d0d28

Browse files
committed
implement x run rustfmt
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 16fc26c commit c0d0d28

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,55 @@ impl Step for CyclicStep {
392392
builder.ensure(CyclicStep { n: self.n.saturating_sub(1) })
393393
}
394394
}
395+
396+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
397+
pub struct Rustfmt;
398+
399+
impl Step for Rustfmt {
400+
type Output = ();
401+
const ONLY_HOSTS: bool = true;
402+
403+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
404+
run.path("src/tools/rustfmt")
405+
}
406+
407+
fn make_run(run: RunConfig<'_>) {
408+
run.builder.ensure(Rustfmt);
409+
}
410+
411+
fn run(self, builder: &Builder<'_>) {
412+
let host = builder.build.build;
413+
414+
// `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
415+
// Change the stage to 1 if it's not set explicitly.
416+
let stage = if builder.config.is_explicit_stage() || builder.top_stage >= 1 {
417+
builder.top_stage
418+
} else {
419+
1
420+
};
421+
422+
if stage == 0 {
423+
eprintln!("rustfmt cannot be run at stage 0");
424+
std::process::exit(1);
425+
}
426+
427+
let compiler = builder.compiler(stage, host);
428+
let rustfmt_build = builder.ensure(tool::Rustfmt { compiler, target: host });
429+
430+
let mut rustfmt = tool::prepare_tool_cargo(
431+
builder,
432+
rustfmt_build.build_compiler,
433+
Mode::ToolRustc,
434+
host,
435+
Kind::Run,
436+
"src/tools/rustfmt",
437+
SourceType::InTree,
438+
&[],
439+
);
440+
441+
rustfmt.args(["--bin", "rustfmt", "--"]);
442+
rustfmt.args(builder.config.args());
443+
444+
rustfmt.into_cmd().run(builder);
445+
}
446+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ impl<'a> Builder<'a> {
11141114
run::UnicodeTableGenerator,
11151115
run::FeaturesStatusDump,
11161116
run::CyclicStep,
1117+
run::Rustfmt,
11171118
),
11181119
Kind::Setup => {
11191120
describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)

0 commit comments

Comments
 (0)