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