Skip to content

Commit

Permalink
Merge pull request #132 from rust-cross/pb-download-sysroot
Browse files Browse the repository at this point in the history
Add progress bar for sysroot download
  • Loading branch information
messense authored Dec 21, 2024
2 parents a286b15 + 29c6870 commit 7a2c717
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ impl<'a> Clang<'a> {

fn setup_msvc_sysroot(&self, cache_dir: PathBuf) -> Result<PathBuf> {
let msvc_sysroot_dir = cache_dir.join("windows-msvc-sysroot");
let done_mark_file = msvc_sysroot_dir.join("DONE");
if msvc_sysroot_dir.is_dir() {
// Already downloaded and unpacked
return Ok(msvc_sysroot_dir);
if done_mark_file.is_file() {
// Already downloaded and unpacked
return Ok(msvc_sysroot_dir);
} else {
// Download again
fs::remove_dir_all(&msvc_sysroot_dir)
.context("Failed to remove existing msvc sysroot")?;
}
}

let agent = http_agent()?;
Expand All @@ -166,6 +173,7 @@ impl<'a> Clang<'a> {
.unwrap_or_else(|_| FALLBACK_DOWNLOAD_URL.to_string());
self.download_msvc_sysroot(&cache_dir, agent, &download_url)
.context("Failed to unpack msvc sysroot")?;
fs::write(done_mark_file, download_url)?;
Ok(msvc_sysroot_dir)
}

Expand Down Expand Up @@ -203,12 +211,36 @@ impl<'a> Clang<'a> {
agent: ureq::Agent,
download_url: &str,
) -> Result<()> {
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use xz2::read::XzDecoder;

let response = agent.get(download_url).call()?;
let tar = XzDecoder::new(response.into_reader());
let len = response
.header("content-length")
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
let pb = ProgressBar::new(len);
pb.set_draw_target(ProgressDrawTarget::stdout());
pb.set_style(
ProgressStyle::default_bar()
.template(
"{spinner:.green} {prefix:.bold} [{elapsed}] {wide_bar:.green} {bytes}/{total_bytes} {msg}",
)?
.progress_chars("=> "),
);
pb.set_prefix("sysroot");
pb.set_message("⏬ Downloading");
if pb.is_hidden() {
eprintln!("⏬ Start downloading MSVC sysroot...");
}
let reader = pb.wrap_read(response.into_reader());
let tar = XzDecoder::new(reader);
let mut archive = tar::Archive::new(tar);
archive.unpack(cache_dir)?;
pb.finish_with_message("Download completed");
if pb.is_hidden() {
eprintln!("✅ Finished downloading MSVC sysroot.");
}
Ok(())
}

Expand Down
6 changes: 6 additions & 0 deletions src/compiler/clang_cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ impl<'a> ClangCl<'a> {
.collect();

mp.set_move_cursor(true);
if mp.is_hidden() {
eprintln!("⏬ Start downloading MSVC CRT...");
}
ctx.execute(
pkgs,
work_items,
Expand All @@ -335,6 +338,9 @@ impl<'a> ClangCl<'a> {
if unpack.exists() {
let _ = fs::remove_dir_all(unpack);
}
if mp.is_hidden() {
eprintln!("✅ Finished downloading MSVC CRT.");
}
Ok(())
}

Expand Down

0 comments on commit 7a2c717

Please sign in to comment.