Skip to content

Commit

Permalink
feat: uninstall-toolchain command (#487)
Browse files Browse the repository at this point in the history
* feat: uninstall-toolchain command

* fix: more verbose installs and clean up downloaded archive
  • Loading branch information
sagar-a16z authored Oct 22, 2024
1 parent 99aaab2 commit 58b8f5c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
64 changes: 62 additions & 2 deletions jolt-core/src/host/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ pub fn install_toolchain() -> Result<()> {
download_toolchain(&client, &toolchain_url)
}))?;
unpack_toolchain()?;
remove_archive()?;
link_toolchain()?;
write_tag_file()?;
println!(
"\"riscv32im-jolt-zkvm-elf\" toolchain installed successfully at {:?}",
jolt_dir()
);
}
Ok(())
}
Expand All @@ -39,7 +44,6 @@ pub fn install_no_std_toolchain() -> Result<()> {
std::process::Command::new("rustup")
.args(["target", "add", "riscv32im-unknown-none-elf"])
.output()?;

Ok(())
}

Expand Down Expand Up @@ -114,7 +118,6 @@ async fn download_toolchain(client: &Client, url: &str) -> Result<()> {
fs::create_dir(&jolt_dir)?;
}

println!("Downloading toolchain");
let mut response = client.get(url).send().await?;
if response.status().is_success() {
let mut file = File::create(output_path)?;
Expand Down Expand Up @@ -146,6 +149,14 @@ async fn download_toolchain(client: &Client, url: &str) -> Result<()> {
}
}

fn remove_archive() -> Result<()> {
let toolchain_archive = jolt_dir().join("rust-toolchain.tar.gz");
if toolchain_archive.exists() {
fs::remove_file(&toolchain_archive)?;
}
Ok(())
}

fn toolchain_url() -> String {
let target = target_lexicon::HOST;
format!(
Expand All @@ -154,6 +165,55 @@ fn toolchain_url() -> String {
)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn uninstall_no_std_toolchain() -> Result<()> {
std::process::Command::new("rustup")
.args(["target", "remove", "riscv32im-unknown-none-elf"])
.output()?;

println!("\"riscv32im-unknown-none-elf\" toolchain uninstalled successfully");
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
/// Uninstalls the toolchain if it is already installed
pub fn uninstall_toolchain() -> Result<()> {
if !has_toolchain() {
println!("Toolchain is not installed");
return Ok(());
}

// Remove the linked toolchain from rustup
let output = std::process::Command::new("rustup")
.args(["toolchain", "remove", "riscv32im-jolt-zkvm-elf"])
.output()?;

if !output.status.success() {
bail!(
"Failed to remove toolchain: {}",
String::from_utf8(output.stderr)?
);
}

// Remove the unpacked toolchain directory
let link_path = jolt_dir().join("rust/build/host/stage2");
if link_path.exists() {
fs::remove_dir_all(&link_path)?;
}

// Remove the downloaded toolchain archive
remove_archive()?;

// Remove the toolchain tag file
let tag_file = toolchain_tag_file();
if tag_file.exists() {
fs::remove_file(&tag_file)?;
}

println!("\"riscv32im-jolt-zkvm-elf\" toolchain uninstalled successfully");
Ok(())
}

fn has_toolchain() -> bool {
let tag_path = toolchain_tag_file();
if let Ok(tag) = read_to_string(tag_path) {
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ enum Command {
},
/// Installs the required RISC-V toolchains for Rust
InstallToolchain,
/// Uninstalls the RISC-V toolchains for Rust
UninstallToolchain,
/// Handles preprocessing and generates WASM compatible files
BuildWasm,
}
Expand All @@ -41,6 +43,7 @@ fn main() {
match cli.command {
Command::New { name, wasm } => create_project(name, wasm),
Command::InstallToolchain => install_toolchain(),
Command::UninstallToolchain => uninstall_toolchain(),
Command::BuildWasm => build_wasm(),
}
}
Expand All @@ -61,6 +64,12 @@ fn install_toolchain() {
display_welcome();
}

fn uninstall_toolchain() {
if let Err(err) = toolchain::uninstall_toolchain() {
panic!("toolchain uninstall failed: {}", err);
}
}

fn create_folder_structure(name: &str) -> Result<()> {
fs::create_dir(name)?;
fs::create_dir(format!("{}/src", name))?;
Expand Down

0 comments on commit 58b8f5c

Please sign in to comment.