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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
//! See https://github.com/matklad/cargo-xtask/.
//!
//! This binary defines various auxiliary build commands, which are not
//! expressible with just `cargo`. Notably, it provides `cargo xtask codegen`
//! for code generation and `cargo xtask install` for installation of
//! rust-analyzer server and client.
//!
//! This binary is integrated into the `cargo` command line by using an alias in
//! `.cargo/config`.
mod codegen;
mod ast_src;
#[cfg(test)]
mod tidy;
mod install;
mod release;
mod dist;
mod metrics;
mod pre_cache;
use anyhow::{bail, Result};
use codegen::CodegenCmd;
use pico_args::Arguments;
use std::{
env,
path::{Path, PathBuf},
};
use walkdir::{DirEntry, WalkDir};
use xshell::{cmd, cp, pushd, pushenv};
use crate::{
codegen::Mode,
dist::DistCmd,
install::{InstallCmd, Malloc, ServerOpt},
metrics::MetricsCmd,
pre_cache::PreCacheCmd,
release::{PromoteCmd, ReleaseCmd},
};
fn main() -> Result<()> {
let _d = pushd(project_root())?;
let mut args = Arguments::from_env();
let subcommand = args.subcommand()?.unwrap_or_default();
match subcommand.as_str() {
"install" => {
if args.contains(["-h", "--help"]) {
eprintln!(
"\
cargo xtask install
Install rust-analyzer server or editor plugin.
USAGE:
cargo xtask install [FLAGS]
FLAGS:
--client[=CLIENT] Install only VS Code plugin.
CLIENT is one of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'
--server Install only the language server
--mimalloc Use mimalloc allocator for server
--jemalloc Use jemalloc allocator for server
-h, --help Prints help information
"
);
return Ok(());
}
let server = args.contains("--server");
let client_code = args.contains("--client");
if server && client_code {
eprintln!(
"error: The argument `--server` cannot be used with `--client`\n\n\
For more information try --help"
);
return Ok(());
}
let malloc = if args.contains("--mimalloc") {
Malloc::Mimalloc
} else if args.contains("--jemalloc") {
Malloc::Jemalloc
} else {
Malloc::System
};
let client_opt = args.opt_value_from_str("--client")?;
finish_args(args)?;
InstallCmd {
client: if server { None } else { Some(client_opt.unwrap_or_default()) },
server: if client_code { None } else { Some(ServerOpt { malloc }) },
}
.run()
}
"codegen" => {
let features = args.contains("--features");
finish_args(args)?;
CodegenCmd { features }.run()
}
"lint" => {
finish_args(args)?;
run_clippy()
}
"fuzz-tests" => {
finish_args(args)?;
run_fuzzer()
}
"pre-cache" => {
finish_args(args)?;
PreCacheCmd.run()
}
"release" => {
let dry_run = args.contains("--dry-run");
finish_args(args)?;
ReleaseCmd { dry_run }.run()
}
"promote" => {
let dry_run = args.contains("--dry-run");
finish_args(args)?;
PromoteCmd { dry_run }.run()
}
"dist" => {
let nightly = args.contains("--nightly");
let client_version: Option<String> = args.opt_value_from_str("--client")?;
finish_args(args)?;
DistCmd { nightly, client_version }.run()
}
"metrics" => {
let dry_run = args.contains("--dry-run");
finish_args(args)?;
MetricsCmd { dry_run }.run()
}
"bb" => {
let suffix: String = args.free_from_str()?;
finish_args(args)?;
{
let _d = pushd("./crates/rust-analyzer")?;
cmd!("cargo build --release --features jemalloc").run()?;
}
cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", suffix))?;
Ok(())
}
_ => {
eprintln!(
"\
cargo xtask
Run custom build command.
USAGE:
cargo xtask <SUBCOMMAND>
SUBCOMMANDS:
fuzz-tests
codegen
install
lint
dist
promote
bb"
);
Ok(())
}
}
}
fn finish_args(args: Arguments) -> Result<()> {
if !args.finish().is_empty() {
bail!("Unused arguments.");
}
Ok(())
}
fn project_root() -> PathBuf {
Path::new(
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
)
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}
fn rust_files() -> impl Iterator<Item = PathBuf> {
rust_files_in(&project_root().join("crates"))
}
#[cfg(test)]
fn cargo_files() -> impl Iterator<Item = PathBuf> {
files_in(&project_root(), "toml")
.filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
}
fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
files_in(path, "rs")
}
fn run_rustfmt(mode: Mode) -> Result<()> {
let _dir = pushd(project_root())?;
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
ensure_rustfmt()?;
let check = match mode {
Mode::Overwrite => &[][..],
Mode::Verify => &["--", "--check"],
};
cmd!("cargo fmt {check...}").run()?;
Ok(())
}
fn ensure_rustfmt() -> Result<()> {
let out = cmd!("rustfmt --version").read()?;
if !out.contains("stable") {
bail!(
"Failed to run rustfmt from toolchain 'stable'. \
Please run `rustup component add rustfmt --toolchain stable` to install it.",
)
}
Ok(())
}
fn run_clippy() -> Result<()> {
if cmd!("cargo clippy --version").read().is_err() {
bail!(
"Failed run cargo clippy. \
Please run `rustup component add clippy` to install it.",
)
}
let allowed_lints = "
-A clippy::collapsible_if
-A clippy::needless_pass_by_value
-A clippy::nonminimal_bool
-A clippy::redundant_pattern_matching
"
.split_ascii_whitespace();
cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?;
Ok(())
}
fn run_fuzzer() -> Result<()> {
let _d = pushd("./crates/syntax")?;
let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
if cmd!("cargo fuzz --help").read().is_err() {
cmd!("cargo install cargo-fuzz").run()?;
};
// Expecting nightly rustc
let out = cmd!("rustc --version").read()?;
if !out.contains("nightly") {
bail!("fuzz tests require nightly rustc")
}
cmd!("cargo fuzz run parser").run()?;
Ok(())
}
fn date_iso() -> Result<String> {
let res = cmd!("date --iso --utc").read()?;
Ok(res)
}
fn is_release_tag(tag: &str) -> bool {
tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
}
fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
let iter = WalkDir::new(path);
return iter
.into_iter()
.filter_entry(|e| !is_hidden(e))
.map(|e| e.unwrap())
.filter(|e| !e.file_type().is_dir())
.map(|e| e.into_path())
.filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
fn is_hidden(entry: &DirEntry) -> bool {
entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
}
}
|