1 #![deny(rust_2018_idioms)]
6 use std::collections::BTreeMap;
9 use std::io::{self, Read, Write};
10 use std::path::{PathBuf, Path};
11 use std::process::{Command, Stdio};
13 static HOSTS: &[&str] = &[
14 "aarch64-unknown-linux-gnu",
15 "arm-unknown-linux-gnueabi",
16 "arm-unknown-linux-gnueabihf",
17 "armv7-unknown-linux-gnueabihf",
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",
43 static TARGETS: &[&str] = &[
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",
59 "armv7-linux-androideabi",
60 "thumbv7neon-linux-androideabi",
61 "armv7-unknown-linux-gnueabihf",
62 "thumbv7neon-unknown-linux-gnueabihf",
63 "armv7-unknown-linux-musleabihf",
65 "armebv7r-none-eabihf",
69 "asmjs-unknown-emscripten",
71 "i586-pc-windows-msvc",
72 "i586-unknown-linux-gnu",
73 "i586-unknown-linux-musl",
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",
113 "x86_64-apple-darwin",
115 "x86_64-fortanix-unknown-sgx",
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",
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",
132 static DOCS_TARGETS: &[&str] = &[
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",
143 static MINGW: &[&str] = &[
144 "i686-pc-windows-gnu",
145 "x86_64-pc-windows-gnu",
149 #[serde(rename_all = "kebab-case")]
151 manifest_version: String,
153 pkg: BTreeMap<String, Package>,
154 renames: BTreeMap<String, Rename>,
155 profiles: BTreeMap<String, Vec<String>>,
161 git_commit_hash: Option<String>,
162 target: BTreeMap<String, Target>,
170 #[derive(Serialize, Default)]
174 hash: Option<String>,
175 xz_url: Option<String>,
176 xz_hash: Option<String>,
177 components: Option<Vec<Component>>,
178 extensions: Option<Vec<Component>>,
182 fn unavailable() -> Self { Self::default() }
192 fn from_str(pkg: &str, target: &str) -> Self {
193 Self { pkg: pkg.to_string(), target: target.to_string() }
198 ($e:expr) => (match $e {
200 Err(e) => panic!("{} failed with {}", stringify!($e), e),
205 rust_release: String,
206 cargo_release: String,
208 clippy_release: String,
209 rustfmt_release: String,
210 llvm_tools_release: String,
211 lldb_release: String,
212 miri_release: String,
216 gpg_passphrase: String,
217 digests: BTreeMap<String, String>,
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>,
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>,
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();
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.");
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();
269 // Do not ask for a passphrase while manually testing
270 let mut passphrase = String::new();
272 t!(io::stdin().read_to_string(&mut passphrase));
287 gpg_passphrase: passphrase,
288 digests: BTreeMap::new(),
295 clippy_version: None,
296 rustfmt_version: None,
297 llvm_tools_version: None,
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,
314 enum PkgType { RustSrc, Cargo, Rls, Clippy, Rustfmt, LlvmTools, Lldb, Miri, Other }
317 fn from_component(component: &str) -> Self {
320 "rust-src" => RustSrc,
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,
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");
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");
355 self.digest_and_sign();
356 let manifest = self.build_manifest();
357 self.write_channel_files(&self.rust_release, &manifest);
359 if self.rust_release != "beta" && self.rust_release != "nightly" {
360 self.write_channel_files("stable", &manifest);
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);
369 assert!(self.digests.insert(filename.to_string(), digest).is_none());
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(),
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));
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);
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"
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"
420 fn add_renames_to(&self, manifest: &mut Manifest) {
421 let mut rename = |from: &str, to: &str| manifest.renames.insert(
423 Rename { to: to.to_owned() }
425 rename("rls", "rls-preview");
426 rename("rustfmt", "rustfmt-preview");
427 rename("clippy", "clippy-preview");
428 rename("miri", "miri-preview");
431 fn rust_package(&mut self, manifest: &Manifest) -> Package {
432 let mut pkg = Package {
433 version: self.cached_version("rust")
435 .expect("Couldn't find Rust version")
437 git_commit_hash: self.cached_git_commit_hash("rust").clone(),
438 target: BTreeMap::new(),
441 if let Some(target) = self.target_host_combination(host, &manifest) {
442 pkg.target.insert(host.to_string(), target);
444 pkg.target.insert(host.to_string(), Target::unavailable());
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();
459 let host_component = |pkg| Component::from_str(pkg, host);
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"),
469 if host.contains("pc-windows-gnu") {
470 components.push(host_component("rust-mingw"));
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"),
487 .filter(|&&target| target != host)
488 .map(|target| Component::from_str("rust-std", target))
490 extensions.push(Component::from_str("rust-src", "*"));
492 // If the components/extensions don't actually exist for this
493 // particular host/target combination then nix it entirely from our
495 let has_component = |c: &Component| {
499 let pkg = match manifest.pkg.get(&c.pkg) {
501 None => return false,
503 pkg.target.get(&c.target).is_some()
505 extensions.retain(&has_component);
506 components.retain(&has_component);
510 url: Some(self.url(&filename)),
512 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
514 components: Some(components),
515 extensions: Some(extensions),
519 fn profile(&mut self,
521 dst: &mut BTreeMap<String, Vec<String>>,
523 dst.insert(profile_name.to_owned(), pkgs.iter().map(|s| (*s).to_owned()).collect());
526 fn package(&mut self,
528 dst: &mut BTreeMap<String, Package>,
530 let (version, mut is_present) = self.cached_version(pkgname)
533 .map(|version| (version, true))
534 .unwrap_or_default();
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
542 let targets = targets.iter().map(|name| {
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()),
549 let xz_filename = filename.replace(".tar.gz", ".tar.xz");
550 let xz_digest = self.digests.remove(&xz_filename);
552 (name.to_string(), Target {
554 url: Some(self.url(&filename)),
556 xz_url: xz_digest.as_ref().map(|_| self.url(&xz_filename)),
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())
568 dst.insert(pkgname.to_string(), Package {
570 git_commit_hash: self.cached_git_commit_hash(pkgname).clone(),
575 fn url(&self, filename: &str) -> String {
582 fn filename(&self, component: &str, target: &str) -> String {
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),
597 fn cached_version(&self, component: &str) -> &Option<String> {
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,
611 fn cached_git_commit_hash(&self, component: &str) -> &Option<String> {
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,
625 fn version(&self, component: &str, target: &str) -> Option<String> {
626 self.untar(component, target, |filename| format!("{}/version", filename))
629 fn git_commit_hash(&self, component: &str, target: &str) -> Option<String> {
630 self.untar(component, target, |filename| format!("{}/git-commit-hash", filename))
633 fn untar<F>(&self, component: &str, target: &str, dir: F) -> Option<String>
635 F: FnOnce(String) -> String
637 let mut cmd = Command::new("tar");
638 let filename = self.filename(component, target);
640 .arg(self.input.join(&filename))
641 .arg(dir(filename.replace(".tar.gz", "")))
643 let output = t!(cmd.output());
644 if output.status.success() {
645 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
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())
657 assert!(sha.status.success());
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));
663 let stdout = String::from_utf8_lossy(&sha.stdout);
664 stdout.split_whitespace().next().unwrap().to_string()
667 fn sign(&self, path: &Path) {
668 if !self.should_sign {
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")
680 .arg("--passphrase-fd").arg("0")
681 .arg("--personal-digest-preferences").arg("SHA512")
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());
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");
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));