From 5d5137cebce6c3906238fd82d728017cac89a5c4 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 12 Feb 2024 21:20:12 -0400 Subject: [PATCH] Rework the download function to only contain the platform-specific code --- build_system/src/config.rs | 156 ++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/build_system/src/config.rs b/build_system/src/config.rs index 475f9b300f7..c9bfcb9e6ba 100644 --- a/build_system/src/config.rs +++ b/build_system/src/config.rs @@ -190,83 +190,6 @@ pub fn rustc_command_vec(&self) -> Vec<&dyn AsRef> { command } - fn download_gccjit( - &self, - output_dir: &Path, - libgccjit_so_name: &str, - commit: &str, - ) -> Result<(), String> { - // Download time! - let tempfile_name = format!("{}.download", libgccjit_so_name); - let tempfile = output_dir.join(&tempfile_name); - let is_in_ci = std::env::var("GITHUB_ACTIONS").is_ok(); - - let url = format!( - "https://github.com/antoyo/gcc/releases/download/master-{}/libgccjit.so", - commit, - ); - - println!("Downloading `{}`...", url); - // Try curl. If that fails and we are on windows, fallback to PowerShell. - let mut ret = run_command_with_output( - &[ - &"curl", - &"--speed-time", - &"30", - &"--speed-limit", - &"10", // timeout if speed is < 10 bytes/sec for > 30 seconds - &"--connect-timeout", - &"30", // timeout if cannot connect within 30 seconds - &"-o", - &tempfile_name, - &"--retry", - &"3", - &"-SRfL", - if is_in_ci { &"-s" } else { &"--progress-bar" }, - &url.as_str(), - ], - Some(&output_dir), - ); - if ret.is_err() && cfg!(windows) { - eprintln!("Fallback to PowerShell"); - ret = run_command_with_output( - &[ - &"PowerShell.exe", - &"/nologo", - &"-Command", - &"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;", - &format!( - "(New-Object System.Net.WebClient).DownloadFile('{}', '{}')", - url, - tempfile_name, - ).as_str(), - ], - Some(&output_dir), - ); - } - ret?; - - let libgccjit_so = output_dir.join(libgccjit_so_name); - // If we reach this point, it means the file was correctly downloaded, so let's - // rename it! - std::fs::rename(&tempfile, &libgccjit_so).map_err(|err| { - format!( - "Failed to rename `{}` into `{}`: {:?}", - tempfile.display(), - libgccjit_so.display(), - err, - ) - })?; - - println!("Downloaded libgccjit.so version {} successfully!", commit); - // We need to create a link named `libgccjit.so.0` because that's what the linker is - // looking for. - create_symlink( - &libgccjit_so, - output_dir.join(&format!("{}.0", libgccjit_so_name)), - ) - } - fn download_gccjit_if_needed(&mut self) -> Result<(), String> { let output_dir = Path::new( std::env::var("CARGO_TARGET_DIR") @@ -313,7 +236,38 @@ fn download_gccjit_if_needed(&mut self) -> Result<(), String> { let libgccjit_so_name = "libgccjit.so"; let libgccjit_so = output_dir.join(libgccjit_so_name); if !libgccjit_so.is_file() && !self.no_download { - self.download_gccjit(&output_dir, libgccjit_so_name, commit)?; + // Download time! + let tempfile_name = format!("{}.download", libgccjit_so_name); + let tempfile = output_dir.join(&tempfile_name); + let is_in_ci = std::env::var("GITHUB_ACTIONS").is_ok(); + + let url = format!( + "https://github.com/antoyo/gcc/releases/download/master-{}/libgccjit.so", + commit, + ); + + println!("Downloading `{}`...", url); + download_gccjit(url, &output_dir, tempfile_name, !is_in_ci)?; + + let libgccjit_so = output_dir.join(libgccjit_so_name); + // If we reach this point, it means the file was correctly downloaded, so let's + // rename it! + std::fs::rename(&tempfile, &libgccjit_so).map_err(|err| { + format!( + "Failed to rename `{}` into `{}`: {:?}", + tempfile.display(), + libgccjit_so.display(), + err, + ) + })?; + + println!("Downloaded libgccjit.so version {} successfully!", commit); + // We need to create a link named `libgccjit.so.0` because that's what the linker is + // looking for. + create_symlink( + &libgccjit_so, + output_dir.join(&format!("{}.0", libgccjit_so_name)), + )?; } self.gcc_path = output_dir.display().to_string(); @@ -547,3 +501,49 @@ pub fn show_usage() { ); } } + +fn download_gccjit( + url: String, + output_dir: &Path, + tempfile_name: String, + with_progress_bar: bool, +) -> Result<(), String> { + // Try curl. If that fails and we are on windows, fallback to PowerShell. + let mut ret = run_command_with_output( + &[ + &"curl", + &"--speed-time", + &"30", + &"--speed-limit", + &"10", // timeout if speed is < 10 bytes/sec for > 30 seconds + &"--connect-timeout", + &"30", // timeout if cannot connect within 30 seconds + &"-o", + &tempfile_name, + &"--retry", + &"3", + &"-SRfL", + if with_progress_bar { &"--progress-bar" } else { &"-s" }, + &url.as_str(), + ], + Some(&output_dir), + ); + if ret.is_err() && cfg!(windows) { + eprintln!("Fallback to PowerShell"); + ret = run_command_with_output( + &[ + &"PowerShell.exe", + &"/nologo", + &"-Command", + &"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;", + &format!( + "(New-Object System.Net.WebClient).DownloadFile('{}', '{}')", + url, + tempfile_name, + ).as_str(), + ], + Some(&output_dir), + ); + } + ret +}