Skip to content

Commit 0be8768

Browse files
committed
Auto merge of rust-lang#96602 - TApplencourt:patch-1, r=Mark-Simulacrum
boostrap.py use curl by default Fixes rust-lang#61611
2 parents 7fb2d0e + ad7dbe1 commit 0be8768

File tree

4 files changed

+98
-50
lines changed

4 files changed

+98
-50
lines changed

src/bootstrap/bootstrap.py

+35-18
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,42 @@ def download(path, url, probably_big, verbose, help_on_error=None):
110110

111111

112112
def _download(path, url, probably_big, verbose, exception, help_on_error=None):
113+
# Try to use curl (potentially available on win32
114+
# https://devblogs.microsoft.com/commandline/tar-and-curl-come-to-windows/)
115+
# If an error occurs:
116+
# - If we are on win32 fallback to powershell
117+
# - Otherwise raise the error if appropriate
113118
if probably_big or verbose:
114119
print("downloading {}".format(url))
115-
# see https://serverfault.com/questions/301128/how-to-download
116-
if sys.platform == 'win32':
117-
run(["PowerShell.exe", "/nologo", "-Command",
118-
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
119-
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')".format(url, path)],
120-
verbose=verbose,
121-
exception=exception)
122-
else:
120+
121+
platform_is_win32 = sys.platform == 'win32'
122+
try:
123123
if probably_big or verbose:
124124
option = "-#"
125125
else:
126126
option = "-s"
127-
require(["curl", "--version"])
127+
# If curl is not present on Win32, we shoud not sys.exit
128+
# but raise `CalledProcessError` or `OSError` instead
129+
require(["curl", "--version"], exception=platform_is_win32)
128130
run(["curl", option,
129131
"-L", # Follow redirect.
130132
"-y", "30", "-Y", "10", # timeout if speed is < 10 bytes/sec for > 30 seconds
131133
"--connect-timeout", "30", # timeout if cannot connect within 30 seconds
132134
"--retry", "3", "-Sf", "-o", path, url],
133135
verbose=verbose,
134-
exception=exception,
136+
exception=True, # Will raise RuntimeError on failure
135137
help_on_error=help_on_error)
138+
except (subprocess.CalledProcessError, OSError, RuntimeError):
139+
# see http://serverfault.com/questions/301128/how-to-download
140+
if platform_is_win32:
141+
run(["PowerShell.exe", "/nologo", "-Command",
142+
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
143+
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')".format(url, path)],
144+
verbose=verbose,
145+
exception=exception)
146+
# Check if the RuntimeError raised by run(curl) should be silenced
147+
elif verbose or exception:
148+
raise
136149

137150

138151
def verify(path, expected, verbose):
@@ -198,19 +211,23 @@ def run(args, verbose=False, exception=False, is_bootstrap=False, help_on_error=
198211
sys.exit(err)
199212

200213

201-
def require(cmd, exit=True):
214+
def require(cmd, exit=True, exception=False):
202215
'''Run a command, returning its output.
203216
On error,
204-
If `exit` is `True`, exit the process.
205-
Otherwise, return None.'''
217+
If `exception` is `True`, raise the error
218+
Otherwise If `exit` is `True`, exit the process
219+
Else return None.'''
206220
try:
207221
return subprocess.check_output(cmd).strip()
208222
except (subprocess.CalledProcessError, OSError) as exc:
209-
if not exit:
210-
return None
211-
print("error: unable to run `{}`: {}".format(' '.join(cmd), exc))
212-
print("Please make sure it's installed and in the path.")
213-
sys.exit(1)
223+
if exception:
224+
raise
225+
elif exit:
226+
print("error: unable to run `{}`: {}".format(' '.join(cmd), exc))
227+
print("Please make sure it's installed and in the path.")
228+
sys.exit(1)
229+
return None
230+
214231

215232

216233
def format_build_time(duration):

src/bootstrap/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ use once_cell::sync::OnceCell;
122122
use crate::builder::Kind;
123123
use crate::config::{LlvmLibunwind, TargetSelection};
124124
use crate::util::{
125-
exe, libdir, mtime, output, run, run_suppressed, t, try_run, try_run_suppressed, CiEnv,
125+
check_run, exe, libdir, mtime, output, run, run_suppressed, t, try_run, try_run_suppressed,
126+
CiEnv,
126127
};
127128

128129
mod builder;
@@ -961,6 +962,17 @@ impl Build {
961962
try_run_suppressed(cmd)
962963
}
963964

965+
/// Runs a command, printing out nice contextual information if it fails.
966+
/// Returns false if do not execute at all, otherwise returns its
967+
/// `status.success()`.
968+
fn check_run(&self, cmd: &mut Command) -> bool {
969+
if self.config.dry_run {
970+
return true;
971+
}
972+
self.verbose(&format!("running: {:?}", cmd));
973+
check_run(cmd, self.is_verbose())
974+
}
975+
964976
pub fn is_verbose(&self) -> bool {
965977
self.verbosity > 0
966978
}

src/bootstrap/native.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -306,39 +306,40 @@ fn download_component(builder: &Builder<'_>, base: &str, url: &str, dest_path: &
306306

307307
fn download_with_retries(builder: &Builder<'_>, tempfile: &str, url: &str) {
308308
println!("downloading {}", url);
309-
310-
// FIXME: check if curl is installed instead of skipping straight to powershell
311-
if builder.build.build.contains("windows-msvc") {
312-
for _ in 0..3 {
313-
if builder.try_run(Command::new("PowerShell.exe").args(&[
314-
"/nologo",
315-
"-Command",
316-
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
317-
&format!(
318-
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
319-
url, tempfile
320-
),
321-
])) {
322-
return;
309+
// Try curl. If that fails and we are on windows, fallback to PowerShell.
310+
if !builder.check_run(Command::new("curl").args(&[
311+
"-#",
312+
"-y",
313+
"30",
314+
"-Y",
315+
"10", // timeout if speed is < 10 bytes/sec for > 30 seconds
316+
"--connect-timeout",
317+
"30", // timeout if cannot connect within 30 seconds
318+
"--retry",
319+
"3",
320+
"-Sf",
321+
"-o",
322+
tempfile,
323+
url,
324+
])) {
325+
if builder.build.build.contains("windows-msvc") {
326+
println!("Fallback to PowerShell");
327+
for _ in 0..3 {
328+
if builder.try_run(Command::new("PowerShell.exe").args(&[
329+
"/nologo",
330+
"-Command",
331+
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
332+
&format!(
333+
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
334+
url, tempfile
335+
),
336+
])) {
337+
return;
338+
}
339+
println!("\nspurious failure, trying again");
323340
}
324-
println!("\nspurious failure, trying again");
325341
}
326-
} else {
327-
builder.run(Command::new("curl").args(&[
328-
"-#",
329-
"-y",
330-
"30",
331-
"-Y",
332-
"10", // timeout if speed is < 10 bytes/sec for > 30 seconds
333-
"--connect-timeout",
334-
"30", // timeout if cannot connect within 30 seconds
335-
"--retry",
336-
"3",
337-
"-Sf",
338-
"-o",
339-
tempfile,
340-
url,
341-
]));
342+
std::process::exit(1);
342343
}
343344
}
344345

src/bootstrap/util.rs

+18
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> bool {
346346
status.success()
347347
}
348348

349+
pub fn check_run(cmd: &mut Command, print_cmd_on_fail: bool) -> bool {
350+
let status = match cmd.status() {
351+
Ok(status) => status,
352+
Err(e) => {
353+
println!("failed to execute command: {:?}\nerror: {}", cmd, e);
354+
return false;
355+
}
356+
};
357+
if !status.success() && print_cmd_on_fail {
358+
println!(
359+
"\n\ncommand did not execute successfully: {:?}\n\
360+
expected success, got: {}\n\n",
361+
cmd, status
362+
);
363+
}
364+
status.success()
365+
}
366+
349367
pub fn run_suppressed(cmd: &mut Command) {
350368
if !try_run_suppressed(cmd) {
351369
std::process::exit(1);

0 commit comments

Comments
 (0)