about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAntoni Boucher <bouanto@zoho.com>2024-02-12 21:20:12 -0400
committerAntoni Boucher <bouanto@zoho.com>2024-02-12 21:20:12 -0400
commit5d5137cebce6c3906238fd82d728017cac89a5c4 (patch)
tree9ade1440557b26d3f7337afbf7e4a70af3f114be
parentac708d948540c53c0c763dd4ee81440f5ac2271b (diff)
downloadrust-5d5137cebce6c3906238fd82d728017cac89a5c4.tar.gz
rust-5d5137cebce6c3906238fd82d728017cac89a5c4.zip
Rework the download function to only contain the platform-specific code
-rw-r--r--build_system/src/config.rs156
1 files 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 @@ impl ConfigInfo {
         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 @@ impl ConfigInfo {
         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 @@ impl ConfigInfo {
         );
     }
 }
+
+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
+}