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