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