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