about summary refs log tree commit diff
path: root/clippy_dev/src/setup/toolchain.rs
blob: 2966629cf70a397b7d0e8b50b6dce00c6b619b9b (plain)
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
use std::env::consts::EXE_SUFFIX;
use std::env::current_dir;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;

use crate::utils::exit_if_err;

use super::verify_inside_clippy_dir;

pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
    if !verify_inside_clippy_dir() {
        return;
    }

    let rustup_home = std::env::var("RUSTUP_HOME").unwrap();
    let toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap();

    let src = PathBuf::from_iter([&rustup_home, "toolchains", &toolchain]);
    let dest = PathBuf::from_iter([&rustup_home, "toolchains", name]);

    if dest.exists() {
        if force {
            fs::remove_dir_all(&dest).unwrap();
        } else {
            println!("{} already exists, pass `--force` to override it", dest.display());
            return;
        }
    }

    for entry in WalkDir::new(&src) {
        let entry = entry.unwrap();
        let relative = entry.path().strip_prefix(&src).unwrap();

        if relative.starts_with("bin")
            && matches!(
                relative.file_stem().and_then(OsStr::to_str),
                Some("cargo-clippy" | "clippy-driver")
            )
        {
            continue;
        }

        let target = dest.join(relative);
        if entry.file_type().is_dir() {
            fs::create_dir(&target).unwrap();
        } else {
            fs::hard_link(entry.path(), target).unwrap();
        }
    }

    let status = Command::new("cargo")
        .arg("build")
        .args(release.then_some("--release"))
        .status();
    exit_if_err(status);

    install_bin("cargo-clippy", &dest, standalone, release);
    install_bin("clippy-driver", &dest, standalone, release);

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

fn install_bin(bin: &str, dest: &Path, standalone: bool, release: bool) {
    #[cfg(windows)]
    use std::os::windows::fs::symlink_file as symlink;

    #[cfg(not(windows))]
    use std::os::unix::fs::symlink;

    let profile = if release { "release" } else { "debug" };
    let file_name = format!("{bin}{EXE_SUFFIX}");

    let mut src = current_dir().unwrap();
    src.extend(["target", profile, &file_name]);

    let mut dest = dest.to_path_buf();
    dest.extend(["bin", &file_name]);

    if standalone {
        fs::copy(src, dest).unwrap();
    } else {
        symlink(src, dest).unwrap();
    }
}