]> git.lizzy.rs Git - rust.git/blob - src/tools/build-manifest/src/main.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[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 extern crate rustc_serialize;
13
14 use std::collections::BTreeMap;
15 use std::env;
16 use std::fs::File;
17 use std::io::{self, Read, Write};
18 use std::path::{PathBuf, Path};
19 use std::process::{Command, Stdio};
20
21 static HOSTS: &'static [&'static str] = &[
22     "aarch64-unknown-linux-gnu",
23     "arm-unknown-linux-gnueabi",
24     "arm-unknown-linux-gnueabihf",
25     "armv7-unknown-linux-gnueabihf",
26     "i686-apple-darwin",
27     "i686-pc-windows-gnu",
28     "i686-pc-windows-msvc",
29     "i686-unknown-linux-gnu",
30     "mips-unknown-linux-gnu",
31     "mips64-unknown-linux-gnuabi64",
32     "mips64el-unknown-linux-gnuabi64",
33     "mipsel-unknown-linux-gnu",
34     "powerpc-unknown-linux-gnu",
35     "powerpc64-unknown-linux-gnu",
36     "powerpc64le-unknown-linux-gnu",
37     "s390x-unknown-linux-gnu",
38     "x86_64-apple-darwin",
39     "x86_64-pc-windows-gnu",
40     "x86_64-pc-windows-msvc",
41     "x86_64-unknown-freebsd",
42     "x86_64-unknown-linux-gnu",
43     "x86_64-unknown-netbsd",
44 ];
45
46 static TARGETS: &'static [&'static str] = &[
47     "aarch64-apple-ios",
48     "aarch64-unknown-fuchsia",
49     "aarch64-linux-android",
50     "aarch64-unknown-linux-gnu",
51     "arm-linux-androideabi",
52     "arm-unknown-linux-gnueabi",
53     "arm-unknown-linux-gnueabihf",
54     "arm-unknown-linux-musleabi",
55     "arm-unknown-linux-musleabihf",
56     "armv7-apple-ios",
57     "armv7-linux-androideabi",
58     "armv7-unknown-linux-gnueabihf",
59     "armv7-unknown-linux-musleabihf",
60     "armv7s-apple-ios",
61     "asmjs-unknown-emscripten",
62     "i386-apple-ios",
63     "i586-pc-windows-msvc",
64     "i586-unknown-linux-gnu",
65     "i686-apple-darwin",
66     "i686-linux-android",
67     "i686-pc-windows-gnu",
68     "i686-pc-windows-msvc",
69     "i686-unknown-freebsd",
70     "i686-unknown-linux-gnu",
71     "i686-unknown-linux-musl",
72     "mips-unknown-linux-gnu",
73     "mips-unknown-linux-musl",
74     "mips64-unknown-linux-gnuabi64",
75     "mips64el-unknown-linux-gnuabi64",
76     "mipsel-unknown-linux-gnu",
77     "mipsel-unknown-linux-musl",
78     "powerpc-unknown-linux-gnu",
79     "powerpc64-unknown-linux-gnu",
80     "powerpc64le-unknown-linux-gnu",
81     "s390x-unknown-linux-gnu",
82     "sparc64-unknown-linux-gnu",
83     "wasm32-unknown-emscripten",
84     "x86_64-apple-darwin",
85     "x86_64-apple-ios",
86     "x86_64-pc-windows-gnu",
87     "x86_64-pc-windows-msvc",
88     "x86_64-rumprun-netbsd",
89     "x86_64-unknown-freebsd",
90     "x86_64-unknown-fuchsia",
91     "x86_64-unknown-linux-gnu",
92     "x86_64-unknown-linux-musl",
93     "x86_64-unknown-netbsd",
94 ];
95
96 static MINGW: &'static [&'static str] = &[
97     "i686-pc-windows-gnu",
98     "x86_64-pc-windows-gnu",
99 ];
100
101 struct Manifest {
102     manifest_version: String,
103     date: String,
104     pkg: BTreeMap<String, Package>,
105 }
106
107 #[derive(RustcEncodable)]
108 struct Package {
109     version: String,
110     target: BTreeMap<String, Target>,
111 }
112
113 #[derive(RustcEncodable)]
114 struct Target {
115     available: bool,
116     url: Option<String>,
117     hash: Option<String>,
118     components: Option<Vec<Component>>,
119     extensions: Option<Vec<Component>>,
120 }
121
122 #[derive(RustcEncodable)]
123 struct Component {
124     pkg: String,
125     target: String,
126 }
127
128 macro_rules! t {
129     ($e:expr) => (match $e {
130         Ok(e) => e,
131         Err(e) => panic!("{} failed with {}", stringify!($e), e),
132     })
133 }
134
135 struct Builder {
136     rust_release: String,
137     cargo_release: String,
138     rls_release: String,
139     input: PathBuf,
140     output: PathBuf,
141     gpg_passphrase: String,
142     digests: BTreeMap<String, String>,
143     s3_address: String,
144     date: String,
145     rust_version: String,
146     cargo_version: String,
147     rls_version: String,
148 }
149
150 fn main() {
151     let mut args = env::args().skip(1);
152     let input = PathBuf::from(args.next().unwrap());
153     let output = PathBuf::from(args.next().unwrap());
154     let date = args.next().unwrap();
155     let rust_release = args.next().unwrap();
156     let cargo_release = args.next().unwrap();
157     let rls_release = args.next().unwrap();
158     let s3_address = args.next().unwrap();
159     let mut passphrase = String::new();
160     t!(io::stdin().read_to_string(&mut passphrase));
161
162     Builder {
163         rust_release: rust_release,
164         cargo_release: cargo_release,
165         rls_release: rls_release,
166         input: input,
167         output: output,
168         gpg_passphrase: passphrase,
169         digests: BTreeMap::new(),
170         s3_address: s3_address,
171         date: date,
172         rust_version: String::new(),
173         cargo_version: String::new(),
174         rls_version: String::new(),
175     }.build();
176 }
177
178 impl Builder {
179     fn build(&mut self) {
180         self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
181         self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
182         self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
183
184         self.digest_and_sign();
185         let Manifest { manifest_version, date, pkg } = self.build_manifest();
186
187         // Unfortunately we can't use derive(RustcEncodable) here because the
188         // version field is called `manifest-version`, not `manifest_version`.
189         // In lieu of that just create the table directly here with a `BTreeMap`
190         // and wrap it up in a `Value::Table`.
191         let mut manifest = BTreeMap::new();
192         manifest.insert("manifest-version".to_string(),
193                         toml::Value::String(manifest_version));
194         manifest.insert("date".to_string(), toml::Value::String(date.clone()));
195         manifest.insert("pkg".to_string(), toml::encode(&pkg));
196         let manifest = toml::Value::Table(manifest).to_string();
197
198         let filename = format!("channel-rust-{}.toml", self.rust_release);
199         self.write_manifest(&manifest, &filename);
200
201         let filename = format!("channel-rust-{}-date.txt", self.rust_release);
202         self.write_date_stamp(&date, &filename);
203
204         if self.rust_release != "beta" && self.rust_release != "nightly" {
205             self.write_manifest(&manifest, "channel-rust-stable.toml");
206             self.write_date_stamp(&date, "channel-rust-stable-date.txt");
207         }
208     }
209
210     fn digest_and_sign(&mut self) {
211         for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
212             let filename = file.file_name().unwrap().to_str().unwrap();
213             let digest = self.hash(&file);
214             self.sign(&file);
215             assert!(self.digests.insert(filename.to_string(), digest).is_none());
216         }
217     }
218
219     fn build_manifest(&mut self) -> Manifest {
220         let mut manifest = Manifest {
221             manifest_version: "2".to_string(),
222             date: self.date.to_string(),
223             pkg: BTreeMap::new(),
224         };
225
226         self.package("rustc", &mut manifest.pkg, HOSTS);
227         self.package("cargo", &mut manifest.pkg, HOSTS);
228         self.package("rust-mingw", &mut manifest.pkg, MINGW);
229         self.package("rust-std", &mut manifest.pkg, TARGETS);
230         self.package("rust-docs", &mut manifest.pkg, TARGETS);
231         self.package("rust-src", &mut manifest.pkg, &["*"]);
232         self.package("rls", &mut manifest.pkg, HOSTS);
233         self.package("rust-analysis", &mut manifest.pkg, TARGETS);
234
235         let mut pkg = Package {
236             version: self.cached_version("rust").to_string(),
237             target: BTreeMap::new(),
238         };
239         for host in HOSTS {
240             let filename = self.filename("rust", host);
241             let digest = match self.digests.remove(&filename) {
242                 Some(digest) => digest,
243                 None => {
244                     pkg.target.insert(host.to_string(), Target {
245                         available: false,
246                         url: None,
247                         hash: None,
248                         components: None,
249                         extensions: None,
250                     });
251                     continue
252                 }
253             };
254             let mut components = Vec::new();
255             let mut extensions = Vec::new();
256
257             // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
258             // if it's available for the target.
259             components.extend(vec![
260                 Component { pkg: "rustc".to_string(), target: host.to_string() },
261                 Component { pkg: "rust-std".to_string(), target: host.to_string() },
262                 Component { pkg: "cargo".to_string(), target: host.to_string() },
263                 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
264             ]);
265             if host.contains("pc-windows-gnu") {
266                 components.push(Component {
267                     pkg: "rust-mingw".to_string(),
268                     target: host.to_string(),
269                 });
270             }
271
272             extensions.push(Component {
273                 pkg: "rls".to_string(),
274                 target: host.to_string(),
275             });
276             extensions.push(Component {
277                 pkg: "rust-analysis".to_string(),
278                 target: host.to_string(),
279             });
280             for target in TARGETS {
281                 if target != host {
282                     extensions.push(Component {
283                         pkg: "rust-std".to_string(),
284                         target: target.to_string(),
285                     });
286                 }
287             }
288             extensions.push(Component {
289                 pkg: "rust-src".to_string(),
290                 target: "*".to_string(),
291             });
292
293             pkg.target.insert(host.to_string(), Target {
294                 available: true,
295                 url: Some(self.url("rust", host)),
296                 hash: Some(digest),
297                 components: Some(components),
298                 extensions: Some(extensions),
299             });
300         }
301         manifest.pkg.insert("rust".to_string(), pkg);
302
303         return manifest
304     }
305
306     fn package(&mut self,
307                pkgname: &str,
308                dst: &mut BTreeMap<String, Package>,
309                targets: &[&str]) {
310         let targets = targets.iter().map(|name| {
311             let filename = self.filename(pkgname, name);
312             let digest = match self.digests.remove(&filename) {
313                 Some(digest) => digest,
314                 None => {
315                     return (name.to_string(), Target {
316                         available: false,
317                         url: None,
318                         hash: None,
319                         components: None,
320                         extensions: None,
321                     })
322                 }
323             };
324
325             (name.to_string(), Target {
326                 available: true,
327                 url: Some(self.url(pkgname, name)),
328                 hash: Some(digest),
329                 components: None,
330                 extensions: None,
331             })
332         }).collect();
333
334         dst.insert(pkgname.to_string(), Package {
335             version: self.cached_version(pkgname).to_string(),
336             target: targets,
337         });
338     }
339
340     fn url(&self, component: &str, target: &str) -> String {
341         format!("{}/{}/{}",
342                 self.s3_address,
343                 self.date,
344                 self.filename(component, target))
345     }
346
347     fn filename(&self, component: &str, target: &str) -> String {
348         if component == "rust-src" {
349             format!("rust-src-{}.tar.gz", self.rust_release)
350         } else if component == "cargo" {
351             format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
352         } else if component == "rls" {
353             format!("rls-{}-{}.tar.gz", self.rls_release, target)
354         } else {
355             format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
356         }
357     }
358
359     fn cached_version(&self, component: &str) -> &str {
360         if component == "cargo" {
361             &self.cargo_version
362         } else if component == "rls" {
363             &self.rls_version
364         } else {
365             &self.rust_version
366         }
367     }
368
369     fn version(&self, component: &str, target: &str) -> String {
370         let mut cmd = Command::new("tar");
371         let filename = self.filename(component, target);
372         cmd.arg("xf")
373            .arg(self.input.join(&filename))
374            .arg(format!("{}/version", filename.replace(".tar.gz", "")))
375            .arg("-O");
376         let version = t!(cmd.output());
377         if !version.status.success() {
378             panic!("failed to learn version:\n\n{:?}\n\n{}\n\n{}",
379                    cmd,
380                    String::from_utf8_lossy(&version.stdout),
381                    String::from_utf8_lossy(&version.stderr));
382         }
383         String::from_utf8_lossy(&version.stdout).trim().to_string()
384     }
385
386     fn hash(&self, path: &Path) -> String {
387         let sha = t!(Command::new("shasum")
388                         .arg("-a").arg("256")
389                         .arg(path.file_name().unwrap())
390                         .current_dir(path.parent().unwrap())
391                         .output());
392         assert!(sha.status.success());
393
394         let filename = path.file_name().unwrap().to_str().unwrap();
395         let sha256 = self.output.join(format!("{}.sha256", filename));
396         t!(t!(File::create(&sha256)).write_all(&sha.stdout));
397
398         let stdout = String::from_utf8_lossy(&sha.stdout);
399         stdout.split_whitespace().next().unwrap().to_string()
400     }
401
402     fn sign(&self, path: &Path) {
403         let filename = path.file_name().unwrap().to_str().unwrap();
404         let asc = self.output.join(format!("{}.asc", filename));
405         println!("signing: {:?}", path);
406         let mut cmd = Command::new("gpg");
407         cmd.arg("--no-tty")
408             .arg("--yes")
409             .arg("--passphrase-fd").arg("0")
410             .arg("--armor")
411             .arg("--output").arg(&asc)
412             .arg("--detach-sign").arg(path)
413             .stdin(Stdio::piped());
414         let mut child = t!(cmd.spawn());
415         t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
416         assert!(t!(child.wait()).success());
417     }
418
419     fn write_manifest(&self, manifest: &str, name: &str) {
420         let dst = self.output.join(name);
421         t!(t!(File::create(&dst)).write_all(manifest.as_bytes()));
422         self.hash(&dst);
423         self.sign(&dst);
424     }
425
426     fn write_date_stamp(&self, date: &str, name: &str) {
427         let dst = self.output.join(name);
428         t!(t!(File::create(&dst)).write_all(date.as_bytes()));
429         self.hash(&dst);
430         self.sign(&dst);
431     }
432 }