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