Skip to content

Commit b80a999

Browse files
Improve code readability and add more code comments
1 parent 1096b1b commit b80a999

File tree

1 file changed

+80
-66
lines changed

1 file changed

+80
-66
lines changed

build_system/src/config.rs

+80-66
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,83 @@ impl ConfigInfo {
190190
command
191191
}
192192

193+
fn download_gccjit(
194+
&self,
195+
output_dir: &Path,
196+
libgccjit_so_name: &str,
197+
commit: &str,
198+
) -> Result<(), String> {
199+
// Download time!
200+
let tempfile_name = format!("{}.download", libgccjit_so_name);
201+
let tempfile = output_dir.join(&tempfile_name);
202+
let is_in_ci = std::env::var("GITHUB_ACTIONS").is_ok();
203+
204+
let url = format!(
205+
"https://github.com/antoyo/gcc/releases/download/master-{}/libgccjit.so",
206+
commit,
207+
);
208+
209+
println!("Downloading `{}`...", url);
210+
// Try curl. If that fails and we are on windows, fallback to PowerShell.
211+
let mut ret = run_command_with_output(
212+
&[
213+
&"curl",
214+
&"--speed-time",
215+
&"30",
216+
&"--speed-limit",
217+
&"10", // timeout if speed is < 10 bytes/sec for > 30 seconds
218+
&"--connect-timeout",
219+
&"30", // timeout if cannot connect within 30 seconds
220+
&"-o",
221+
&tempfile_name,
222+
&"--retry",
223+
&"3",
224+
&"-SRfL",
225+
if is_in_ci { &"-s" } else { &"--progress-bar" },
226+
&url.as_str(),
227+
],
228+
Some(&output_dir),
229+
);
230+
if ret.is_err() && cfg!(windows) {
231+
eprintln!("Fallback to PowerShell");
232+
ret = run_command_with_output(
233+
&[
234+
&"PowerShell.exe",
235+
&"/nologo",
236+
&"-Command",
237+
&"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
238+
&format!(
239+
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
240+
url,
241+
tempfile_name,
242+
).as_str(),
243+
],
244+
Some(&output_dir),
245+
);
246+
}
247+
ret?;
248+
249+
let libgccjit_so = output_dir.join(libgccjit_so_name);
250+
// If we reach this point, it means the file was correctly downloaded, so let's
251+
// rename it!
252+
std::fs::rename(&tempfile, &libgccjit_so).map_err(|err| {
253+
format!(
254+
"Failed to rename `{}` into `{}`: {:?}",
255+
tempfile.display(),
256+
libgccjit_so.display(),
257+
err,
258+
)
259+
})?;
260+
261+
println!("Downloaded libgccjit.so version {} successfully!", commit);
262+
// We need to create a link named `libgccjit.so.0` because that's what the linker is
263+
// looking for.
264+
create_symlink(
265+
&libgccjit_so,
266+
output_dir.join(&format!("{}.0", libgccjit_so_name)),
267+
)
268+
}
269+
193270
fn download_gccjit_if_needed(&mut self) -> Result<(), String> {
194271
let output_dir = Path::new(
195272
std::env::var("CARGO_TARGET_DIR")
@@ -206,6 +283,8 @@ impl ConfigInfo {
206283
)
207284
})?;
208285
let commit = content.trim();
286+
// This is a very simple check to ensure this is not a path. For the rest, it'll just fail
287+
// when trying to download the file so we should be fine.
209288
if commit.contains('/') || commit.contains('\\') {
210289
return Err(format!(
211290
"{}: invalid commit hash `{}`",
@@ -234,72 +313,7 @@ impl ConfigInfo {
234313
let libgccjit_so_name = "libgccjit.so";
235314
let libgccjit_so = output_dir.join(libgccjit_so_name);
236315
if !libgccjit_so.is_file() && !self.no_download {
237-
// Download time!
238-
let tempfile_name = "libgccjit.so.download";
239-
let tempfile = output_dir.join(tempfile_name);
240-
let is_in_ci = std::env::var("GITHUB_ACTIONS").is_ok();
241-
242-
let url = format!(
243-
"https://github.com/antoyo/gcc/releases/download/master-{}/libgccjit.so",
244-
commit,
245-
);
246-
247-
println!("Downloading `{}`...", url);
248-
// Try curl. If that fails and we are on windows, fallback to PowerShell.
249-
let mut ret = run_command_with_output(
250-
&[
251-
&"curl",
252-
&"--speed-time",
253-
&"30",
254-
&"--speed-limit",
255-
&"10", // timeout if speed is < 10 bytes/sec for > 30 seconds
256-
&"--connect-timeout",
257-
&"30", // timeout if cannot connect within 30 seconds
258-
&"-o",
259-
&tempfile_name,
260-
&"--retry",
261-
&"3",
262-
&"-SRfL",
263-
if is_in_ci { &"-s" } else { &"--progress-bar" },
264-
&url.as_str(),
265-
],
266-
Some(&output_dir),
267-
);
268-
if ret.is_err() && cfg!(windows) {
269-
eprintln!("Fallback to PowerShell");
270-
ret = run_command_with_output(
271-
&[
272-
&"PowerShell.exe",
273-
&"/nologo",
274-
&"-Command",
275-
&"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
276-
&format!(
277-
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
278-
url,
279-
tempfile_name,
280-
).as_str(),
281-
],
282-
Some(&output_dir),
283-
);
284-
}
285-
ret?;
286-
287-
// If we reach this point, it means the file was correctly downloaded, so let's
288-
// rename it!
289-
std::fs::rename(&tempfile, &libgccjit_so).map_err(|err| {
290-
format!(
291-
"Failed to rename `{}` into `{}`: {:?}",
292-
tempfile.display(),
293-
libgccjit_so.display(),
294-
err,
295-
)
296-
})?;
297-
298-
println!("Downloaded libgccjit.so version {} successfully!", commit);
299-
create_symlink(
300-
&libgccjit_so,
301-
output_dir.join(&format!("{}.0", libgccjit_so_name)),
302-
)?;
316+
self.download_gccjit(&output_dir, libgccjit_so_name, commit)?;
303317
}
304318

305319
self.gcc_path = output_dir.display().to_string();

0 commit comments

Comments
 (0)