@@ -20,6 +20,7 @@ use crate::channel::GitInfo;
2020pub use crate :: flags:: Subcommand ;
2121use crate :: flags:: { Color , Flags } ;
2222use crate :: util:: { exe, output, program_out_of_date, t} ;
23+ use crate :: RustfmtMetadata ;
2324use once_cell:: sync:: OnceCell ;
2425use serde:: { Deserialize , Deserializer } ;
2526
@@ -1483,25 +1484,8 @@ fn download_ci_rustc_commit(download_rustc: Option<StringOrBool>, verbose: bool)
14831484}
14841485
14851486fn 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
15681549fn 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
15801553fn 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( ) ) ;
0 commit comments