]> git.lizzy.rs Git - rust.git/blob - src/tools/build-manifest/src/main.rs
Only install rustc-dev by default on nightly
[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 #![deny(warnings)]
8
9 use toml;
10 use serde::Serialize;
11
12 use std::collections::BTreeMap;
13 use std::env;
14 use std::fs;
15 use std::io::{self, Read, Write};
16 use std::path::{PathBuf, Path};
17 use std::process::{Command, Stdio};
18
19 static HOSTS: &[&str] = &[
20     "aarch64-unknown-linux-gnu",
21     "arm-unknown-linux-gnueabi",
22     "arm-unknown-linux-gnueabihf",
23     "armv7-unknown-linux-gnueabihf",
24     "i686-apple-darwin",
25     "i686-pc-windows-gnu",
26     "i686-pc-windows-msvc",
27     "i686-unknown-linux-gnu",
28     "mips-unknown-linux-gnu",
29     "mips64-unknown-linux-gnuabi64",
30     "mips64el-unknown-linux-gnuabi64",
31     "mipsel-unknown-linux-gnu",
32     "mipsisa32r6-unknown-linux-gnu",
33     "mipsisa32r6el-unknown-linux-gnu",
34     "mipsisa64r6-unknown-linux-gnuabi64",
35     "mipsisa64r6el-unknown-linux-gnuabi64",
36     "powerpc-unknown-linux-gnu",
37     "powerpc64-unknown-linux-gnu",
38     "powerpc64le-unknown-linux-gnu",
39     "s390x-unknown-linux-gnu",
40     "x86_64-apple-darwin",
41     "x86_64-pc-windows-gnu",
42     "x86_64-pc-windows-msvc",
43     "x86_64-unknown-freebsd",
44     "x86_64-unknown-linux-gnu",
45     "x86_64-unknown-linux-musl",
46     "x86_64-unknown-netbsd",
47 ];
48
49 static TARGETS: &[&str] = &[
50     "aarch64-apple-ios",
51     "aarch64-fuchsia",
52     "aarch64-linux-android",
53     "aarch64-pc-windows-msvc",
54     "aarch64-unknown-cloudabi",
55     "aarch64-unknown-linux-gnu",
56     "aarch64-unknown-linux-musl",
57     "aarch64-unknown-redox",
58     "arm-linux-androideabi",
59     "arm-unknown-linux-gnueabi",
60     "arm-unknown-linux-gnueabihf",
61     "arm-unknown-linux-musleabi",
62     "arm-unknown-linux-musleabihf",
63     "armv5te-unknown-linux-gnueabi",
64     "armv5te-unknown-linux-musleabi",
65     "armv7-apple-ios",
66     "armv7-linux-androideabi",
67     "thumbv7neon-linux-androideabi",
68     "armv7-unknown-linux-gnueabi",
69     "armv7-unknown-linux-gnueabihf",
70     "thumbv7neon-unknown-linux-gnueabihf",
71     "armv7-unknown-linux-musleabi",
72     "armv7-unknown-linux-musleabihf",
73     "armebv7r-none-eabi",
74     "armebv7r-none-eabihf",
75     "armv7r-none-eabi",
76     "armv7r-none-eabihf",
77     "armv7s-apple-ios",
78     "asmjs-unknown-emscripten",
79     "i386-apple-ios",
80     "i586-pc-windows-msvc",
81     "i586-unknown-linux-gnu",
82     "i586-unknown-linux-musl",
83     "i686-apple-darwin",
84     "i686-linux-android",
85     "i686-pc-windows-gnu",
86     "i686-pc-windows-msvc",
87     "i686-unknown-freebsd",
88     "i686-unknown-linux-gnu",
89     "i686-unknown-linux-musl",
90     "mips-unknown-linux-gnu",
91     "mips-unknown-linux-musl",
92     "mips64-unknown-linux-gnuabi64",
93     "mips64el-unknown-linux-gnuabi64",
94     "mipsisa32r6-unknown-linux-gnu",
95     "mipsisa32r6el-unknown-linux-gnu",
96     "mipsisa64r6-unknown-linux-gnuabi64",
97     "mipsisa64r6el-unknown-linux-gnuabi64",
98     "mipsel-unknown-linux-gnu",
99     "mipsel-unknown-linux-musl",
100     "nvptx64-nvidia-cuda",
101     "powerpc-unknown-linux-gnu",
102     "powerpc64-unknown-linux-gnu",
103     "powerpc64le-unknown-linux-gnu",
104     "riscv32i-unknown-none-elf",
105     "riscv32imc-unknown-none-elf",
106     "riscv32imac-unknown-none-elf",
107     "riscv64imac-unknown-none-elf",
108     "riscv64gc-unknown-none-elf",
109     "s390x-unknown-linux-gnu",
110     "sparc64-unknown-linux-gnu",
111     "sparcv9-sun-solaris",
112     "thumbv6m-none-eabi",
113     "thumbv7em-none-eabi",
114     "thumbv7em-none-eabihf",
115     "thumbv7m-none-eabi",
116     "thumbv8m.base-none-eabi",
117     "thumbv8m.main-none-eabi",
118     "thumbv8m.main-none-eabihf",
119     "wasm32-unknown-emscripten",
120     "wasm32-unknown-unknown",
121     "wasm32-wasi",
122     "x86_64-apple-darwin",
123     "x86_64-apple-ios",
124     "x86_64-fortanix-unknown-sgx",
125     "x86_64-fuchsia",
126     "x86_64-linux-android",
127     "x86_64-pc-windows-gnu",
128     "x86_64-pc-windows-msvc",
129     "x86_64-rumprun-netbsd",
130     "x86_64-sun-solaris",
131     "x86_64-pc-solaris",
132     "x86_64-unknown-cloudabi",
133     "x86_64-unknown-freebsd",
134     "x86_64-unknown-linux-gnu",
135     "x86_64-unknown-linux-gnux32",
136     "x86_64-unknown-linux-musl",
137     "x86_64-unknown-netbsd",
138     "x86_64-unknown-redox",
139 ];
140
141 static DOCS_TARGETS: &[&str] = &[
142     "i686-apple-darwin",
143     "i686-pc-windows-gnu",
144     "i686-pc-windows-msvc",
145     "i686-unknown-linux-gnu",
146     "x86_64-apple-darwin",
147     "x86_64-pc-windows-gnu",
148     "x86_64-pc-windows-msvc",
149     "x86_64-unknown-linux-gnu",
150 ];
151
152 static MINGW: &[&str] = &[
153     "i686-pc-windows-gnu",
154     "x86_64-pc-windows-gnu",
155 ];
156
157 #[derive(Serialize)]
158 #[serde(rename_all = "kebab-case")]
159 struct Manifest {
160     manifest_version: String,
161     date: String,
162     pkg: BTreeMap<String, Package>,
163     renames: BTreeMap<String, Rename>,
164     profiles: BTreeMap<String, Vec<String>>,
165 }
166
167 #[derive(Serialize)]
168 struct Package {
169     version: String,
170     git_commit_hash: Option<String>,
171     target: BTreeMap<String, Target>,
172 }
173
174 #[derive(Serialize)]
175 struct Rename {
176     to: String,
177 }
178
179 #[derive(Serialize, Default)]
180 struct Target {
181     available: bool,
182     url: Option<String>,
183     hash: Option<String>,
184     xz_url: Option<String>,
185     xz_hash: Option<String>,
186     components: Option<Vec<Component>>,
187     extensions: Option<Vec<Component>>,
188 }
189
190 impl Target {
191     fn unavailable() -> Self { Self::default() }
192 }
193
194 #[derive(Serialize)]
195 struct Component {
196     pkg: String,
197     target: String,
198 }
199
200 impl Component {
201     fn from_str(pkg: &str, target: &str) -> Self {
202         Self { pkg: pkg.to_string(), target: target.to_string() }
203     }
204 }
205
206 macro_rules! t {
207     ($e:expr) => (match $e {
208         Ok(e) => e,
209         Err(e) => panic!("{} failed with {}", stringify!($e), e),
210     })
211 }
212
213 struct Builder {
214     rust_release: String,
215     cargo_release: String,
216     rls_release: String,
217     clippy_release: String,
218     rustfmt_release: String,
219     llvm_tools_release: String,
220     lldb_release: String,
221     miri_release: String,
222
223     input: PathBuf,
224     output: PathBuf,
225     gpg_passphrase: String,
226     digests: BTreeMap<String, String>,
227     s3_address: String,
228     date: String,
229
230     rust_version: Option<String>,
231     cargo_version: Option<String>,
232     rls_version: Option<String>,
233     clippy_version: Option<String>,
234     rustfmt_version: Option<String>,
235     llvm_tools_version: Option<String>,
236     lldb_version: Option<String>,
237     miri_version: Option<String>,
238
239     rust_git_commit_hash: Option<String>,
240     cargo_git_commit_hash: Option<String>,
241     rls_git_commit_hash: Option<String>,
242     clippy_git_commit_hash: Option<String>,
243     rustfmt_git_commit_hash: Option<String>,
244     llvm_tools_git_commit_hash: Option<String>,
245     lldb_git_commit_hash: Option<String>,
246     miri_git_commit_hash: Option<String>,
247
248     should_sign: bool,
249 }
250
251 fn main() {
252     // Avoid signing packages while manually testing
253     // Do NOT set this envvar in CI
254     let should_sign = env::var("BUILD_MANIFEST_DISABLE_SIGNING").is_err();
255
256     // Safety check to ensure signing is always enabled on CI
257     // The CI environment variable is set by both Travis and AppVeyor
258     if !should_sign && env::var("CI").is_ok() {
259         println!("The 'BUILD_MANIFEST_DISABLE_SIGNING' env var can't be enabled on CI.");
260         println!("If you're not running this on CI, unset the 'CI' env var.");
261         panic!();
262     }
263
264     let mut args = env::args().skip(1);
265     let input = PathBuf::from(args.next().unwrap());
266     let output = PathBuf::from(args.next().unwrap());
267     let date = args.next().unwrap();
268     let rust_release = args.next().unwrap();
269     let s3_address = args.next().unwrap();
270     let cargo_release = args.next().unwrap();
271     let rls_release = args.next().unwrap();
272     let clippy_release = args.next().unwrap();
273     let miri_release = args.next().unwrap();
274     let rustfmt_release = args.next().unwrap();
275     let llvm_tools_release = args.next().unwrap();
276     let lldb_release = args.next().unwrap();
277
278     // Do not ask for a passphrase while manually testing
279     let mut passphrase = String::new();
280     if should_sign {
281         // `x.py` passes the passphrase via stdin.
282         t!(io::stdin().read_to_string(&mut passphrase));
283     }
284
285     Builder {
286         rust_release,
287         cargo_release,
288         rls_release,
289         clippy_release,
290         rustfmt_release,
291         llvm_tools_release,
292         lldb_release,
293         miri_release,
294
295         input,
296         output,
297         gpg_passphrase: passphrase,
298         digests: BTreeMap::new(),
299         s3_address,
300         date,
301
302         rust_version: None,
303         cargo_version: None,
304         rls_version: None,
305         clippy_version: None,
306         rustfmt_version: None,
307         llvm_tools_version: None,
308         lldb_version: None,
309         miri_version: None,
310
311         rust_git_commit_hash: None,
312         cargo_git_commit_hash: None,
313         rls_git_commit_hash: None,
314         clippy_git_commit_hash: None,
315         rustfmt_git_commit_hash: None,
316         llvm_tools_git_commit_hash: None,
317         lldb_git_commit_hash: None,
318         miri_git_commit_hash: None,
319
320         should_sign,
321     }.build();
322 }
323
324 enum PkgType { RustSrc, Cargo, Rls, Clippy, Rustfmt, LlvmTools, Lldb, Miri, Other }
325
326 impl PkgType {
327     fn from_component(component: &str) -> Self {
328         use PkgType::*;
329         match component {
330             "rust-src" => RustSrc,
331             "cargo" => Cargo,
332             "rls" | "rls-preview" => Rls,
333             "clippy" | "clippy-preview" => Clippy,
334             "rustfmt" | "rustfmt-preview" => Rustfmt,
335             "llvm-tools" | "llvm-tools-preview" => LlvmTools,
336             "lldb" | "lldb-preview" => Lldb,
337             "miri" | "miri-preview" => Miri,
338             _ => Other,
339         }
340     }
341 }
342
343 impl Builder {
344     fn build(&mut self) {
345         self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
346         self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
347         self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
348         self.clippy_version = self.version("clippy", "x86_64-unknown-linux-gnu");
349         self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
350         self.llvm_tools_version = self.version("llvm-tools", "x86_64-unknown-linux-gnu");
351         // lldb is only built for macOS.
352         self.lldb_version = self.version("lldb", "x86_64-apple-darwin");
353         self.miri_version = self.version("miri", "x86_64-unknown-linux-gnu");
354
355         self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
356         self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
357         self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
358         self.clippy_git_commit_hash = self.git_commit_hash("clippy", "x86_64-unknown-linux-gnu");
359         self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
360         self.llvm_tools_git_commit_hash = self.git_commit_hash("llvm-tools",
361                                                                "x86_64-unknown-linux-gnu");
362         self.lldb_git_commit_hash = self.git_commit_hash("lldb", "x86_64-unknown-linux-gnu");
363         self.miri_git_commit_hash = self.git_commit_hash("miri", "x86_64-unknown-linux-gnu");
364
365         self.digest_and_sign();
366         let manifest = self.build_manifest();
367         self.write_channel_files(&self.rust_release, &manifest);
368
369         if self.rust_release != "beta" && self.rust_release != "nightly" {
370             self.write_channel_files("stable", &manifest);
371         }
372     }
373
374     /// Hash all files, compute their signatures, and collect the hashes in `self.digests`.
375     fn digest_and_sign(&mut self) {
376         for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
377             let filename = file.file_name().unwrap().to_str().unwrap();
378             let digest = self.hash(&file);
379             self.sign(&file);
380             assert!(self.digests.insert(filename.to_string(), digest).is_none());
381         }
382     }
383
384     fn build_manifest(&mut self) -> Manifest {
385         let mut manifest = Manifest {
386             manifest_version: "2".to_string(),
387             date: self.date.to_string(),
388             pkg: BTreeMap::new(),
389             renames: BTreeMap::new(),
390             profiles: BTreeMap::new(),
391         };
392         self.add_packages_to(&mut manifest);
393         self.add_profiles_to(&mut manifest);
394         self.add_renames_to(&mut manifest);
395         manifest.pkg.insert("rust".to_string(), self.rust_package(&manifest));
396         manifest
397     }
398
399     fn add_packages_to(&mut self, manifest: &mut Manifest) {
400         let mut package = |name, targets| self.package(name, &mut manifest.pkg, targets);
401         package("rustc", HOSTS);
402         package("rustc-dev", HOSTS);
403         package("cargo", HOSTS);
404         package("rust-mingw", MINGW);
405         package("rust-std", TARGETS);
406         package("rust-docs", DOCS_TARGETS);
407         package("rust-src", &["*"]);
408         package("rls-preview", HOSTS);
409         package("clippy-preview", HOSTS);
410         package("miri-preview", HOSTS);
411         package("rustfmt-preview", HOSTS);
412         package("rust-analysis", TARGETS);
413         package("llvm-tools-preview", TARGETS);
414         package("lldb-preview", TARGETS);
415     }
416
417     fn add_profiles_to(&mut self, manifest: &mut Manifest) {
418         let mut profile = |name, pkgs| self.profile(name, &mut manifest.profiles, pkgs);
419         profile("minimal", &["rustc", "cargo", "rust-std", "rust-mingw"]);
420         profile("default", &[
421             "rustc", "cargo", "rust-std", "rust-mingw",
422             "rust-docs", "rustfmt-preview", "clippy-preview"
423         ]);
424         profile("complete", &[
425             "rustc", "cargo", "rust-std", "rust-mingw",
426             "rust-docs", "rustfmt-preview", "clippy-preview",
427             "rls-preview", "rust-src", "llvm-tools-preview",
428             "lldb-preview", "rust-analysis", "miri-preview"
429         ]);
430     }
431
432     fn add_renames_to(&self, manifest: &mut Manifest) {
433         let mut rename = |from: &str, to: &str| manifest.renames.insert(
434             from.to_owned(),
435             Rename { to: to.to_owned() }
436         );
437         rename("rls", "rls-preview");
438         rename("rustfmt", "rustfmt-preview");
439         rename("clippy", "clippy-preview");
440         rename("miri", "miri-preview");
441     }
442
443     fn rust_package(&mut self, manifest: &Manifest) -> Package {
444         let mut pkg = Package {
445             version: self.cached_version("rust")
446                          .as_ref()
447                          .expect("Couldn't find Rust version")
448                          .clone(),
449             git_commit_hash: self.cached_git_commit_hash("rust").clone(),
450             target: BTreeMap::new(),
451         };
452         for host in HOSTS {
453             if let Some(target) = self.target_host_combination(host, &manifest) {
454                 pkg.target.insert(host.to_string(), target);
455             } else {
456                 pkg.target.insert(host.to_string(), Target::unavailable());
457                 continue
458             }
459         }
460         pkg
461     }
462
463     fn target_host_combination(&mut self, host: &str, manifest: &Manifest) -> Option<Target> {
464         let filename = self.filename("rust", host);
465         let digest = self.digests.remove(&filename)?;
466         let xz_filename = filename.replace(".tar.gz", ".tar.xz");
467         let xz_digest = self.digests.remove(&xz_filename);
468         let mut components = Vec::new();
469         let mut extensions = Vec::new();
470
471         let host_component = |pkg| Component::from_str(pkg, host);
472
473         // rustc/rust-std/cargo/docs are all required,
474         // and so is rust-mingw if it's available for the target.
475         components.extend(vec![
476             host_component("rustc"),
477             host_component("rust-std"),
478             host_component("cargo"),
479             host_component("rust-docs"),
480         ]);
481         if host.contains("pc-windows-gnu") {
482             components.push(host_component("rust-mingw"));
483         }
484
485         // The compiler libraries are not stable for end users, but `rustc-dev` was only recently
486         // split out of `rust-std`. We'll include it by default as a transition for nightly users,
487         // but ship it as an optional component on the beta and stable channels.
488         if self.rust_release == "nightly" {
489             components.push(host_component("rustc-dev"));
490         } else {
491             extensions.push(host_component("rustc-dev"));
492         }
493
494         // Tools are always present in the manifest,
495         // but might be marked as unavailable if they weren't built.
496         extensions.extend(vec![
497             host_component("clippy-preview"),
498             host_component("miri-preview"),
499             host_component("rls-preview"),
500             host_component("rustfmt-preview"),
501             host_component("llvm-tools-preview"),
502             host_component("lldb-preview"),
503             host_component("rust-analysis"),
504         ]);
505
506         extensions.extend(
507             TARGETS.iter()
508                 .filter(|&&target| target != host)
509                 .map(|target| Component::from_str("rust-std", target))
510         );
511         extensions.extend(
512             HOSTS.iter()
513                 .filter(|&&target| target != host)
514                 .map(|target| Component::from_str("rustc-dev", target))
515         );
516         extensions.push(Component::from_str("rust-src", "*"));
517
518         // If the components/extensions don't actually exist for this
519         // particular host/target combination then nix it entirely from our
520         // lists.
521         let has_component = |c: &Component| {
522             if c.target == "*" {
523                 return true
524             }
525             let pkg = match manifest.pkg.get(&c.pkg) {
526                 Some(p) => p,
527                 None => return false,
528             };
529             pkg.target.get(&c.target).is_some()
530         };
531         extensions.retain(&has_component);
532         components.retain(&has_component);
533
534         Some(Target {
535             available: true,
536             url: Some(self.url(&filename)),
537             hash: Some(digest),
538             xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
539             xz_hash: xz_digest,
540             components: Some(components),
541             extensions: Some(extensions),
542         })
543     }
544
545     fn profile(&mut self,
546                profile_name: &str,
547                dst: &mut BTreeMap<String, Vec<String>>,
548                pkgs: &[&str]) {
549         dst.insert(profile_name.to_owned(), pkgs.iter().map(|s| (*s).to_owned()).collect());
550     }
551
552     fn package(&mut self,
553                pkgname: &str,
554                dst: &mut BTreeMap<String, Package>,
555                targets: &[&str]) {
556         let (version, mut is_present) = self.cached_version(pkgname)
557             .as_ref()
558             .cloned()
559             .map(|version| (version, true))
560             .unwrap_or_default(); // `is_present` defaults to `false` here.
561
562         // Miri is nightly-only; never ship it for other trains.
563         if pkgname == "miri-preview" && self.rust_release != "nightly" {
564             is_present = false; // Pretend the component is entirely missing.
565         }
566
567         let targets = targets.iter().map(|name| {
568             if is_present {
569                 // The component generally exists, but it might still be missing for this target.
570                 let filename = self.filename(pkgname, name);
571                 let digest = match self.digests.remove(&filename) {
572                     Some(digest) => digest,
573                     // This component does not exist for this target -- skip it.
574                     None => return (name.to_string(), Target::unavailable()),
575                 };
576                 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
577                 let xz_digest = self.digests.remove(&xz_filename);
578
579                 (name.to_string(), Target {
580                     available: true,
581                     url: Some(self.url(&filename)),
582                     hash: Some(digest),
583                     xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
584                     xz_hash: xz_digest,
585                     components: None,
586                     extensions: None,
587                 })
588             } else {
589                 // If the component is not present for this build add it anyway but mark it as
590                 // unavailable -- this way rustup won't allow upgrades without --force
591                 (name.to_string(), Target::unavailable())
592             }
593         }).collect();
594
595         dst.insert(pkgname.to_string(), Package {
596             version,
597             git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
598             target: targets,
599         });
600     }
601
602     fn url(&self, filename: &str) -> String {
603         format!("{}/{}/{}",
604                 self.s3_address,
605                 self.date,
606                 filename)
607     }
608
609     fn filename(&self, component: &str, target: &str) -> String {
610         use PkgType::*;
611         match PkgType::from_component(component) {
612             RustSrc => format!("rust-src-{}.tar.gz", self.rust_release),
613             Cargo => format!("cargo-{}-{}.tar.gz", self.cargo_release, target),
614             Rls => format!("rls-{}-{}.tar.gz", self.rls_release, target),
615             Clippy => format!("clippy-{}-{}.tar.gz", self.clippy_release, target),
616             Rustfmt => format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target),
617             LlvmTools => format!("llvm-tools-{}-{}.tar.gz", self.llvm_tools_release, target),
618             Lldb => format!("lldb-{}-{}.tar.gz", self.lldb_release, target),
619             Miri => format!("miri-{}-{}.tar.gz", self.miri_release, target),
620             Other => format!("{}-{}-{}.tar.gz", component, self.rust_release, target),
621         }
622     }
623
624     fn cached_version(&self, component: &str) -> &Option<String> {
625         use PkgType::*;
626         match PkgType::from_component(component) {
627             Cargo => &self.cargo_version,
628             Rls => &self.rls_version,
629             Clippy => &self.clippy_version,
630             Rustfmt => &self.rustfmt_version,
631             LlvmTools => &self.llvm_tools_version,
632             Lldb => &self.lldb_version,
633             Miri => &self.miri_version,
634             _ => &self.rust_version,
635         }
636     }
637
638     fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
639         use PkgType::*;
640         match PkgType::from_component(component) {
641             Cargo => &self.cargo_git_commit_hash,
642             Rls => &self.rls_git_commit_hash,
643             Clippy => &self.clippy_git_commit_hash,
644             Rustfmt => &self.rustfmt_git_commit_hash,
645             LlvmTools => &self.llvm_tools_git_commit_hash,
646             Lldb => &self.lldb_git_commit_hash,
647             Miri => &self.miri_git_commit_hash,
648             _ => &self.rust_git_commit_hash,
649         }
650     }
651
652     fn version(&self, component: &str, target: &str) -> Option<String> {
653         self.untar(component, target, |filename| format!("{}/version", filename))
654     }
655
656     fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
657         self.untar(component, target, |filename| format!("{}/git-commit-hash", filename))
658     }
659
660     fn untar<F>(&self, component: &str, target: &str, dir: F) -> Option<String>
661     where
662         F: FnOnce(String) -> String
663     {
664         let mut cmd = Command::new("tar");
665         let filename = self.filename(component, target);
666         cmd.arg("xf")
667            .arg(self.input.join(&filename))
668            .arg(dir(filename.replace(".tar.gz", "")))
669            .arg("-O");
670         let output = t!(cmd.output());
671         if output.status.success() {
672             Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
673         } else {
674             None
675         }
676     }
677
678     fn hash(&self, path: &Path) -> String {
679         let sha = t!(Command::new("shasum")
680                         .arg("-a").arg("256")
681                         .arg(path.file_name().unwrap())
682                         .current_dir(path.parent().unwrap())
683                         .output());
684         assert!(sha.status.success());
685
686         let filename = path.file_name().unwrap().to_str().unwrap();
687         let sha256 = self.output.join(format!("{}.sha256", filename));
688         t!(fs::write(&sha256, &sha.stdout));
689
690         let stdout = String::from_utf8_lossy(&sha.stdout);
691         stdout.split_whitespace().next().unwrap().to_string()
692     }
693
694     fn sign(&self, path: &Path) {
695         if !self.should_sign {
696             return;
697         }
698
699         let filename = path.file_name().unwrap().to_str().unwrap();
700         let asc = self.output.join(format!("{}.asc", filename));
701         println!("signing: {:?}", path);
702         let mut cmd = Command::new("gpg");
703         cmd.arg("--pinentry-mode=loopback")
704             .arg("--no-tty")
705             .arg("--yes")
706             .arg("--batch")
707             .arg("--passphrase-fd").arg("0")
708             .arg("--personal-digest-preferences").arg("SHA512")
709             .arg("--armor")
710             .arg("--output").arg(&asc)
711             .arg("--detach-sign").arg(path)
712             .stdin(Stdio::piped());
713         let mut child = t!(cmd.spawn());
714         t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
715         assert!(t!(child.wait()).success());
716     }
717
718     fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
719         self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
720         self.write(&manifest.date, channel_name, "-date.txt");
721         self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
722                    channel_name, "-git-commit-hash.txt");
723     }
724
725     fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
726         let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
727         t!(fs::write(&dst, contents));
728         self.hash(&dst);
729         self.sign(&dst);
730     }
731 }