1 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
3 use crate::{t, VERSION};
4 use std::env::consts::EXE_SUFFIX;
5 use std::fmt::Write as _;
8 use std::path::{Path, PathBuf, MAIN_SEPARATOR};
9 use std::process::Command;
10 use std::str::FromStr;
11 use std::{fmt, fs, io};
13 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
23 fn include_path(&self, src_path: &Path) -> PathBuf {
24 PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
27 pub fn all() -> impl Iterator<Item = Self> {
29 // N.B. these are ordered by how they are displayed, not alphabetically
30 [Library, Compiler, Codegen, Tools, User].iter().copied()
33 pub fn purpose(&self) -> String {
36 Library => "Contribute to the standard library",
37 Compiler => "Contribute to the compiler itself",
38 Codegen => "Contribute to the compiler, and also modify LLVM or codegen",
39 Tools => "Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri)",
40 User => "Install Rust from source",
45 pub fn all_for_help(indent: &str) -> String {
46 let mut out = String::new();
47 for choice in Profile::all() {
48 writeln!(&mut out, "{}{}: {}", indent, choice, choice.purpose()).unwrap();
53 pub fn as_str(&self) -> &'static str {
55 Profile::Compiler => "compiler",
56 Profile::Codegen => "codegen",
57 Profile::Library => "library",
58 Profile::Tools => "tools",
59 Profile::User => "user",
64 impl FromStr for Profile {
67 fn from_str(s: &str) -> Result<Self, Self::Err> {
69 "lib" | "library" => Ok(Profile::Library),
70 "compiler" => Ok(Profile::Compiler),
71 "llvm" | "codegen" => Ok(Profile::Codegen),
72 "maintainer" | "user" => Ok(Profile::User),
73 "tools" | "tool" | "rustdoc" | "clippy" | "miri" | "rustfmt" | "rls" => {
76 _ => Err(format!("unknown profile: '{}'", s)),
81 impl fmt::Display for Profile {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 f.write_str(self.as_str())
87 impl Step for Profile {
89 const DEFAULT: bool = true;
91 fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
92 for choice in Profile::all() {
93 run = run.alias(choice.as_str());
98 fn make_run(run: RunConfig<'_>) {
99 // for Profile, `run.paths` will have 1 and only 1 element
100 // this is because we only accept at most 1 path from user input.
101 // If user calls `x.py setup` without arguments, the interactive TUI
102 // will guide user to provide one.
103 let profile = if run.paths.len() > 1 {
104 // HACK: `builder` runs this step with all paths if no path was passed.
105 t!(interactive_path())
110 .assert_single_path()
120 run.builder.ensure(profile);
123 fn run(self, builder: &Builder<'_>) {
124 setup(&builder.build.config, self)
128 pub fn setup(config: &Config, profile: Profile) {
130 ["build", config.build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string());
132 if !rustup_installed() && profile != Profile::User {
133 eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
134 } else if stage_dir_exists(&stage_path[..]) && !config.dry_run() {
135 attempt_toolchain_link(&stage_path[..]);
138 let suggestions = match profile {
139 Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
143 "test src/test/rustdoc*",
144 "test src/tools/clippy",
145 "test src/tools/miri",
146 "test src/tools/rustfmt",
148 Profile::Library => &["check", "build", "test library/std", "doc"],
149 Profile::User => &["dist", "build"],
152 if !config.dry_run() {
153 t!(install_git_hook_maybe(&config));
158 println!("To get started, try one of the following commands:");
159 for cmd in suggestions {
160 println!("- `x.py {}`", cmd);
163 if profile != Profile::User {
165 "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
169 let path = &config.config.clone().unwrap_or(PathBuf::from("config.toml"));
170 setup_config_toml(path, profile, config);
173 fn setup_config_toml(path: &PathBuf, profile: Profile, config: &Config) {
177 "error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
180 eprintln!("help: try adding `profile = \"{}\"` at the top of {}", profile, path.display());
182 "note: this will use the configuration in {}",
183 profile.include_path(&config.src).display()
185 crate::detail_exit(1);
188 let settings = format!(
189 "# Includes one of the default files in src/bootstrap/defaults\n\
191 changelog-seen = {}\n",
195 t!(fs::write(path, settings));
197 let include_path = profile.include_path(&config.src);
198 println!("`x.py` will now use the configuration at {}", include_path.display());
201 fn rustup_installed() -> bool {
202 Command::new("rustup")
204 .stdout(std::process::Stdio::null())
206 .map_or(false, |output| output.status.success())
209 fn stage_dir_exists(stage_path: &str) -> bool {
210 match fs::create_dir(&stage_path) {
212 Err(_) => Path::new(&stage_path).exists(),
216 fn attempt_toolchain_link(stage_path: &str) {
217 if toolchain_is_linked() {
221 if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
223 "Failed to create a template for stage 1 toolchain or confirm that it already exists"
228 if try_link_toolchain(&stage_path) {
230 "Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
233 eprintln!("`rustup` failed to link stage 1 build to `stage1` toolchain");
235 "To manually link stage 1 build to `stage1` toolchain, run:\n
236 `rustup toolchain link stage1 {}`",
242 fn toolchain_is_linked() -> bool {
243 match Command::new("rustup")
244 .args(&["toolchain", "list"])
245 .stdout(std::process::Stdio::piped())
248 Ok(toolchain_list) => {
249 if !String::from_utf8_lossy(&toolchain_list.stdout).contains("stage1") {
252 // The toolchain has already been linked.
254 "`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
258 // In this case, we don't know if the `stage1` toolchain has been linked;
259 // but `rustup` failed, so let's not go any further.
261 "`rustup` failed to list current toolchains; not attempting to link `stage1` toolchain"
268 fn try_link_toolchain(stage_path: &str) -> bool {
269 Command::new("rustup")
270 .stdout(std::process::Stdio::null())
271 .args(&["toolchain", "link", "stage1", &stage_path])
273 .map_or(false, |output| output.status.success())
276 fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
277 let pathbuf = PathBuf::from(stage_path);
279 if fs::create_dir_all(pathbuf.join("lib")).is_err() {
283 let pathbuf = pathbuf.join("bin");
284 if fs::create_dir_all(&pathbuf).is_err() {
288 let pathbuf = pathbuf.join(format!("rustc{}", EXE_SUFFIX));
290 if pathbuf.exists() {
294 // Take care not to overwrite the file
295 let result = File::options().append(true).create(true).open(&pathbuf);
303 // Used to get the path for `Subcommand::Setup`
304 pub fn interactive_path() -> io::Result<Profile> {
305 fn abbrev_all() -> impl Iterator<Item = ((String, String), Profile)> {
308 .map(|(letter, number)| (letter.to_string(), number.to_string()))
312 fn parse_with_abbrev(input: &str) -> Result<Profile, String> {
313 let input = input.trim().to_lowercase();
314 for ((letter, number), profile) in abbrev_all() {
315 if input == letter || input == number {
322 println!("Welcome to the Rust project! What do you want to do with x.py?");
323 for ((letter, _), profile) in abbrev_all() {
324 println!("{}) {}: {}", letter, profile, profile.purpose());
326 let template = loop {
328 "Please choose one ({}): ",
329 abbrev_all().map(|((l, _), _)| l).collect::<Vec<_>>().join("/")
331 io::stdout().flush()?;
332 let mut input = String::new();
333 io::stdin().read_line(&mut input)?;
334 if input.is_empty() {
335 eprintln!("EOF on stdin, when expecting answer to question. Giving up.");
336 crate::detail_exit(1);
338 break match parse_with_abbrev(&input) {
339 Ok(profile) => profile,
341 eprintln!("error: {}", err);
342 eprintln!("note: press Ctrl+C to exit");
350 // install a git hook to automatically run tidy --bless, if they want
351 fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
352 let git = t!(config.git().args(&["rev-parse", "--git-common-dir"]).output().map(|output| {
353 assert!(output.status.success(), "failed to run `git`");
354 PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
356 let dst = git.join("hooks").join("pre-push");
358 // The git hook has already been set up, or the user already has a custom hook.
362 let mut input = String::new();
365 "Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
366 If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` before
367 pushing your code to ensure your code is up to par. If you decide later that this behavior is
368 undesirable, simply delete the `pre-push` file from .git/hooks."
371 let should_install = loop {
372 print!("Would you like to install the git hook?: [y/N] ");
373 io::stdout().flush()?;
375 io::stdin().read_line(&mut input)?;
376 break match input.trim().to_lowercase().as_str() {
378 "n" | "no" | "" => false,
380 eprintln!("error: unrecognized option '{}'", input.trim());
381 eprintln!("note: press Ctrl+C to exit");
388 let src = config.src.join("src").join("etc").join("pre-push.sh");
389 match fs::hard_link(src, &dst) {
391 "error: could not create hook {}: do you already have the git hook installed?\n{}",
395 Ok(_) => println!("Linked `src/etc/pre-push.sh` to `.git/hooks/pre-push`"),
398 println!("Ok, skipping installation!");