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 "s390x-unknown-linux-gnu",
93 "sparc-unknown-linux-gnu",
94 "sparc64-unknown-linux-gnu",
95 "sparcv9-sun-solaris",
97 "thumbv7em-none-eabi",
98 "thumbv7em-none-eabihf",
100 "wasm32-unknown-emscripten",
101 "wasm32-unknown-unknown",
102 "x86_64-apple-darwin",
105 "x86_64-linux-android",
106 "x86_64-pc-windows-gnu",
107 "x86_64-pc-windows-msvc",
108 "x86_64-rumprun-netbsd",
109 "x86_64-sun-solaris",
110 "x86_64-unknown-cloudabi",
111 "x86_64-unknown-freebsd",
112 "x86_64-unknown-hermit",
113 "x86_64-unknown-linux-gnu",
114 "x86_64-unknown-linux-gnux32",
115 "x86_64-unknown-linux-musl",
116 "x86_64-unknown-netbsd",
117 "x86_64-unknown-redox",
120 static MINGW: &'static [&'static str] = &[
121 "i686-pc-windows-gnu",
122 "x86_64-pc-windows-gnu",
126 #[serde(rename_all = "kebab-case")]
128 manifest_version: String,
130 pkg: BTreeMap<String, Package>,
131 renames: BTreeMap<String, Rename>
137 git_commit_hash: Option<String>,
138 target: BTreeMap<String, Target>,
150 hash: Option<String>,
151 xz_url: Option<String>,
152 xz_hash: Option<String>,
153 components: Option<Vec<Component>>,
154 extensions: Option<Vec<Component>>,
158 fn unavailable() -> Target {
178 ($e:expr) => (match $e {
180 Err(e) => panic!("{} failed with {}", stringify!($e), e),
185 rust_release: String,
186 cargo_release: String,
188 clippy_release: String,
189 rustfmt_release: String,
190 llvm_tools_release: String,
194 gpg_passphrase: String,
195 digests: BTreeMap<String, String>,
199 rust_version: Option<String>,
200 cargo_version: Option<String>,
201 rls_version: Option<String>,
202 clippy_version: Option<String>,
203 rustfmt_version: Option<String>,
204 llvm_tools_version: Option<String>,
206 rust_git_commit_hash: Option<String>,
207 cargo_git_commit_hash: Option<String>,
208 rls_git_commit_hash: Option<String>,
209 clippy_git_commit_hash: Option<String>,
210 rustfmt_git_commit_hash: Option<String>,
211 llvm_tools_git_commit_hash: Option<String>,
215 let mut args = env::args().skip(1);
216 let input = PathBuf::from(args.next().unwrap());
217 let output = PathBuf::from(args.next().unwrap());
218 let date = args.next().unwrap();
219 let rust_release = args.next().unwrap();
220 let cargo_release = args.next().unwrap();
221 let rls_release = args.next().unwrap();
222 let clippy_release = args.next().unwrap();
223 let rustfmt_release = args.next().unwrap();
224 let llvm_tools_release = args.next().unwrap();
225 let s3_address = args.next().unwrap();
226 let mut passphrase = String::new();
227 t!(io::stdin().read_to_string(&mut passphrase));
239 gpg_passphrase: passphrase,
240 digests: BTreeMap::new(),
247 clippy_version: None,
248 rustfmt_version: None,
249 llvm_tools_version: None,
251 rust_git_commit_hash: None,
252 cargo_git_commit_hash: None,
253 rls_git_commit_hash: None,
254 clippy_git_commit_hash: None,
255 rustfmt_git_commit_hash: None,
256 llvm_tools_git_commit_hash: None,
261 fn build(&mut self) {
262 self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
263 self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
264 self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
265 self.clippy_version = self.version("clippy", "x86_64-unknown-linux-gnu");
266 self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
267 self.llvm_tools_version = self.version("llvm-tools", "x86_64-unknown-linux-gnu");
269 self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
270 self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
271 self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
272 self.clippy_git_commit_hash = self.git_commit_hash("clippy", "x86_64-unknown-linux-gnu");
273 self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
274 self.llvm_tools_git_commit_hash = self.git_commit_hash("llvm-tools",
275 "x86_64-unknown-linux-gnu");
277 self.digest_and_sign();
278 let manifest = self.build_manifest();
279 self.write_channel_files(&self.rust_release, &manifest);
281 if self.rust_release != "beta" && self.rust_release != "nightly" {
282 self.write_channel_files("stable", &manifest);
286 fn digest_and_sign(&mut self) {
287 for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
288 let filename = file.file_name().unwrap().to_str().unwrap();
289 let digest = self.hash(&file);
291 assert!(self.digests.insert(filename.to_string(), digest).is_none());
295 fn build_manifest(&mut self) -> Manifest {
296 let mut manifest = Manifest {
297 manifest_version: "2".to_string(),
298 date: self.date.to_string(),
299 pkg: BTreeMap::new(),
300 renames: BTreeMap::new(),
303 self.package("rustc", &mut manifest.pkg, HOSTS);
304 self.package("cargo", &mut manifest.pkg, HOSTS);
305 self.package("rust-mingw", &mut manifest.pkg, MINGW);
306 self.package("rust-std", &mut manifest.pkg, TARGETS);
307 self.package("rust-docs", &mut manifest.pkg, TARGETS);
308 self.package("rust-src", &mut manifest.pkg, &["*"]);
309 self.package("rls-preview", &mut manifest.pkg, HOSTS);
310 self.package("clippy-preview", &mut manifest.pkg, HOSTS);
311 self.package("rustfmt-preview", &mut manifest.pkg, HOSTS);
312 self.package("rust-analysis", &mut manifest.pkg, TARGETS);
313 self.package("llvm-tools-preview", &mut manifest.pkg, TARGETS);
315 let clippy_present = manifest.pkg.contains_key("clippy-preview");
316 let rls_present = manifest.pkg.contains_key("rls-preview");
317 let rustfmt_present = manifest.pkg.contains_key("rustfmt-preview");
318 let llvm_tools_present = manifest.pkg.contains_key("llvm-tools-preview");
321 manifest.renames.insert("rls".to_owned(), Rename { to: "rls-preview".to_owned() });
324 let mut pkg = Package {
325 version: self.cached_version("rust")
327 .expect("Couldn't find Rust version")
329 git_commit_hash: self.cached_git_commit_hash("rust").clone(),
330 target: BTreeMap::new(),
333 let filename = self.filename("rust", host);
334 let digest = match self.digests.remove(&filename) {
335 Some(digest) => digest,
337 pkg.target.insert(host.to_string(), Target::unavailable());
341 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
342 let xz_digest = self.digests.remove(&xz_filename);
343 let mut components = Vec::new();
344 let mut extensions = Vec::new();
346 // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
347 // if it's available for the target.
348 components.extend(vec![
349 Component { pkg: "rustc".to_string(), target: host.to_string() },
350 Component { pkg: "rust-std".to_string(), target: host.to_string() },
351 Component { pkg: "cargo".to_string(), target: host.to_string() },
352 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
354 if host.contains("pc-windows-gnu") {
355 components.push(Component {
356 pkg: "rust-mingw".to_string(),
357 target: host.to_string(),
362 extensions.push(Component {
363 pkg: "clippy-preview".to_string(),
364 target: host.to_string(),
368 extensions.push(Component {
369 pkg: "rls-preview".to_string(),
370 target: host.to_string(),
374 extensions.push(Component {
375 pkg: "rustfmt-preview".to_string(),
376 target: host.to_string(),
379 if llvm_tools_present {
380 extensions.push(Component {
381 pkg: "llvm-tools-preview".to_string(),
382 target: host.to_string(),
385 extensions.push(Component {
386 pkg: "rust-analysis".to_string(),
387 target: host.to_string(),
389 for target in TARGETS {
391 extensions.push(Component {
392 pkg: "rust-std".to_string(),
393 target: target.to_string(),
397 extensions.push(Component {
398 pkg: "rust-src".to_string(),
399 target: "*".to_string(),
402 // If the components/extensions don't actually exist for this
403 // particular host/target combination then nix it entirely from our
406 let has_component = |c: &Component| {
410 let pkg = match manifest.pkg.get(&c.pkg) {
412 None => return false,
414 let target = match pkg.target.get(&c.target) {
416 None => return false,
420 extensions.retain(&has_component);
421 components.retain(&has_component);
424 pkg.target.insert(host.to_string(), Target {
426 url: Some(self.url(&filename)),
428 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
430 components: Some(components),
431 extensions: Some(extensions),
434 manifest.pkg.insert("rust".to_string(), pkg);
439 fn package(&mut self,
441 dst: &mut BTreeMap<String, Package>,
443 let version = match *self.cached_version(pkgname) {
444 Some(ref version) => version.clone(),
446 println!("Skipping package {}", pkgname);
451 let targets = targets.iter().map(|name| {
452 let filename = self.filename(pkgname, name);
453 let digest = match self.digests.remove(&filename) {
454 Some(digest) => digest,
455 None => return (name.to_string(), Target::unavailable()),
457 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
458 let xz_digest = self.digests.remove(&xz_filename);
460 (name.to_string(), Target {
462 url: Some(self.url(&filename)),
464 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
471 dst.insert(pkgname.to_string(), Package {
473 git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
478 fn url(&self, filename: &str) -> String {
485 fn filename(&self, component: &str, target: &str) -> String {
486 if component == "rust-src" {
487 format!("rust-src-{}.tar.gz", self.rust_release)
488 } else if component == "cargo" {
489 format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
490 } else if component == "rls" || component == "rls-preview" {
491 format!("rls-{}-{}.tar.gz", self.rls_release, target)
492 } else if component == "clippy" || component == "clippy-preview" {
493 format!("clippy-{}-{}.tar.gz", self.clippy_release, target)
494 } else if component == "rustfmt" || component == "rustfmt-preview" {
495 format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target)
496 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
497 format!("llvm-tools-{}-{}.tar.gz", self.llvm_tools_release, target)
499 format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
503 fn cached_version(&self, component: &str) -> &Option<String> {
504 if component == "cargo" {
506 } else if component == "rls" || component == "rls-preview" {
508 } else if component == "clippy" || component == "clippy-preview" {
510 } else if component == "rustfmt" || component == "rustfmt-preview" {
511 &self.rustfmt_version
512 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
513 &self.llvm_tools_version
519 fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
520 if component == "cargo" {
521 &self.cargo_git_commit_hash
522 } else if component == "rls" || component == "rls-preview" {
523 &self.rls_git_commit_hash
524 } else if component == "clippy" || component == "clippy-preview" {
525 &self.clippy_git_commit_hash
526 } else if component == "rustfmt" || component == "rustfmt-preview" {
527 &self.rustfmt_git_commit_hash
528 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
529 &self.llvm_tools_git_commit_hash
531 &self.rust_git_commit_hash
535 fn version(&self, component: &str, target: &str) -> Option<String> {
536 let mut cmd = Command::new("tar");
537 let filename = self.filename(component, target);
539 .arg(self.input.join(&filename))
540 .arg(format!("{}/version", filename.replace(".tar.gz", "")))
542 let output = t!(cmd.output());
543 if output.status.success() {
544 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
546 // Perhaps we didn't build this package.
551 fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
552 let mut cmd = Command::new("tar");
553 let filename = self.filename(component, target);
555 .arg(self.input.join(&filename))
556 .arg(format!("{}/git-commit-hash", filename.replace(".tar.gz", "")))
558 let output = t!(cmd.output());
559 if output.status.success() {
560 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
566 fn hash(&self, path: &Path) -> String {
567 let sha = t!(Command::new("shasum")
568 .arg("-a").arg("256")
569 .arg(path.file_name().unwrap())
570 .current_dir(path.parent().unwrap())
572 assert!(sha.status.success());
574 let filename = path.file_name().unwrap().to_str().unwrap();
575 let sha256 = self.output.join(format!("{}.sha256", filename));
576 t!(t!(File::create(&sha256)).write_all(&sha.stdout));
578 let stdout = String::from_utf8_lossy(&sha.stdout);
579 stdout.split_whitespace().next().unwrap().to_string()
582 fn sign(&self, path: &Path) {
583 let filename = path.file_name().unwrap().to_str().unwrap();
584 let asc = self.output.join(format!("{}.asc", filename));
585 println!("signing: {:?}", path);
586 let mut cmd = Command::new("gpg");
589 .arg("--passphrase-fd").arg("0")
590 .arg("--personal-digest-preferences").arg("SHA512")
592 .arg("--output").arg(&asc)
593 .arg("--detach-sign").arg(path)
594 .stdin(Stdio::piped());
595 let mut child = t!(cmd.spawn());
596 t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
597 assert!(t!(child.wait()).success());
600 fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
601 self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
602 self.write(&manifest.date, channel_name, "-date.txt");
603 self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
604 channel_name, "-git-commit-hash.txt");
607 fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
608 let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
609 t!(t!(File::create(&dst)).write_all(contents.as_bytes()));