Skip to content

Commit

Permalink
Rollup merge of rust-lang#96584 - bentongxyz:x-setup-h-v-should-work,…
Browse files Browse the repository at this point in the history
… 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!
  • Loading branch information
matthiaskrgr authored Nov 17, 2022
2 parents b6097f2 + b2bc65b commit a3d4434
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::flags::{Color, Subcommand};
use crate::install;
use crate::native;
use crate::run;
use crate::setup;
use crate::test;
use crate::tool::{self, SourceType};
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
Expand Down Expand Up @@ -433,8 +434,11 @@ impl<'a> ShouldRun<'a> {

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

Expand Down Expand Up @@ -818,7 +823,8 @@ impl<'a> Builder<'a> {
Subcommand::Install { ref paths } => (Kind::Install, &paths[..]),
Subcommand::Run { ref paths, .. } => (Kind::Run, &paths[..]),
Subcommand::Format { .. } => (Kind::Format, &[][..]),
Subcommand::Clean { .. } | Subcommand::Setup { .. } => {
Subcommand::Setup { ref path } => (Kind::Setup, std::slice::from_ref(path)),
Subcommand::Clean { .. } => {
panic!()
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub enum Subcommand {
args: Vec<String>,
},
Setup {
profile: Profile,
path: PathBuf,
},
}

Expand Down Expand Up @@ -637,7 +637,7 @@ Arguments:
} else {
t!(crate::setup::interactive_path())
};
Subcommand::Setup { profile }
Subcommand::Setup { path: PathBuf::from(profile.as_str()) }
}
};

Expand Down
4 changes: 0 additions & 4 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,6 @@ impl Build {
return clean::clean(self, all);
}

if let Subcommand::Setup { profile } = &self.config.cmd {
return setup::setup(&self.config, *profile);
}

// Download rustfmt early so that it can be used in rust-analyzer configs.
let _ = &builder::Builder::new(&self).initial_rustfmt();

Expand Down
62 changes: 53 additions & 9 deletions src/bootstrap/setup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::{t, VERSION};
use crate::{Config, TargetSelection};
use std::env::consts::EXE_SUFFIX;
Expand All @@ -11,7 +12,7 @@ use std::{
io::{self, Write},
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Profile {
Compiler,
Codegen,
Expand Down Expand Up @@ -50,6 +51,16 @@ impl Profile {
}
out
}

pub fn as_str(&self) -> &'static str {
match self {
Profile::Compiler => "compiler",
Profile::Codegen => "codegen",
Profile::Library => "library",
Profile::Tools => "tools",
Profile::User => "user",
}
}
}

impl FromStr for Profile {
Expand All @@ -71,13 +82,43 @@ impl FromStr for Profile {

impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Profile::Compiler => write!(f, "compiler"),
Profile::Codegen => write!(f, "codegen"),
Profile::Library => write!(f, "library"),
Profile::User => write!(f, "user"),
Profile::Tools => write!(f, "tools"),
f.write_str(self.as_str())
}
}

impl Step for Profile {
type Output = ();
const DEFAULT: bool = true;

fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
for choice in Profile::all() {
run = run.alias(choice.as_str());
}
run
}

fn make_run(run: RunConfig<'_>) {
// for Profile, `run.paths` will have 1 and only 1 element
// this is because we only accept at most 1 path from user input.
// If user calls `x.py setup` without arguments, the interacctive TUI
// will guide user to provide one.
let profile: Profile = run
.paths
.first()
.unwrap()
.assert_single_path()
.path
.to_owned()
.into_os_string()
.into_string()
.unwrap()
.parse()
.unwrap();
run.builder.ensure(profile);
}

fn run(self, builder: &Builder<'_>) {
setup(&builder.build.config, self)
}
}

Expand All @@ -103,6 +144,7 @@ pub fn setup(config: &Config, profile: Profile) {
changelog-seen = {}\n",
profile, VERSION
);

t!(fs::write(path, settings));

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

if !rustup_installed() && profile != Profile::User {
eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
} else if stage_dir_exists(&stage_path[..]) {
} else if stage_dir_exists(&stage_path[..]) && !config.dry_run {
attempt_toolchain_link(&stage_path[..]);
}

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

println!();

t!(install_git_hook_maybe(&config));
if !config.dry_run {
t!(install_git_hook_maybe(&config));
}

println!();

Expand Down

0 comments on commit a3d4434

Please sign in to comment.