diff --git a/src/env/mod.rs b/src/env/mod.rs index 7eaee86e..14644602 100644 --- a/src/env/mod.rs +++ b/src/env/mod.rs @@ -16,7 +16,7 @@ pub fn set_env(toolchain_dir: &Path) -> Result<(), Error> { #[cfg(windows)] if cfg!(windows) { windows::write_env_files(toolchain_dir)?; - windows::update_env(toolchain_dir)?; + windows::update_env()?; } #[cfg(unix)] if cfg!(unix) { diff --git a/src/env/shell.rs b/src/env/shell.rs index d94dc216..dff658b1 100644 --- a/src/env/shell.rs +++ b/src/env/shell.rs @@ -301,10 +301,12 @@ impl UnixShell for Fish { } } +#[cfg(unix)] pub(crate) fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> { cmds.iter().cloned().find(|&s| has_cmd(s)) } +#[cfg(unix)] fn has_cmd(cmd: &str) -> bool { let cmd = format!("{}{}", cmd, env::consts::EXE_SUFFIX); let path = env::var("PATH").unwrap_or_default(); diff --git a/src/env/windows.rs b/src/env/windows.rs index 8cb71c52..64b196b2 100644 --- a/src/env/windows.rs +++ b/src/env/windows.rs @@ -9,7 +9,7 @@ use winreg::{ const LEGACY_EXPORT_FILE: &str = "export-esp.ps1"; // Clean the environment for Windows. -pub(super) fn clean_env(toolchain_dir: &Path) -> Result<(), Error> { +pub(super) fn clean_env(_install_dir: &Path) -> Result<(), Error> { delete_env_variable("LIBCLANG_PATH")?; delete_env_variable("CLANG_PATH")?; if let Some(path) = env::var_os("PATH") { @@ -56,28 +56,31 @@ fn remove_legacy_export_file() -> Result<(), Error> { } // Update the environment for Windows. -pub(super) fn update_env(toolchain_dir: &Path) -> Result<(), Error> { +pub(super) fn update_env() -> Result<(), Error> { let mut path = env::var("PATH").unwrap_or_default(); - if let Some(xtensa_gcc) = env::var_os("XTENSA_GCC") { - if !path.contains(xtensa_gcc.into()) { - path = format!("{};{}", xtensa_gcc.into(), path); + if let Ok(xtensa_gcc) = env::var("XTENSA_GCC") { + let xtensa_gcc: &str = &xtensa_gcc; + if !path.contains(xtensa_gcc) { + path = format!("{};{}", xtensa_gcc, path); } } - if let Some(riscv_gcc) = env::var_os("RISCV_GCC") { - if !path.contains(riscv_gcc.into()) { - path = format!("{};{}", riscv_gcc.into(), path); + if let Ok(riscv_gcc) = env::var("RISCV_GCC") { + let riscv_gcc: &str = &riscv_gcc; + if !path.contains(riscv_gcc) { + path = format!("{};{}", riscv_gcc, path); } } - if let Some(libclang_path) = env::var_os("LIBCLANG_PATH") { - set_env_variable("LIBCLANG_PATH", &libclang_path.to_string_lossy())?; + if let Ok(libclang_path) = env::var("LIBCLANG_PATH") { + set_env_variable("LIBCLANG_PATH", &libclang_path)?; } - if let Some(clang_path) = env::var_os("CLANG_PATH") { - if !path.contains(clang_path.into()) { - path = format!("{};{}", clang_path.into(), path); + if let Ok(clang_path) = env::var("CLANG_PATH") { + let clang_path: &str = &clang_path; + if !path.contains(clang_path) { + path = format!("{};{}", clang_path, path); } } set_env_variable("PATH", &path)?; diff --git a/src/toolchain/gcc.rs b/src/toolchain/gcc.rs index 3cd65c9c..686da4d1 100644 --- a/src/toolchain/gcc.rs +++ b/src/toolchain/gcc.rs @@ -32,7 +32,12 @@ pub struct Gcc { impl Gcc { /// Gets the binary path. fn get_bin_path(&self) -> String { - format!("{}/{}/bin", &self.path.to_str().unwrap(), &self.arch) + #[cfg(windows)] + let bin_path = + format!("{}/{}/bin", &self.path.to_str().unwrap(), &self.arch).replace('/', "\\"); + #[cfg(unix)] + let bin_path = format!("{}/{}/bin", &self.path.to_str().unwrap(), &self.arch); + bin_path } /// Create a new instance with default values and proper toolchain name. @@ -81,23 +86,12 @@ impl Installable for Gcc { .await?; } - #[cfg(windows)] - if cfg!(windows) { - let windows_path = self.get_bin_path().replace('/', "\\"); - if self.arch == RISCV_GCC { - env::set_var("RISCV_GCC", self.get_bin_path()); - } else { - env::set_var("XTENSA_GCC", self.get_bin_path()); - } - } - #[cfg(unix)] - if cfg!(unix) { - if self.arch == RISCV_GCC { - env::set_var("RISCV_GCC", self.get_bin_path()); - } else { - env::set_var("XTENSA_GCC", self.get_bin_path()); - } + if self.arch == RISCV_GCC { + env::set_var("RISCV_GCC", self.get_bin_path()); + } else { + env::set_var("XTENSA_GCC", self.get_bin_path()); } + Ok(()) } diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index cccce795..4d83e205 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -430,6 +430,7 @@ mod tests { logging::initialize_logger, toolchain::rust::{get_cargo_home, get_rustup_home, XtensaRust}, }; + use std::env; #[test] fn test_xtensa_rust_parse_version() {