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.
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.
13 extern crate serde_derive;
15 use std::collections::BTreeMap;
18 use std::io::{self, Read, Write};
19 use std::path::{PathBuf, Path};
20 use std::process::{Command, Stdio};
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",
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",
47 static TARGETS: &'static [&'static str] = &[
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",
62 "armv7-linux-androideabi",
63 "armv7-unknown-cloudabi-eabihf",
64 "armv7-unknown-linux-gnueabihf",
65 "armv7-unknown-linux-musleabihf",
66 "armebv7r-none-eabihf",
68 "asmjs-unknown-emscripten",
70 "i586-pc-windows-msvc",
71 "i586-unknown-linux-gnu",
72 "i586-unknown-linux-musl",
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 "riscv32imac-unknown-none-elf",
93 "s390x-unknown-linux-gnu",
94 "sparc-unknown-linux-gnu",
95 "sparc64-unknown-linux-gnu",
96 "sparcv9-sun-solaris",
98 "thumbv7em-none-eabi",
99 "thumbv7em-none-eabihf",
100 "thumbv7m-none-eabi",
101 "wasm32-unknown-emscripten",
102 "wasm32-unknown-unknown",
103 "x86_64-apple-darwin",
106 "x86_64-linux-android",
107 "x86_64-pc-windows-gnu",
108 "x86_64-pc-windows-msvc",
109 "x86_64-rumprun-netbsd",
110 "x86_64-sun-solaris",
111 "x86_64-unknown-cloudabi",
112 "x86_64-unknown-freebsd",
113 "x86_64-unknown-hermit",
114 "x86_64-unknown-linux-gnu",
115 "x86_64-unknown-linux-gnux32",
116 "x86_64-unknown-linux-musl",
117 "x86_64-unknown-netbsd",
118 "x86_64-unknown-redox",
121 static MINGW: &'static [&'static str] = &[
122 "i686-pc-windows-gnu",
123 "x86_64-pc-windows-gnu",
127 #[serde(rename_all = "kebab-case")]
129 manifest_version: String,
131 pkg: BTreeMap<String, Package>,
132 renames: BTreeMap<String, Rename>
138 git_commit_hash: Option<String>,
139 target: BTreeMap<String, Target>,
151 hash: Option<String>,
152 xz_url: Option<String>,
153 xz_hash: Option<String>,
154 components: Option<Vec<Component>>,
155 extensions: Option<Vec<Component>>,
159 fn unavailable() -> Target {
179 ($e:expr) => (match $e {
181 Err(e) => panic!("{} failed with {}", stringify!($e), e),
186 rust_release: String,
187 cargo_release: String,
189 clippy_release: String,
190 rustfmt_release: String,
191 llvm_tools_release: String,
195 gpg_passphrase: String,
196 digests: BTreeMap<String, String>,
200 rust_version: Option<String>,
201 cargo_version: Option<String>,
202 rls_version: Option<String>,
203 clippy_version: Option<String>,
204 rustfmt_version: Option<String>,
205 llvm_tools_version: Option<String>,
207 rust_git_commit_hash: Option<String>,
208 cargo_git_commit_hash: Option<String>,
209 rls_git_commit_hash: Option<String>,
210 clippy_git_commit_hash: Option<String>,
211 rustfmt_git_commit_hash: Option<String>,
212 llvm_tools_git_commit_hash: Option<String>,
216 let mut args = env::args().skip(1);
217 let input = PathBuf::from(args.next().unwrap());
218 let output = PathBuf::from(args.next().unwrap());
219 let date = args.next().unwrap();
220 let rust_release = args.next().unwrap();
221 let cargo_release = args.next().unwrap();
222 let rls_release = args.next().unwrap();
223 let clippy_release = args.next().unwrap();
224 let rustfmt_release = args.next().unwrap();
225 let llvm_tools_release = args.next().unwrap();
226 let s3_address = args.next().unwrap();
227 let mut passphrase = String::new();
228 t!(io::stdin().read_to_string(&mut passphrase));
240 gpg_passphrase: passphrase,
241 digests: BTreeMap::new(),
248 clippy_version: None,
249 rustfmt_version: None,
250 llvm_tools_version: None,
252 rust_git_commit_hash: None,
253 cargo_git_commit_hash: None,
254 rls_git_commit_hash: None,
255 clippy_git_commit_hash: None,
256 rustfmt_git_commit_hash: None,
257 llvm_tools_git_commit_hash: None,
262 fn build(&mut self) {
263 self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
264 self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
265 self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
266 self.clippy_version = self.version("clippy", "x86_64-unknown-linux-gnu");
267 self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
268 self.llvm_tools_version = self.version("llvm-tools", "x86_64-unknown-linux-gnu");
270 self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
271 self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
272 self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
273 self.clippy_git_commit_hash = self.git_commit_hash("clippy", "x86_64-unknown-linux-gnu");
274 self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
275 self.llvm_tools_git_commit_hash = self.git_commit_hash("llvm-tools",
276 "x86_64-unknown-linux-gnu");
278 self.digest_and_sign();
279 let manifest = self.build_manifest();
280 self.write_channel_files(&self.rust_release, &manifest);
282 if self.rust_release != "beta" && self.rust_release != "nightly" {
283 self.write_channel_files("stable", &manifest);
287 fn digest_and_sign(&mut self) {
288 for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
289 let filename = file.file_name().unwrap().to_str().unwrap();
290 let digest = self.hash(&file);
292 assert!(self.digests.insert(filename.to_string(), digest).is_none());
296 fn build_manifest(&mut self) -> Manifest {
297 let mut manifest = Manifest {
298 manifest_version: "2".to_string(),
299 date: self.date.to_string(),
300 pkg: BTreeMap::new(),
301 renames: BTreeMap::new(),
304 self.package("rustc", &mut manifest.pkg, HOSTS);
305 self.package("cargo", &mut manifest.pkg, HOSTS);
306 self.package("rust-mingw", &mut manifest.pkg, MINGW);
307 self.package("rust-std", &mut manifest.pkg, TARGETS);
308 self.package("rust-docs", &mut manifest.pkg, TARGETS);
309 self.package("rust-src", &mut manifest.pkg, &["*"]);
310 self.package("rls-preview", &mut manifest.pkg, HOSTS);
311 self.package("clippy-preview", &mut manifest.pkg, HOSTS);
312 self.package("rustfmt-preview", &mut manifest.pkg, HOSTS);
313 self.package("rust-analysis", &mut manifest.pkg, TARGETS);
314 self.package("llvm-tools-preview", &mut manifest.pkg, TARGETS);
316 let clippy_present = manifest.pkg.contains_key("clippy-preview");
317 let rls_present = manifest.pkg.contains_key("rls-preview");
318 let rustfmt_present = manifest.pkg.contains_key("rustfmt-preview");
319 let llvm_tools_present = manifest.pkg.contains_key("llvm-tools-preview");
322 manifest.renames.insert("rls".to_owned(), Rename { to: "rls-preview".to_owned() });
325 let mut pkg = Package {
326 version: self.cached_version("rust")
328 .expect("Couldn't find Rust version")
330 git_commit_hash: self.cached_git_commit_hash("rust").clone(),
331 target: BTreeMap::new(),
334 let filename = self.filename("rust", host);
335 let digest = match self.digests.remove(&filename) {
336 Some(digest) => digest,
338 pkg.target.insert(host.to_string(), Target::unavailable());
342 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
343 let xz_digest = self.digests.remove(&xz_filename);
344 let mut components = Vec::new();
345 let mut extensions = Vec::new();
347 // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
348 // if it's available for the target.
349 components.extend(vec![
350 Component { pkg: "rustc".to_string(), target: host.to_string() },
351 Component { pkg: "rust-std".to_string(), target: host.to_string() },
352 Component { pkg: "cargo".to_string(), target: host.to_string() },
353 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
355 if host.contains("pc-windows-gnu") {
356 components.push(Component {
357 pkg: "rust-mingw".to_string(),
358 target: host.to_string(),
363 extensions.push(Component {
364 pkg: "clippy-preview".to_string(),
365 target: host.to_string(),
369 extensions.push(Component {
370 pkg: "rls-preview".to_string(),
371 target: host.to_string(),
375 extensions.push(Component {
376 pkg: "rustfmt-preview".to_string(),
377 target: host.to_string(),
380 if llvm_tools_present {
381 extensions.push(Component {
382 pkg: "llvm-tools-preview".to_string(),
383 target: host.to_string(),
386 extensions.push(Component {
387 pkg: "rust-analysis".to_string(),
388 target: host.to_string(),
390 for target in TARGETS {
392 extensions.push(Component {
393 pkg: "rust-std".to_string(),
394 target: target.to_string(),
398 extensions.push(Component {
399 pkg: "rust-src".to_string(),
400 target: "*".to_string(),
403 // If the components/extensions don't actually exist for this
404 // particular host/target combination then nix it entirely from our
407 let has_component = |c: &Component| {
411 let pkg = match manifest.pkg.get(&c.pkg) {
413 None => return false,
415 let target = match pkg.target.get(&c.target) {
417 None => return false,
421 extensions.retain(&has_component);
422 components.retain(&has_component);
425 pkg.target.insert(host.to_string(), Target {
427 url: Some(self.url(&filename)),
429 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
431 components: Some(components),
432 extensions: Some(extensions),
435 manifest.pkg.insert("rust".to_string(), pkg);
440 fn package(&mut self,
442 dst: &mut BTreeMap<String, Package>,
444 let version = match *self.cached_version(pkgname) {
445 Some(ref version) => version.clone(),
447 println!("Skipping package {}", pkgname);
452 let targets = targets.iter().map(|name| {
453 let filename = self.filename(pkgname, name);
454 let digest = match self.digests.remove(&filename) {
455 Some(digest) => digest,
456 None => return (name.to_string(), Target::unavailable()),
458 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
459 let xz_digest = self.digests.remove(&xz_filename);
461 (name.to_string(), Target {
463 url: Some(self.url(&filename)),
465 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
472 dst.insert(pkgname.to_string(), Package {
474 git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
479 fn url(&self, filename: &str) -> String {
486 fn filename(&self, component: &str, target: &str) -> String {
487 if component == "rust-src" {
488 format!("rust-src-{}.tar.gz", self.rust_release)
489 } else if component == "cargo" {
490 format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
491 } else if component == "rls" || component == "rls-preview" {
492 format!("rls-{}-{}.tar.gz", self.rls_release, target)
493 } else if component == "clippy" || component == "clippy-preview" {
494 format!("clippy-{}-{}.tar.gz", self.clippy_release, target)
495 } else if component == "rustfmt" || component == "rustfmt-preview" {
496 format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target)
497 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
498 format!("llvm-tools-{}-{}.tar.gz", self.llvm_tools_release, target)
500 format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
504 fn cached_version(&self, component: &str) -> &Option<String> {
505 if component == "cargo" {
507 } else if component == "rls" || component == "rls-preview" {
509 } else if component == "clippy" || component == "clippy-preview" {
511 } else if component == "rustfmt" || component == "rustfmt-preview" {
512 &self.rustfmt_version
513 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
514 &self.llvm_tools_version
520 fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
521 if component == "cargo" {
522 &self.cargo_git_commit_hash
523 } else if component == "rls" || component == "rls-preview" {
524 &self.rls_git_commit_hash
525 } else if component == "clippy" || component == "clippy-preview" {
526 &self.clippy_git_commit_hash
527 } else if component == "rustfmt" || component == "rustfmt-preview" {
528 &self.rustfmt_git_commit_hash
529 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
530 &self.llvm_tools_git_commit_hash
532 &self.rust_git_commit_hash
536 fn version(&self, component: &str, target: &str) -> Option<String> {
537 let mut cmd = Command::new("tar");
538 let filename = self.filename(component, target);
540 .arg(self.input.join(&filename))
541 .arg(format!("{}/version", filename.replace(".tar.gz", "")))
543 let output = t!(cmd.output());
544 if output.status.success() {
545 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
547 // Perhaps we didn't build this package.
552 fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
553 let mut cmd = Command::new("tar");
554 let filename = self.filename(component, target);
556 .arg(self.input.join(&filename))
557 .arg(format!("{}/git-commit-hash", filename.replace(".tar.gz", "")))
559 let output = t!(cmd.output());
560 if output.status.success() {
561 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
567 fn hash(&self, path: &Path) -> String {
568 let sha = t!(Command::new("shasum")
569 .arg("-a").arg("256")
570 .arg(path.file_name().unwrap())
571 .current_dir(path.parent().unwrap())
573 assert!(sha.status.success());
575 let filename = path.file_name().unwrap().to_str().unwrap();
576 let sha256 = self.output.join(format!("{}.sha256", filename));
577 t!(t!(File::create(&sha256)).write_all(&sha.stdout));
579 let stdout = String::from_utf8_lossy(&sha.stdout);
580 stdout.split_whitespace().next().unwrap().to_string()
583 fn sign(&self, path: &Path) {
584 let filename = path.file_name().unwrap().to_str().unwrap();
585 let asc = self.output.join(format!("{}.asc", filename));
586 println!("signing: {:?}", path);
587 let mut cmd = Command::new("gpg");
590 .arg("--passphrase-fd").arg("0")
591 .arg("--personal-digest-preferences").arg("SHA512")
593 .arg("--output").arg(&asc)
594 .arg("--detach-sign").arg(path)
595 .stdin(Stdio::piped());
596 let mut child = t!(cmd.spawn());
597 t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
598 assert!(t!(child.wait()).success());
601 fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
602 self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
603 self.write(&manifest.date, channel_name, "-date.txt");
604 self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
605 channel_name, "-git-commit-hash.txt");
608 fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
609 let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
610 t!(t!(File::create(&dst)).write_all(contents.as_bytes()));