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-pc-windows-msvc",
52 "aarch64-unknown-cloudabi",
53 "aarch64-unknown-linux-gnu",
54 "aarch64-unknown-linux-musl",
55 "arm-linux-androideabi",
56 "arm-unknown-linux-gnueabi",
57 "arm-unknown-linux-gnueabihf",
58 "arm-unknown-linux-musleabi",
59 "arm-unknown-linux-musleabihf",
60 "armv5te-unknown-linux-gnueabi",
61 "armv5te-unknown-linux-musleabi",
63 "armv7-linux-androideabi",
64 "thumbv7neon-linux-androideabi",
65 "armv7-unknown-linux-gnueabihf",
66 "thumbv7neon-unknown-linux-gnueabihf",
67 "armv7-unknown-linux-musleabihf",
69 "armebv7r-none-eabihf",
73 "asmjs-unknown-emscripten",
75 "i586-pc-windows-msvc",
76 "i586-unknown-linux-gnu",
77 "i586-unknown-linux-musl",
80 "i686-pc-windows-gnu",
81 "i686-pc-windows-msvc",
82 "i686-unknown-freebsd",
83 "i686-unknown-linux-gnu",
84 "i686-unknown-linux-musl",
85 "mips-unknown-linux-gnu",
86 "mips-unknown-linux-musl",
87 "mips64-unknown-linux-gnuabi64",
88 "mips64el-unknown-linux-gnuabi64",
89 "mipsel-unknown-linux-gnu",
90 "mipsel-unknown-linux-musl",
91 "powerpc-unknown-linux-gnu",
92 "powerpc64-unknown-linux-gnu",
93 "powerpc64le-unknown-linux-gnu",
94 "riscv32imc-unknown-none-elf",
95 "riscv32imac-unknown-none-elf",
96 "s390x-unknown-linux-gnu",
97 "sparc64-unknown-linux-gnu",
98 "sparcv9-sun-solaris",
100 "thumbv7em-none-eabi",
101 "thumbv7em-none-eabihf",
102 "thumbv7m-none-eabi",
103 "wasm32-unknown-emscripten",
104 "wasm32-unknown-unknown",
105 "x86_64-apple-darwin",
108 "x86_64-linux-android",
109 "x86_64-pc-windows-gnu",
110 "x86_64-pc-windows-msvc",
111 "x86_64-rumprun-netbsd",
112 "x86_64-sun-solaris",
113 "x86_64-unknown-cloudabi",
114 "x86_64-unknown-freebsd",
115 "x86_64-unknown-linux-gnu",
116 "x86_64-unknown-linux-gnux32",
117 "x86_64-unknown-linux-musl",
118 "x86_64-unknown-netbsd",
119 "x86_64-unknown-redox",
122 static DOCS_TARGETS: &'static [&'static str] = &[
124 "i686-pc-windows-gnu",
125 "i686-pc-windows-msvc",
126 "i686-unknown-linux-gnu",
127 "x86_64-apple-darwin",
128 "x86_64-pc-windows-gnu",
129 "x86_64-pc-windows-msvc",
130 "x86_64-unknown-linux-gnu",
133 static MINGW: &'static [&'static str] = &[
134 "i686-pc-windows-gnu",
135 "x86_64-pc-windows-gnu",
139 #[serde(rename_all = "kebab-case")]
141 manifest_version: String,
143 pkg: BTreeMap<String, Package>,
144 renames: BTreeMap<String, Rename>
150 git_commit_hash: Option<String>,
151 target: BTreeMap<String, Target>,
163 hash: Option<String>,
164 xz_url: Option<String>,
165 xz_hash: Option<String>,
166 components: Option<Vec<Component>>,
167 extensions: Option<Vec<Component>>,
171 fn unavailable() -> Target {
191 ($e:expr) => (match $e {
193 Err(e) => panic!("{} failed with {}", stringify!($e), e),
198 rust_release: String,
199 cargo_release: String,
201 clippy_release: String,
202 rustfmt_release: String,
203 llvm_tools_release: String,
204 lldb_release: String,
208 gpg_passphrase: String,
209 digests: BTreeMap<String, String>,
213 rust_version: Option<String>,
214 cargo_version: Option<String>,
215 rls_version: Option<String>,
216 clippy_version: Option<String>,
217 rustfmt_version: Option<String>,
218 llvm_tools_version: Option<String>,
219 lldb_version: Option<String>,
221 rust_git_commit_hash: Option<String>,
222 cargo_git_commit_hash: Option<String>,
223 rls_git_commit_hash: Option<String>,
224 clippy_git_commit_hash: Option<String>,
225 rustfmt_git_commit_hash: Option<String>,
226 llvm_tools_git_commit_hash: Option<String>,
227 lldb_git_commit_hash: Option<String>,
233 // Avoid signing packages while manually testing
234 // Do NOT set this envvar in CI
235 let should_sign = env::var("BUILD_MANIFEST_DISABLE_SIGNING").is_err();
237 // Safety check to ensure signing is always enabled on CI
238 // The CI environment variable is set by both Travis and AppVeyor
239 if !should_sign && env::var("CI").is_ok() {
240 println!("The 'BUILD_MANIFEST_DISABLE_SIGNING' env var can't be enabled on CI.");
241 println!("If you're not running this on CI, unset the 'CI' env var.");
245 let mut args = env::args().skip(1);
246 let input = PathBuf::from(args.next().unwrap());
247 let output = PathBuf::from(args.next().unwrap());
248 let date = args.next().unwrap();
249 let rust_release = args.next().unwrap();
250 let cargo_release = args.next().unwrap();
251 let rls_release = args.next().unwrap();
252 let clippy_release = args.next().unwrap();
253 let rustfmt_release = args.next().unwrap();
254 let llvm_tools_release = args.next().unwrap();
255 let lldb_release = args.next().unwrap();
256 let s3_address = args.next().unwrap();
258 // Do not ask for a passphrase while manually testing
259 let mut passphrase = String::new();
261 t!(io::stdin().read_to_string(&mut passphrase));
275 gpg_passphrase: passphrase,
276 digests: BTreeMap::new(),
283 clippy_version: None,
284 rustfmt_version: None,
285 llvm_tools_version: None,
288 rust_git_commit_hash: None,
289 cargo_git_commit_hash: None,
290 rls_git_commit_hash: None,
291 clippy_git_commit_hash: None,
292 rustfmt_git_commit_hash: None,
293 llvm_tools_git_commit_hash: None,
294 lldb_git_commit_hash: None,
301 fn build(&mut self) {
302 self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
303 self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
304 self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
305 self.clippy_version = self.version("clippy", "x86_64-unknown-linux-gnu");
306 self.rustfmt_version = self.version("rustfmt", "x86_64-unknown-linux-gnu");
307 self.llvm_tools_version = self.version("llvm-tools", "x86_64-unknown-linux-gnu");
308 // lldb is only built for macOS.
309 self.lldb_version = self.version("lldb", "x86_64-apple-darwin");
311 self.rust_git_commit_hash = self.git_commit_hash("rust", "x86_64-unknown-linux-gnu");
312 self.cargo_git_commit_hash = self.git_commit_hash("cargo", "x86_64-unknown-linux-gnu");
313 self.rls_git_commit_hash = self.git_commit_hash("rls", "x86_64-unknown-linux-gnu");
314 self.clippy_git_commit_hash = self.git_commit_hash("clippy", "x86_64-unknown-linux-gnu");
315 self.rustfmt_git_commit_hash = self.git_commit_hash("rustfmt", "x86_64-unknown-linux-gnu");
316 self.llvm_tools_git_commit_hash = self.git_commit_hash("llvm-tools",
317 "x86_64-unknown-linux-gnu");
318 self.lldb_git_commit_hash = self.git_commit_hash("lldb", "x86_64-unknown-linux-gnu");
320 self.digest_and_sign();
321 let manifest = self.build_manifest();
322 self.write_channel_files(&self.rust_release, &manifest);
324 if self.rust_release != "beta" && self.rust_release != "nightly" {
325 self.write_channel_files("stable", &manifest);
329 fn digest_and_sign(&mut self) {
330 for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
331 let filename = file.file_name().unwrap().to_str().unwrap();
332 let digest = self.hash(&file);
334 assert!(self.digests.insert(filename.to_string(), digest).is_none());
338 fn build_manifest(&mut self) -> Manifest {
339 let mut manifest = Manifest {
340 manifest_version: "2".to_string(),
341 date: self.date.to_string(),
342 pkg: BTreeMap::new(),
343 renames: BTreeMap::new(),
346 self.package("rustc", &mut manifest.pkg, HOSTS);
347 self.package("cargo", &mut manifest.pkg, HOSTS);
348 self.package("rust-mingw", &mut manifest.pkg, MINGW);
349 self.package("rust-std", &mut manifest.pkg, TARGETS);
350 self.package("rust-docs", &mut manifest.pkg, DOCS_TARGETS);
351 self.package("rust-src", &mut manifest.pkg, &["*"]);
352 self.package("rls-preview", &mut manifest.pkg, HOSTS);
353 self.package("clippy-preview", &mut manifest.pkg, HOSTS);
354 self.package("rustfmt-preview", &mut manifest.pkg, HOSTS);
355 self.package("rust-analysis", &mut manifest.pkg, TARGETS);
356 self.package("llvm-tools-preview", &mut manifest.pkg, TARGETS);
357 self.package("lldb-preview", &mut manifest.pkg, TARGETS);
359 manifest.renames.insert("rls".to_owned(), Rename { to: "rls-preview".to_owned() });
360 manifest.renames.insert("rustfmt".to_owned(), Rename { to: "rustfmt-preview".to_owned() });
361 manifest.renames.insert("clippy".to_owned(), Rename { to: "clippy-preview".to_owned() });
363 let mut pkg = Package {
364 version: self.cached_version("rust")
366 .expect("Couldn't find Rust version")
368 git_commit_hash: self.cached_git_commit_hash("rust").clone(),
369 target: BTreeMap::new(),
372 let filename = self.filename("rust", host);
373 let digest = match self.digests.remove(&filename) {
374 Some(digest) => digest,
376 pkg.target.insert(host.to_string(), Target::unavailable());
380 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
381 let xz_digest = self.digests.remove(&xz_filename);
382 let mut components = Vec::new();
383 let mut extensions = Vec::new();
385 // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
386 // if it's available for the target.
387 components.extend(vec![
388 Component { pkg: "rustc".to_string(), target: host.to_string() },
389 Component { pkg: "rust-std".to_string(), target: host.to_string() },
390 Component { pkg: "cargo".to_string(), target: host.to_string() },
391 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
393 if host.contains("pc-windows-gnu") {
394 components.push(Component {
395 pkg: "rust-mingw".to_string(),
396 target: host.to_string(),
400 // Tools are always present in the manifest, but might be marked as unavailable if they
402 extensions.extend(vec![
403 Component { pkg: "clippy-preview".to_string(), target: host.to_string() },
404 Component { pkg: "rls-preview".to_string(), target: host.to_string() },
405 Component { pkg: "rustfmt-preview".to_string(), target: host.to_string() },
406 Component { pkg: "llvm-tools-preview".to_string(), target: host.to_string() },
407 Component { pkg: "lldb-preview".to_string(), target: host.to_string() },
408 Component { pkg: "rust-analysis".to_string(), target: host.to_string() },
411 for target in TARGETS {
413 extensions.push(Component {
414 pkg: "rust-std".to_string(),
415 target: target.to_string(),
419 extensions.push(Component {
420 pkg: "rust-src".to_string(),
421 target: "*".to_string(),
424 // If the components/extensions don't actually exist for this
425 // particular host/target combination then nix it entirely from our
428 let has_component = |c: &Component| {
432 let pkg = match manifest.pkg.get(&c.pkg) {
434 None => return false,
436 pkg.target.get(&c.target).is_some()
438 extensions.retain(&has_component);
439 components.retain(&has_component);
442 pkg.target.insert(host.to_string(), Target {
444 url: Some(self.url(&filename)),
446 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
448 components: Some(components),
449 extensions: Some(extensions),
452 manifest.pkg.insert("rust".to_string(), pkg);
457 fn package(&mut self,
459 dst: &mut BTreeMap<String, Package>,
461 let (version, is_present) = match *self.cached_version(pkgname) {
462 Some(ref version) => (version.clone(), true),
463 None => (String::new(), false),
466 let targets = targets.iter().map(|name| {
468 let filename = self.filename(pkgname, name);
469 let digest = match self.digests.remove(&filename) {
470 Some(digest) => digest,
471 None => return (name.to_string(), Target::unavailable()),
473 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
474 let xz_digest = self.digests.remove(&xz_filename);
476 (name.to_string(), Target {
478 url: Some(self.url(&filename)),
480 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
486 // If the component is not present for this build add it anyway but mark it as
487 // unavailable -- this way rustup won't allow upgrades without --force
488 (name.to_string(), Target {
500 dst.insert(pkgname.to_string(), Package {
502 git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
507 fn url(&self, filename: &str) -> String {
514 fn filename(&self, component: &str, target: &str) -> String {
515 if component == "rust-src" {
516 format!("rust-src-{}.tar.gz", self.rust_release)
517 } else if component == "cargo" {
518 format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
519 } else if component == "rls" || component == "rls-preview" {
520 format!("rls-{}-{}.tar.gz", self.rls_release, target)
521 } else if component == "clippy" || component == "clippy-preview" {
522 format!("clippy-{}-{}.tar.gz", self.clippy_release, target)
523 } else if component == "rustfmt" || component == "rustfmt-preview" {
524 format!("rustfmt-{}-{}.tar.gz", self.rustfmt_release, target)
525 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
526 format!("llvm-tools-{}-{}.tar.gz", self.llvm_tools_release, target)
527 } else if component == "lldb" || component == "lldb-preview" {
528 format!("lldb-{}-{}.tar.gz", self.lldb_release, target)
530 format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
534 fn cached_version(&self, component: &str) -> &Option<String> {
535 if component == "cargo" {
537 } else if component == "rls" || component == "rls-preview" {
539 } else if component == "clippy" || component == "clippy-preview" {
541 } else if component == "rustfmt" || component == "rustfmt-preview" {
542 &self.rustfmt_version
543 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
544 &self.llvm_tools_version
545 } else if component == "lldb" || component == "lldb-preview" {
552 fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
553 if component == "cargo" {
554 &self.cargo_git_commit_hash
555 } else if component == "rls" || component == "rls-preview" {
556 &self.rls_git_commit_hash
557 } else if component == "clippy" || component == "clippy-preview" {
558 &self.clippy_git_commit_hash
559 } else if component == "rustfmt" || component == "rustfmt-preview" {
560 &self.rustfmt_git_commit_hash
561 } else if component == "llvm-tools" || component == "llvm-tools-preview" {
562 &self.llvm_tools_git_commit_hash
563 } else if component == "lldb" || component == "lldb-preview" {
564 &self.lldb_git_commit_hash
566 &self.rust_git_commit_hash
570 fn version(&self, component: &str, target: &str) -> Option<String> {
571 let mut cmd = Command::new("tar");
572 let filename = self.filename(component, target);
574 .arg(self.input.join(&filename))
575 .arg(format!("{}/version", filename.replace(".tar.gz", "")))
577 let output = t!(cmd.output());
578 if output.status.success() {
579 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
581 // Perhaps we didn't build this package.
586 fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
587 let mut cmd = Command::new("tar");
588 let filename = self.filename(component, target);
590 .arg(self.input.join(&filename))
591 .arg(format!("{}/git-commit-hash", filename.replace(".tar.gz", "")))
593 let output = t!(cmd.output());
594 if output.status.success() {
595 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
601 fn hash(&self, path: &Path) -> String {
602 let sha = t!(Command::new("shasum")
603 .arg("-a").arg("256")
604 .arg(path.file_name().unwrap())
605 .current_dir(path.parent().unwrap())
607 assert!(sha.status.success());
609 let filename = path.file_name().unwrap().to_str().unwrap();
610 let sha256 = self.output.join(format!("{}.sha256", filename));
611 t!(fs::write(&sha256, &sha.stdout));
613 let stdout = String::from_utf8_lossy(&sha.stdout);
614 stdout.split_whitespace().next().unwrap().to_string()
617 fn sign(&self, path: &Path) {
618 if !self.should_sign {
622 let filename = path.file_name().unwrap().to_str().unwrap();
623 let asc = self.output.join(format!("{}.asc", filename));
624 println!("signing: {:?}", path);
625 let mut cmd = Command::new("gpg");
626 cmd.arg("--pinentry-mode=loopback")
630 .arg("--passphrase-fd").arg("0")
631 .arg("--personal-digest-preferences").arg("SHA512")
633 .arg("--output").arg(&asc)
634 .arg("--detach-sign").arg(path)
635 .stdin(Stdio::piped());
636 let mut child = t!(cmd.spawn());
637 t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
638 assert!(t!(child.wait()).success());
641 fn write_channel_files(&self, channel_name: &str, manifest: &Manifest) {
642 self.write(&toml::to_string(&manifest).unwrap(), channel_name, ".toml");
643 self.write(&manifest.date, channel_name, "-date.txt");
644 self.write(manifest.pkg["rust"].git_commit_hash.as_ref().unwrap(),
645 channel_name, "-git-commit-hash.txt");
648 fn write(&self, contents: &str, channel_name: &str, suffix: &str) {
649 let dst = self.output.join(format!("channel-rust-{}{}", channel_name, suffix));
650 t!(fs::write(&dst, contents));