Skip to content

Commit

Permalink
Merge pull request #107 from SergioGasquez/fix/riscv-uninstall
Browse files Browse the repository at this point in the history
Fix RiscV installation/uninstallation
  • Loading branch information
SergioGasquez authored Dec 23, 2022
2 parents 6dbb862 + c9306bd commit 736627d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum Error {
WrongWindowsArguments,
#[diagnostic(code(espup::failed_to_remove_directory))]
#[error(
"{} Failed to remove '{0}' direcretory. Please, manually verify that the directory is properly removed and run 'espup uninstall' again.",
"{} Failed to remove '{0}' directory. Please, manually verify that the directory is properly removed and run 'espup uninstall' again.",
emoji::ERROR
)]
FailedToRemoveDirectory(String),
Expand Down
31 changes: 25 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use espup::{
},
gcc::{get_toolchain_name, Gcc},
llvm::Llvm,
rust::{check_rust_installation, Crate, RiscVTarget, XtensaRust},
rust::{check_rust_installation, uninstall_riscv_target, Crate, RiscVTarget, XtensaRust},
Installable,
},
update::check_for_update,
Expand Down Expand Up @@ -194,7 +194,7 @@ async fn install(args: InstallOpts) -> Result<()> {

to_install.push(Box::new(llvm));

if targets.contains(&Target::ESP32C3) {
if targets.contains(&Target::ESP32C3) || targets.contains(&Target::ESP32C2) {
let riscv_target = RiscVTarget::new(&args.nightly_version);
to_install.push(Box::new(riscv_target));
}
Expand All @@ -211,7 +211,14 @@ async fn install(args: InstallOpts) -> Result<()> {
};
} else {
for target in &targets {
let gcc = Gcc::new(target, &host_triple);
if target == &Target::ESP32 || target == &Target::ESP32S2 || target == &Target::ESP32S3
{
let gcc = Gcc::new(target, &host_triple);
to_install.push(Box::new(gcc));
}
}
if targets.contains(&Target::ESP32C3) || targets.contains(&Target::ESP32C2) {
let gcc = Gcc::new(&Target::ESP32C2, &host_triple);
to_install.push(Box::new(gcc));
}
}
Expand All @@ -223,20 +230,19 @@ async fn install(args: InstallOpts) -> Result<()> {
}

// With a list of applications to install, install them all in parallel.
let (tx, mut rx) = mpsc::channel::<Vec<String>>(32);
let (tx, mut rx) = mpsc::channel::<Result<Vec<String>, Error>>(32);
let installable_items = to_install.len();
for app in to_install {
let tx = tx.clone();
tokio::spawn(async move {
let res = app.install().await;
let res = res.unwrap();
tx.send(res).await.unwrap();
});
}

// Read the results of the install tasks as they complete.
for _ in 0..installable_items {
let names = rx.recv().await.unwrap();
let names = rx.recv().await.unwrap()?;
exports.extend(names);
}

Expand Down Expand Up @@ -302,6 +308,10 @@ async fn uninstall(args: UninstallOpts) -> Result<()> {
.map_err(|_| Error::FailedToRemoveDirectory(llvm_path.display().to_string()))?;
}

if config.targets.contains(&Target::ESP32C3) || config.targets.contains(&Target::ESP32C2) {
uninstall_riscv_target(&config.nightly_version)?;
}

if let Some(esp_idf_version) = config.esp_idf_version {
info!("{} Deleting ESP-IDF {}", emoji::WRENCH, esp_idf_version);
config.esp_idf_version = None;
Expand All @@ -322,6 +332,15 @@ async fn uninstall(args: UninstallOpts) -> Result<()> {
})?;
} else {
info!("{} Deleting GCC targets", emoji::WRENCH);
if config.targets.contains(&Target::ESP32C3) || config.targets.contains(&Target::ESP32C2) {
config.targets.remove(&Target::ESP32C3);
config.targets.remove(&Target::ESP32C2);
config.save()?;
// All RISC-V targets use the same GCC toolchain
let riscv_gcc_path = get_tool_path(&get_toolchain_name(&Target::ESP32C3));
remove_dir_all(&riscv_gcc_path)
.map_err(|_| Error::FailedToRemoveDirectory(riscv_gcc_path))?;
}
for target in &config.targets.clone() {
config.targets.remove(target);
config.save()?;
Expand Down
34 changes: 17 additions & 17 deletions src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ impl Installable for Gcc {
emoji::WARN,
&gcc_path
);
return Ok(vec![]); // No exports
} else {
let gcc_file = format!(
"{}-gcc{}-{}-{}.{}",
self.toolchain_name,
self.version,
self.release,
get_arch(&self.host_triple).unwrap(),
extension
);
let gcc_dist_url = format!("{}/{}/{}", self.repository_url, self.release, gcc_file);
download_file(
gcc_dist_url,
&format!("{}.{}", &self.toolchain_name, extension),
&gcc_path,
true,
)
.await?;
}
let gcc_file = format!(
"{}-gcc{}-{}-{}.{}",
self.toolchain_name,
self.version,
self.release,
get_arch(&self.host_triple).unwrap(),
extension
);
let gcc_dist_url = format!("{}/{}/{}", self.repository_url, self.release, gcc_file);
download_file(
gcc_dist_url,
&format!("{}.{}", &self.toolchain_name, extension),
&gcc_path,
true,
)
.await?;
let mut exports: Vec<String> = Vec::new();

#[cfg(windows)]
Expand Down
20 changes: 19 additions & 1 deletion src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl RiscVTarget {
#[async_trait]
impl Installable for RiscVTarget {
async fn install(&self) -> Result<Vec<String>, Error> {
info!("{} Installing RiscV target", emoji::WRENCH);
info!("{} Installing RISC-V target", emoji::WRENCH);
cmd!(
"rustup",
"component",
Expand All @@ -282,6 +282,7 @@ impl Installable for RiscVTarget {
"add",
"--toolchain",
&self.nightly_version,
"riscv32imc-unknown-none-elf",
"riscv32imac-unknown-none-elf"
)
.run()?;
Expand Down Expand Up @@ -429,6 +430,23 @@ fn install_rust_nightly(version: &str) -> Result<()> {
Ok(())
}

/// Uninstalls the RISC-V target.
pub fn uninstall_riscv_target(nightly_version: &str) -> Result<()> {
info!("{} Uninstalling RISC-V target", emoji::WRENCH);
cmd!(
"rustup",
"target",
"remove",
"--toolchain",
nightly_version,
"riscv32imc-unknown-none-elf",
"riscv32imac-unknown-none-elf"
)
.run()
.into_diagnostic()?;
Ok(())
}

#[cfg(test)]
mod tests {
use crate::toolchain::rust::{Crate, XtensaRust};
Expand Down

0 comments on commit 736627d

Please sign in to comment.