]> git.lizzy.rs Git - rust.git/blob - src/tools/build-manifest/src/main.rs
Unify API of `Scalar` and `ScalarMaybeUndef`
[rust.git] / src / tools / build-manifest / src / main.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 extern crate toml;
12 #[macro_use]
13 extern crate serde_derive;
14
15 use std::collections::BTreeMap;
16 use std::env;
17 use std::fs::File;
18 use std::io::{self, Read, Write};
19 use std::path::{PathBuf, Path};
20 use std::process::{Command, Stdio};
21
22 static HOSTS: &'static [&'static str] = &[
23     "aarch64-unknown-linux-gnu",
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     "powerpc-unknown-linux-gnu",
36     "powerpc64-unknown-linux-gnu",
37     "powerpc64le-unknown-linux-gnu",
38     "s390x-unknown-linux-gnu",
39     "x86_64-apple-darwin",
40     "x86_64-pc-windows-gnu",
41     "x86_64-pc-windows-msvc",
42     "x86_64-unknown-freebsd",
43     "x86_64-unknown-linux-gnu",
44     "x86_64-unknown-netbsd",
45 ];
46
47 static TARGETS: &'static [&'static str] = &[
48     "aarch64-apple-ios",
49     "aarch64-fuchsia",
50     "aarch64-linux-android",
51     "aarch64-unknown-cloudabi",
52     "aarch64-unknown-linux-gnu",
53     "aarch64-unknown-linux-musl",
54     "arm-linux-androideabi",
55     "arm-unknown-linux-gnueabi",
56     "arm-unknown-linux-gnueabihf",
57     "arm-unknown-linux-musleabi",
58     "arm-unknown-linux-musleabihf",
59     "armv5te-unknown-linux-gnueabi",
60     "armv5te-unknown-linux-musleabi",
61     "armv7-apple-ios",
62     "armv7-linux-androideabi",
63     "armv7-unknown-cloudabi-eabihf",
64     "armv7-unknown-linux-gnueabihf",
65     "armv7-unknown-linux-musleabihf",
66     "armebv7r-none-eabihf",
67     "armv7s-apple-ios",
68     "asmjs-unknown-emscripten",
69     "i386-apple-ios",
70     "i586-pc-windows-msvc",
71     "i586-unknown-linux-gnu",
72     "i586-unknown-linux-musl",
73     "i686-apple-darwin",
74     "i686-linux-android",
75     "i686-pc-windows-gnu",
76     "i686-pc-windows-msvc",
77     "i686-unknown-cloudabi",
78     "i686-unknown-freebsd",
79     "i686-unknown-linux-gnu",
80     "i686-unknown-linux-musl",
81     "mips-unknown-linux-gnu",
82     "mips-unknown-linux-musl",
83     "mips64-unknown-linux-gnuabi64",
84     "mips64el-unknown-linux-gnuabi64",
85     "mipsel-unknown-linux-gnu",
86     "mipsel-unknown-linux-musl",
87     "powerpc-unknown-linux-gnu",
88     "powerpc-unknown-linux-gnuspe",
89     "powerpc64-unknown-linux-gnu",
90     "powerpc64le-unknown-linux-gnu",
91     "powerpc64le-unknown-linux-musl",
92     "s390x-unknown-linux-gnu",
93     "sparc-unknown-linux-gnu",
94     "sparc64-unknown-linux-gnu",
95     "sparcv9-sun-solaris",
96     "thumbv6m-none-eabi",
97     "thumbv7em-none-eabi",
98     "thumbv7em-none-eabihf",
99     "thumbv7m-none-eabi",
100     "wasm32-unknown-emscripten",
101     "wasm32-unknown-unknown",
102     "x86_64-apple-darwin",
103     "x86_64-apple-ios",
104     "x86_64-fuchsia",
105     "x86_64-linux-android",
106     "x86_64-pc-windows-gnu",
107     "x86_64-pc-windows-msvc",
108     "x86_64-rumprun-netbsd",
109     "x86_64-sun-solaris",
110     "x86_64-unknown-cloudabi",
111     "x86_64-unknown-freebsd",
112     "x86_64-unknown-hermit",
113     "x86_64-unknown-linux-gnu",
114     "x86_64-unknown-linux-gnux32",
115     "x86_64-unknown-linux-musl",
116     "x86_64-unknown-netbsd",
117     "x86_64-unknown-redox",
118 ];
119
120 static MINGW: &'static [&'static str] = &[
121     "i686-pc-windows-gnu",
122     "x86_64-pc-windows-gnu",
123 ];
124
125 #[derive(Serialize)]
126 #[serde(rename_all = "kebab-case")]
127 struct Manifest {
128     manifest_version: String,
129     date: String,
130     pkg: BTreeMap<String, Package>,
131     renames: BTreeMap<String, Rename>
132 }
133
134 #[derive(Serialize)]
135 struct Package {
136     version: String,
137     git_commit_hash: Option<String>,
138     target: BTreeMap<String, Target>,
139 }
140
141 #[derive(Serialize)]
142 struct Rename {
143     to: String,
144 }
145
146 #[derive(Serialize)]
147 struct Target {
148     available: bool,
149     url: Option<String>,
150     hash: Option<String>,
151     xz_url: Option<String>,
152     xz_hash: Option<String>,
153     components: Option<Vec<Component>>,
154     extensions: Option<Vec<Component>>,
155 }
156
157 impl Target {
158     fn unavailable() -> Target {
159         Target {
160             available: false,
161             url: None,
162             hash: None,
163             xz_url: None,
164             xz_hash: None,
165             components: None,
166             extensions: None,
167         }
168     }
169 }
170
171 #[derive(Serialize)]
172 struct Component {
173     pkg: String,
174     target: String,
175 }
176
177 macro_rules! t {
178     ($e:expr) => (match $e {
179         Ok(e) => e,
180         Err(e) => panic!("{} failed with {}", stringify!($e), e),
181     })
182 }
183
184 struct Builder {
185     rust_release: String,
186     cargo_release: String,
187     rls_release: String,
188     clippy_release: String,
189     rustfmt_release: String,
190     llvm_tools_release: String,
191
192     input: PathBuf,
193     output: PathBuf,
194     gpg_passphrase: String,
195     digests: BTreeMap<String, String>,
196     s3_address: String,
197     date: String,
198
199     rust_version: Option<String>,
200     cargo_version: Option<String>,
201     rls_version: Option<String>,
202     clippy_version: Option<String>,
203     rustfmt_version: Option<String>,
204     llvm_tools_version: Option<String>,
205
206     rust_git_commit_hash: Option<String>,
207     cargo_git_commit_hash: Option<String>,
208     rls_git_commit_hash: Option<String>,
209     clippy_git_commit_hash: Option<String>,
210     rustfmt_git_commit_hash: Option<String>,
211     llvm_tools_git_commit_hash: Option<String>,
212 }
213
214 fn main() {
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 rust_release = args.next().unwrap();
220     let cargo_release = args.next().unwrap();
221     let rls_release = args.next().unwrap();
222     let clippy_release = args.next().unwrap();
223     let rustfmt_release = args.next().unwrap();
224     let llvm_tools_release = args.next().unwrap();
225     let s3_address = args.next().unwrap();
226     let mut passphrase = String::new();
227     t!(io::stdin().read_to_string(&mut passphrase));
228
229     Builder {
230         rust_release,
231         cargo_release,
232         rls_release,
233         clippy_release,
234         rustfmt_release,
235         llvm_tools_release,
236
237         input,
238         output,
239         gpg_passphrase: passphrase,
240         digests: BTreeMap::new(),
241         s3_address,
242         date,
243
244         rust_version: None,
245         cargo_version: None,
246         rls_version: None,
247         clippy_version: None,
248         rustfmt_version: None,
249         llvm_tools_version: None,
250
251         rust_git_commit_hash: None,
252         cargo_git_commit_hash: None,
253         rls_git_commit_hash: None,
254         clippy_git_commit_hash: None,
255         rustfmt_git_commit_hash: None,
256         llvm_tools_git_commit_hash: None,
257     }.build();
258 }
259
260 impl Builder {
261     fn build(&mut self) {
262         self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
263         self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
264         self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
265         self.clippy_version = self.version("clippy", "x86_64-unknown-linux-gnu");
266         self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
267         self.llvm_tools_version = self.version("llvm-tools", "x86_64-unknown-linux-gnu");
268
269         self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
270         self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
271         self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
272         self.clippy_git_commit_hash = self.git_commit_hash("clippy", "x86_64-unknown-linux-gnu");
273         self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
274         self.llvm_tools_git_commit_hash = self.git_commit_hash("llvm-tools",
275                                                                "x86_64-unknown-linux-gnu");
276
277         self.digest_and_sign();
278         let manifest = self.build_manifest();
279         self.write_channel_files(&self.rust_release, &manifest);
280
281         if self.rust_release != "beta" && self.rust_release != "nightly" {
282             self.write_channel_files("stable", &manifest);
283         }
284     }
285
286     fn digest_and_sign(&mut self) {
287         for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
288             let filename = file.file_name().unwrap().to_str().unwrap();
289             let digest = self.hash(&file);
290             self.sign(&file);
291             assert!(self.digests.insert(filename.to_string(), digest).is_none());
292         }
293     }
294
295     fn build_manifest(&mut self) -> Manifest {
296         let mut manifest = Manifest {
297             manifest_version: "2".to_string(),
298             date: self.date.to_string(),
299             pkg: BTreeMap::new(),
300             renames: BTreeMap::new(),
301         };
302
303         self.package("rustc", &mut manifest.pkg, HOSTS);
304         self.package("cargo", &mut manifest.pkg, HOSTS);
305         self.package("rust-mingw", &mut manifest.pkg, MINGW);
306         self.package("rust-std", &mut manifest.pkg, TARGETS);
307         self.package("rust-docs", &mut manifest.pkg, TARGETS);
308         self.package("rust-src", &mut manifest.pkg, &["*"]);
309         self.package("rls-preview", &mut manifest.pkg, HOSTS);
310         self.package("clippy-preview", &mut manifest.pkg, HOSTS);
311         self.package("rustfmt-preview", &mut manifest.pkg, HOSTS);
312         self.package("rust-analysis", &mut manifest.pkg, TARGETS);
313         self.package("llvm-tools-preview", &mut manifest.pkg, TARGETS);
314
315         let clippy_present = manifest.pkg.contains_key("clippy-preview");
316         let rls_present = manifest.pkg.contains_key("rls-preview");
317         let rustfmt_present = manifest.pkg.contains_key("rustfmt-preview");
318         let llvm_tools_present = manifest.pkg.contains_key("llvm-tools-preview");
319
320         if rls_present {
321             manifest.renames.insert("rls".to_owned(), Rename { to: "rls-preview".to_owned() });
322         }
323
324         let mut pkg = Package {
325             version: self.cached_version("rust")
326                          .as_ref()
327                          .expect("Couldn't find Rust version")
328                          .clone(),
329             git_commit_hash: self.cached_git_commit_hash("rust").clone(),
330             target: BTreeMap::new(),
331         };
332         for host in HOSTS {
333             let filename = self.filename("rust", host);
334             let digest = match self.digests.remove(&filename) {
335                 Some(digest) => digest,
336                 None => {
337                     pkg.target.insert(host.to_string(), Target::unavailable());
338                     continue
339                 }
340             };
341             let xz_filename = filename.replace(".tar.gz", ".tar.xz");
342             let xz_digest = self.digests.remove(&xz_filename);
343             let mut components = Vec::new();
344             let mut extensions = Vec::new();
345
346             // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
347             // if it's available for the target.
348             components.extend(vec![
349                 Component { pkg: "rustc".to_string(), target: host.to_string() },
350                 Component { pkg: "rust-std".to_string(), target: host.to_string() },
351                 Component { pkg: "cargo".to_string(), target: host.to_string() },
352                 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
353             ]);
354             if host.contains("pc-windows-gnu") {
355                 components.push(Component {
356                     pkg: "rust-mingw".to_string(),
357                     target: host.to_string(),
358                 });
359             }
360
361             if clippy_present {
362                 extensions.push(Component {
363                     pkg: "clippy-preview".to_string(),
364                     target: host.to_string(),
365                 });
366             }
367             if rls_present {
368                 extensions.push(Component {
369                     pkg: "rls-preview".to_string(),
370                     target: host.to_string(),
371                 });
372             }
373             if rustfmt_present {
374                 extensions.push(Component {
375                     pkg: "rustfmt-preview".to_string(),
376                     target: host.to_string(),
377                 });
378             }
379             if llvm_tools_present {
380                 extensions.push(Component {
381                     pkg: "llvm-tools-preview".to_string(),
382                     target: host.to_string(),
383                 });
384             }
385             extensions.push(Component {
386                 pkg: "rust-analysis".to_string(),
387                 target: host.to_string(),
388             });
389             for target in TARGETS {
390                 if target != host {
391                     extensions.push(Component {
392                         pkg: "rust-std".to_string(),
393                         target: target.to_string(),
394                     });
395                 }
396             }
397             extensions.push(Component {
398                 pkg: "rust-src".to_string(),
399                 target: "*".to_string(),
400             });
401
402             // If the components/extensions don't actually exist for this
403             // particular host/target combination then nix it entirely from our
404             // lists.
405             {
406                 let has_component = |c: &Component| {
407                     if c.target == "*" {
408                         return true
409                     }
410                     let pkg = match manifest.pkg.get(&c.pkg) {
411                         Some(p) => p,
412                         None => return false,
413                     };
414                     let target = match pkg.target.get(&c.target) {
415                         Some(t) => t,
416                         None => return false,
417                     };
418                     target.available
419                 };
420                 extensions.retain(&has_component);
421                 components.retain(&has_component);
422             }
423
424             pkg.target.insert(host.to_string(), Target {
425                 available: true,
426                 url: Some(self.url(&filename)),
427                 hash: Some(digest),
428                 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
429                 xz_hash: xz_digest,
430                 components: Some(components),
431                 extensions: Some(extensions),
432             });
433         }
434         manifest.pkg.insert("rust".to_string(), pkg);
435
436         return manifest;
437     }
438
439     fn package(&mut self,
440                pkgname: &str,
441                dst: &mut BTreeMap<String, Package>,
442                targets: &[&str]) {
443         let version = match *self.cached_version(pkgname) {
444             Some(ref version) => version.clone(),
445             None => {
446                 println!("Skipping package {}", pkgname);
447                 return;
448             }
449         };
450
451         let targets = targets.iter().map(|name| {
452             let filename = self.filename(pkgname, name);
453             let digest = match self.digests.remove(&filename) {
454                 Some(digest) => digest,
455                 None => return (name.to_string(), Target::unavailable()),
456             };
457             let xz_filename = filename.replace(".tar.gz", ".tar.xz");
458             let xz_digest = self.digests.remove(&xz_filename);
459
460             (name.to_string(), Target {
461                 available: true,
462                 url: Some(self.url(&filename)),
463                 hash: Some(digest),
464                 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
465                 xz_hash: xz_digest,
466                 components: None,
467                 extensions: None,
468             })
469         }).collect();
470
471         dst.insert(pkgname.to_string(), Package {
472             version,
473             git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
474             target: targets,
475         });
476     }
477
478     fn url(&self, filename: &str) -> String {
479         format!("{}/{}/{}",
480                 self.s3_address,
481                 self.date,
482                 filename)
483     }
484
485     fn filename(&self, component: &str, target: &str) -> String {
486         if component == "rust-src" {
487             format!("rust-src-{}.tar.gz", self.rust_release)
488         } else if component == "cargo" {
489             format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
490         } else if component == "rls" || component == "rls-preview" {
491             format!("rls-{}-{}.tar.gz", self.rls_release, target)
492         } else if component == "clippy" || component == "clippy-preview" {
493             format!("clippy-{}-{}.tar.gz", self.clippy_release, target)
494         } else if component == "rustfmt" || component == "rustfmt-preview" {
495             format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target)
496         } else if component == "llvm-tools" || component == "llvm-tools-preview" {
497             format!("llvm-tools-{}-{}.tar.gz", self.llvm_tools_release, target)
498         } else {
499             format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
500         }
501     }
502
503     fn cached_version(&self, component: &str) -> &Option<String> {
504         if component == "cargo" {
505             &self.cargo_version
506         } else if component == "rls" || component == "rls-preview" {
507             &self.rls_version
508         } else if component == "clippy" || component == "clippy-preview" {
509             &self.clippy_version
510         } else if component == "rustfmt" || component == "rustfmt-preview" {
511             &self.rustfmt_version
512         } else if component == "llvm-tools" || component == "llvm-tools-preview" {
513             &self.llvm_tools_version
514         } else {
515             &self.rust_version
516         }
517     }
518
519     fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
520         if component == "cargo" {
521             &self.cargo_git_commit_hash
522         } else if component == "rls" || component == "rls-preview" {
523             &self.rls_git_commit_hash
524         } else if component == "clippy" || component == "clippy-preview" {
525             &self.clippy_git_commit_hash
526         } else if component == "rustfmt" || component == "rustfmt-preview" {
527             &self.rustfmt_git_commit_hash
528         } else if component == "llvm-tools" || component == "llvm-tools-preview" {
529             &self.llvm_tools_git_commit_hash
530         } else {
531             &self.rust_git_commit_hash
532         }
533     }
534
535     fn version(&self, component: &str, target: &str) -> Option<String> {
536         let mut cmd = Command::new("tar");
537         let filename = self.filename(component, target);
538         cmd.arg("xf")
539            .arg(self.input.join(&filename))
540            .arg(format!("{}/version", filename.replace(".tar.gz", "")))
541            .arg("-O");
542         let output = t!(cmd.output());
543         if output.status.success() {
544             Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
545         } else {
546             // Perhaps we didn't build this package.
547             None
548         }
549     }
550
551     fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
552         let mut cmd = Command::new("tar");
553         let filename = self.filename(component, target);
554         cmd.arg("xf")
555            .arg(self.input.join(&filename))
556            .arg(format!("{}/git-commit-hash", filename.replace(".tar.gz", "")))
557            .arg("-O");
558         let output = t!(cmd.output());
559         if output.status.success() {
560             Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
561         } else {
562             None
563         }
564     }
565
566     fn hash(&self, path: &Path) -> String {
567         let sha = t!(Command::new("shasum")
568                         .arg("-a").arg("256")
569                         .arg(path.file_name().unwrap())
570                         .current_dir(path.parent().unwrap())
571                         .output());
572         assert!(sha.status.success());
573
574         let filename = path.file_name().unwrap().to_str().unwrap();
575         let sha256 = self.output.join(format!("{}.sha256", filename));
576         t!(t!(File::create(&sha256)).write_all(&sha.stdout));
577
578         let stdout = String::from_utf8_lossy(&sha.stdout);
579         stdout.split_whitespace().next().unwrap().to_string()
580     }
581
582     fn sign(&self, path: &Path) {
583         let filename = path.file_name().unwrap().to_str().unwrap();
584         let asc = self.output.join(format!("{}.asc", filename));
585         println!("signing: {:?}", path);
586         let mut cmd = Command::new("gpg");
587         cmd.arg("--no-tty")
588             .arg("--yes")
589             .arg("--passphrase-fd").arg("0")
590             .arg("--personal-digest-preferences").arg("SHA512")
591             .arg("--armor")
592             .arg("--output").arg(&asc)
593             .arg("--detach-sign").arg(path)
594             .stdin(Stdio::piped());
595         let mut child = t!(cmd.spawn());
596         t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
597         assert!(t!(child.wait()).success());
598     }
599
600     fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
601         self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
602         self.write(&manifest.date, channel_name, "-date.txt");
603         self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
604                    channel_name, "-git-commit-hash.txt");
605     }
606
607     fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
608         let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
609         t!(t!(File::create(&dst)).write_all(contents.as_bytes()));
610         self.hash(&dst);
611         self.sign(&dst);
612     }
613 }