]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/setup.rs
Auto merge of #101620 - cjgillot:compute_lint_levels_by_def, r=oli-obk
[rust.git] / src / bootstrap / setup.rs
1 use crate::{t, VERSION};
2 use crate::{Config, TargetSelection};
3 use std::env::consts::EXE_SUFFIX;
4 use std::fmt::Write as _;
5 use std::fs::File;
6 use std::path::{Path, PathBuf, MAIN_SEPARATOR};
7 use std::process::Command;
8 use std::str::FromStr;
9 use std::{
10     env, fmt, fs,
11     io::{self, Write},
12 };
13
14 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
15 pub enum Profile {
16     Compiler,
17     Codegen,
18     Library,
19     Tools,
20     User,
21 }
22
23 impl Profile {
24     fn include_path(&self, src_path: &Path) -> PathBuf {
25         PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
26     }
27
28     pub fn all() -> impl Iterator<Item = Self> {
29         use Profile::*;
30         // N.B. these are ordered by how they are displayed, not alphabetically
31         [Library, Compiler, Codegen, Tools, User].iter().copied()
32     }
33
34     pub fn purpose(&self) -> String {
35         use Profile::*;
36         match self {
37             Library => "Contribute to the standard library",
38             Compiler => "Contribute to the compiler itself",
39             Codegen => "Contribute to the compiler, and also modify LLVM or codegen",
40             Tools => "Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri)",
41             User => "Install Rust from source",
42         }
43         .to_string()
44     }
45
46     pub fn all_for_help(indent: &str) -> String {
47         let mut out = String::new();
48         for choice in Profile::all() {
49             writeln!(&mut out, "{}{}: {}", indent, choice, choice.purpose()).unwrap();
50         }
51         out
52     }
53 }
54
55 impl FromStr for Profile {
56     type Err = String;
57
58     fn from_str(s: &str) -> Result<Self, Self::Err> {
59         match s {
60             "lib" | "library" => Ok(Profile::Library),
61             "compiler" => Ok(Profile::Compiler),
62             "llvm" | "codegen" => Ok(Profile::Codegen),
63             "maintainer" | "user" => Ok(Profile::User),
64             "tools" | "tool" | "rustdoc" | "clippy" | "miri" | "rustfmt" | "rls" => {
65                 Ok(Profile::Tools)
66             }
67             _ => Err(format!("unknown profile: '{}'", s)),
68         }
69     }
70 }
71
72 impl fmt::Display for Profile {
73     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"),
80         }
81     }
82 }
83
84 pub fn setup(config: &Config, profile: Profile) {
85     let path = &config.config;
86
87     if path.exists() {
88         eprintln!(
89             "error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
90             path.display()
91         );
92         eprintln!("help: try adding `profile = \"{}\"` at the top of {}", profile, path.display());
93         eprintln!(
94             "note: this will use the configuration in {}",
95             profile.include_path(&config.src).display()
96         );
97         crate::detail_exit(1);
98     }
99
100     let settings = format!(
101         "# Includes one of the default files in src/bootstrap/defaults\n\
102     profile = \"{}\"\n\
103     changelog-seen = {}\n",
104         profile, VERSION
105     );
106     t!(fs::write(path, settings));
107
108     let include_path = profile.include_path(&config.src);
109     println!("`x.py` will now use the configuration at {}", include_path.display());
110
111     let build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));
112     let stage_path =
113         ["build", build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string());
114
115     println!();
116
117     if !rustup_installed() && profile != Profile::User {
118         eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
119     } else if stage_dir_exists(&stage_path[..]) {
120         attempt_toolchain_link(&stage_path[..]);
121     }
122
123     let suggestions = match profile {
124         Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
125         Profile::Tools => &[
126             "check",
127             "build",
128             "test src/test/rustdoc*",
129             "test src/tools/clippy",
130             "test src/tools/miri",
131             "test src/tools/rustfmt",
132         ],
133         Profile::Library => &["check", "build", "test library/std", "doc"],
134         Profile::User => &["dist", "build"],
135     };
136
137     println!();
138
139     t!(install_git_hook_maybe(&config));
140
141     println!();
142
143     println!("To get started, try one of the following commands:");
144     for cmd in suggestions {
145         println!("- `x.py {}`", cmd);
146     }
147
148     if profile != Profile::User {
149         println!(
150             "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
151         );
152     }
153 }
154
155 fn rustup_installed() -> bool {
156     Command::new("rustup")
157         .arg("--version")
158         .stdout(std::process::Stdio::null())
159         .output()
160         .map_or(false, |output| output.status.success())
161 }
162
163 fn stage_dir_exists(stage_path: &str) -> bool {
164     match fs::create_dir(&stage_path) {
165         Ok(_) => true,
166         Err(_) => Path::new(&stage_path).exists(),
167     }
168 }
169
170 fn attempt_toolchain_link(stage_path: &str) {
171     if toolchain_is_linked() {
172         return;
173     }
174
175     if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
176         eprintln!(
177             "Failed to create a template for stage 1 toolchain or confirm that it already exists"
178         );
179         return;
180     }
181
182     if try_link_toolchain(&stage_path) {
183         println!(
184             "Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
185         );
186     } else {
187         eprintln!("`rustup` failed to link stage 1 build to `stage1` toolchain");
188         eprintln!(
189             "To manually link stage 1 build to `stage1` toolchain, run:\n
190             `rustup toolchain link stage1 {}`",
191             &stage_path
192         );
193     }
194 }
195
196 fn toolchain_is_linked() -> bool {
197     match Command::new("rustup")
198         .args(&["toolchain", "list"])
199         .stdout(std::process::Stdio::piped())
200         .output()
201     {
202         Ok(toolchain_list) => {
203             if !String::from_utf8_lossy(&toolchain_list.stdout).contains("stage1") {
204                 return false;
205             }
206             // The toolchain has already been linked.
207             println!(
208                 "`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
209             );
210         }
211         Err(_) => {
212             // In this case, we don't know if the `stage1` toolchain has been linked;
213             // but `rustup` failed, so let's not go any further.
214             println!(
215                 "`rustup` failed to list current toolchains; not attempting to link `stage1` toolchain"
216             );
217         }
218     }
219     true
220 }
221
222 fn try_link_toolchain(stage_path: &str) -> bool {
223     Command::new("rustup")
224         .stdout(std::process::Stdio::null())
225         .args(&["toolchain", "link", "stage1", &stage_path])
226         .output()
227         .map_or(false, |output| output.status.success())
228 }
229
230 fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
231     let pathbuf = PathBuf::from(stage_path);
232
233     if fs::create_dir_all(pathbuf.join("lib")).is_err() {
234         return false;
235     };
236
237     let pathbuf = pathbuf.join("bin");
238     if fs::create_dir_all(&pathbuf).is_err() {
239         return false;
240     };
241
242     let pathbuf = pathbuf.join(format!("rustc{}", EXE_SUFFIX));
243
244     if pathbuf.exists() {
245         return true;
246     }
247
248     // Take care not to overwrite the file
249     let result = File::options().append(true).create(true).open(&pathbuf);
250     if result.is_err() {
251         return false;
252     }
253
254     return true;
255 }
256
257 // Used to get the path for `Subcommand::Setup`
258 pub fn interactive_path() -> io::Result<Profile> {
259     fn abbrev_all() -> impl Iterator<Item = ((String, String), Profile)> {
260         ('a'..)
261             .zip(1..)
262             .map(|(letter, number)| (letter.to_string(), number.to_string()))
263             .zip(Profile::all())
264     }
265
266     fn parse_with_abbrev(input: &str) -> Result<Profile, String> {
267         let input = input.trim().to_lowercase();
268         for ((letter, number), profile) in abbrev_all() {
269             if input == letter || input == number {
270                 return Ok(profile);
271             }
272         }
273         input.parse()
274     }
275
276     println!("Welcome to the Rust project! What do you want to do with x.py?");
277     for ((letter, _), profile) in abbrev_all() {
278         println!("{}) {}: {}", letter, profile, profile.purpose());
279     }
280     let template = loop {
281         print!(
282             "Please choose one ({}): ",
283             abbrev_all().map(|((l, _), _)| l).collect::<Vec<_>>().join("/")
284         );
285         io::stdout().flush()?;
286         let mut input = String::new();
287         io::stdin().read_line(&mut input)?;
288         if input.is_empty() {
289             eprintln!("EOF on stdin, when expecting answer to question.  Giving up.");
290             crate::detail_exit(1);
291         }
292         break match parse_with_abbrev(&input) {
293             Ok(profile) => profile,
294             Err(err) => {
295                 eprintln!("error: {}", err);
296                 eprintln!("note: press Ctrl+C to exit");
297                 continue;
298             }
299         };
300     };
301     Ok(template)
302 }
303
304 // install a git hook to automatically run tidy --bless, if they want
305 fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
306     let mut input = String::new();
307     println!(
308         "Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
309 If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` before
310 pushing your code to ensure your code is up to par. If you decide later that this behavior is
311 undesirable, simply delete the `pre-push` file from .git/hooks."
312     );
313
314     let should_install = loop {
315         print!("Would you like to install the git hook?: [y/N] ");
316         io::stdout().flush()?;
317         input.clear();
318         io::stdin().read_line(&mut input)?;
319         break match input.trim().to_lowercase().as_str() {
320             "y" | "yes" => true,
321             "n" | "no" | "" => false,
322             _ => {
323                 eprintln!("error: unrecognized option '{}'", input.trim());
324                 eprintln!("note: press Ctrl+C to exit");
325                 continue;
326             }
327         };
328     };
329
330     if should_install {
331         let src = config.src.join("src").join("etc").join("pre-push.sh");
332         let git =
333             t!(config.git().args(&["rev-parse", "--git-common-dir"]).output().map(|output| {
334                 assert!(output.status.success(), "failed to run `git`");
335                 PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
336             }));
337         let dst = git.join("hooks").join("pre-push");
338         match fs::hard_link(src, &dst) {
339             Err(e) => eprintln!(
340                 "error: could not create hook {}: do you already have the git hook installed?\n{}",
341                 dst.display(),
342                 e
343             ),
344             Ok(_) => println!("Linked `src/etc/pre-push.sh` to `.git/hooks/pre-push`"),
345         };
346     } else {
347         println!("Ok, skipping installation!");
348     }
349     Ok(())
350 }