]> git.lizzy.rs Git - rust.git/blob - src/tools/build-manifest/src/main.rs
Tweak move error
[rust.git] / src / tools / build-manifest / src / main.rs
1 //! Build a dist manifest, hash and sign everything.
2 //! This gets called by `promote-release`
3 //! (https://github.com/rust-lang/rust-central-station/tree/master/promote-release)
4 //! via `x.py dist hash-and-sign`; the cmdline arguments are set up
5 //! by rustbuild (in `src/bootstrap/dist.rs`).
6
7 mod checksum;
8 mod manifest;
9 mod versions;
10
11 use crate::checksum::Checksums;
12 use crate::manifest::{Component, Manifest, Package, Rename, Target};
13 use crate::versions::{PkgType, Versions};
14 use std::collections::{BTreeMap, HashMap, HashSet};
15 use std::env;
16 use std::fs::{self, File};
17 use std::path::{Path, PathBuf};
18
19 static HOSTS: &[&str] = &[
20     "aarch64-apple-darwin",
21     "aarch64-pc-windows-msvc",
22     "aarch64-unknown-linux-gnu",
23     "aarch64-unknown-linux-musl",
24     "arm-unknown-linux-gnueabi",
25     "arm-unknown-linux-gnueabihf",
26     "armv7-unknown-linux-gnueabihf",
27     "i686-apple-darwin",
28     "i686-pc-windows-gnu",
29     "i686-pc-windows-msvc",
30     "i686-unknown-linux-gnu",
31     "mips-unknown-linux-gnu",
32     "mips64-unknown-linux-gnuabi64",
33     "mips64el-unknown-linux-gnuabi64",
34     "mipsel-unknown-linux-gnu",
35     "mipsisa32r6-unknown-linux-gnu",
36     "mipsisa32r6el-unknown-linux-gnu",
37     "mipsisa64r6-unknown-linux-gnuabi64",
38     "mipsisa64r6el-unknown-linux-gnuabi64",
39     "powerpc-unknown-linux-gnu",
40     "powerpc64-unknown-linux-gnu",
41     "powerpc64le-unknown-linux-gnu",
42     "riscv64gc-unknown-linux-gnu",
43     "s390x-unknown-linux-gnu",
44     "x86_64-apple-darwin",
45     "x86_64-pc-windows-gnu",
46     "x86_64-pc-windows-msvc",
47     "x86_64-unknown-freebsd",
48     "x86_64-unknown-illumos",
49     "x86_64-unknown-linux-gnu",
50     "x86_64-unknown-linux-musl",
51     "x86_64-unknown-netbsd",
52 ];
53
54 static TARGETS: &[&str] = &[
55     "aarch64-apple-darwin",
56     "aarch64-apple-ios",
57     "aarch64-apple-ios-sim",
58     "aarch64-fuchsia",
59     "aarch64-linux-android",
60     "aarch64-pc-windows-msvc",
61     "aarch64-unknown-hermit",
62     "aarch64-unknown-linux-gnu",
63     "aarch64-unknown-linux-musl",
64     "aarch64-unknown-none",
65     "aarch64-unknown-none-softfloat",
66     "aarch64-unknown-redox",
67     "arm-linux-androideabi",
68     "arm-unknown-linux-gnueabi",
69     "arm-unknown-linux-gnueabihf",
70     "arm-unknown-linux-musleabi",
71     "arm-unknown-linux-musleabihf",
72     "armv5te-unknown-linux-gnueabi",
73     "armv5te-unknown-linux-musleabi",
74     "armv7-apple-ios",
75     "armv7-linux-androideabi",
76     "thumbv7neon-linux-androideabi",
77     "armv7-unknown-linux-gnueabi",
78     "armv7-unknown-linux-gnueabihf",
79     "armv7a-none-eabi",
80     "thumbv7neon-unknown-linux-gnueabihf",
81     "armv7-unknown-linux-musleabi",
82     "armv7-unknown-linux-musleabihf",
83     "armebv7r-none-eabi",
84     "armebv7r-none-eabihf",
85     "armv7r-none-eabi",
86     "armv7r-none-eabihf",
87     "armv7s-apple-ios",
88     "asmjs-unknown-emscripten",
89     "bpfeb-unknown-none",
90     "bpfel-unknown-none",
91     "i386-apple-ios",
92     "i586-pc-windows-msvc",
93     "i586-unknown-linux-gnu",
94     "i586-unknown-linux-musl",
95     "i686-apple-darwin",
96     "i686-linux-android",
97     "i686-pc-windows-gnu",
98     "i686-pc-windows-msvc",
99     "i686-unknown-freebsd",
100     "i686-unknown-linux-gnu",
101     "i686-unknown-linux-musl",
102     "m68k-unknown-linux-gnu",
103     "mips-unknown-linux-gnu",
104     "mips-unknown-linux-musl",
105     "mips64-unknown-linux-gnuabi64",
106     "mips64-unknown-linux-muslabi64",
107     "mips64el-unknown-linux-gnuabi64",
108     "mips64el-unknown-linux-muslabi64",
109     "mipsisa32r6-unknown-linux-gnu",
110     "mipsisa32r6el-unknown-linux-gnu",
111     "mipsisa64r6-unknown-linux-gnuabi64",
112     "mipsisa64r6el-unknown-linux-gnuabi64",
113     "mipsel-unknown-linux-gnu",
114     "mipsel-unknown-linux-musl",
115     "nvptx64-nvidia-cuda",
116     "powerpc-unknown-linux-gnu",
117     "powerpc64-unknown-linux-gnu",
118     "powerpc64le-unknown-linux-gnu",
119     "riscv32i-unknown-none-elf",
120     "riscv32imc-unknown-none-elf",
121     "riscv32imac-unknown-none-elf",
122     "riscv32gc-unknown-linux-gnu",
123     "riscv64imac-unknown-none-elf",
124     "riscv64gc-unknown-none-elf",
125     "riscv64gc-unknown-linux-gnu",
126     "s390x-unknown-linux-gnu",
127     "sparc64-unknown-linux-gnu",
128     "sparcv9-sun-solaris",
129     "thumbv6m-none-eabi",
130     "thumbv7em-none-eabi",
131     "thumbv7em-none-eabihf",
132     "thumbv7m-none-eabi",
133     "thumbv8m.base-none-eabi",
134     "thumbv8m.main-none-eabi",
135     "thumbv8m.main-none-eabihf",
136     "wasm32-unknown-emscripten",
137     "wasm32-unknown-unknown",
138     "wasm32-wasi",
139     "x86_64-apple-darwin",
140     "x86_64-apple-ios",
141     "x86_64-fortanix-unknown-sgx",
142     "x86_64-fuchsia",
143     "x86_64-linux-android",
144     "x86_64-pc-windows-gnu",
145     "x86_64-pc-windows-msvc",
146     "x86_64-sun-solaris",
147     "x86_64-pc-solaris",
148     "x86_64-unknown-freebsd",
149     "x86_64-unknown-illumos",
150     "x86_64-unknown-linux-gnu",
151     "x86_64-unknown-linux-gnux32",
152     "x86_64-unknown-linux-musl",
153     "x86_64-unknown-netbsd",
154     "x86_64-unknown-redox",
155     "x86_64-unknown-hermit",
156 ];
157
158 /// This allows the manifest to contain rust-docs for hosts that don't build
159 /// docs.
160 ///
161 /// Tuples of `(host_partial, host_instead)`. If the host does not have the
162 /// rust-docs component available, then if the host name contains
163 /// `host_partial`, it will use the docs from `host_instead` instead.
164 ///
165 /// The order here matters, more specific entries should be first.
166 static DOCS_FALLBACK: &[(&str, &str)] = &[
167     ("-apple-", "x86_64-apple-darwin"),
168     ("aarch64", "aarch64-unknown-linux-gnu"),
169     ("arm-", "aarch64-unknown-linux-gnu"),
170     ("", "x86_64-unknown-linux-gnu"),
171 ];
172
173 static MSI_INSTALLERS: &[&str] = &[
174     "aarch64-pc-windows-msvc",
175     "i686-pc-windows-gnu",
176     "i686-pc-windows-msvc",
177     "x86_64-pc-windows-gnu",
178     "x86_64-pc-windows-msvc",
179 ];
180
181 static PKG_INSTALLERS: &[&str] = &["x86_64-apple-darwin", "aarch64-apple-darwin"];
182
183 static MINGW: &[&str] = &["i686-pc-windows-gnu", "x86_64-pc-windows-gnu"];
184
185 static NIGHTLY_ONLY_COMPONENTS: &[&str] = &["miri-preview", "rust-analyzer-preview"];
186
187 macro_rules! t {
188     ($e:expr) => {
189         match $e {
190             Ok(e) => e,
191             Err(e) => panic!("{} failed with {}", stringify!($e), e),
192         }
193     };
194 }
195
196 struct Builder {
197     versions: Versions,
198     checksums: Checksums,
199     shipped_files: HashSet<String>,
200
201     input: PathBuf,
202     output: PathBuf,
203     s3_address: String,
204     date: String,
205 }
206
207 fn main() {
208     let num_threads = if let Some(num) = env::var_os("BUILD_MANIFEST_NUM_THREADS") {
209         num.to_str().unwrap().parse().expect("invalid number for BUILD_MANIFEST_NUM_THREADS")
210     } else {
211         num_cpus::get()
212     };
213     rayon::ThreadPoolBuilder::new()
214         .num_threads(num_threads)
215         .build_global()
216         .expect("failed to initialize Rayon");
217
218     let mut args = env::args().skip(1);
219     let input = PathBuf::from(args.next().unwrap());
220     let output = PathBuf::from(args.next().unwrap());
221     let date = args.next().unwrap();
222     let s3_address = args.next().unwrap();
223     let channel = args.next().unwrap();
224
225     Builder {
226         versions: Versions::new(&channel, &input).unwrap(),
227         checksums: t!(Checksums::new()),
228         shipped_files: HashSet::new(),
229
230         input,
231         output,
232         s3_address,
233         date,
234     }
235     .build();
236 }
237
238 impl Builder {
239     fn build(&mut self) {
240         self.check_toolstate();
241         let manifest = self.build_manifest();
242
243         let channel = self.versions.channel().to_string();
244         self.write_channel_files(&channel, &manifest);
245         if channel == "stable" {
246             // channel-rust-1.XX.YY.toml
247             let rust_version = self.versions.rustc_version().to_string();
248             self.write_channel_files(&rust_version, &manifest);
249
250             // channel-rust-1.XX.toml
251             let major_minor = rust_version.split('.').take(2).collect::<Vec<_>>().join(".");
252             self.write_channel_files(&major_minor, &manifest);
253         }
254
255         if let Some(path) = std::env::var_os("BUILD_MANIFEST_SHIPPED_FILES_PATH") {
256             self.write_shipped_files(&Path::new(&path));
257         }
258
259         t!(self.checksums.store_cache());
260     }
261
262     /// If a tool does not pass its tests on *any* of Linux and Windows, don't ship
263     /// it on *all* targets, because tools like Miri can "cross-run" programs for
264     /// different targets, for example, run a program for `x86_64-pc-windows-msvc`
265     /// on `x86_64-unknown-linux-gnu`.
266     /// Right now, we do this only for Miri.
267     fn check_toolstate(&mut self) {
268         for file in &["toolstates-linux.json", "toolstates-windows.json"] {
269             let toolstates: Option<HashMap<String, String>> = File::open(self.input.join(file))
270                 .ok()
271                 .and_then(|f| serde_json::from_reader(&f).ok());
272             let toolstates = toolstates.unwrap_or_else(|| {
273                 println!("WARNING: `{}` missing/malformed; assuming all tools failed", file);
274                 HashMap::default() // Use empty map if anything went wrong.
275             });
276             // Mark some tools as missing based on toolstate.
277             if toolstates.get("miri").map(|s| &*s as &str) != Some("test-pass") {
278                 println!("Miri tests are not passing, removing component");
279                 self.versions.disable_version(&PkgType::Miri);
280                 break;
281             }
282         }
283     }
284
285     fn build_manifest(&mut self) -> Manifest {
286         let mut manifest = Manifest {
287             manifest_version: "2".to_string(),
288             date: self.date.to_string(),
289             pkg: BTreeMap::new(),
290             artifacts: BTreeMap::new(),
291             renames: BTreeMap::new(),
292             profiles: BTreeMap::new(),
293         };
294         self.add_packages_to(&mut manifest);
295         self.add_artifacts_to(&mut manifest);
296         self.add_profiles_to(&mut manifest);
297         self.add_renames_to(&mut manifest);
298         manifest.pkg.insert("rust".to_string(), self.rust_package(&manifest));
299
300         self.checksums.fill_missing_checksums(&mut manifest);
301
302         manifest
303     }
304
305     fn add_packages_to(&mut self, manifest: &mut Manifest) {
306         macro_rules! package {
307             ($name:expr, $targets:expr) => {
308                 self.package($name, &mut manifest.pkg, $targets, &[])
309             };
310         }
311         package!("rustc", HOSTS);
312         package!("rustc-dev", HOSTS);
313         package!("reproducible-artifacts", HOSTS);
314         package!("rustc-docs", HOSTS);
315         package!("cargo", HOSTS);
316         package!("rust-mingw", MINGW);
317         package!("rust-std", TARGETS);
318         self.package("rust-docs", &mut manifest.pkg, HOSTS, DOCS_FALLBACK);
319         package!("rust-src", &["*"]);
320         package!("rls-preview", HOSTS);
321         package!("rust-analyzer-preview", HOSTS);
322         package!("clippy-preview", HOSTS);
323         package!("miri-preview", HOSTS);
324         package!("rustfmt-preview", HOSTS);
325         package!("rust-analysis", TARGETS);
326         package!("llvm-tools-preview", TARGETS);
327     }
328
329     fn add_artifacts_to(&mut self, manifest: &mut Manifest) {
330         manifest.add_artifact("source-code", |artifact| {
331             let tarball = self.versions.tarball_name(&PkgType::Rustc, "src").unwrap();
332             artifact.add_tarball(self, "*", &tarball);
333         });
334
335         manifest.add_artifact("installer-msi", |artifact| {
336             for target in MSI_INSTALLERS {
337                 let msi = self.versions.archive_name(&PkgType::Rust, target, "msi").unwrap();
338                 artifact.add_file(self, target, &msi);
339             }
340         });
341
342         manifest.add_artifact("installer-pkg", |artifact| {
343             for target in PKG_INSTALLERS {
344                 let pkg = self.versions.archive_name(&PkgType::Rust, target, "pkg").unwrap();
345                 artifact.add_file(self, target, &pkg);
346             }
347         });
348     }
349
350     fn add_profiles_to(&mut self, manifest: &mut Manifest) {
351         let mut profile = |name, pkgs| self.profile(name, &mut manifest.profiles, pkgs);
352         profile("minimal", &["rustc", "cargo", "rust-std", "rust-mingw"]);
353         profile(
354             "default",
355             &[
356                 "rustc",
357                 "cargo",
358                 "rust-std",
359                 "rust-mingw",
360                 "rust-docs",
361                 "rustfmt-preview",
362                 "clippy-preview",
363             ],
364         );
365         profile(
366             "complete",
367             &[
368                 "rustc",
369                 "cargo",
370                 "rust-std",
371                 "rust-mingw",
372                 "rust-docs",
373                 "rustfmt-preview",
374                 "clippy-preview",
375                 "rls-preview",
376                 "rust-analyzer-preview",
377                 "rust-src",
378                 "llvm-tools-preview",
379                 "rust-analysis",
380                 "miri-preview",
381             ],
382         );
383
384         // The compiler libraries are not stable for end users, and they're also huge, so we only
385         // `rustc-dev` for nightly users, and only in the "complete" profile. It's still possible
386         // for users to install the additional component manually, if needed.
387         if self.versions.channel() == "nightly" {
388             self.extend_profile("complete", &mut manifest.profiles, &["rustc-dev"]);
389             // Do not include the rustc-docs component for now, as it causes
390             // conflicts with the rust-docs component when installed. See
391             // #75833.
392             // self.extend_profile("complete", &mut manifest.profiles, &["rustc-docs"]);
393         }
394     }
395
396     fn add_renames_to(&self, manifest: &mut Manifest) {
397         let mut rename = |from: &str, to: &str| {
398             manifest.renames.insert(from.to_owned(), Rename { to: to.to_owned() })
399         };
400         rename("rls", "rls-preview");
401         rename("rustfmt", "rustfmt-preview");
402         rename("clippy", "clippy-preview");
403         rename("miri", "miri-preview");
404     }
405
406     fn rust_package(&mut self, manifest: &Manifest) -> Package {
407         let version_info = self.versions.version(&PkgType::Rust).expect("missing Rust tarball");
408         let mut pkg = Package {
409             version: version_info.version.expect("missing Rust version"),
410             git_commit_hash: version_info.git_commit,
411             target: BTreeMap::new(),
412         };
413         for host in HOSTS {
414             if let Some(target) = self.target_host_combination(host, &manifest) {
415                 pkg.target.insert(host.to_string(), target);
416             } else {
417                 pkg.target.insert(host.to_string(), Target::unavailable());
418                 continue;
419             }
420         }
421         pkg
422     }
423
424     fn target_host_combination(&mut self, host: &str, manifest: &Manifest) -> Option<Target> {
425         let filename = self.versions.tarball_name(&PkgType::Rust, host).unwrap();
426
427         let mut target = Target::from_compressed_tar(self, &filename);
428         if !target.available {
429             return None;
430         }
431
432         let mut components = Vec::new();
433         let mut extensions = Vec::new();
434
435         let host_component = |pkg| Component::from_str(pkg, host);
436
437         // rustc/rust-std/cargo/docs are all required,
438         // and so is rust-mingw if it's available for the target.
439         components.extend(vec![
440             host_component("rustc"),
441             host_component("rust-std"),
442             host_component("cargo"),
443             host_component("rust-docs"),
444         ]);
445         if host.contains("pc-windows-gnu") {
446             components.push(host_component("rust-mingw"));
447         }
448
449         // Tools are always present in the manifest,
450         // but might be marked as unavailable if they weren't built.
451         extensions.extend(vec![
452             host_component("clippy-preview"),
453             host_component("miri-preview"),
454             host_component("rls-preview"),
455             host_component("rust-analyzer-preview"),
456             host_component("rustfmt-preview"),
457             host_component("llvm-tools-preview"),
458             host_component("rust-analysis"),
459         ]);
460
461         extensions.extend(
462             TARGETS
463                 .iter()
464                 .filter(|&&target| target != host)
465                 .map(|target| Component::from_str("rust-std", target)),
466         );
467         extensions.extend(HOSTS.iter().map(|target| Component::from_str("rustc-dev", target)));
468         extensions.extend(HOSTS.iter().map(|target| Component::from_str("rustc-docs", target)));
469         extensions.push(Component::from_str("rust-src", "*"));
470
471         // If the components/extensions don't actually exist for this
472         // particular host/target combination then nix it entirely from our
473         // lists.
474         let has_component = |c: &Component| {
475             if c.target == "*" {
476                 return true;
477             }
478             let pkg = match manifest.pkg.get(&c.pkg) {
479                 Some(p) => p,
480                 None => return false,
481             };
482             pkg.target.get(&c.target).is_some()
483         };
484         extensions.retain(&has_component);
485         components.retain(&has_component);
486
487         target.components = Some(components);
488         target.extensions = Some(extensions);
489         Some(target)
490     }
491
492     fn profile(
493         &mut self,
494         profile_name: &str,
495         dst: &mut BTreeMap<String, Vec<String>>,
496         pkgs: &[&str],
497     ) {
498         dst.insert(profile_name.to_owned(), pkgs.iter().map(|s| (*s).to_owned()).collect());
499     }
500
501     fn extend_profile(
502         &mut self,
503         profile_name: &str,
504         dst: &mut BTreeMap<String, Vec<String>>,
505         pkgs: &[&str],
506     ) {
507         dst.get_mut(profile_name)
508             .expect("existing profile")
509             .extend(pkgs.iter().map(|s| (*s).to_owned()));
510     }
511
512     fn package(
513         &mut self,
514         pkgname: &str,
515         dst: &mut BTreeMap<String, Package>,
516         targets: &[&str],
517         fallback: &[(&str, &str)],
518     ) {
519         let version_info = self
520             .versions
521             .version(&PkgType::from_component(pkgname))
522             .expect("failed to load package version");
523         let mut is_present = version_info.present;
524
525         // Never ship nightly-only components for other trains.
526         if self.versions.channel() != "nightly" && NIGHTLY_ONLY_COMPONENTS.contains(&pkgname) {
527             is_present = false; // Pretend the component is entirely missing.
528         }
529
530         macro_rules! tarball_name {
531             ($target_name:expr) => {
532                 self.versions.tarball_name(&PkgType::from_component(pkgname), $target_name).unwrap()
533             };
534         }
535         let mut target_from_compressed_tar = |target_name| {
536             let target = Target::from_compressed_tar(self, &tarball_name!(target_name));
537             if target.available {
538                 return target;
539             }
540             for (substr, fallback_target) in fallback {
541                 if target_name.contains(substr) {
542                     let t = Target::from_compressed_tar(self, &tarball_name!(fallback_target));
543                     // Fallbacks must always be available.
544                     assert!(t.available);
545                     return t;
546                 }
547             }
548             Target::unavailable()
549         };
550
551         let targets = targets
552             .iter()
553             .map(|name| {
554                 let target = if is_present {
555                     target_from_compressed_tar(name)
556                 } else {
557                     // If the component is not present for this build add it anyway but mark it as
558                     // unavailable -- this way rustup won't allow upgrades without --force
559                     Target::unavailable()
560                 };
561                 (name.to_string(), target)
562             })
563             .collect();
564
565         dst.insert(
566             pkgname.to_string(),
567             Package {
568                 version: version_info.version.unwrap_or_default(),
569                 git_commit_hash: version_info.git_commit,
570                 target: targets,
571             },
572         );
573     }
574
575     fn url(&self, path: &Path) -> String {
576         let file_name = path.file_name().unwrap().to_str().unwrap();
577         format!("{}/{}/{}", self.s3_address, self.date, file_name)
578     }
579
580     fn write_channel_files(&mut self, channel_name: &str, manifest: &Manifest) {
581         self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
582         self.write(&manifest.date, channel_name, "-date.txt");
583         self.write(
584             manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
585             channel_name,
586             "-git-commit-hash.txt",
587         );
588     }
589
590     fn write(&mut self, contents: &str, channel_name: &str, suffix: &str) {
591         let name = format!("channel-rust-{}{}", channel_name, suffix);
592         self.shipped_files.insert(name.clone());
593
594         let dst = self.output.join(name);
595         t!(fs::write(&dst, contents));
596     }
597
598     fn write_shipped_files(&self, path: &Path) {
599         let mut files = self.shipped_files.iter().map(|s| s.as_str()).collect::<Vec<_>>();
600         files.sort();
601         let content = format!("{}\n", files.join("\n"));
602
603         t!(std::fs::write(path, content.as_bytes()));
604     }
605 }