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, HashMap}
;
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-linux-android",
49 "aarch64-unknown-linux-gnu",
50 "arm-linux-androideabi",
51 "arm-unknown-linux-gnueabi",
52 "arm-unknown-linux-gnueabihf",
53 "arm-unknown-linux-musleabi",
54 "arm-unknown-linux-musleabihf",
56 "armv7-linux-androideabi",
57 "armv7-unknown-linux-gnueabihf",
58 "armv7-unknown-linux-musleabihf",
60 "asmjs-unknown-emscripten",
62 "i586-pc-windows-msvc",
63 "i586-unknown-linux-gnu",
66 "i686-pc-windows-gnu",
67 "i686-pc-windows-msvc",
68 "i686-unknown-freebsd",
69 "i686-unknown-linux-gnu",
70 "i686-unknown-linux-musl",
71 "mips-unknown-linux-gnu",
72 "mips-unknown-linux-musl",
73 "mips64-unknown-linux-gnuabi64",
74 "mips64el-unknown-linux-gnuabi64",
75 "mipsel-unknown-linux-gnu",
76 "mipsel-unknown-linux-musl",
77 "powerpc-unknown-linux-gnu",
78 "powerpc64-unknown-linux-gnu",
79 "powerpc64le-unknown-linux-gnu",
80 "s390x-unknown-linux-gnu",
81 "wasm32-unknown-emscripten",
82 "x86_64-apple-darwin",
84 "x86_64-pc-windows-gnu",
85 "x86_64-pc-windows-msvc",
86 "x86_64-rumprun-netbsd",
87 "x86_64-unknown-freebsd",
88 "x86_64-unknown-linux-gnu",
89 "x86_64-unknown-linux-musl",
90 "x86_64-unknown-netbsd",
93 static MINGW
: &'
static [&'
static str] = &[
94 "i686-pc-windows-gnu",
95 "x86_64-pc-windows-gnu",
99 manifest_version
: String
,
101 pkg
: HashMap
<String
, Package
>,
104 #[derive(RustcEncodable)]
107 target
: HashMap
<String
, Target
>,
110 #[derive(RustcEncodable)]
114 hash
: Option
<String
>,
115 components
: Option
<Vec
<Component
>>,
116 extensions
: Option
<Vec
<Component
>>,
119 #[derive(RustcEncodable)]
126 ($e
:expr
) => (match $e
{
128 Err(e
) => panic
!("{} failed with {}", stringify
!($e
), e
),
136 gpg_passphrase
: String
,
137 digests
: HashMap
<String
, String
>,
140 rust_version
: String
,
141 cargo_version
: String
,
145 let mut args
= env
::args().skip(1);
146 let input
= PathBuf
::from(args
.next().unwrap());
147 let output
= PathBuf
::from(args
.next().unwrap());
148 let date
= args
.next().unwrap();
149 let channel
= args
.next().unwrap();
150 let s3_address
= args
.next().unwrap();
151 let mut passphrase
= String
::new();
152 t
!(io
::stdin().read_to_string(&mut passphrase
));
158 gpg_passphrase
: passphrase
,
159 digests
: HashMap
::new(),
160 s3_address
: s3_address
,
162 rust_version
: String
::new(),
163 cargo_version
: String
::new(),
168 fn build(&mut self) {
169 self.rust_version
= self.version("rust", "x86_64-unknown-linux-gnu");
170 self.cargo_version
= self.version("cargo", "x86_64-unknown-linux-gnu");
172 self.digest_and_sign();
173 let Manifest { manifest_version, date, pkg }
= self.build_manifest();
175 // Unfortunately we can't use derive(RustcEncodable) here because the
176 // version field is called `manifest-version`, not `manifest_version`.
177 // In lieu of that just create the table directly here with a `BTreeMap`
178 // and wrap it up in a `Value::Table`.
179 let mut manifest
= BTreeMap
::new();
180 manifest
.insert("manifest-version".to_string(),
181 toml
::Value
::String(manifest_version
));
182 manifest
.insert("date".to_string(), toml
::Value
::String(date
));
183 manifest
.insert("pkg".to_string(), toml
::encode(&pkg
));
184 let manifest
= toml
::Value
::Table(manifest
).to_string();
186 let filename
= format
!("channel-rust-{}.toml", self.channel
);
187 self.write_manifest(&manifest
, &filename
);
189 if self.channel
!= "beta" && self.channel
!= "nightly" {
190 self.write_manifest(&manifest
, "channel-rust-stable.toml");
194 fn digest_and_sign(&mut self) {
195 for file
in t
!(self.input
.read_dir()).map(|e
| t
!(e
).path()) {
196 let filename
= file
.file_name().unwrap().to_str().unwrap();
197 let digest
= self.hash(&file
);
199 assert
!(self.digests
.insert(filename
.to_string(), digest
).is_none());
203 fn build_manifest(&mut self) -> Manifest
{
204 let mut manifest
= Manifest
{
205 manifest_version
: "2".to_string(),
206 date
: self.date
.to_string(),
210 self.package("rustc", &mut manifest
.pkg
, HOSTS
);
211 self.package("cargo", &mut manifest
.pkg
, HOSTS
);
212 self.package("rust-mingw", &mut manifest
.pkg
, MINGW
);
213 self.package("rust-std", &mut manifest
.pkg
, TARGETS
);
214 self.package("rust-docs", &mut manifest
.pkg
, TARGETS
);
215 self.package("rust-src", &mut manifest
.pkg
, &["*"]);
217 let mut pkg
= Package
{
218 version
: self.cached_version("rust").to_string(),
219 target
: HashMap
::new(),
222 let filename
= self.filename("rust", host
);
223 let digest
= match self.digests
.remove(&filename
) {
224 Some(digest
) => digest
,
226 pkg
.target
.insert(host
.to_string(), Target
{
236 let mut components
= Vec
::new();
237 let mut extensions
= Vec
::new();
239 // rustc/rust-std/cargo are all required, and so is rust-mingw if it's
240 // available for the target.
241 components
.extend(vec
![
242 Component { pkg: "rustc".to_string(), target: host.to_string() }
,
243 Component { pkg: "rust-std".to_string(), target: host.to_string() }
,
244 Component { pkg: "cargo".to_string(), target: host.to_string() }
,
246 if host
.contains("pc-windows-gnu") {
247 components
.push(Component
{
248 pkg
: "rust-mingw".to_string(),
249 target
: host
.to_string(),
253 // Docs, other standard libraries, and the source package are all
255 extensions
.push(Component
{
256 pkg
: "rust-docs".to_string(),
257 target
: host
.to_string(),
259 for target
in TARGETS
{
261 extensions
.push(Component
{
262 pkg
: "rust-std".to_string(),
263 target
: target
.to_string(),
267 extensions
.push(Component
{
268 pkg
: "rust-src".to_string(),
269 target
: "*".to_string(),
272 pkg
.target
.insert(host
.to_string(), Target
{
274 url
: Some(self.url("rust", host
)),
275 hash
: Some(to_hex(digest
.as_ref())),
276 components
: Some(components
),
277 extensions
: Some(extensions
),
280 manifest
.pkg
.insert("rust".to_string(), pkg
);
285 fn package(&mut self,
287 dst
: &mut HashMap
<String
, Package
>,
289 let targets
= targets
.iter().map(|name
| {
290 let filename
= self.filename(pkgname
, name
);
291 let digest
= match self.digests
.remove(&filename
) {
292 Some(digest
) => digest
,
294 return (name
.to_string(), Target
{
304 (name
.to_string(), Target
{
306 url
: Some(self.url(pkgname
, name
)),
313 dst
.insert(pkgname
.to_string(), Package
{
314 version
: self.cached_version(pkgname
).to_string(),
319 fn url(&self, component
: &str, target
: &str) -> String
{
323 self.filename(component
, target
))
326 fn filename(&self, component
: &str, target
: &str) -> String
{
327 if component
== "rust-src" {
328 format
!("rust-src-{}.tar.gz", self.channel
)
329 } else if component
== "cargo" {
330 format
!("cargo-nightly-{}.tar.gz", target
)
332 format
!("{}-{}-{}.tar.gz", component
, self.channel
, target
)
336 fn cached_version(&self, component
: &str) -> &str {
337 if component
== "cargo" {
344 fn version(&self, component
: &str, target
: &str) -> String
{
345 let mut cmd
= Command
::new("tar");
346 let filename
= self.filename(component
, target
);
348 .arg(self.input
.join(&filename
))
349 .arg(format
!("{}/version", filename
.replace(".tar.gz", "")))
351 let version
= t
!(cmd
.output());
352 if !version
.status
.success() {
353 panic
!("failed to learn version:\n\n{:?}\n\n{}\n\n{}",
355 String
::from_utf8_lossy(&version
.stdout
),
356 String
::from_utf8_lossy(&version
.stderr
));
358 String
::from_utf8_lossy(&version
.stdout
).trim().to_string()
361 fn hash(&self, path
: &Path
) -> String
{
362 let sha
= t
!(Command
::new("shasum")
363 .arg("-a").arg("256")
364 .arg(path
.file_name().unwrap())
365 .current_dir(path
.parent().unwrap())
367 assert
!(sha
.status
.success());
369 let filename
= path
.file_name().unwrap().to_str().unwrap();
370 let sha256
= self.output
.join(format
!("{}.sha256", filename
));
371 t
!(t
!(File
::create(&sha256
)).write_all(&sha
.stdout
));
373 let stdout
= String
::from_utf8_lossy(&sha
.stdout
);
374 stdout
.split_whitespace().next().unwrap().to_string()
377 fn sign(&self, path
: &Path
) {
378 let filename
= path
.file_name().unwrap().to_str().unwrap();
379 let asc
= self.output
.join(format
!("{}.asc", filename
));
380 println
!("signing: {:?}", path
);
381 let mut cmd
= Command
::new("gpg");
384 .arg("--passphrase-fd").arg("0")
386 .arg("--output").arg(&asc
)
387 .arg("--detach-sign").arg(path
)
388 .stdin(Stdio
::piped());
389 let mut child
= t
!(cmd
.spawn());
390 t
!(child
.stdin
.take().unwrap().write_all(self.gpg_passphrase
.as_bytes()));
391 assert
!(t
!(child
.wait()).success());
394 fn write_manifest(&self, manifest
: &str, name
: &str) {
395 let dst
= self.output
.join(name
);
396 t
!(t
!(File
::create(&dst
)).write_all(manifest
.as_bytes()));
402 fn to_hex(digest
: &[u8]) -> String
{
403 let mut ret
= String
::new();
405 ret
.push(hex((byte
& 0xf0) >> 4));
406 ret
.push(hex(byte
& 0xf));
410 fn hex(b
: u8) -> char {
412 0...9 => (b'
0'
+ b
) as char,
413 _
=> (b'a'
+ b
- 10) as char,