Skip to content

Commit a3d4434

Browse files
authored
Rollup merge of rust-lang#96584 - bentongxyz:x-setup-h-v-should-work, r=jyn514
Fix `x setup -h -v` should work r? `@jyn514` I have to convert profile to path and back in order to remove special-casing in bootstrap. I also check for `dry_run` so that `config.toml` and/ or `.git/hooks/pre-push` will not be created if `--dry-run` is specified. Please help me see if this is ok, thanks alot!
2 parents b6097f2 + b2bc65b commit a3d4434

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

src/bootstrap/builder.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::flags::{Color, Subcommand};
1919
use crate::install;
2020
use crate::native;
2121
use crate::run;
22+
use crate::setup;
2223
use crate::test;
2324
use crate::tool::{self, SourceType};
2425
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
@@ -433,8 +434,11 @@ impl<'a> ShouldRun<'a> {
433434

434435
// single alias, which does not correspond to any on-disk path
435436
pub fn alias(mut self, alias: &str) -> Self {
437+
// exceptional case for `Kind::Setup` because its `library`
438+
// and `compiler` options would otherwise naively match with
439+
// `compiler` and `library` folders respectively.
436440
assert!(
437-
!self.builder.src.join(alias).exists(),
441+
self.kind == Kind::Setup || !self.builder.src.join(alias).exists(),
438442
"use `builder.path()` for real paths: {}",
439443
alias
440444
);
@@ -754,8 +758,9 @@ impl<'a> Builder<'a> {
754758
run::ReplaceVersionPlaceholder,
755759
run::Miri,
756760
),
761+
Kind::Setup => describe!(setup::Profile),
757762
// These commands either don't use paths, or they're special-cased in Build::build()
758-
Kind::Clean | Kind::Format | Kind::Setup => vec![],
763+
Kind::Clean | Kind::Format => vec![],
759764
}
760765
}
761766

@@ -818,7 +823,8 @@ impl<'a> Builder<'a> {
818823
Subcommand::Install { ref paths } => (Kind::Install, &paths[..]),
819824
Subcommand::Run { ref paths, .. } => (Kind::Run, &paths[..]),
820825
Subcommand::Format { .. } => (Kind::Format, &[][..]),
821-
Subcommand::Clean { .. } | Subcommand::Setup { .. } => {
826+
Subcommand::Setup { ref path } => (Kind::Setup, std::slice::from_ref(path)),
827+
Subcommand::Clean { .. } => {
822828
panic!()
823829
}
824830
};

src/bootstrap/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub enum Subcommand {
143143
args: Vec<String>,
144144
},
145145
Setup {
146-
profile: Profile,
146+
path: PathBuf,
147147
},
148148
}
149149

@@ -637,7 +637,7 @@ Arguments:
637637
} else {
638638
t!(crate::setup::interactive_path())
639639
};
640-
Subcommand::Setup { profile }
640+
Subcommand::Setup { path: PathBuf::from(profile.as_str()) }
641641
}
642642
};
643643

src/bootstrap/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,6 @@ impl Build {
693693
return clean::clean(self, all);
694694
}
695695

696-
if let Subcommand::Setup { profile } = &self.config.cmd {
697-
return setup::setup(&self.config, *profile);
698-
}
699-
700696
// Download rustfmt early so that it can be used in rust-analyzer configs.
701697
let _ = &builder::Builder::new(&self).initial_rustfmt();
702698

src/bootstrap/setup.rs

+53-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
12
use crate::{t, VERSION};
23
use crate::{Config, TargetSelection};
34
use std::env::consts::EXE_SUFFIX;
@@ -11,7 +12,7 @@ use std::{
1112
io::{self, Write},
1213
};
1314

14-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
1516
pub enum Profile {
1617
Compiler,
1718
Codegen,
@@ -50,6 +51,16 @@ impl Profile {
5051
}
5152
out
5253
}
54+
55+
pub fn as_str(&self) -> &'static str {
56+
match self {
57+
Profile::Compiler => "compiler",
58+
Profile::Codegen => "codegen",
59+
Profile::Library => "library",
60+
Profile::Tools => "tools",
61+
Profile::User => "user",
62+
}
63+
}
5364
}
5465

5566
impl FromStr for Profile {
@@ -71,13 +82,43 @@ impl FromStr for Profile {
7182

7283
impl fmt::Display for Profile {
7384
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74-
match self {
75-
Profile::Compiler => write!(f, "compiler"),
76-
Profile::Codegen => write!(f, "codegen"),
77-
Profile::Library => write!(f, "library"),
78-
Profile::User => write!(f, "user"),
79-
Profile::Tools => write!(f, "tools"),
85+
f.write_str(self.as_str())
86+
}
87+
}
88+
89+
impl Step for Profile {
90+
type Output = ();
91+
const DEFAULT: bool = true;
92+
93+
fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
94+
for choice in Profile::all() {
95+
run = run.alias(choice.as_str());
8096
}
97+
run
98+
}
99+
100+
fn make_run(run: RunConfig<'_>) {
101+
// for Profile, `run.paths` will have 1 and only 1 element
102+
// this is because we only accept at most 1 path from user input.
103+
// If user calls `x.py setup` without arguments, the interacctive TUI
104+
// will guide user to provide one.
105+
let profile: Profile = run
106+
.paths
107+
.first()
108+
.unwrap()
109+
.assert_single_path()
110+
.path
111+
.to_owned()
112+
.into_os_string()
113+
.into_string()
114+
.unwrap()
115+
.parse()
116+
.unwrap();
117+
run.builder.ensure(profile);
118+
}
119+
120+
fn run(self, builder: &Builder<'_>) {
121+
setup(&builder.build.config, self)
81122
}
82123
}
83124

@@ -103,6 +144,7 @@ pub fn setup(config: &Config, profile: Profile) {
103144
changelog-seen = {}\n",
104145
profile, VERSION
105146
);
147+
106148
t!(fs::write(path, settings));
107149

108150
let include_path = profile.include_path(&config.src);
@@ -116,7 +158,7 @@ pub fn setup(config: &Config, profile: Profile) {
116158

117159
if !rustup_installed() && profile != Profile::User {
118160
eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
119-
} else if stage_dir_exists(&stage_path[..]) {
161+
} else if stage_dir_exists(&stage_path[..]) && !config.dry_run {
120162
attempt_toolchain_link(&stage_path[..]);
121163
}
122164

@@ -136,7 +178,9 @@ pub fn setup(config: &Config, profile: Profile) {
136178

137179
println!();
138180

139-
t!(install_git_hook_maybe(&config));
181+
if !config.dry_run {
182+
t!(install_git_hook_maybe(&config));
183+
}
140184

141185
println!();
142186

0 commit comments

Comments
 (0)