]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/dist.rs
Rollup merge of #40497 - wesleywiser:fix_sidebar, r=GuillaumeGomez
[rust.git] / src / bootstrap / dist.rs
1 // Copyright 2016 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.
4 //
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.
10
11 //! Implementation of the various distribution aspects of the compiler.
12 //!
13 //! This module is responsible for creating tarballs of the standard library,
14 //! compiler, and documentation. This ends up being what we distribute to
15 //! everyone as well.
16 //!
17 //! No tarball is actually created literally in this file, but rather we shell
18 //! out to `rust-installer` still. This may one day be replaced with bits and
19 //! pieces of `rustup.rs`!
20
21 use std::env;
22 use std::fs::{self, File};
23 use std::io::{Read, Write};
24 use std::path::{PathBuf, Path};
25 use std::process::{Command, Stdio};
26
27 use build_helper::output;
28
29 #[cfg(not(target_os = "solaris"))]
30 const SH_CMD: &'static str = "sh";
31 // On Solaris, sh is the historical bourne shell, not a POSIX shell, or bash.
32 #[cfg(target_os = "solaris")]
33 const SH_CMD: &'static str = "bash";
34
35 use {Build, Compiler, Mode};
36 use channel;
37 use util::{cp_r, libdir, is_dylib, cp_filtered, copy, exe};
38
39 fn pkgname(build: &Build, component: &str) -> String {
40     assert!(component.starts_with("rust")); // does not work with cargo
41     format!("{}-{}", component, build.rust_package_vers())
42 }
43
44 fn distdir(build: &Build) -> PathBuf {
45     build.out.join("dist")
46 }
47
48 pub fn tmpdir(build: &Build) -> PathBuf {
49     build.out.join("tmp/dist")
50 }
51
52 /// Builds the `rust-docs` installer component.
53 ///
54 /// Slurps up documentation from the `stage`'s `host`.
55 pub fn docs(build: &Build, stage: u32, host: &str) {
56     println!("Dist docs stage{} ({})", stage, host);
57     if !build.config.docs {
58         println!("\tskipping - docs disabled");
59         return
60     }
61
62     let name = pkgname(build, "rust-docs");
63     let image = tmpdir(build).join(format!("{}-{}-image", name, host));
64     let _ = fs::remove_dir_all(&image);
65
66     let dst = image.join("share/doc/rust/html");
67     t!(fs::create_dir_all(&dst));
68     let src = build.out.join(host).join("doc");
69     cp_r(&src, &dst);
70
71     let mut cmd = Command::new(SH_CMD);
72     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
73        .arg("--product-name=Rust-Documentation")
74        .arg("--rel-manifest-dir=rustlib")
75        .arg("--success-message=Rust-documentation-is-installed.")
76        .arg(format!("--image-dir={}", sanitize_sh(&image)))
77        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
78        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
79        .arg(format!("--package-name={}-{}", name, host))
80        .arg("--component-name=rust-docs")
81        .arg("--legacy-manifest-dirs=rustlib,cargo")
82        .arg("--bulk-dirs=share/doc/rust/html");
83     build.run(&mut cmd);
84     t!(fs::remove_dir_all(&image));
85
86     // As part of this step, *also* copy the docs directory to a directory which
87     // buildbot typically uploads.
88     if host == build.config.build {
89         let dst = distdir(build).join("doc").join(build.rust_package_vers());
90         t!(fs::create_dir_all(&dst));
91         cp_r(&src, &dst);
92     }
93 }
94
95 /// Build the `rust-mingw` installer component.
96 ///
97 /// This contains all the bits and pieces to run the MinGW Windows targets
98 /// without any extra installed software (e.g. we bundle gcc, libraries, etc).
99 /// Currently just shells out to a python script, but that should be rewritten
100 /// in Rust.
101 pub fn mingw(build: &Build, host: &str) {
102     println!("Dist mingw ({})", host);
103     let name = pkgname(build, "rust-mingw");
104     let image = tmpdir(build).join(format!("{}-{}-image", name, host));
105     let _ = fs::remove_dir_all(&image);
106     t!(fs::create_dir_all(&image));
107
108     // The first argument to the script is a "temporary directory" which is just
109     // thrown away (this contains the runtime DLLs included in the rustc package
110     // above) and the second argument is where to place all the MinGW components
111     // (which is what we want).
112     //
113     // FIXME: this script should be rewritten into Rust
114     let mut cmd = Command::new(build.python());
115     cmd.arg(build.src.join("src/etc/make-win-dist.py"))
116        .arg(tmpdir(build))
117        .arg(&image)
118        .arg(host);
119     build.run(&mut cmd);
120
121     let mut cmd = Command::new(SH_CMD);
122     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
123        .arg("--product-name=Rust-MinGW")
124        .arg("--rel-manifest-dir=rustlib")
125        .arg("--success-message=Rust-MinGW-is-installed.")
126        .arg(format!("--image-dir={}", sanitize_sh(&image)))
127        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
128        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
129        .arg(format!("--package-name={}-{}", name, host))
130        .arg("--component-name=rust-mingw")
131        .arg("--legacy-manifest-dirs=rustlib,cargo");
132     build.run(&mut cmd);
133     t!(fs::remove_dir_all(&image));
134 }
135
136 /// Creates the `rustc` installer component.
137 pub fn rustc(build: &Build, stage: u32, host: &str) {
138     println!("Dist rustc stage{} ({})", stage, host);
139     let name = pkgname(build, "rustc");
140     let image = tmpdir(build).join(format!("{}-{}-image", name, host));
141     let _ = fs::remove_dir_all(&image);
142     let overlay = tmpdir(build).join(format!("{}-{}-overlay", name, host));
143     let _ = fs::remove_dir_all(&overlay);
144
145     // Prepare the rustc "image", what will actually end up getting installed
146     prepare_image(build, stage, host, &image);
147
148     // Prepare the overlay which is part of the tarball but won't actually be
149     // installed
150     let cp = |file: &str| {
151         install(&build.src.join(file), &overlay, 0o644);
152     };
153     cp("COPYRIGHT");
154     cp("LICENSE-APACHE");
155     cp("LICENSE-MIT");
156     cp("README.md");
157     // tiny morsel of metadata is used by rust-packaging
158     let version = build.rust_version();
159     t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes()));
160
161     // On MinGW we've got a few runtime DLL dependencies that we need to
162     // include. The first argument to this script is where to put these DLLs
163     // (the image we're creating), and the second argument is a junk directory
164     // to ignore all other MinGW stuff the script creates.
165     //
166     // On 32-bit MinGW we're always including a DLL which needs some extra
167     // licenses to distribute. On 64-bit MinGW we don't actually distribute
168     // anything requiring us to distribute a license, but it's likely the
169     // install will *also* include the rust-mingw package, which also needs
170     // licenses, so to be safe we just include it here in all MinGW packages.
171     //
172     // FIXME: this script should be rewritten into Rust
173     if host.contains("pc-windows-gnu") {
174         let mut cmd = Command::new(build.python());
175         cmd.arg(build.src.join("src/etc/make-win-dist.py"))
176            .arg(&image)
177            .arg(tmpdir(build))
178            .arg(host);
179         build.run(&mut cmd);
180
181         let dst = image.join("share/doc");
182         t!(fs::create_dir_all(&dst));
183         cp_r(&build.src.join("src/etc/third-party"), &dst);
184     }
185
186     // Finally, wrap everything up in a nice tarball!
187     let mut cmd = Command::new(SH_CMD);
188     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
189        .arg("--product-name=Rust")
190        .arg("--rel-manifest-dir=rustlib")
191        .arg("--success-message=Rust-is-ready-to-roll.")
192        .arg(format!("--image-dir={}", sanitize_sh(&image)))
193        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
194        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
195        .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay)))
196        .arg(format!("--package-name={}-{}", name, host))
197        .arg("--component-name=rustc")
198        .arg("--legacy-manifest-dirs=rustlib,cargo");
199     build.run(&mut cmd);
200     t!(fs::remove_dir_all(&image));
201     t!(fs::remove_dir_all(&overlay));
202
203     fn prepare_image(build: &Build, stage: u32, host: &str, image: &Path) {
204         let src = build.sysroot(&Compiler::new(stage, host));
205         let libdir = libdir(host);
206
207         // Copy rustc/rustdoc binaries
208         t!(fs::create_dir_all(image.join("bin")));
209         cp_r(&src.join("bin"), &image.join("bin"));
210
211         // Copy runtime DLLs needed by the compiler
212         if libdir != "bin" {
213             for entry in t!(src.join(libdir).read_dir()).map(|e| t!(e)) {
214                 let name = entry.file_name();
215                 if let Some(s) = name.to_str() {
216                     if is_dylib(s) {
217                         install(&entry.path(), &image.join(libdir), 0o644);
218                     }
219                 }
220             }
221         }
222
223         // Man pages
224         t!(fs::create_dir_all(image.join("share/man/man1")));
225         cp_r(&build.src.join("man"), &image.join("share/man/man1"));
226
227         // Debugger scripts
228         debugger_scripts(build, &image, host);
229
230         // Misc license info
231         let cp = |file: &str| {
232             install(&build.src.join(file), &image.join("share/doc/rust"), 0o644);
233         };
234         cp("COPYRIGHT");
235         cp("LICENSE-APACHE");
236         cp("LICENSE-MIT");
237         cp("README.md");
238     }
239 }
240
241 /// Copies debugger scripts for `host` into the `sysroot` specified.
242 pub fn debugger_scripts(build: &Build,
243                         sysroot: &Path,
244                         host: &str) {
245     let cp_debugger_script = |file: &str| {
246         let dst = sysroot.join("lib/rustlib/etc");
247         t!(fs::create_dir_all(&dst));
248         install(&build.src.join("src/etc/").join(file), &dst, 0o644);
249     };
250     if host.contains("windows-msvc") {
251         // no debugger scripts
252     } else {
253         cp_debugger_script("debugger_pretty_printers_common.py");
254
255         // gdb debugger scripts
256         install(&build.src.join("src/etc/rust-gdb"), &sysroot.join("bin"),
257                 0o755);
258
259         cp_debugger_script("gdb_load_rust_pretty_printers.py");
260         cp_debugger_script("gdb_rust_pretty_printing.py");
261
262         // lldb debugger scripts
263         install(&build.src.join("src/etc/rust-lldb"), &sysroot.join("bin"),
264                 0o755);
265
266         cp_debugger_script("lldb_rust_formatters.py");
267     }
268 }
269
270 /// Creates the `rust-std` installer component as compiled by `compiler` for the
271 /// target `target`.
272 pub fn std(build: &Build, compiler: &Compiler, target: &str) {
273     println!("Dist std stage{} ({} -> {})", compiler.stage, compiler.host,
274              target);
275
276     // The only true set of target libraries came from the build triple, so
277     // let's reduce redundant work by only producing archives from that host.
278     if compiler.host != build.config.build {
279         println!("\tskipping, not a build host");
280         return
281     }
282
283     let name = pkgname(build, "rust-std");
284     let image = tmpdir(build).join(format!("{}-{}-image", name, target));
285     let _ = fs::remove_dir_all(&image);
286
287     let dst = image.join("lib/rustlib").join(target);
288     t!(fs::create_dir_all(&dst));
289     let src = build.sysroot(compiler).join("lib/rustlib");
290     cp_r(&src.join(target), &dst);
291
292     let mut cmd = Command::new(SH_CMD);
293     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
294        .arg("--product-name=Rust")
295        .arg("--rel-manifest-dir=rustlib")
296        .arg("--success-message=std-is-standing-at-the-ready.")
297        .arg(format!("--image-dir={}", sanitize_sh(&image)))
298        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
299        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
300        .arg(format!("--package-name={}-{}", name, target))
301        .arg(format!("--component-name=rust-std-{}", target))
302        .arg("--legacy-manifest-dirs=rustlib,cargo");
303     build.run(&mut cmd);
304     t!(fs::remove_dir_all(&image));
305 }
306
307 pub fn rust_src_location(build: &Build) -> PathBuf {
308     let plain_name = format!("rustc-{}-src", build.rust_package_vers());
309     distdir(build).join(&format!("{}.tar.gz", plain_name))
310 }
311
312 /// Creates a tarball of save-analysis metadata, if available.
313 pub fn analysis(build: &Build, compiler: &Compiler, target: &str) {
314     if !build.config.rust_save_analysis {
315         return
316     }
317
318     println!("Dist analysis");
319
320     if compiler.host != build.config.build {
321         println!("\tskipping, not a build host");
322         return
323     }
324
325     // Package save-analysis from stage1 if not doing a full bootstrap, as the
326     // stage2 artifacts is simply copied from stage1 in that case.
327     let compiler = if build.force_use_stage1(compiler, target) {
328         Compiler::new(1, compiler.host)
329     } else {
330         compiler.clone()
331     };
332
333     let name = pkgname(build, "rust-analysis");
334     let image = tmpdir(build).join(format!("{}-{}-image", name, target));
335
336     let src = build.stage_out(&compiler, Mode::Libstd).join(target).join("release").join("deps");
337
338     let image_src = src.join("save-analysis");
339     let dst = image.join("lib/rustlib").join(target).join("analysis");
340     t!(fs::create_dir_all(&dst));
341     println!("image_src: {:?}, dst: {:?}", image_src, dst);
342     cp_r(&image_src, &dst);
343
344     let mut cmd = Command::new(SH_CMD);
345     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
346        .arg("--product-name=Rust")
347        .arg("--rel-manifest-dir=rustlib")
348        .arg("--success-message=save-analysis-saved.")
349        .arg(format!("--image-dir={}", sanitize_sh(&image)))
350        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
351        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
352        .arg(format!("--package-name={}-{}", name, target))
353        .arg(format!("--component-name=rust-analysis-{}", target))
354        .arg("--legacy-manifest-dirs=rustlib,cargo");
355     build.run(&mut cmd);
356     t!(fs::remove_dir_all(&image));
357 }
358
359 const CARGO_VENDOR_VERSION: &'static str = "0.1.4";
360
361 /// Creates the `rust-src` installer component and the plain source tarball
362 pub fn rust_src(build: &Build) {
363     if !build.config.rust_dist_src {
364         return
365     }
366
367     println!("Dist src");
368
369     let name = pkgname(build, "rust-src");
370     let image = tmpdir(build).join(format!("{}-image", name));
371     let _ = fs::remove_dir_all(&image);
372
373     let dst = image.join("lib/rustlib/src");
374     let dst_src = dst.join("rust");
375     t!(fs::create_dir_all(&dst_src));
376
377     // This is the set of root paths which will become part of the source package
378     let src_files = [
379         "COPYRIGHT",
380         "LICENSE-APACHE",
381         "LICENSE-MIT",
382         "CONTRIBUTING.md",
383         "README.md",
384         "RELEASES.md",
385         "configure",
386         "x.py",
387     ];
388     let src_dirs = [
389         "man",
390         "src",
391         "cargo",
392     ];
393
394     let filter_fn = move |path: &Path| {
395         let spath = match path.to_str() {
396             Some(path) => path,
397             None => return false,
398         };
399         if spath.ends_with("~") || spath.ends_with(".pyc") {
400             return false
401         }
402         if spath.contains("llvm/test") || spath.contains("llvm\\test") {
403             if spath.ends_with(".ll") ||
404                spath.ends_with(".td") ||
405                spath.ends_with(".s") {
406                 return false
407             }
408         }
409
410         let excludes = [
411             "CVS", "RCS", "SCCS", ".git", ".gitignore", ".gitmodules",
412             ".gitattributes", ".cvsignore", ".svn", ".arch-ids", "{arch}",
413             "=RELEASE-ID", "=meta-update", "=update", ".bzr", ".bzrignore",
414             ".bzrtags", ".hg", ".hgignore", ".hgrags", "_darcs",
415         ];
416         !path.iter()
417              .map(|s| s.to_str().unwrap())
418              .any(|s| excludes.contains(&s))
419     };
420
421     // Copy the directories using our filter
422     for item in &src_dirs {
423         let dst = &dst_src.join(item);
424         t!(fs::create_dir(dst));
425         cp_filtered(&build.src.join(item), dst, &filter_fn);
426     }
427     // Copy the files normally
428     for item in &src_files {
429         copy(&build.src.join(item), &dst_src.join(item));
430     }
431
432     // Get cargo-vendor installed, if it isn't already.
433     let mut has_cargo_vendor = false;
434     let mut cmd = Command::new(&build.cargo);
435     for line in output(cmd.arg("install").arg("--list")).lines() {
436         has_cargo_vendor |= line.starts_with("cargo-vendor ");
437     }
438     if !has_cargo_vendor {
439         let mut cmd = Command::new(&build.cargo);
440         cmd.arg("install")
441            .arg("--force")
442            .arg("--debug")
443            .arg("--vers").arg(CARGO_VENDOR_VERSION)
444            .arg("cargo-vendor")
445            .env("RUSTC", &build.rustc);
446         build.run(&mut cmd);
447     }
448
449     // Vendor all Cargo dependencies
450     let mut cmd = Command::new(&build.cargo);
451     cmd.arg("vendor")
452        .current_dir(&dst_src.join("src"));
453     build.run(&mut cmd);
454
455     // Create source tarball in rust-installer format
456     let mut cmd = Command::new(SH_CMD);
457     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
458        .arg("--product-name=Rust")
459        .arg("--rel-manifest-dir=rustlib")
460        .arg("--success-message=Awesome-Source.")
461        .arg(format!("--image-dir={}", sanitize_sh(&image)))
462        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
463        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
464        .arg(format!("--package-name={}", name))
465        .arg("--component-name=rust-src")
466        .arg("--legacy-manifest-dirs=rustlib,cargo");
467     build.run(&mut cmd);
468
469     // Rename directory, so that root folder of tarball has the correct name
470     let plain_name = format!("rustc-{}-src", build.rust_package_vers());
471     let plain_dst_src = tmpdir(build).join(&plain_name);
472     let _ = fs::remove_dir_all(&plain_dst_src);
473     t!(fs::create_dir_all(&plain_dst_src));
474     cp_r(&dst_src, &plain_dst_src);
475
476     // Create the version file
477     write_file(&plain_dst_src.join("version"), build.rust_version().as_bytes());
478
479     // Create plain source tarball
480     let mut cmd = Command::new("tar");
481     cmd.arg("-czf").arg(sanitize_sh(&rust_src_location(build)))
482        .arg(&plain_name)
483        .current_dir(tmpdir(build));
484     build.run(&mut cmd);
485
486     t!(fs::remove_dir_all(&image));
487     t!(fs::remove_dir_all(&plain_dst_src));
488 }
489
490 fn install(src: &Path, dstdir: &Path, perms: u32) {
491     let dst = dstdir.join(src.file_name().unwrap());
492     t!(fs::create_dir_all(dstdir));
493     t!(fs::copy(src, &dst));
494     chmod(&dst, perms);
495 }
496
497 #[cfg(unix)]
498 fn chmod(path: &Path, perms: u32) {
499     use std::os::unix::fs::*;
500     t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
501 }
502 #[cfg(windows)]
503 fn chmod(_path: &Path, _perms: u32) {}
504
505 // We have to run a few shell scripts, which choke quite a bit on both `\`
506 // characters and on `C:\` paths, so normalize both of them away.
507 pub fn sanitize_sh(path: &Path) -> String {
508     let path = path.to_str().unwrap().replace("\\", "/");
509     return change_drive(&path).unwrap_or(path);
510
511     fn change_drive(s: &str) -> Option<String> {
512         let mut ch = s.chars();
513         let drive = ch.next().unwrap_or('C');
514         if ch.next() != Some(':') {
515             return None
516         }
517         if ch.next() != Some('/') {
518             return None
519         }
520         Some(format!("/{}/{}", drive, &s[drive.len_utf8() + 2..]))
521     }
522 }
523
524 fn write_file(path: &Path, data: &[u8]) {
525     let mut vf = t!(fs::File::create(path));
526     t!(vf.write_all(data));
527 }
528
529 pub fn cargo(build: &Build, stage: u32, target: &str) {
530     println!("Dist cargo stage{} ({})", stage, target);
531     let compiler = Compiler::new(stage, &build.config.build);
532
533     let src = build.src.join("cargo");
534     let etc = src.join("src/etc");
535     let release_num = build.cargo_release_num();
536     let name = format!("cargo-{}", build.package_vers(&release_num));
537     let version = build.cargo_info.version(build, &release_num);
538
539     let tmp = tmpdir(build);
540     let image = tmp.join("cargo-image");
541     drop(fs::remove_dir_all(&image));
542     t!(fs::create_dir_all(&image));
543
544     // Prepare the image directory
545     t!(fs::create_dir_all(image.join("share/zsh/site-functions")));
546     t!(fs::create_dir_all(image.join("etc/bash_completions.d")));
547     let cargo = build.cargo_out(&compiler, Mode::Tool, target)
548                      .join(exe("cargo", target));
549     install(&cargo, &image.join("bin"), 0o755);
550     for man in t!(etc.join("man").read_dir()) {
551         let man = t!(man);
552         install(&man.path(), &image.join("share/man/man1"), 0o644);
553     }
554     install(&etc.join("_cargo"), &image.join("share/zsh/site-functions"), 0o644);
555     copy(&etc.join("cargo.bashcomp.sh"),
556          &image.join("etc/bash_completions.d/cargo"));
557     let doc = image.join("share/doc/cargo");
558     install(&src.join("README.md"), &doc, 0o644);
559     install(&src.join("LICENSE-MIT"), &doc, 0o644);
560     install(&src.join("LICENSE-APACHE"), &doc, 0o644);
561     install(&src.join("LICENSE-THIRD-PARTY"), &doc, 0o644);
562
563     // Prepare the overlay
564     let overlay = tmp.join("cargo-overlay");
565     drop(fs::remove_dir_all(&overlay));
566     t!(fs::create_dir_all(&overlay));
567     install(&src.join("README.md"), &overlay, 0o644);
568     install(&src.join("LICENSE-MIT"), &overlay, 0o644);
569     install(&src.join("LICENSE-APACHE"), &overlay, 0o644);
570     install(&src.join("LICENSE-THIRD-PARTY"), &overlay, 0o644);
571     t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes()));
572
573     // Generate the installer tarball
574     let mut cmd = Command::new("sh");
575     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
576        .arg("--product-name=Rust")
577        .arg("--rel-manifest-dir=rustlib")
578        .arg("--success-message=Rust-is-ready-to-roll.")
579        .arg(format!("--image-dir={}", sanitize_sh(&image)))
580        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
581        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
582        .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay)))
583        .arg(format!("--package-name={}-{}", name, target))
584        .arg("--component-name=cargo")
585        .arg("--legacy-manifest-dirs=rustlib,cargo");
586     build.run(&mut cmd);
587 }
588
589 /// Creates a combined installer for the specified target in the provided stage.
590 pub fn extended(build: &Build, stage: u32, target: &str) {
591     println!("Dist extended stage{} ({})", stage, target);
592
593     let dist = distdir(build);
594     let cargo_vers = build.cargo_release_num();
595     let rustc_installer = dist.join(format!("{}-{}.tar.gz",
596                                             pkgname(build, "rustc"),
597                                             target));
598     let cargo_installer = dist.join(format!("cargo-{}-{}.tar.gz",
599                                             build.package_vers(&cargo_vers),
600                                             target));
601     let docs_installer = dist.join(format!("{}-{}.tar.gz",
602                                            pkgname(build, "rust-docs"),
603                                            target));
604     let mingw_installer = dist.join(format!("{}-{}.tar.gz",
605                                             pkgname(build, "rust-mingw"),
606                                             target));
607     let std_installer = dist.join(format!("{}-{}.tar.gz",
608                                           pkgname(build, "rust-std"),
609                                           target));
610
611     let tmp = tmpdir(build);
612     let overlay = tmp.join("extended-overlay");
613     let etc = build.src.join("src/etc/installer");
614     let work = tmp.join("work");
615
616     let _ = fs::remove_dir_all(&overlay);
617     install(&build.src.join("COPYRIGHT"), &overlay, 0o644);
618     install(&build.src.join("LICENSE-APACHE"), &overlay, 0o644);
619     install(&build.src.join("LICENSE-MIT"), &overlay, 0o644);
620     let version = build.rust_version();
621     t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes()));
622     install(&etc.join("README.md"), &overlay, 0o644);
623
624     // When rust-std package split from rustc, we needed to ensure that during
625     // upgrades rustc was upgraded before rust-std. To avoid rustc clobbering
626     // the std files during uninstall. To do this ensure that rustc comes
627     // before rust-std in the list below.
628     let mut input_tarballs = format!("{},{},{},{}",
629                                      sanitize_sh(&rustc_installer),
630                                      sanitize_sh(&cargo_installer),
631                                      sanitize_sh(&docs_installer),
632                                      sanitize_sh(&std_installer));
633     if target.contains("pc-windows-gnu") {
634         input_tarballs.push_str(",");
635         input_tarballs.push_str(&sanitize_sh(&mingw_installer));
636     }
637
638     let mut cmd = Command::new(SH_CMD);
639     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/combine-installers.sh")))
640        .arg("--product-name=Rust")
641        .arg("--rel-manifest-dir=rustlib")
642        .arg("--success-message=Rust-is-ready-to-roll.")
643        .arg(format!("--work-dir={}", sanitize_sh(&work)))
644        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
645        .arg(format!("--package-name={}-{}", pkgname(build, "rust"), target))
646        .arg("--legacy-manifest-dirs=rustlib,cargo")
647        .arg(format!("--input-tarballs={}", input_tarballs))
648        .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay)));
649     build.run(&mut cmd);
650
651     let mut license = String::new();
652     t!(t!(File::open(build.src.join("COPYRIGHT"))).read_to_string(&mut license));
653     license.push_str("\n");
654     t!(t!(File::open(build.src.join("LICENSE-APACHE"))).read_to_string(&mut license));
655     license.push_str("\n");
656     t!(t!(File::open(build.src.join("LICENSE-MIT"))).read_to_string(&mut license));
657
658     let rtf = r"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Arial;}}\nowwrap\fs18";
659     let mut rtf = rtf.to_string();
660     rtf.push_str("\n");
661     for line in license.lines() {
662         rtf.push_str(line);
663         rtf.push_str("\\line ");
664     }
665     rtf.push_str("}");
666
667     if target.contains("apple-darwin") {
668         let pkg = tmp.join("pkg");
669         let _ = fs::remove_dir_all(&pkg);
670         t!(fs::create_dir_all(pkg.join("rustc")));
671         t!(fs::create_dir_all(pkg.join("cargo")));
672         t!(fs::create_dir_all(pkg.join("rust-docs")));
673         t!(fs::create_dir_all(pkg.join("rust-std")));
674
675         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rustc"), target)),
676              &pkg.join("rustc"));
677         cp_r(&work.join(&format!("cargo-nightly-{}", target)),
678              &pkg.join("cargo"));
679         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-docs"), target)),
680              &pkg.join("rust-docs"));
681         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-std"), target)),
682              &pkg.join("rust-std"));
683
684         install(&etc.join("pkg/postinstall"), &pkg.join("rustc"), 0o755);
685         install(&etc.join("pkg/postinstall"), &pkg.join("cargo"), 0o755);
686         install(&etc.join("pkg/postinstall"), &pkg.join("rust-docs"), 0o755);
687         install(&etc.join("pkg/postinstall"), &pkg.join("rust-std"), 0o755);
688
689         let pkgbuild = |component: &str| {
690             let mut cmd = Command::new("pkgbuild");
691             cmd.arg("--identifier").arg(format!("org.rust-lang.{}", component))
692                .arg("--scripts").arg(pkg.join(component))
693                .arg("--nopayload")
694                .arg(pkg.join(component).with_extension("pkg"));
695             build.run(&mut cmd);
696         };
697         pkgbuild("rustc");
698         pkgbuild("cargo");
699         pkgbuild("rust-docs");
700         pkgbuild("rust-std");
701
702         // create an 'uninstall' package
703         install(&etc.join("pkg/postinstall"), &pkg.join("uninstall"), 0o755);
704         pkgbuild("uninstall");
705
706         t!(fs::create_dir_all(pkg.join("res")));
707         t!(t!(File::create(pkg.join("res/LICENSE.txt"))).write_all(license.as_bytes()));
708         install(&etc.join("gfx/rust-logo.png"), &pkg.join("res"), 0o644);
709         let mut cmd = Command::new("productbuild");
710         cmd.arg("--distribution").arg(etc.join("pkg/Distribution.xml"))
711            .arg("--resources").arg(pkg.join("res"))
712            .arg(distdir(build).join(format!("{}-{}.pkg",
713                                              pkgname(build, "rust"),
714                                              target)))
715            .arg("--package-path").arg(&pkg);
716         build.run(&mut cmd);
717     }
718
719     if target.contains("windows") {
720         let exe = tmp.join("exe");
721         let _ = fs::remove_dir_all(&exe);
722         t!(fs::create_dir_all(exe.join("rustc")));
723         t!(fs::create_dir_all(exe.join("cargo")));
724         t!(fs::create_dir_all(exe.join("rust-docs")));
725         t!(fs::create_dir_all(exe.join("rust-std")));
726         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rustc"), target))
727                   .join("rustc"),
728              &exe.join("rustc"));
729         cp_r(&work.join(&format!("cargo-nightly-{}", target))
730                   .join("cargo"),
731              &exe.join("cargo"));
732         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-docs"), target))
733                   .join("rust-docs"),
734              &exe.join("rust-docs"));
735         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-std"), target))
736                   .join(format!("rust-std-{}", target)),
737              &exe.join("rust-std"));
738
739         t!(fs::remove_file(exe.join("rustc/manifest.in")));
740         t!(fs::remove_file(exe.join("cargo/manifest.in")));
741         t!(fs::remove_file(exe.join("rust-docs/manifest.in")));
742         t!(fs::remove_file(exe.join("rust-std/manifest.in")));
743
744         if target.contains("windows-gnu") {
745             t!(fs::create_dir_all(exe.join("rust-mingw")));
746             cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-mingw"), target))
747                       .join("rust-mingw"),
748                  &exe.join("rust-mingw"));
749             t!(fs::remove_file(exe.join("rust-mingw/manifest.in")));
750         }
751
752         install(&etc.join("exe/rust.iss"), &exe, 0o644);
753         install(&etc.join("exe/modpath.iss"), &exe, 0o644);
754         install(&etc.join("exe/upgrade.iss"), &exe, 0o644);
755         install(&etc.join("gfx/rust-logo.ico"), &exe, 0o644);
756         t!(t!(File::create(exe.join("LICENSE.txt"))).write_all(license.as_bytes()));
757
758         // Generate exe installer
759         let mut cmd = Command::new("iscc");
760         cmd.arg("rust.iss")
761            .current_dir(&exe);
762         if target.contains("windows-gnu") {
763             cmd.arg("/dMINGW");
764         }
765         add_env(build, &mut cmd, target);
766         build.run(&mut cmd);
767         install(&exe.join(format!("{}-{}.exe", pkgname(build, "rust"), target)),
768                 &distdir(build),
769                 0o755);
770
771         // Generate msi installer
772         let wix = PathBuf::from(env::var_os("WIX").unwrap());
773         let heat = wix.join("bin/heat.exe");
774         let candle = wix.join("bin/candle.exe");
775         let light = wix.join("bin/light.exe");
776
777         let heat_flags = ["-nologo", "-gg", "-sfrag", "-srd", "-sreg"];
778         build.run(Command::new(&heat)
779                         .current_dir(&exe)
780                         .arg("dir")
781                         .arg("rustc")
782                         .args(&heat_flags)
783                         .arg("-cg").arg("RustcGroup")
784                         .arg("-dr").arg("Rustc")
785                         .arg("-var").arg("var.RustcDir")
786                         .arg("-out").arg(exe.join("RustcGroup.wxs")));
787         build.run(Command::new(&heat)
788                         .current_dir(&exe)
789                         .arg("dir")
790                         .arg("rust-docs")
791                         .args(&heat_flags)
792                         .arg("-cg").arg("DocsGroup")
793                         .arg("-dr").arg("Docs")
794                         .arg("-var").arg("var.DocsDir")
795                         .arg("-out").arg(exe.join("DocsGroup.wxs"))
796                         .arg("-t").arg(etc.join("msi/squash-components.xsl")));
797         build.run(Command::new(&heat)
798                         .current_dir(&exe)
799                         .arg("dir")
800                         .arg("cargo")
801                         .args(&heat_flags)
802                         .arg("-cg").arg("CargoGroup")
803                         .arg("-dr").arg("Cargo")
804                         .arg("-var").arg("var.CargoDir")
805                         .arg("-out").arg(exe.join("CargoGroup.wxs"))
806                         .arg("-t").arg(etc.join("msi/remove-duplicates.xsl")));
807         build.run(Command::new(&heat)
808                         .current_dir(&exe)
809                         .arg("dir")
810                         .arg("rust-std")
811                         .args(&heat_flags)
812                         .arg("-cg").arg("StdGroup")
813                         .arg("-dr").arg("Std")
814                         .arg("-var").arg("var.StdDir")
815                         .arg("-out").arg(exe.join("StdGroup.wxs")));
816         if target.contains("windows-gnu") {
817             build.run(Command::new(&heat)
818                             .current_dir(&exe)
819                             .arg("dir")
820                             .arg("rust-mingw")
821                             .args(&heat_flags)
822                             .arg("-cg").arg("GccGroup")
823                             .arg("-dr").arg("Gcc")
824                             .arg("-var").arg("var.GccDir")
825                             .arg("-out").arg(exe.join("GccGroup.wxs")));
826         }
827
828         let candle = |input: &Path| {
829             let output = exe.join(input.file_stem().unwrap())
830                             .with_extension("wixobj");
831             let arch = if target.contains("x86_64") {"x64"} else {"x86"};
832             let mut cmd = Command::new(&candle);
833             cmd.current_dir(&exe)
834                .arg("-nologo")
835                .arg("-dRustcDir=rustc")
836                .arg("-dDocsDir=rust-docs")
837                .arg("-dCargoDir=cargo")
838                .arg("-dStdDir=rust-std")
839                .arg("-arch").arg(&arch)
840                .arg("-out").arg(&output)
841                .arg(&input);
842             add_env(build, &mut cmd, target);
843
844             if target.contains("windows-gnu") {
845                cmd.arg("-dGccDir=rust-mingw");
846             }
847             build.run(&mut cmd);
848         };
849         candle(&etc.join("msi/rust.wxs"));
850         candle(&etc.join("msi/ui.wxs"));
851         candle(&etc.join("msi/rustwelcomedlg.wxs"));
852         candle("RustcGroup.wxs".as_ref());
853         candle("DocsGroup.wxs".as_ref());
854         candle("CargoGroup.wxs".as_ref());
855         candle("StdGroup.wxs".as_ref());
856
857         if target.contains("windows-gnu") {
858             candle("GccGroup.wxs".as_ref());
859         }
860
861         t!(t!(File::create(exe.join("LICENSE.rtf"))).write_all(rtf.as_bytes()));
862         install(&etc.join("gfx/banner.bmp"), &exe, 0o644);
863         install(&etc.join("gfx/dialogbg.bmp"), &exe, 0o644);
864
865         let filename = format!("{}-{}.msi", pkgname(build, "rust"), target);
866         let mut cmd = Command::new(&light);
867         cmd.arg("-nologo")
868            .arg("-ext").arg("WixUIExtension")
869            .arg("-ext").arg("WixUtilExtension")
870            .arg("-out").arg(exe.join(&filename))
871            .arg("rust.wixobj")
872            .arg("ui.wixobj")
873            .arg("rustwelcomedlg.wixobj")
874            .arg("RustcGroup.wixobj")
875            .arg("DocsGroup.wixobj")
876            .arg("CargoGroup.wixobj")
877            .arg("StdGroup.wixobj")
878            .current_dir(&exe);
879
880         if target.contains("windows-gnu") {
881            cmd.arg("GccGroup.wixobj");
882         }
883         // ICE57 wrongly complains about the shortcuts
884         cmd.arg("-sice:ICE57");
885
886         build.run(&mut cmd);
887
888         t!(fs::rename(exe.join(&filename), distdir(build).join(&filename)));
889     }
890 }
891
892 fn add_env(build: &Build, cmd: &mut Command, target: &str) {
893     let mut parts = channel::CFG_RELEASE_NUM.split('.');
894     cmd.env("CFG_RELEASE_INFO", build.rust_version())
895        .env("CFG_RELEASE_NUM", channel::CFG_RELEASE_NUM)
896        .env("CFG_RELEASE", build.rust_release())
897        .env("CFG_PRERELEASE_VERSION", channel::CFG_PRERELEASE_VERSION)
898        .env("CFG_VER_MAJOR", parts.next().unwrap())
899        .env("CFG_VER_MINOR", parts.next().unwrap())
900        .env("CFG_VER_PATCH", parts.next().unwrap())
901        .env("CFG_VER_BUILD", "0") // just needed to build
902        .env("CFG_PACKAGE_VERS", build.rust_package_vers())
903        .env("CFG_PACKAGE_NAME", pkgname(build, "rust"))
904        .env("CFG_BUILD", target)
905        .env("CFG_CHANNEL", &build.config.channel);
906
907     if target.contains("windows-gnu") {
908        cmd.env("CFG_MINGW", "1")
909           .env("CFG_ABI", "GNU");
910     } else {
911        cmd.env("CFG_MINGW", "0")
912           .env("CFG_ABI", "MSVC");
913     }
914
915     if target.contains("x86_64") {
916        cmd.env("CFG_PLATFORM", "x64");
917     } else {
918        cmd.env("CFG_PLATFORM", "x86");
919     }
920 }
921
922 pub fn hash_and_sign(build: &Build) {
923     let compiler = Compiler::new(0, &build.config.build);
924     let mut cmd = build.tool_cmd(&compiler, "build-manifest");
925     let sign = build.config.dist_sign_folder.as_ref().unwrap_or_else(|| {
926         panic!("\n\nfailed to specify `dist.sign-folder` in `config.toml`\n\n")
927     });
928     let addr = build.config.dist_upload_addr.as_ref().unwrap_or_else(|| {
929         panic!("\n\nfailed to specify `dist.upload-addr` in `config.toml`\n\n")
930     });
931     let file = build.config.dist_gpg_password_file.as_ref().unwrap_or_else(|| {
932         panic!("\n\nfailed to specify `dist.gpg-password-file` in `config.toml`\n\n")
933     });
934     let mut pass = String::new();
935     t!(t!(File::open(&file)).read_to_string(&mut pass));
936
937     let today = output(Command::new("date").arg("+%Y-%m-%d"));
938
939     cmd.arg(sign);
940     cmd.arg(distdir(build));
941     cmd.arg(today.trim());
942     cmd.arg(build.rust_package_vers());
943     cmd.arg(build.package_vers(&build.cargo_release_num()));
944     cmd.arg(addr);
945
946     t!(fs::create_dir_all(distdir(build)));
947
948     let mut child = t!(cmd.stdin(Stdio::piped()).spawn());
949     t!(child.stdin.take().unwrap().write_all(pass.as_bytes()));
950     let status = t!(child.wait());
951     assert!(status.success());
952 }