]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! Discovery of `cargo` & `rustc` executables. |
2 | ||
3 | #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)] | |
4 | ||
5 | use std::{env, iter, path::PathBuf}; | |
6 | ||
7 | pub fn cargo() -> PathBuf { | |
8 | get_path_for_executable("cargo") | |
9 | } | |
10 | ||
11 | pub fn rustc() -> PathBuf { | |
12 | get_path_for_executable("rustc") | |
13 | } | |
14 | ||
15 | pub fn rustup() -> PathBuf { | |
16 | get_path_for_executable("rustup") | |
17 | } | |
18 | ||
19 | pub fn rustfmt() -> PathBuf { | |
20 | get_path_for_executable("rustfmt") | |
21 | } | |
22 | ||
23 | /// Return a `PathBuf` to use for the given executable. | |
24 | /// | |
25 | /// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that | |
26 | /// gives a valid Cargo executable; or it may return a full path to a valid | |
27 | /// Cargo. | |
28 | fn get_path_for_executable(executable_name: &'static str) -> PathBuf { | |
29 | // The current implementation checks three places for an executable to use: | |
30 | // 1) Appropriate environment variable (erroring if this is set but not a usable executable) | |
31 | // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc | |
32 | // 2) `<executable_name>` | |
33 | // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH | |
353b0b11 FG |
34 | // 3) `$CARGO_HOME/bin/<executable_name>` |
35 | // where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html) | |
36 | // example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset. | |
064997fb FG |
37 | // It seems that this is a reasonable place to try for cargo, rustc, and rustup |
38 | let env_var = executable_name.to_ascii_uppercase(); | |
9c376795 | 39 | if let Some(path) = env::var_os(env_var) { |
064997fb FG |
40 | return path.into(); |
41 | } | |
42 | ||
43 | if lookup_in_path(executable_name) { | |
44 | return executable_name.into(); | |
45 | } | |
46 | ||
353b0b11 | 47 | if let Some(mut path) = get_cargo_home() { |
064997fb FG |
48 | path.push("bin"); |
49 | path.push(executable_name); | |
50 | if let Some(path) = probe(path) { | |
51 | return path; | |
52 | } | |
53 | } | |
54 | ||
55 | executable_name.into() | |
56 | } | |
57 | ||
58 | fn lookup_in_path(exec: &str) -> bool { | |
59 | let paths = env::var_os("PATH").unwrap_or_default(); | |
60 | env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some() | |
61 | } | |
62 | ||
353b0b11 FG |
63 | fn get_cargo_home() -> Option<PathBuf> { |
64 | if let Some(path) = env::var_os("CARGO_HOME") { | |
65 | return Some(path.into()); | |
66 | } | |
67 | ||
68 | if let Some(mut path) = home::home_dir() { | |
69 | path.push(".cargo"); | |
70 | return Some(path); | |
71 | } | |
72 | ||
73 | None | |
74 | } | |
75 | ||
064997fb FG |
76 | fn probe(path: PathBuf) -> Option<PathBuf> { |
77 | let with_extension = match env::consts::EXE_EXTENSION { | |
78 | "" => None, | |
79 | it => Some(path.with_extension(it)), | |
80 | }; | |
81 | iter::once(path).chain(with_extension).find(|it| it.is_file()) | |
82 | } |