cargo_flux/
cargo-flux.rs

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
use std::{
    env,
    path::PathBuf,
    process::{Command, exit},
};

use anyhow::Result;
use flux_bin::utils::{
    EXIT_ERR, FLUX_SYSROOT, LIB_PATH, get_flux_driver_path, get_rust_toolchain,
    get_rustc_driver_lib_path, prepend_path_to_env_var, sysroot_dir,
};

fn main() {
    let exit_code = match run() {
        Ok(code) => code,
        Err(e) => {
            println!("Failed to run cargo-flux, error={e}");
            EXIT_ERR
        }
    };
    exit(exit_code)
}

fn run() -> Result<i32> {
    let sysroot = sysroot_dir();
    let flux_driver_path = get_flux_driver_path(&sysroot)?;
    let rust_toolchain = get_rust_toolchain()?;
    let ld_library_path = get_rustc_driver_lib_path(&rust_toolchain)?;
    let extended_lib_path = prepend_path_to_env_var(LIB_PATH, ld_library_path)?;

    // Cargo can be called like `cargo [OPTIONS] flux`, so we skip all arguments until `flux` is
    // found.
    let args = env::args()
        .skip_while(|arg| arg != "flux")
        .skip(1)
        .collect::<Vec<_>>();

    let cargo_path = env::var("CARGO_PATH").unwrap_or_else(|_| "cargo".to_string());
    let cargo_target = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string());
    let cargo_target = PathBuf::from_iter([cargo_target, "flux".to_string()]);

    let mut cmd = Command::new(cargo_path);

    // Unless we are calling `cargo flux clean` append a check
    match &args[..] {
        [subcommand, ..] if subcommand == "clean" => {}
        _ => {
            cmd.arg("check");
        }
    }

    let exit_code = cmd
        .args(args)
        .env(LIB_PATH, extended_lib_path)
        .env(FLUX_SYSROOT, sysroot)
        .env("FLUX_BUILD_SYSROOT", "1")
        .env("FLUX_CARGO", "1")
        .env("RUST_TOOLCHAIN", rust_toolchain.clone())
        .env("RUSTUP_TOOLCHAIN", rust_toolchain)
        .env("RUSTC", flux_driver_path)
        .env("CARGO_TARGET_DIR", cargo_target)
        .status()?
        .code();

    Ok(exit_code.unwrap_or(EXIT_ERR))
}