rust/xtask/src/main.rs

280 lines
7.6 KiB
Rust
Raw Normal View History

2019-10-26 09:20:44 -05:00
//! 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`
2019-10-30 15:17:27 -05:00
//! for code generation and `cargo xtask install` for installation of
2019-10-26 09:20:44 -05:00
//! 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;
2019-11-20 00:47:14 -06:00
use anyhow::{bail, Result};
2020-08-18 12:31:06 -05:00
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,
2020-07-24 08:59:01 -05:00
dist::DistCmd,
install::{InstallCmd, Malloc, ServerOpt},
2020-07-24 17:16:21 -05:00
metrics::MetricsCmd,
pre_cache::PreCacheCmd,
2020-07-07 11:12:22 -05:00
release::{PromoteCmd, ReleaseCmd},
2018-12-31 07:14:06 -06:00
};
2018-07-30 06:06:22 -05:00
fn main() -> Result<()> {
2020-10-16 12:46:03 -05:00
let _d = pushd(project_root())?;
2020-01-08 04:27:31 -06:00
let mut args = Arguments::from_env();
let subcommand = args.subcommand()?.unwrap_or_default();
2020-01-07 07:42:56 -06:00
match subcommand.as_str() {
2019-10-17 11:36:55 -05:00
"install" => {
2020-01-07 07:42:56 -06:00
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
2021-01-18 12:25:55 -06:00
--mimalloc Use mimalloc allocator for server
--jemalloc Use jemalloc allocator for server
-h, --help Prints help information
2020-01-07 07:42:56 -06:00
"
);
2019-09-10 10:17:11 -05:00
return Ok(());
}
2020-01-07 07:42:56 -06:00
let server = args.contains("--server");
let client_code = args.contains("--client");
2019-09-10 10:17:11 -05:00
if server && client_code {
2020-01-07 07:42:56 -06:00
eprintln!(
"error: The argument `--server` cannot be used with `--client`\n\n\
2020-01-07 07:42:56 -06:00
For more information try --help"
);
return Ok(());
}
2020-01-07 07:42:56 -06:00
2021-01-18 12:25:55 -06:00
let malloc = if args.contains("--mimalloc") {
Malloc::Mimalloc
} else if args.contains("--jemalloc") {
Malloc::Jemalloc
} else {
Malloc::System
};
2020-01-07 07:42:56 -06:00
let client_opt = args.opt_value_from_str("--client")?;
finish_args(args)?;
2020-01-07 07:42:56 -06:00
InstallCmd {
client: if server { None } else { Some(client_opt.unwrap_or_default()) },
2020-07-13 19:12:49 -05:00
server: if client_code { None } else { Some(ServerOpt { malloc }) },
2020-01-07 07:42:56 -06:00
}
.run()
}
2019-10-17 11:36:55 -05:00
"codegen" => {
2020-08-18 12:31:06 -05:00
let features = args.contains("--features");
finish_args(args)?;
2020-08-18 12:31:06 -05:00
CodegenCmd { features }.run()
}
2020-01-07 07:42:56 -06:00
"lint" => {
finish_args(args)?;
2020-01-07 07:42:56 -06:00
run_clippy()
}
"fuzz-tests" => {
finish_args(args)?;
2020-01-07 07:42:56 -06:00
run_fuzzer()
}
"pre-cache" => {
finish_args(args)?;
PreCacheCmd.run()
}
2020-02-10 08:32:03 -06:00
"release" => {
2020-02-14 11:33:30 -06:00
let dry_run = args.contains("--dry-run");
finish_args(args)?;
2020-06-08 07:00:30 -05:00
ReleaseCmd { dry_run }.run()
2020-02-10 08:32:03 -06:00
}
2020-07-07 11:12:22 -05:00
"promote" => {
let dry_run = args.contains("--dry-run");
finish_args(args)?;
2020-07-07 11:12:22 -05:00
PromoteCmd { dry_run }.run()
}
"dist" => {
2020-04-08 04:47:40 -05:00
let nightly = args.contains("--nightly");
let client_version: Option<String> = args.opt_value_from_str("--client")?;
finish_args(args)?;
2020-07-24 08:59:01 -05:00
DistCmd { nightly, client_version }.run()
}
2020-07-24 17:16:21 -05:00
"metrics" => {
let dry_run = args.contains("--dry-run");
finish_args(args)?;
2020-07-24 17:16:21 -05:00
MetricsCmd { dry_run }.run()
}
2021-01-11 12:39:16 -06:00
"bb" => {
let suffix: String = args.free_from_str()?;
finish_args(args)?;
{
let _d = pushd("./crates/rust-analyzer")?;
cmd!("cargo build --release --features jemalloc").run()?;
}
2021-01-11 12:39:16 -06:00
cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", suffix))?;
Ok(())
}
2020-01-07 07:42:56 -06:00
_ => {
eprintln!(
"\
cargo xtask
Run custom build command.
USAGE:
cargo xtask <SUBCOMMAND>
SUBCOMMANDS:
fuzz-tests
codegen
install
lint
2020-07-24 08:59:01 -05:00
dist
2021-01-11 12:39:16 -06:00
promote
bb"
2020-01-07 07:42:56 -06:00
);
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)
}
}