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] = &[
49 "aarch64-linux-android",
50 "aarch64-unknown-cloudabi",
51 "aarch64-unknown-fuchsia",
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 "s390x-unknown-linux-gnu",
92 "sparc-unknown-linux-gnu",
93 "sparc64-unknown-linux-gnu",
94 "sparcv9-sun-solaris",
96 "thumbv7em-none-eabi",
97 "thumbv7em-none-eabihf",
99 "wasm32-unknown-emscripten",
100 "wasm32-unknown-unknown",
101 "x86_64-apple-darwin",
103 "x86_64-linux-android",
104 "x86_64-pc-windows-gnu",
105 "x86_64-pc-windows-msvc",
106 "x86_64-rumprun-netbsd",
107 "x86_64-sun-solaris",
108 "x86_64-unknown-cloudabi",
109 "x86_64-unknown-freebsd",
110 "x86_64-unknown-fuchsia",
111 "x86_64-unknown-linux-gnu",
112 "x86_64-unknown-linux-gnux32",
113 "x86_64-unknown-linux-musl",
114 "x86_64-unknown-netbsd",
115 "x86_64-unknown-redox",
118 static MINGW: &'static [&'static str] = &[
119 "i686-pc-windows-gnu",
120 "x86_64-pc-windows-gnu",
124 #[serde(rename_all = "kebab-case")]
126 manifest_version: String,
128 pkg: BTreeMap<String, Package>,
129 renames: BTreeMap<String, Rename>
135 git_commit_hash: Option<String>,
136 target: BTreeMap<String, Target>,
148 hash: Option<String>,
149 xz_url: Option<String>,
150 xz_hash: Option<String>,
151 components: Option<Vec<Component>>,
152 extensions: Option<Vec<Component>>,
156 fn unavailable() -> Target {
176 ($e:expr) => (match $e {
178 Err(e) => panic!("{} failed with {}", stringify!($e), e),
183 rust_release: String,
184 cargo_release: String,
186 rustfmt_release: String,
190 gpg_passphrase: String,
191 digests: BTreeMap<String, String>,
195 rust_version: Option<String>,
196 cargo_version: Option<String>,
197 rls_version: Option<String>,
198 rustfmt_version: Option<String>,
200 rust_git_commit_hash: Option<String>,
201 cargo_git_commit_hash: Option<String>,
202 rls_git_commit_hash: Option<String>,
203 rustfmt_git_commit_hash: Option<String>,
207 let mut args = env::args().skip(1);
208 let input = PathBuf::from(args.next().unwrap());
209 let output = PathBuf::from(args.next().unwrap());
210 let date = args.next().unwrap();
211 let rust_release = args.next().unwrap();
212 let cargo_release = args.next().unwrap();
213 let rls_release = args.next().unwrap();
214 let rustfmt_release = args.next().unwrap();
215 let _llvm_tools_vers = args.next().unwrap(); // FIXME do something with it?
216 let s3_address = args.next().unwrap();
217 let mut passphrase = String::new();
218 t!(io::stdin().read_to_string(&mut passphrase));
228 gpg_passphrase: passphrase,
229 digests: BTreeMap::new(),
236 rustfmt_version: None,
238 rust_git_commit_hash: None,
239 cargo_git_commit_hash: None,
240 rls_git_commit_hash: None,
241 rustfmt_git_commit_hash: None,
246 fn build(&mut self) {
247 self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
248 self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
249 self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
250 self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
252 self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
253 self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
254 self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
255 self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
257 self.digest_and_sign();
258 let manifest = self.build_manifest();
259 self.write_channel_files(&self.rust_release, &manifest);
261 if self.rust_release != "beta" && self.rust_release != "nightly" {
262 self.write_channel_files("stable", &manifest);
266 fn digest_and_sign(&mut self) {
267 for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
268 let filename = file.file_name().unwrap().to_str().unwrap();
269 let digest = self.hash(&file);
271 assert!(self.digests.insert(filename.to_string(), digest).is_none());
275 fn build_manifest(&mut self) -> Manifest {
276 let mut manifest = Manifest {
277 manifest_version: "2".to_string(),
278 date: self.date.to_string(),
279 pkg: BTreeMap::new(),
280 renames: BTreeMap::new(),
283 self.package("rustc", &mut manifest.pkg, HOSTS);
284 self.package("cargo", &mut manifest.pkg, HOSTS);
285 self.package("rust-mingw", &mut manifest.pkg, MINGW);
286 self.package("rust-std", &mut manifest.pkg, TARGETS);
287 self.package("rust-docs", &mut manifest.pkg, TARGETS);
288 self.package("rust-src", &mut manifest.pkg, &["*"]);
289 self.package("rls-preview", &mut manifest.pkg, HOSTS);
290 self.package("rustfmt-preview", &mut manifest.pkg, HOSTS);
291 self.package("rust-analysis", &mut manifest.pkg, TARGETS);
293 let rls_present = manifest.pkg.contains_key("rls-preview");
294 let rustfmt_present = manifest.pkg.contains_key("rustfmt-preview");
297 manifest.renames.insert("rls".to_owned(), Rename { to: "rls-preview".to_owned() });
300 let mut pkg = Package {
301 version: self.cached_version("rust")
303 .expect("Couldn't find Rust version")
305 git_commit_hash: self.cached_git_commit_hash("rust").clone(),
306 target: BTreeMap::new(),
309 let filename = self.filename("rust", host);
310 let digest = match self.digests.remove(&filename) {
311 Some(digest) => digest,
313 pkg.target.insert(host.to_string(), Target::unavailable());
317 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
318 let xz_digest = self.digests.remove(&xz_filename);
319 let mut components = Vec::new();
320 let mut extensions = Vec::new();
322 // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
323 // if it's available for the target.
324 components.extend(vec![
325 Component { pkg: "rustc".to_string(), target: host.to_string() },
326 Component { pkg: "rust-std".to_string(), target: host.to_string() },
327 Component { pkg: "cargo".to_string(), target: host.to_string() },
328 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
330 if host.contains("pc-windows-gnu") {
331 components.push(Component {
332 pkg: "rust-mingw".to_string(),
333 target: host.to_string(),
338 extensions.push(Component {
339 pkg: "rls-preview".to_string(),
340 target: host.to_string(),
344 extensions.push(Component {
345 pkg: "rustfmt-preview".to_string(),
346 target: host.to_string(),
349 extensions.push(Component {
350 pkg: "rust-analysis".to_string(),
351 target: host.to_string(),
353 for target in TARGETS {
355 extensions.push(Component {
356 pkg: "rust-std".to_string(),
357 target: target.to_string(),
361 extensions.push(Component {
362 pkg: "rust-src".to_string(),
363 target: "*".to_string(),
366 // If the components/extensions don't actually exist for this
367 // particular host/target combination then nix it entirely from our
370 let has_component = |c: &Component| {
374 let pkg = match manifest.pkg.get(&c.pkg) {
376 None => return false,
378 let target = match pkg.target.get(&c.target) {
380 None => return false,
384 extensions.retain(&has_component);
385 components.retain(&has_component);
388 pkg.target.insert(host.to_string(), Target {
390 url: Some(self.url(&filename)),
392 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
394 components: Some(components),
395 extensions: Some(extensions),
398 manifest.pkg.insert("rust".to_string(), pkg);
403 fn package(&mut self,
405 dst: &mut BTreeMap<String, Package>,
407 let version = match *self.cached_version(pkgname) {
408 Some(ref version) => version.clone(),
410 println!("Skipping package {}", pkgname);
415 let targets = targets.iter().map(|name| {
416 let filename = self.filename(pkgname, name);
417 let digest = match self.digests.remove(&filename) {
418 Some(digest) => digest,
419 None => return (name.to_string(), Target::unavailable()),
421 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
422 let xz_digest = self.digests.remove(&xz_filename);
424 (name.to_string(), Target {
426 url: Some(self.url(&filename)),
428 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
435 dst.insert(pkgname.to_string(), Package {
437 git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
442 fn url(&self, filename: &str) -> String {
449 fn filename(&self, component: &str, target: &str) -> String {
450 if component == "rust-src" {
451 format!("rust-src-{}.tar.gz", self.rust_release)
452 } else if component == "cargo" {
453 format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
454 } else if component == "rls" || component == "rls-preview" {
455 format!("rls-{}-{}.tar.gz", self.rls_release, target)
456 } else if component == "rustfmt" || component == "rustfmt-preview" {
457 format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target)
459 format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
463 fn cached_version(&self, component: &str) -> &Option<String> {
464 if component == "cargo" {
466 } else if component == "rls" || component == "rls-preview" {
468 } else if component == "rustfmt" || component == "rustfmt-preview" {
469 &self.rustfmt_version
475 fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
476 if component == "cargo" {
477 &self.cargo_git_commit_hash
478 } else if component == "rls" || component == "rls-preview" {
479 &self.rls_git_commit_hash
480 } else if component == "rustfmt" || component == "rustfmt-preview" {
481 &self.rustfmt_git_commit_hash
483 &self.rust_git_commit_hash
487 fn version(&self, component: &str, target: &str) -> Option<String> {
488 let mut cmd = Command::new("tar");
489 let filename = self.filename(component, target);
491 .arg(self.input.join(&filename))
492 .arg(format!("{}/version", filename.replace(".tar.gz", "")))
494 let output = t!(cmd.output());
495 if output.status.success() {
496 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
498 // Perhaps we didn't build this package.
503 fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
504 let mut cmd = Command::new("tar");
505 let filename = self.filename(component, target);
507 .arg(self.input.join(&filename))
508 .arg(format!("{}/git-commit-hash", filename.replace(".tar.gz", "")))
510 let output = t!(cmd.output());
511 if output.status.success() {
512 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
518 fn hash(&self, path: &Path) -> String {
519 let sha = t!(Command::new("shasum")
520 .arg("-a").arg("256")
521 .arg(path.file_name().unwrap())
522 .current_dir(path.parent().unwrap())
524 assert!(sha.status.success());
526 let filename = path.file_name().unwrap().to_str().unwrap();
527 let sha256 = self.output.join(format!("{}.sha256", filename));
528 t!(t!(File::create(&sha256)).write_all(&sha.stdout));
530 let stdout = String::from_utf8_lossy(&sha.stdout);
531 stdout.split_whitespace().next().unwrap().to_string()
534 fn sign(&self, path: &Path) {
535 let filename = path.file_name().unwrap().to_str().unwrap();
536 let asc = self.output.join(format!("{}.asc", filename));
537 println!("signing: {:?}", path);
538 let mut cmd = Command::new("gpg");
541 .arg("--passphrase-fd").arg("0")
542 .arg("--personal-digest-preferences").arg("SHA512")
544 .arg("--output").arg(&asc)
545 .arg("--detach-sign").arg(path)
546 .stdin(Stdio::piped());
547 let mut child = t!(cmd.spawn());
548 t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
549 assert!(t!(child.wait()).success());
552 fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
553 self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
554 self.write(&manifest.date, channel_name, "-date.txt");
555 self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
556 channel_name, "-git-commit-hash.txt");
559 fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
560 let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
561 t!(t!(File::create(&dst)).write_all(contents.as_bytes()));