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