Skip to content

Commit 6115f4e

Browse files
committed
Add a DownloadSource enum
This simplifies the arguments to `download_component` in config.rs. It also moves stage0.json metadata handling to `Build::new`, making it easier to download the stage0 compiler in rustbuild later if necessary.
1 parent a9ca4b9 commit 6115f4e

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

src/bootstrap/config.rs

+26-43
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::channel::GitInfo;
2020
pub use crate::flags::Subcommand;
2121
use crate::flags::{Color, Flags};
2222
use crate::util::{exe, output, program_out_of_date, t};
23+
use crate::RustfmtMetadata;
2324
use once_cell::sync::OnceCell;
2425
use serde::{Deserialize, Deserializer};
2526

@@ -1483,25 +1484,8 @@ fn download_ci_rustc_commit(download_rustc: Option<StringOrBool>, verbose: bool)
14831484
}
14841485

14851486
fn maybe_download_rustfmt(builder: &Builder<'_>) -> Option<PathBuf> {
1486-
#[derive(Deserialize)]
1487-
struct Stage0Metadata {
1488-
dist_server: String,
1489-
checksums_sha256: HashMap<String, String>,
1490-
rustfmt: Option<RustfmtMetadata>,
1491-
}
1492-
#[derive(Deserialize)]
1493-
struct RustfmtMetadata {
1494-
date: String,
1495-
version: String,
1496-
}
1497-
1498-
let stage0_json = builder.read(&builder.src.join("src").join("stage0.json"));
1499-
let Stage0Metadata { dist_server, checksums_sha256, rustfmt } =
1500-
t!(serde_json::from_str::<Stage0Metadata>(&stage0_json));
1501-
let RustfmtMetadata { date, version } = rustfmt?;
1487+
let RustfmtMetadata { date, version } = builder.stage0_metadata.rustfmt.as_ref()?;
15021488
let channel = format!("{version}-{date}");
1503-
let mut dist_server = env::var("RUSTUP_DIST_SERVER").unwrap_or(dist_server);
1504-
dist_server.push_str("/dist");
15051489

15061490
let host = builder.config.build;
15071491
let rustfmt_path = builder.config.initial_rustc.with_file_name(exe("rustfmt", host));
@@ -1512,15 +1496,7 @@ fn maybe_download_rustfmt(builder: &Builder<'_>) -> Option<PathBuf> {
15121496
}
15131497

15141498
let filename = format!("rustfmt-{version}-{build}.tar.xz", build = host.triple);
1515-
download_component(
1516-
builder,
1517-
&dist_server,
1518-
filename,
1519-
"rustfmt-preview",
1520-
&date,
1521-
"stage0",
1522-
Some(checksums_sha256),
1523-
);
1499+
download_component(builder, DownloadSource::Dist, filename, "rustfmt-preview", &date, "stage0");
15241500

15251501
builder.fix_bin_or_dylib(&bin_root.join("bin").join("rustfmt"));
15261502
builder.fix_bin_or_dylib(&bin_root.join("bin").join("cargo-fmt"));
@@ -1563,28 +1539,24 @@ fn download_ci_rustc(builder: &Builder<'_>, commit: &str) {
15631539
}
15641540
}
15651541

1542+
pub(crate) enum DownloadSource {
1543+
CI,
1544+
Dist,
1545+
}
1546+
15661547
/// Download a single component of a CI-built toolchain (not necessarily a published nightly).
15671548
// NOTE: intentionally takes an owned string to avoid downloading multiple times by accident
15681549
fn download_ci_component(builder: &Builder<'_>, filename: String, prefix: &str, commit: &str) {
1569-
download_component(
1570-
builder,
1571-
"https://ci-artifacts.rust-lang.org/rustc-builds",
1572-
filename,
1573-
prefix,
1574-
commit,
1575-
"ci-rustc",
1576-
None,
1577-
)
1550+
download_component(builder, DownloadSource::CI, filename, prefix, commit, "ci-rustc")
15781551
}
15791552

15801553
fn download_component(
15811554
builder: &Builder<'_>,
1582-
base_url: &str,
1555+
mode: DownloadSource,
15831556
filename: String,
15841557
prefix: &str,
15851558
key: &str,
15861559
destination: &str,
1587-
checksums: Option<HashMap<String, String>>,
15881560
) {
15891561
let cache_dst = builder.out.join("cache");
15901562
let cache_dir = cache_dst.join(key);
@@ -1594,20 +1566,31 @@ fn download_component(
15941566

15951567
let bin_root = builder.out.join(builder.config.build.triple).join(destination);
15961568
let tarball = cache_dir.join(&filename);
1597-
let url = format!("{key}/{filename}");
1569+
let (base_url, url, should_verify) = match mode {
1570+
DownloadSource::CI => (
1571+
"https://ci-artifacts.rust-lang.org/rustc-builds".to_string(),
1572+
format!("{key}/{filename}"),
1573+
false,
1574+
),
1575+
DownloadSource::Dist => {
1576+
let dist_server = env::var("RUSTUP_DIST_SERVER")
1577+
.unwrap_or(builder.stage0_metadata.dist_server.to_string());
1578+
// NOTE: make `dist` part of the URL because that's how it's stored in src/stage0.json
1579+
(dist_server, format!("dist/{key}/{filename}"), true)
1580+
}
1581+
};
15981582

15991583
// For the beta compiler, put special effort into ensuring the checksums are valid.
16001584
// FIXME: maybe we should do this for download-rustc as well? but it would be a pain to update
16011585
// this on each and every nightly ...
1602-
let checksum = if let Some(checksums) = &checksums {
1586+
let checksum = if should_verify {
16031587
let error = format!(
16041588
"src/stage0.json doesn't contain a checksum for {url}. \
16051589
Pre-built artifacts might not be available for this \
16061590
target at this time, see https://doc.rust-lang.org/nightly\
16071591
/rustc/platform-support.html for more information."
16081592
);
1609-
// TODO: add an enum { Commit, Published } so we don't have to hardcode `dist` in two places
1610-
let sha256 = checksums.get(&format!("dist/{url}")).expect(&error);
1593+
let sha256 = builder.stage0_metadata.checksums_sha256.get(&url).expect(&error);
16111594
if tarball.exists() {
16121595
if builder.verify(&tarball, sha256) {
16131596
builder.unpack(&tarball, &bin_root, prefix);
@@ -1627,7 +1610,7 @@ fn download_component(
16271610
None
16281611
};
16291612

1630-
builder.download_component(base_url, &url, &tarball, "");
1613+
builder.download_component(&base_url, &url, &tarball, "");
16311614
if let Some(sha256) = checksum {
16321615
if !builder.verify(&tarball, sha256) {
16331616
panic!("failed to verify {}", tarball.display());

src/bootstrap/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ use std::os::windows::fs::symlink_file;
118118

119119
use filetime::FileTime;
120120
use once_cell::sync::OnceCell;
121+
use serde::Deserialize;
121122

122123
use crate::builder::Kind;
123124
use crate::config::{LlvmLibunwind, TargetSelection};
@@ -294,6 +295,7 @@ pub struct Build {
294295
targets: Vec<TargetSelection>,
295296

296297
// Stage 0 (downloaded) compiler, lld and cargo or their local rust equivalents
298+
stage0_metadata: Stage0Metadata,
297299
initial_rustc: PathBuf,
298300
initial_cargo: PathBuf,
299301
initial_lld: PathBuf,
@@ -320,6 +322,18 @@ pub struct Build {
320322
metrics: metrics::BuildMetrics,
321323
}
322324

325+
#[derive(Deserialize)]
326+
struct Stage0Metadata {
327+
dist_server: String,
328+
checksums_sha256: HashMap<String, String>,
329+
rustfmt: Option<RustfmtMetadata>,
330+
}
331+
#[derive(Deserialize)]
332+
struct RustfmtMetadata {
333+
date: String,
334+
version: String,
335+
}
336+
323337
#[derive(Debug)]
324338
struct Crate {
325339
name: Interned<String>,
@@ -468,7 +482,11 @@ impl Build {
468482
bootstrap_out
469483
};
470484

485+
let stage0_json = t!(std::fs::read_to_string(&src.join("src").join("stage0.json")));
486+
let stage0_metadata = t!(serde_json::from_str::<Stage0Metadata>(&stage0_json));
487+
471488
let mut build = Build {
489+
stage0_metadata,
472490
initial_rustc: config.initial_rustc.clone(),
473491
initial_cargo: config.initial_cargo.clone(),
474492
initial_lld,

0 commit comments

Comments
 (0)