Skip to content

Commit 735bed7

Browse files
authored
Add cargo dev setup toolchain --standalone (#14230)
Allows creating a toolchain that's independent of the local build, for example to make two separate toolchains with slight differences without requiring two checkouts of clippy changelog: none
2 parents 1750411 + 058ae80 commit 735bed7

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

clippy_dev/src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ fn main() {
5454
setup::git_hook::install_hook(force_override);
5555
}
5656
},
57-
SetupSubcommand::Toolchain { force, release, name } => setup::toolchain::create(force, release, &name),
57+
SetupSubcommand::Toolchain {
58+
standalone,
59+
force,
60+
release,
61+
name,
62+
} => setup::toolchain::create(standalone, force, release, &name),
5863
SetupSubcommand::VscodeTasks { remove, force_override } => {
5964
if remove {
6065
setup::vscode::remove_tasks();
@@ -271,14 +276,25 @@ enum SetupSubcommand {
271276
force_override: bool,
272277
},
273278
/// Install a rustup toolchain pointing to the local clippy build
279+
///
280+
/// This creates a toolchain with symlinks pointing at
281+
/// `target/.../{clippy-driver,cargo-clippy}`, rebuilds of the project will be reflected in the
282+
/// created toolchain unless `--standalone` is passed
274283
Toolchain {
284+
#[arg(long, short)]
285+
/// Create a standalone toolchain by copying the clippy binaries instead
286+
/// of symlinking them
287+
///
288+
/// Use this for example to create a toolchain, make a small change and then make another
289+
/// toolchain with a different name in order to easily compare the two
290+
standalone: bool,
275291
#[arg(long, short)]
276292
/// Override an existing toolchain
277293
force: bool,
278294
#[arg(long, short)]
279295
/// Point to --release clippy binary
280296
release: bool,
281-
#[arg(long, default_value = "clippy")]
297+
#[arg(long, short, default_value = "clippy")]
282298
/// Name of the toolchain
283299
name: String,
284300
},

clippy_dev/src/setup/toolchain.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ use std::env::current_dir;
33
use std::ffi::OsStr;
44
use std::fs;
55
use std::path::{Path, PathBuf};
6+
use std::process::Command;
67
use walkdir::WalkDir;
78

9+
use crate::utils::exit_if_err;
10+
811
use super::verify_inside_clippy_dir;
912

10-
pub fn create(force: bool, release: bool, name: &str) {
13+
pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
1114
if !verify_inside_clippy_dir() {
1215
return;
1316
}
@@ -48,14 +51,22 @@ pub fn create(force: bool, release: bool, name: &str) {
4851
}
4952
}
5053

51-
symlink_bin("cargo-clippy", &dest, release);
52-
symlink_bin("clippy-driver", &dest, release);
54+
let status = Command::new("cargo")
55+
.arg("build")
56+
.args(release.then_some("--release"))
57+
.status();
58+
exit_if_err(status);
59+
60+
install_bin("cargo-clippy", &dest, standalone, release);
61+
install_bin("clippy-driver", &dest, standalone, release);
5362

5463
println!("Created toolchain {name}, use it in other projects with e.g. `cargo +{name} clippy`");
55-
println!("Note: This will need to be re-run whenever the Clippy `rust-toolchain` changes");
64+
if !standalone {
65+
println!("Note: This will need to be re-run whenever the Clippy `rust-toolchain` changes");
66+
}
5667
}
5768

58-
fn symlink_bin(bin: &str, dest: &Path, release: bool) {
69+
fn install_bin(bin: &str, dest: &Path, standalone: bool, release: bool) {
5970
#[cfg(windows)]
6071
use std::os::windows::fs::symlink_file as symlink;
6172

@@ -71,5 +82,9 @@ fn symlink_bin(bin: &str, dest: &Path, release: bool) {
7182
let mut dest = dest.to_path_buf();
7283
dest.extend(["bin", &file_name]);
7384

74-
symlink(src, dest).unwrap();
85+
if standalone {
86+
fs::copy(src, dest).unwrap();
87+
} else {
88+
symlink(src, dest).unwrap();
89+
}
7590
}

0 commit comments

Comments
 (0)