1 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
3 use crate::{t, VERSION};
5 use std::env::consts::EXE_SUFFIX;
6 use std::fmt::Write as _;
9 use std::path::{Path, PathBuf, MAIN_SEPARATOR};
10 use std::process::Command;
11 use std::str::FromStr;
12 use std::{fmt, fs, io};
17 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
26 /// A list of historical hashes of `src/etc/vscode_settings.json`.
27 /// New entries should be appended whenever this is updated so we can detect
28 /// outdated vs. user-modified settings files.
29 static SETTINGS_HASHES: &[&str] = &[
30 "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
31 "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
33 static VSCODE_SETTINGS: &str = include_str!("../etc/vscode_settings.json");
36 fn include_path(&self, src_path: &Path) -> PathBuf {
37 PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
40 pub fn all() -> impl Iterator<Item = Self> {
42 // N.B. these are ordered by how they are displayed, not alphabetically
43 [Library, Compiler, Codegen, Tools, User].iter().copied()
46 pub fn purpose(&self) -> String {
49 Library => "Contribute to the standard library",
50 Compiler => "Contribute to the compiler itself",
51 Codegen => "Contribute to the compiler, and also modify LLVM or codegen",
52 Tools => "Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri)",
53 User => "Install Rust from source",
58 pub fn all_for_help(indent: &str) -> String {
59 let mut out = String::new();
60 for choice in Profile::all() {
61 writeln!(&mut out, "{}{}: {}", indent, choice, choice.purpose()).unwrap();
66 pub fn as_str(&self) -> &'static str {
68 Profile::Compiler => "compiler",
69 Profile::Codegen => "codegen",
70 Profile::Library => "library",
71 Profile::Tools => "tools",
72 Profile::User => "user",
77 impl FromStr for Profile {
80 fn from_str(s: &str) -> Result<Self, Self::Err> {
82 "lib" | "library" => Ok(Profile::Library),
83 "compiler" => Ok(Profile::Compiler),
84 "llvm" | "codegen" => Ok(Profile::Codegen),
85 "maintainer" | "user" => Ok(Profile::User),
86 "tools" | "tool" | "rustdoc" | "clippy" | "miri" | "rustfmt" | "rls" => {
89 _ => Err(format!("unknown profile: '{}'", s)),
94 impl fmt::Display for Profile {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 f.write_str(self.as_str())
100 impl Step for Profile {
102 const DEFAULT: bool = true;
104 fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
105 for choice in Profile::all() {
106 run = run.alias(choice.as_str());
111 fn make_run(run: RunConfig<'_>) {
112 if run.builder.config.dry_run() {
116 // for Profile, `run.paths` will have 1 and only 1 element
117 // this is because we only accept at most 1 path from user input.
118 // If user calls `x.py setup` without arguments, the interactive TUI
119 // will guide user to provide one.
120 let profile = if run.paths.len() > 1 {
121 // HACK: `builder` runs this step with all paths if no path was passed.
122 t!(interactive_path())
127 .assert_single_path()
137 run.builder.ensure(profile);
140 fn run(self, builder: &Builder<'_>) {
141 setup(&builder.build.config, self)
145 pub fn setup(config: &Config, profile: Profile) {
147 ["build", config.build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string());
149 if !rustup_installed() && profile != Profile::User {
150 eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
151 } else if stage_dir_exists(&stage_path[..]) && !config.dry_run() {
152 attempt_toolchain_link(&stage_path[..]);
155 let suggestions = match profile {
156 Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
160 "test tests/rustdoc*",
161 "test src/tools/clippy",
162 "test src/tools/miri",
163 "test src/tools/rustfmt",
165 Profile::Library => &["check", "build", "test library/std", "doc"],
166 Profile::User => &["dist", "build"],
169 if !config.dry_run() {
170 t!(install_git_hook_maybe(&config));
171 t!(create_vscode_settings_maybe(&config));
176 println!("To get started, try one of the following commands:");
177 for cmd in suggestions {
178 println!("- `x.py {}`", cmd);
181 if profile != Profile::User {
183 "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
187 let path = &config.config.clone().unwrap_or(PathBuf::from("config.toml"));
188 setup_config_toml(path, profile, config);
191 fn setup_config_toml(path: &PathBuf, profile: Profile, config: &Config) {
195 "error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
198 eprintln!("help: try adding `profile = \"{}\"` at the top of {}", profile, path.display());
200 "note: this will use the configuration in {}",
201 profile.include_path(&config.src).display()
203 crate::detail_exit(1);
206 let settings = format!(
207 "# Includes one of the default files in src/bootstrap/defaults\n\
209 changelog-seen = {}\n",
213 t!(fs::write(path, settings));
215 let include_path = profile.include_path(&config.src);
216 println!("`x.py` will now use the configuration at {}", include_path.display());
219 fn rustup_installed() -> bool {
220 Command::new("rustup")
222 .stdout(std::process::Stdio::null())
224 .map_or(false, |output| output.status.success())
227 fn stage_dir_exists(stage_path: &str) -> bool {
228 match fs::create_dir(&stage_path) {
230 Err(_) => Path::new(&stage_path).exists(),
234 fn attempt_toolchain_link(stage_path: &str) {
235 if toolchain_is_linked() {
239 if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
241 "Failed to create a template for stage 1 toolchain or confirm that it already exists"
246 if try_link_toolchain(&stage_path) {
248 "Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
251 eprintln!("`rustup` failed to link stage 1 build to `stage1` toolchain");
253 "To manually link stage 1 build to `stage1` toolchain, run:\n
254 `rustup toolchain link stage1 {}`",
260 fn toolchain_is_linked() -> bool {
261 match Command::new("rustup")
262 .args(&["toolchain", "list"])
263 .stdout(std::process::Stdio::piped())
266 Ok(toolchain_list) => {
267 if !String::from_utf8_lossy(&toolchain_list.stdout).contains("stage1") {
270 // The toolchain has already been linked.
272 "`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
276 // In this case, we don't know if the `stage1` toolchain has been linked;
277 // but `rustup` failed, so let's not go any further.
279 "`rustup` failed to list current toolchains; not attempting to link `stage1` toolchain"
286 fn try_link_toolchain(stage_path: &str) -> bool {
287 Command::new("rustup")
288 .stdout(std::process::Stdio::null())
289 .args(&["toolchain", "link", "stage1", &stage_path])
291 .map_or(false, |output| output.status.success())
294 fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
295 let pathbuf = PathBuf::from(stage_path);
297 if fs::create_dir_all(pathbuf.join("lib")).is_err() {
301 let pathbuf = pathbuf.join("bin");
302 if fs::create_dir_all(&pathbuf).is_err() {
306 let pathbuf = pathbuf.join(format!("rustc{}", EXE_SUFFIX));
308 if pathbuf.exists() {
312 // Take care not to overwrite the file
313 let result = File::options().append(true).create(true).open(&pathbuf);
321 // Used to get the path for `Subcommand::Setup`
322 pub fn interactive_path() -> io::Result<Profile> {
323 fn abbrev_all() -> impl Iterator<Item = ((String, String), Profile)> {
326 .map(|(letter, number)| (letter.to_string(), number.to_string()))
330 fn parse_with_abbrev(input: &str) -> Result<Profile, String> {
331 let input = input.trim().to_lowercase();
332 for ((letter, number), profile) in abbrev_all() {
333 if input == letter || input == number {
340 println!("Welcome to the Rust project! What do you want to do with x.py?");
341 for ((letter, _), profile) in abbrev_all() {
342 println!("{}) {}: {}", letter, profile, profile.purpose());
344 let template = loop {
346 "Please choose one ({}): ",
347 abbrev_all().map(|((l, _), _)| l).collect::<Vec<_>>().join("/")
349 io::stdout().flush()?;
350 let mut input = String::new();
351 io::stdin().read_line(&mut input)?;
352 if input.is_empty() {
353 eprintln!("EOF on stdin, when expecting answer to question. Giving up.");
354 crate::detail_exit(1);
356 break match parse_with_abbrev(&input) {
357 Ok(profile) => profile,
359 eprintln!("error: {}", err);
360 eprintln!("note: press Ctrl+C to exit");
375 /// Prompt a user for a answer, looping until they enter an accepted input or nothing
376 fn prompt_user(prompt: &str) -> io::Result<Option<PromptResult>> {
377 let mut input = String::new();
380 io::stdout().flush()?;
382 io::stdin().read_line(&mut input)?;
383 match input.trim().to_lowercase().as_str() {
384 "y" | "yes" => return Ok(Some(PromptResult::Yes)),
385 "n" | "no" => return Ok(Some(PromptResult::No)),
386 "p" | "print" => return Ok(Some(PromptResult::Print)),
387 "" => return Ok(None),
389 eprintln!("error: unrecognized option '{}'", input.trim());
390 eprintln!("note: press Ctrl+C to exit");
396 // install a git hook to automatically run tidy, if they want
397 fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
398 let git = t!(config.git().args(&["rev-parse", "--git-common-dir"]).output().map(|output| {
399 assert!(output.status.success(), "failed to run `git`");
400 PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
402 let dst = git.join("hooks").join("pre-push");
404 // The git hook has already been set up, or the user already has a custom hook.
409 "\nRust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
410 If you'd like, x.py can install a git hook for you that will automatically run `test tidy` before
411 pushing your code to ensure your code is up to par. If you decide later that this behavior is
412 undesirable, simply delete the `pre-push` file from .git/hooks."
415 if prompt_user("Would you like to install the git hook?: [y/N]")? != Some(PromptResult::Yes) {
416 println!("Ok, skipping installation!");
419 let src = config.src.join("src").join("etc").join("pre-push.sh");
420 match fs::hard_link(src, &dst) {
423 "error: could not create hook {}: do you already have the git hook installed?\n{}",
429 Ok(_) => println!("Linked `src/etc/pre-push.sh` to `.git/hooks/pre-push`"),
434 /// Create a `.vscode/settings.json` file for rustc development, or just print it
435 fn create_vscode_settings_maybe(config: &Config) -> io::Result<()> {
436 let (current_hash, historical_hashes) = SETTINGS_HASHES.split_last().unwrap();
437 let vscode_settings = config.src.join(".vscode").join("settings.json");
438 // If None, no settings.json exists
439 // If Some(true), is a previous version of settings.json
440 // If Some(false), is not a previous version (i.e. user modified)
441 // If it's up to date we can just skip this
442 let mut mismatched_settings = None;
443 if let Ok(current) = fs::read_to_string(&vscode_settings) {
444 let mut hasher = sha2::Sha256::new();
445 hasher.update(¤t);
446 let hash = hex::encode(hasher.finalize().as_slice());
447 if hash == *current_hash {
449 } else if historical_hashes.contains(&hash.as_str()) {
450 mismatched_settings = Some(true);
452 mismatched_settings = Some(false);
456 "\nx.py can automatically install the recommended `.vscode/settings.json` file for rustc development"
458 match mismatched_settings {
459 Some(true) => eprintln!(
460 "warning: existing `.vscode/settings.json` is out of date, x.py will update it"
462 Some(false) => eprintln!(
463 "warning: existing `.vscode/settings.json` has been modified by user, x.py will back it up and replace it"
467 let should_create = match prompt_user(
468 "Would you like to create/update `settings.json`, or only print suggested settings?: [y/p/N]",
470 Some(PromptResult::Yes) => true,
471 Some(PromptResult::Print) => false,
473 println!("Ok, skipping settings!");
478 let path = config.src.join(".vscode");
480 fs::create_dir(&path)?;
482 let verb = match mismatched_settings {
483 // exists but outdated, we can replace this
484 Some(true) => "Updated",
485 // exists but user modified, back it up
487 // exists and is not current version or outdated, so back it up
488 let mut backup = vscode_settings.clone();
489 backup.set_extension("bak");
490 eprintln!("warning: copying `settings.json` to `settings.json.bak`");
491 fs::copy(&vscode_settings, &backup)?;
496 fs::write(&vscode_settings, &VSCODE_SETTINGS)?;
497 println!("{verb} `.vscode/settings.json`");
499 println!("\n{VSCODE_SETTINGS}");