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.
12 extern crate rustc_serialize;
14 use std::collections::BTreeMap;
17 use std::io::{self, Read, Write};
18 use std::path::{PathBuf, Path};
19 use std::process::{Command, Stdio};
21 static HOSTS: &'static [&'static str] = &[
22 "aarch64-unknown-linux-gnu",
23 "arm-unknown-linux-gnueabi",
24 "arm-unknown-linux-gnueabihf",
25 "armv7-unknown-linux-gnueabihf",
27 "i686-pc-windows-gnu",
28 "i686-pc-windows-msvc",
29 "i686-unknown-linux-gnu",
30 "mips-unknown-linux-gnu",
31 "mips64-unknown-linux-gnuabi64",
32 "mips64el-unknown-linux-gnuabi64",
33 "mipsel-unknown-linux-gnu",
34 "powerpc-unknown-linux-gnu",
35 "powerpc64-unknown-linux-gnu",
36 "powerpc64le-unknown-linux-gnu",
37 "s390x-unknown-linux-gnu",
38 "x86_64-apple-darwin",
39 "x86_64-pc-windows-gnu",
40 "x86_64-pc-windows-msvc",
41 "x86_64-unknown-freebsd",
42 "x86_64-unknown-linux-gnu",
43 "x86_64-unknown-netbsd",
46 static TARGETS: &'static [&'static str] = &[
48 "aarch64-unknown-fuchsia",
49 "aarch64-linux-android",
50 "aarch64-unknown-linux-gnu",
51 "arm-linux-androideabi",
52 "arm-unknown-linux-gnueabi",
53 "arm-unknown-linux-gnueabihf",
54 "arm-unknown-linux-musleabi",
55 "arm-unknown-linux-musleabihf",
57 "armv7-linux-androideabi",
58 "armv7-unknown-linux-gnueabihf",
59 "armv7-unknown-linux-musleabihf",
61 "asmjs-unknown-emscripten",
63 "i586-pc-windows-msvc",
64 "i586-unknown-linux-gnu",
67 "i686-pc-windows-gnu",
68 "i686-pc-windows-msvc",
69 "i686-unknown-freebsd",
70 "i686-unknown-linux-gnu",
71 "i686-unknown-linux-musl",
72 "mips-unknown-linux-gnu",
73 "mips-unknown-linux-musl",
74 "mips64-unknown-linux-gnuabi64",
75 "mips64el-unknown-linux-gnuabi64",
76 "mipsel-unknown-linux-gnu",
77 "mipsel-unknown-linux-musl",
78 "powerpc-unknown-linux-gnu",
79 "powerpc64-unknown-linux-gnu",
80 "powerpc64le-unknown-linux-gnu",
81 "s390x-unknown-linux-gnu",
82 "sparc64-unknown-linux-gnu",
83 "wasm32-unknown-emscripten",
84 "x86_64-linux-android",
85 "x86_64-apple-darwin",
87 "x86_64-pc-windows-gnu",
88 "x86_64-pc-windows-msvc",
89 "x86_64-rumprun-netbsd",
90 "x86_64-unknown-freebsd",
91 "x86_64-unknown-fuchsia",
92 "x86_64-unknown-linux-gnu",
93 "x86_64-unknown-linux-musl",
94 "x86_64-unknown-netbsd",
97 static MINGW: &'static [&'static str] = &[
98 "i686-pc-windows-gnu",
99 "x86_64-pc-windows-gnu",
103 manifest_version: String,
105 pkg: BTreeMap<String, Package>,
108 #[derive(RustcEncodable)]
111 target: BTreeMap<String, Target>,
114 #[derive(RustcEncodable)]
118 hash: Option<String>,
119 components: Option<Vec<Component>>,
120 extensions: Option<Vec<Component>>,
123 #[derive(RustcEncodable)]
130 ($e:expr) => (match $e {
132 Err(e) => panic!("{} failed with {}", stringify!($e), e),
137 rust_release: String,
138 cargo_release: String,
142 gpg_passphrase: String,
143 digests: BTreeMap<String, String>,
146 rust_version: String,
147 cargo_version: String,
152 let mut args = env::args().skip(1);
153 let input = PathBuf::from(args.next().unwrap());
154 let output = PathBuf::from(args.next().unwrap());
155 let date = args.next().unwrap();
156 let rust_release = args.next().unwrap();
157 let cargo_release = args.next().unwrap();
158 let rls_release = args.next().unwrap();
159 let s3_address = args.next().unwrap();
160 let mut passphrase = String::new();
161 t!(io::stdin().read_to_string(&mut passphrase));
164 rust_release: rust_release,
165 cargo_release: cargo_release,
166 rls_release: rls_release,
169 gpg_passphrase: passphrase,
170 digests: BTreeMap::new(),
171 s3_address: s3_address,
173 rust_version: String::new(),
174 cargo_version: String::new(),
175 rls_version: String::new(),
180 fn build(&mut self) {
181 self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
182 self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");
183 self.rls_version = self.version("rls", "x86_64-unknown-linux-gnu");
185 self.digest_and_sign();
186 let Manifest { manifest_version, date, pkg } = self.build_manifest();
188 // Unfortunately we can't use derive(RustcEncodable) here because the
189 // version field is called `manifest-version`, not `manifest_version`.
190 // In lieu of that just create the table directly here with a `BTreeMap`
191 // and wrap it up in a `Value::Table`.
192 let mut manifest = BTreeMap::new();
193 manifest.insert("manifest-version".to_string(),
194 toml::Value::String(manifest_version));
195 manifest.insert("date".to_string(), toml::Value::String(date.clone()));
196 manifest.insert("pkg".to_string(), toml::encode(&pkg));
197 let manifest = toml::Value::Table(manifest).to_string();
199 let filename = format!("channel-rust-{}.toml", self.rust_release);
200 self.write_manifest(&manifest, &filename);
202 let filename = format!("channel-rust-{}-date.txt", self.rust_release);
203 self.write_date_stamp(&date, &filename);
205 if self.rust_release != "beta" && self.rust_release != "nightly" {
206 self.write_manifest(&manifest, "channel-rust-stable.toml");
207 self.write_date_stamp(&date, "channel-rust-stable-date.txt");
211 fn digest_and_sign(&mut self) {
212 for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
213 let filename = file.file_name().unwrap().to_str().unwrap();
214 let digest = self.hash(&file);
216 assert!(self.digests.insert(filename.to_string(), digest).is_none());
220 fn build_manifest(&mut self) -> Manifest {
221 let mut manifest = Manifest {
222 manifest_version: "2".to_string(),
223 date: self.date.to_string(),
224 pkg: BTreeMap::new(),
227 self.package("rustc", &mut manifest.pkg, HOSTS);
228 self.package("cargo", &mut manifest.pkg, HOSTS);
229 self.package("rust-mingw", &mut manifest.pkg, MINGW);
230 self.package("rust-std", &mut manifest.pkg, TARGETS);
231 self.package("rust-docs", &mut manifest.pkg, TARGETS);
232 self.package("rust-src", &mut manifest.pkg, &["*"]);
233 self.package("rls", &mut manifest.pkg, HOSTS);
234 self.package("rust-analysis", &mut manifest.pkg, TARGETS);
236 let mut pkg = Package {
237 version: self.cached_version("rust").to_string(),
238 target: BTreeMap::new(),
241 let filename = self.filename("rust", host);
242 let digest = match self.digests.remove(&filename) {
243 Some(digest) => digest,
245 pkg.target.insert(host.to_string(), Target {
255 let mut components = Vec::new();
256 let mut extensions = Vec::new();
258 // rustc/rust-std/cargo/docs are all required, and so is rust-mingw
259 // if it's available for the target.
260 components.extend(vec![
261 Component { pkg: "rustc".to_string(), target: host.to_string() },
262 Component { pkg: "rust-std".to_string(), target: host.to_string() },
263 Component { pkg: "cargo".to_string(), target: host.to_string() },
264 Component { pkg: "rust-docs".to_string(), target: host.to_string() },
266 if host.contains("pc-windows-gnu") {
267 components.push(Component {
268 pkg: "rust-mingw".to_string(),
269 target: host.to_string(),
273 extensions.push(Component {
274 pkg: "rls".to_string(),
275 target: host.to_string(),
277 extensions.push(Component {
278 pkg: "rust-analysis".to_string(),
279 target: host.to_string(),
281 for target in TARGETS {
283 extensions.push(Component {
284 pkg: "rust-std".to_string(),
285 target: target.to_string(),
289 extensions.push(Component {
290 pkg: "rust-src".to_string(),
291 target: "*".to_string(),
294 pkg.target.insert(host.to_string(), Target {
296 url: Some(self.url("rust", host)),
298 components: Some(components),
299 extensions: Some(extensions),
302 manifest.pkg.insert("rust".to_string(), pkg);
307 fn package(&mut self,
309 dst: &mut BTreeMap<String, Package>,
311 let targets = targets.iter().map(|name| {
312 let filename = self.filename(pkgname, name);
313 let digest = match self.digests.remove(&filename) {
314 Some(digest) => digest,
316 return (name.to_string(), Target {
326 (name.to_string(), Target {
328 url: Some(self.url(pkgname, name)),
335 dst.insert(pkgname.to_string(), Package {
336 version: self.cached_version(pkgname).to_string(),
341 fn url(&self, component: &str, target: &str) -> String {
345 self.filename(component, target))
348 fn filename(&self, component: &str, target: &str) -> String {
349 if component == "rust-src" {
350 format!("rust-src-{}.tar.gz", self.rust_release)
351 } else if component == "cargo" {
352 format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
353 } else if component == "rls" {
354 format!("rls-{}-{}.tar.gz", self.rls_release, target)
356 format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
360 fn cached_version(&self, component: &str) -> &str {
361 if component == "cargo" {
363 } else if component == "rls" {
370 fn version(&self, component: &str, target: &str) -> String {
371 let mut cmd = Command::new("tar");
372 let filename = self.filename(component, target);
374 .arg(self.input.join(&filename))
375 .arg(format!("{}/version", filename.replace(".tar.gz", "")))
377 let version = t!(cmd.output());
378 if !version.status.success() {
379 panic!("failed to learn version:\n\n{:?}\n\n{}\n\n{}",
381 String::from_utf8_lossy(&version.stdout),
382 String::from_utf8_lossy(&version.stderr));
384 String::from_utf8_lossy(&version.stdout).trim().to_string()
387 fn hash(&self, path: &Path) -> String {
388 let sha = t!(Command::new("shasum")
389 .arg("-a").arg("256")
390 .arg(path.file_name().unwrap())
391 .current_dir(path.parent().unwrap())
393 assert!(sha.status.success());
395 let filename = path.file_name().unwrap().to_str().unwrap();
396 let sha256 = self.output.join(format!("{}.sha256", filename));
397 t!(t!(File::create(&sha256)).write_all(&sha.stdout));
399 let stdout = String::from_utf8_lossy(&sha.stdout);
400 stdout.split_whitespace().next().unwrap().to_string()
403 fn sign(&self, path: &Path) {
404 let filename = path.file_name().unwrap().to_str().unwrap();
405 let asc = self.output.join(format!("{}.asc", filename));
406 println!("signing: {:?}", path);
407 let mut cmd = Command::new("gpg");
410 .arg("--passphrase-fd").arg("0")
412 .arg("--output").arg(&asc)
413 .arg("--detach-sign").arg(path)
414 .stdin(Stdio::piped());
415 let mut child = t!(cmd.spawn());
416 t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
417 assert!(t!(child.wait()).success());
420 fn write_manifest(&self, manifest: &str, name: &str) {
421 let dst = self.output.join(name);
422 t!(t!(File::create(&dst)).write_all(manifest.as_bytes()));
427 fn write_date_stamp(&self, date: &str, name: &str) {
428 let dst = self.output.join(name);
429 t!(t!(File::create(&dst)).write_all(date.as_bytes()));