1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
use crate::{t, VERSION};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{
env, fmt, fs,
io::{self, Write},
};
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum Profile {
Compiler,
Codegen,
Library,
User,
}
impl Profile {
fn include_path(&self, src_path: &Path) -> PathBuf {
PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
}
pub fn all() -> impl Iterator<Item = Self> {
[Profile::Compiler, Profile::Codegen, Profile::Library, Profile::User].iter().copied()
}
}
impl FromStr for Profile {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"a" | "lib" | "library" => Ok(Profile::Library),
"b" | "compiler" | "rustdoc" => Ok(Profile::Compiler),
"c" | "llvm" | "codegen" => Ok(Profile::Codegen),
"d" | "maintainer" | "user" => Ok(Profile::User),
_ => Err(format!("unknown profile: '{}'", s)),
}
}
}
impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Profile::Compiler => write!(f, "compiler"),
Profile::Codegen => write!(f, "codegen"),
Profile::Library => write!(f, "library"),
Profile::User => write!(f, "user"),
}
}
}
pub fn setup(src_path: &Path, profile: Profile) {
let cfg_file = env::var_os("BOOTSTRAP_CONFIG").map(PathBuf::from);
if cfg_file.as_ref().map_or(false, |f| f.exists()) {
let file = cfg_file.unwrap();
println!(
"error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
file.display()
);
println!("help: try adding `profile = \"{}\"` at the top of {}", profile, file.display());
println!(
"note: this will use the configuration in {}",
profile.include_path(src_path).display()
);
std::process::exit(1);
}
let path = cfg_file.unwrap_or_else(|| src_path.join("config.toml"));
let settings = format!(
"# Includes one of the default files in src/bootstrap/defaults\n\
profile = \"{}\"\n\
changelog-seen = {}\n",
profile, VERSION
);
t!(fs::write(path, settings));
let include_path = profile.include_path(src_path);
println!("`x.py` will now use the configuration at {}", include_path.display());
let suggestions = match profile {
Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
Profile::Library => &["check", "build", "test library/std", "doc"],
Profile::User => &["dist", "build"],
};
println!();
t!(install_git_hook_maybe(src_path));
println!();
println!("To get started, try one of the following commands:");
for cmd in suggestions {
println!("- `x.py {}`", cmd);
}
if profile != Profile::User {
println!(
"For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
);
}
}
// Used to get the path for `Subcommand::Setup`
pub fn interactive_path() -> io::Result<Profile> {
let mut input = String::new();
println!(
"Welcome to the Rust project! What do you want to do with x.py?
a) Contribute to the standard library
b) Contribute to the compiler or rustdoc
c) Contribute to the compiler, and also modify LLVM or codegen
d) Install Rust from source"
);
let template = loop {
print!("Please choose one (a/b/c/d): ");
io::stdout().flush()?;
io::stdin().read_line(&mut input)?;
break match input.trim().to_lowercase().parse() {
Ok(profile) => profile,
Err(err) => {
println!("error: {}", err);
println!("note: press Ctrl+C to exit");
continue;
}
};
};
Ok(template)
}
// install a git hook to automatically run tidy --bless, if they want
fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> {
let mut input = String::new();
println!(
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit
to ensure your code is up to par. If you decide later that this behavior is undesirable,
simply delete the `pre-commit` file from .git/hooks."
);
let should_install = loop {
print!("Would you like to install the git hook?: [y/N] ");
io::stdout().flush()?;
input.clear();
io::stdin().read_line(&mut input)?;
break match input.trim().to_lowercase().as_str() {
"y" | "yes" => true,
"n" | "no" | "" => false,
_ => {
println!("error: unrecognized option '{}'", input.trim());
println!("note: press Ctrl+C to exit");
continue;
}
};
};
Ok(if should_install {
let src = src_path.join("src").join("etc").join("pre-commit.sh");
let dst = src_path.join(".git").join("hooks").join("pre-commit");
match fs::hard_link(src, dst) {
Err(e) => println!(
"x.py encountered an error -- do you already have the git hook installed?\n{}",
e
),
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`"),
};
} else {
println!("Ok, skipping installation!");
})
}
|