2020-08-12 09:52:28 -05:00
|
|
|
//! Discovery of `cargo` & `rustc` executables.
|
2022-07-20 07:59:42 -05:00
|
|
|
|
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
|
|
|
|
|
2020-05-08 07:54:29 -05:00
|
|
|
use std::{env, iter, path::PathBuf};
|
2020-05-05 16:41:47 -05:00
|
|
|
|
2020-05-08 07:54:29 -05:00
|
|
|
pub fn cargo() -> PathBuf {
|
|
|
|
get_path_for_executable("cargo")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rustc() -> PathBuf {
|
|
|
|
get_path_for_executable("rustc")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rustup() -> PathBuf {
|
|
|
|
get_path_for_executable("rustup")
|
|
|
|
}
|
2020-05-05 15:44:43 -05:00
|
|
|
|
2020-06-26 18:28:06 -05:00
|
|
|
pub fn rustfmt() -> PathBuf {
|
|
|
|
get_path_for_executable("rustfmt")
|
|
|
|
}
|
|
|
|
|
2020-05-06 14:39:11 -05:00
|
|
|
/// Return a `PathBuf` to use for the given executable.
|
2020-05-05 15:44:43 -05:00
|
|
|
///
|
|
|
|
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
|
|
|
|
/// gives a valid Cargo executable; or it may return a full path to a valid
|
|
|
|
/// Cargo.
|
2020-05-08 07:54:29 -05:00
|
|
|
fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
|
2020-05-05 15:44:43 -05:00
|
|
|
// The current implementation checks three places for an executable to use:
|
|
|
|
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
|
|
|
|
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
|
|
|
|
// 2) `<executable_name>`
|
2020-05-05 16:07:10 -05:00
|
|
|
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
|
2020-05-05 15:44:43 -05:00
|
|
|
// 3) `~/.cargo/bin/<executable_name>`
|
|
|
|
// example: for cargo, this tries ~/.cargo/bin/cargo
|
|
|
|
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
|
|
|
|
let env_var = executable_name.to_ascii_uppercase();
|
2020-05-08 07:54:29 -05:00
|
|
|
if let Some(path) = env::var_os(&env_var) {
|
|
|
|
return path.into();
|
2020-05-08 05:34:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-08 07:54:29 -05:00
|
|
|
if lookup_in_path(executable_name) {
|
|
|
|
return executable_name.into();
|
2020-05-08 05:34:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut path) = home::home_dir() {
|
|
|
|
path.push(".cargo");
|
|
|
|
path.push("bin");
|
|
|
|
path.push(executable_name);
|
2020-06-26 19:00:08 -05:00
|
|
|
if let Some(path) = probe(path) {
|
2020-05-08 07:54:29 -05:00
|
|
|
return path;
|
2020-05-05 15:44:43 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-26 19:00:08 -05:00
|
|
|
|
2020-05-08 07:54:29 -05:00
|
|
|
executable_name.into()
|
2020-05-05 15:44:43 -05:00
|
|
|
}
|
|
|
|
|
2020-05-08 07:54:29 -05:00
|
|
|
fn lookup_in_path(exec: &str) -> bool {
|
|
|
|
let paths = env::var_os("PATH").unwrap_or_default();
|
2020-06-26 19:00:08 -05:00
|
|
|
env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn probe(path: PathBuf) -> Option<PathBuf> {
|
|
|
|
let with_extension = match env::consts::EXE_EXTENSION {
|
|
|
|
"" => None,
|
|
|
|
it => Some(path.with_extension(it)),
|
|
|
|
};
|
|
|
|
iter::once(path).chain(with_extension).find(|it| it.is_file())
|
2020-05-05 15:44:43 -05:00
|
|
|
}
|