summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/toolchain
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/rust-analyzer/crates/toolchain
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rust-analyzer/crates/toolchain')
-rw-r--r--src/tools/rust-analyzer/crates/toolchain/Cargo.toml13
-rw-r--r--src/tools/rust-analyzer/crates/toolchain/src/lib.rs69
2 files changed, 82 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/crates/toolchain/Cargo.toml b/src/tools/rust-analyzer/crates/toolchain/Cargo.toml
new file mode 100644
index 000000000..7d3b9e09e
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/toolchain/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "toolchain"
+version = "0.0.0"
+description = "TBD"
+license = "MIT OR Apache-2.0"
+edition = "2021"
+rust-version = "1.57"
+
+[lib]
+doctest = false
+
+[dependencies]
+home = "0.5.3"
diff --git a/src/tools/rust-analyzer/crates/toolchain/src/lib.rs b/src/tools/rust-analyzer/crates/toolchain/src/lib.rs
new file mode 100644
index 000000000..b05da7691
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/toolchain/src/lib.rs
@@ -0,0 +1,69 @@
+//! Discovery of `cargo` & `rustc` executables.
+
+#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+
+use std::{env, iter, path::PathBuf};
+
+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")
+}
+
+pub fn rustfmt() -> PathBuf {
+ get_path_for_executable("rustfmt")
+}
+
+/// Return a `PathBuf` to use for the given executable.
+///
+/// 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.
+fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
+ // 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>`
+ // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
+ // 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();
+ if let Some(path) = env::var_os(&env_var) {
+ return path.into();
+ }
+
+ if lookup_in_path(executable_name) {
+ return executable_name.into();
+ }
+
+ if let Some(mut path) = home::home_dir() {
+ path.push(".cargo");
+ path.push("bin");
+ path.push(executable_name);
+ if let Some(path) = probe(path) {
+ return path;
+ }
+ }
+
+ executable_name.into()
+}
+
+fn lookup_in_path(exec: &str) -> bool {
+ let paths = env::var_os("PATH").unwrap_or_default();
+ 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())
+}