1 use crate::TargetSelection;
2 use crate::{t, VERSION};
3 use std::env::consts::EXE_SUFFIX;
4 use std::fmt::Write as _;
6 use std::path::{Path, PathBuf, MAIN_SEPARATOR};
7 use std::process::Command;
14 #[derive(Clone, Copy, Eq, PartialEq)]
24 fn include_path(&self, src_path: &Path) -> PathBuf {
25 PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
28 pub fn all() -> impl Iterator<Item = Self> {
30 // N.B. these are ordered by how they are displayed, not alphabetically
31 [Library, Compiler, Codegen, Tools, User].iter().copied()
34 pub fn purpose(&self) -> String {
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",
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();
55 impl FromStr for Profile {
58 fn from_str(s: &str) -> Result<Self, Self::Err> {
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" => {
67 _ => Err(format!("unknown profile: '{}'", s)),
72 impl fmt::Display for Profile {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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"),
84 pub fn setup(src_path: &Path, profile: Profile) {
85 let cfg_file = env::var_os("BOOTSTRAP_CONFIG").map(PathBuf::from);
87 if cfg_file.as_ref().map_or(false, |f| f.exists()) {
88 let file = cfg_file.unwrap();
90 "error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
93 println!("help: try adding `profile = \"{}\"` at the top of {}", profile, file.display());
95 "note: this will use the configuration in {}",
96 profile.include_path(src_path).display()
98 std::process::exit(1);
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\
105 changelog-seen = {}\n",
108 t!(fs::write(path, settings));
110 let include_path = profile.include_path(src_path);
111 println!("`x.py` will now use the configuration at {}", include_path.display());
113 let build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));
115 ["build", build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string());
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[..]);
125 let suggestions = match profile {
126 Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
130 "test src/test/rustdoc*",
131 "test src/tools/clippy",
132 "test src/tools/miri",
133 "test src/tools/rustfmt",
135 Profile::Library => &["check", "build", "test library/std", "doc"],
136 Profile::User => &["dist", "build"],
141 t!(install_git_hook_maybe(src_path));
145 println!("To get started, try one of the following commands:");
146 for cmd in suggestions {
147 println!("- `x.py {}`", cmd);
150 if profile != Profile::User {
152 "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
157 fn rustup_installed() -> bool {
158 Command::new("rustup")
160 .stdout(std::process::Stdio::null())
162 .map_or(false, |output| output.status.success())
165 fn stage_dir_exists(stage_path: &str) -> bool {
166 match fs::create_dir(&stage_path[..]) {
168 Err(_) => Path::new(&stage_path[..]).exists(),
172 fn attempt_toolchain_link(stage_path: &str) {
173 if toolchain_is_linked() {
177 if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
179 "Failed to create a template for stage 1 toolchain or confirm that it already exists"
184 if try_link_toolchain(&stage_path[..]) {
186 "Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
189 println!("`rustup` failed to link stage 1 build to `stage1` toolchain");
191 "To manually link stage 1 build to `stage1` toolchain, run:\n
192 `rustup toolchain link stage1 {}`",
198 fn toolchain_is_linked() -> bool {
199 match Command::new("rustup")
200 .args(&["toolchain", "list"])
201 .stdout(std::process::Stdio::piped())
204 Ok(toolchain_list) => {
205 if !String::from_utf8_lossy(&toolchain_list.stdout).contains("stage1") {
208 // The toolchain has already been linked.
210 "`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
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.
217 "`rustup` failed to list current toolchains; not attempting to link `stage1` toolchain"
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[..]])
229 .map_or(false, |output| output.status.success())
232 fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
233 let pathbuf = PathBuf::from(stage_path);
235 if fs::create_dir_all(pathbuf.join("lib")).is_err() {
239 let pathbuf = pathbuf.join("bin");
240 if fs::create_dir_all(&pathbuf).is_err() {
244 let pathbuf = pathbuf.join(format!("rustc{}", EXE_SUFFIX));
246 if pathbuf.exists() {
250 // Take care not to overwrite the file
251 let result = File::options().append(true).create(true).open(&pathbuf);
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)> {
264 .map(|(letter, number)| (letter.to_string(), number.to_string()))
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 {
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());
282 let template = loop {
284 "Please choose one ({}): ",
285 abbrev_all().map(|((l, _), _)| l).collect::<Vec<_>>().join("/")
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);
294 break match parse_with_abbrev(&input) {
295 Ok(profile) => profile,
297 println!("error: {}", err);
298 println!("note: press Ctrl+C to exit");
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();
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."
316 let should_install = loop {
317 print!("Would you like to install the git hook?: [y/N] ");
318 io::stdout().flush()?;
320 io::stdin().read_line(&mut input)?;
321 break match input.trim().to_lowercase().as_str() {
323 "n" | "no" | "" => false,
325 println!("error: unrecognized option '{}'", input.trim());
326 println!("note: press Ctrl+C to exit");
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(
336 assert!(output.status.success(), "failed to run `git`");
337 PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
340 let dst = git.join("hooks").join("pre-push");
341 match fs::hard_link(src, &dst) {
343 "error: could not create hook {}: do you already have the git hook installed?\n{}",
347 Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-push`"),
350 println!("Ok, skipping installation!");