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