]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/dist.rs
Build an RLS package as part of the dist target
[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     if component == "cargo" {
41         format!("{}-{}", component, build.cargo_package_vers())
42     } else {
43         assert!(component.starts_with("rust"));
44         format!("{}-{}", component, build.rust_package_vers())
45     }
46 }
47
48 fn distdir(build: &Build) -> PathBuf {
49     build.out.join("dist")
50 }
51
52 pub fn tmpdir(build: &Build) -> PathBuf {
53     build.out.join("tmp/dist")
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 = Command::new(SH_CMD);
76     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
77        .arg("--product-name=Rust-Documentation")
78        .arg("--rel-manifest-dir=rustlib")
79        .arg("--success-message=Rust-documentation-is-installed.")
80        .arg(format!("--image-dir={}", sanitize_sh(&image)))
81        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
82        .arg(format!("--output-dir={}", sanitize_sh(&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 = Command::new(SH_CMD);
126     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
127        .arg("--product-name=Rust-MinGW")
128        .arg("--rel-manifest-dir=rustlib")
129        .arg("--success-message=Rust-MinGW-is-installed.")
130        .arg(format!("--image-dir={}", sanitize_sh(&image)))
131        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
132        .arg(format!("--output-dir={}", sanitize_sh(&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 = Command::new(SH_CMD);
192     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
193        .arg("--product-name=Rust")
194        .arg("--rel-manifest-dir=rustlib")
195        .arg("--success-message=Rust-is-ready-to-roll.")
196        .arg(format!("--image-dir={}", sanitize_sh(&image)))
197        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
198        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
199        .arg(format!("--non-installed-overlay={}", sanitize_sh(&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         // no debugger scripts
256     } else {
257         cp_debugger_script("debugger_pretty_printers_common.py");
258
259         // gdb debugger scripts
260         install(&build.src.join("src/etc/rust-gdb"), &sysroot.join("bin"),
261                 0o755);
262
263         cp_debugger_script("gdb_load_rust_pretty_printers.py");
264         cp_debugger_script("gdb_rust_pretty_printing.py");
265
266         // lldb debugger scripts
267         install(&build.src.join("src/etc/rust-lldb"), &sysroot.join("bin"),
268                 0o755);
269
270         cp_debugger_script("lldb_rust_formatters.py");
271     }
272 }
273
274 /// Creates the `rust-std` installer component as compiled by `compiler` for the
275 /// target `target`.
276 pub fn std(build: &Build, compiler: &Compiler, target: &str) {
277     println!("Dist std stage{} ({} -> {})", compiler.stage, compiler.host,
278              target);
279
280     // The only true set of target libraries came from the build triple, so
281     // let's reduce redundant work by only producing archives from that host.
282     if compiler.host != build.config.build {
283         println!("\tskipping, not a build host");
284         return
285     }
286
287     let name = pkgname(build, "rust-std");
288     let image = tmpdir(build).join(format!("{}-{}-image", name, target));
289     let _ = fs::remove_dir_all(&image);
290
291     let dst = image.join("lib/rustlib").join(target);
292     t!(fs::create_dir_all(&dst));
293     let src = build.sysroot(compiler).join("lib/rustlib");
294     cp_r(&src.join(target), &dst);
295
296     let mut cmd = Command::new(SH_CMD);
297     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
298        .arg("--product-name=Rust")
299        .arg("--rel-manifest-dir=rustlib")
300        .arg("--success-message=std-is-standing-at-the-ready.")
301        .arg(format!("--image-dir={}", sanitize_sh(&image)))
302        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
303        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
304        .arg(format!("--package-name={}-{}", name, target))
305        .arg(format!("--component-name=rust-std-{}", target))
306        .arg("--legacy-manifest-dirs=rustlib,cargo");
307     build.run(&mut cmd);
308     t!(fs::remove_dir_all(&image));
309 }
310
311 pub fn rust_src_location(build: &Build) -> PathBuf {
312     let plain_name = format!("rustc-{}-src", build.rust_package_vers());
313     distdir(build).join(&format!("{}.tar.gz", plain_name))
314 }
315
316 /// Creates a tarball of save-analysis metadata, if available.
317 pub fn analysis(build: &Build, compiler: &Compiler, target: &str) {
318     if !build.config.rust_save_analysis {
319         return
320     }
321
322     println!("Dist analysis");
323
324     if compiler.host != build.config.build {
325         println!("\tskipping, not a build host");
326         return
327     }
328
329     // Package save-analysis from stage1 if not doing a full bootstrap, as the
330     // stage2 artifacts is simply copied from stage1 in that case.
331     let compiler = if build.force_use_stage1(compiler, target) {
332         Compiler::new(1, compiler.host)
333     } else {
334         compiler.clone()
335     };
336
337     let name = pkgname(build, "rust-analysis");
338     let image = tmpdir(build).join(format!("{}-{}-image", name, target));
339
340     let src = build.stage_out(&compiler, Mode::Libstd).join(target).join("release").join("deps");
341
342     let image_src = src.join("save-analysis");
343     let dst = image.join("lib/rustlib").join(target).join("analysis");
344     t!(fs::create_dir_all(&dst));
345     println!("image_src: {:?}, dst: {:?}", image_src, dst);
346     cp_r(&image_src, &dst);
347
348     let mut cmd = Command::new(SH_CMD);
349     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
350        .arg("--product-name=Rust")
351        .arg("--rel-manifest-dir=rustlib")
352        .arg("--success-message=save-analysis-saved.")
353        .arg(format!("--image-dir={}", sanitize_sh(&image)))
354        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
355        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
356        .arg(format!("--package-name={}-{}", name, target))
357        .arg(format!("--component-name=rust-analysis-{}", target))
358        .arg("--legacy-manifest-dirs=rustlib,cargo");
359     build.run(&mut cmd);
360     t!(fs::remove_dir_all(&image));
361 }
362
363 const CARGO_VENDOR_VERSION: &'static str = "0.1.4";
364
365 /// Creates the `rust-src` installer component and the plain source tarball
366 pub fn rust_src(build: &Build) {
367     if !build.config.rust_dist_src {
368         return
369     }
370
371     println!("Dist src");
372
373     let name = pkgname(build, "rust-src");
374     let image = tmpdir(build).join(format!("{}-image", name));
375     let _ = fs::remove_dir_all(&image);
376
377     let dst = image.join("lib/rustlib/src");
378     let dst_src = dst.join("rust");
379     t!(fs::create_dir_all(&dst_src));
380
381     // This is the set of root paths which will become part of the source package
382     let src_files = [
383         "COPYRIGHT",
384         "LICENSE-APACHE",
385         "LICENSE-MIT",
386         "CONTRIBUTING.md",
387         "README.md",
388         "RELEASES.md",
389         "configure",
390         "x.py",
391     ];
392     let src_dirs = [
393         "man",
394         "src",
395         "cargo",
396         "rls",
397     ];
398
399     let filter_fn = move |path: &Path| {
400         let spath = match path.to_str() {
401             Some(path) => path,
402             None => return false,
403         };
404         if spath.ends_with("~") || spath.ends_with(".pyc") {
405             return false
406         }
407         if spath.contains("llvm/test") || spath.contains("llvm\\test") {
408             if spath.ends_with(".ll") ||
409                spath.ends_with(".td") ||
410                spath.ends_with(".s") {
411                 return false
412             }
413         }
414
415         let excludes = [
416             "CVS", "RCS", "SCCS", ".git", ".gitignore", ".gitmodules",
417             ".gitattributes", ".cvsignore", ".svn", ".arch-ids", "{arch}",
418             "=RELEASE-ID", "=meta-update", "=update", ".bzr", ".bzrignore",
419             ".bzrtags", ".hg", ".hgignore", ".hgrags", "_darcs",
420         ];
421         !path.iter()
422              .map(|s| s.to_str().unwrap())
423              .any(|s| excludes.contains(&s))
424     };
425
426     // Copy the directories using our filter
427     for item in &src_dirs {
428         let dst = &dst_src.join(item);
429         t!(fs::create_dir(dst));
430         cp_filtered(&build.src.join(item), dst, &filter_fn);
431     }
432     // Copy the files normally
433     for item in &src_files {
434         copy(&build.src.join(item), &dst_src.join(item));
435     }
436
437     // If we're building from git sources, we need to vendor a complete distribution.
438     if build.src_is_git {
439         // Get cargo-vendor installed, if it isn't already.
440         let mut has_cargo_vendor = false;
441         let mut cmd = Command::new(&build.cargo);
442         for line in output(cmd.arg("install").arg("--list")).lines() {
443             has_cargo_vendor |= line.starts_with("cargo-vendor ");
444         }
445         if !has_cargo_vendor {
446             let mut cmd = Command::new(&build.cargo);
447             cmd.arg("install")
448                .arg("--force")
449                .arg("--debug")
450                .arg("--vers").arg(CARGO_VENDOR_VERSION)
451                .arg("cargo-vendor")
452                .env("RUSTC", &build.rustc);
453             build.run(&mut cmd);
454         }
455
456         // Vendor all Cargo dependencies
457         let mut cmd = Command::new(&build.cargo);
458         cmd.arg("vendor")
459            .current_dir(&dst_src.join("src"));
460         build.run(&mut cmd);
461     }
462
463     // Create source tarball in rust-installer format
464     let mut cmd = Command::new(SH_CMD);
465     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
466        .arg("--product-name=Rust")
467        .arg("--rel-manifest-dir=rustlib")
468        .arg("--success-message=Awesome-Source.")
469        .arg(format!("--image-dir={}", sanitize_sh(&image)))
470        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
471        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
472        .arg(format!("--package-name={}", name))
473        .arg("--component-name=rust-src")
474        .arg("--legacy-manifest-dirs=rustlib,cargo");
475     build.run(&mut cmd);
476
477     // Rename directory, so that root folder of tarball has the correct name
478     let plain_name = format!("rustc-{}-src", build.rust_package_vers());
479     let plain_dst_src = tmpdir(build).join(&plain_name);
480     let _ = fs::remove_dir_all(&plain_dst_src);
481     t!(fs::create_dir_all(&plain_dst_src));
482     cp_r(&dst_src, &plain_dst_src);
483
484     // Create the version file
485     write_file(&plain_dst_src.join("version"), build.rust_version().as_bytes());
486
487     // Create plain source tarball
488     let mut cmd = Command::new("tar");
489     cmd.arg("-czf").arg(sanitize_sh(&rust_src_location(build)))
490        .arg(&plain_name)
491        .current_dir(tmpdir(build));
492     build.run(&mut cmd);
493
494     t!(fs::remove_dir_all(&image));
495     t!(fs::remove_dir_all(&plain_dst_src));
496 }
497
498 fn install(src: &Path, dstdir: &Path, perms: u32) {
499     let dst = dstdir.join(src.file_name().unwrap());
500     t!(fs::create_dir_all(dstdir));
501     t!(fs::copy(src, &dst));
502     chmod(&dst, perms);
503 }
504
505 #[cfg(unix)]
506 fn chmod(path: &Path, perms: u32) {
507     use std::os::unix::fs::*;
508     t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
509 }
510 #[cfg(windows)]
511 fn chmod(_path: &Path, _perms: u32) {}
512
513 // We have to run a few shell scripts, which choke quite a bit on both `\`
514 // characters and on `C:\` paths, so normalize both of them away.
515 pub fn sanitize_sh(path: &Path) -> String {
516     let path = path.to_str().unwrap().replace("\\", "/");
517     return change_drive(&path).unwrap_or(path);
518
519     fn change_drive(s: &str) -> Option<String> {
520         let mut ch = s.chars();
521         let drive = ch.next().unwrap_or('C');
522         if ch.next() != Some(':') {
523             return None
524         }
525         if ch.next() != Some('/') {
526             return None
527         }
528         Some(format!("/{}/{}", drive, &s[drive.len_utf8() + 2..]))
529     }
530 }
531
532 fn write_file(path: &Path, data: &[u8]) {
533     let mut vf = t!(fs::File::create(path));
534     t!(vf.write_all(data));
535 }
536
537 pub fn cargo(build: &Build, stage: u32, target: &str) {
538     println!("Dist cargo stage{} ({})", stage, target);
539     let compiler = Compiler::new(stage, &build.config.build);
540
541     let src = build.src.join("cargo");
542     let etc = src.join("src/etc");
543     let release_num = build.cargo_release_num();
544     let name = pkgname(build, "cargo");
545     let version = build.cargo_info.version(build, &release_num);
546
547     let tmp = tmpdir(build);
548     let image = tmp.join("cargo-image");
549     drop(fs::remove_dir_all(&image));
550     t!(fs::create_dir_all(&image));
551
552     // Prepare the image directory
553     t!(fs::create_dir_all(image.join("share/zsh/site-functions")));
554     t!(fs::create_dir_all(image.join("etc/bash_completions.d")));
555     let cargo = build.cargo_out(&compiler, Mode::Tool, target)
556                      .join(exe("cargo", target));
557     install(&cargo, &image.join("bin"), 0o755);
558     for man in t!(etc.join("man").read_dir()) {
559         let man = t!(man);
560         install(&man.path(), &image.join("share/man/man1"), 0o644);
561     }
562     install(&etc.join("_cargo"), &image.join("share/zsh/site-functions"), 0o644);
563     copy(&etc.join("cargo.bashcomp.sh"),
564          &image.join("etc/bash_completions.d/cargo"));
565     let doc = image.join("share/doc/cargo");
566     install(&src.join("README.md"), &doc, 0o644);
567     install(&src.join("LICENSE-MIT"), &doc, 0o644);
568     install(&src.join("LICENSE-APACHE"), &doc, 0o644);
569     install(&src.join("LICENSE-THIRD-PARTY"), &doc, 0o644);
570
571     // Prepare the overlay
572     let overlay = tmp.join("cargo-overlay");
573     drop(fs::remove_dir_all(&overlay));
574     t!(fs::create_dir_all(&overlay));
575     install(&src.join("README.md"), &overlay, 0o644);
576     install(&src.join("LICENSE-MIT"), &overlay, 0o644);
577     install(&src.join("LICENSE-APACHE"), &overlay, 0o644);
578     install(&src.join("LICENSE-THIRD-PARTY"), &overlay, 0o644);
579     t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes()));
580
581     // Generate the installer tarball
582     let mut cmd = Command::new("sh");
583     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
584        .arg("--product-name=Rust")
585        .arg("--rel-manifest-dir=rustlib")
586        .arg("--success-message=Rust-is-ready-to-roll.")
587        .arg(format!("--image-dir={}", sanitize_sh(&image)))
588        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
589        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
590        .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay)))
591        .arg(format!("--package-name={}-{}", name, target))
592        .arg("--component-name=cargo")
593        .arg("--legacy-manifest-dirs=rustlib,cargo");
594     build.run(&mut cmd);
595 }
596
597 pub fn rls(build: &Build, stage: u32, target: &str) {
598     println!("Dist RLS stage{} ({})", stage, target);
599     let compiler = Compiler::new(stage, &build.config.build);
600
601     let src = build.src.join("rls");
602     let release_num = build.rls_release_num();
603     let name = format!("rls-{}", build.package_vers(&release_num));
604
605     let tmp = tmpdir(build);
606     let image = tmp.join("rls-image");
607     drop(fs::remove_dir_all(&image));
608     t!(fs::create_dir_all(&image));
609
610     // Prepare the image directory
611     let rls = build.cargo_out(&compiler, Mode::Tool, target)
612                      .join(exe("rls", target));
613     install(&rls, &image.join("bin"), 0o755);
614     let doc = image.join("share/doc/rls");
615     install(&src.join("README.md"), &doc, 0o644);
616     install(&src.join("LICENSE-MIT"), &doc, 0o644);
617     install(&src.join("LICENSE-APACHE"), &doc, 0o644);
618
619     // Generate the installer tarball
620     let mut cmd = Command::new("sh");
621     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh")))
622        .arg("--product-name=Rust")
623        .arg("--rel-manifest-dir=rustlib")
624        .arg("--success-message=RLS-ready-to-serve.")
625        .arg(format!("--image-dir={}", sanitize_sh(&image)))
626        .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build))))
627        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
628        .arg(format!("--package-name={}-{}", name, target))
629        .arg("--component-name=rls")
630        .arg("--legacy-manifest-dirs=rustlib,cargo");
631     build.run(&mut cmd);
632 }
633
634 /// Creates a combined installer for the specified target in the provided stage.
635 pub fn extended(build: &Build, stage: u32, target: &str) {
636     println!("Dist extended stage{} ({})", stage, target);
637
638     let dist = distdir(build);
639     let rustc_installer = dist.join(format!("{}-{}.tar.gz",
640                                             pkgname(build, "rustc"),
641                                             target));
642     let cargo_installer = dist.join(format!("{}-{}.tar.gz",
643                                             pkgname(build, "cargo"),
644                                             target));
645     let rls_installer = dist.join(format!("{}.tar.gz",
646                                           pkgname(build, "rls")));
647     let analysis_installer = dist.join(format!("{}-{}.tar.gz",
648                                                pkgname(build, "rust-analysis"),
649                                                target));
650     let docs_installer = dist.join(format!("{}-{}.tar.gz",
651                                            pkgname(build, "rust-docs"),
652                                            target));
653     let mingw_installer = dist.join(format!("{}-{}.tar.gz",
654                                             pkgname(build, "rust-mingw"),
655                                             target));
656     let std_installer = dist.join(format!("{}-{}.tar.gz",
657                                           pkgname(build, "rust-std"),
658                                           target));
659
660     let tmp = tmpdir(build);
661     let overlay = tmp.join("extended-overlay");
662     let etc = build.src.join("src/etc/installer");
663     let work = tmp.join("work");
664
665     let _ = fs::remove_dir_all(&overlay);
666     install(&build.src.join("COPYRIGHT"), &overlay, 0o644);
667     install(&build.src.join("LICENSE-APACHE"), &overlay, 0o644);
668     install(&build.src.join("LICENSE-MIT"), &overlay, 0o644);
669     let version = build.rust_version();
670     t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes()));
671     install(&etc.join("README.md"), &overlay, 0o644);
672
673     // When rust-std package split from rustc, we needed to ensure that during
674     // upgrades rustc was upgraded before rust-std. To avoid rustc clobbering
675     // the std files during uninstall. To do this ensure that rustc comes
676     // before rust-std in the list below.
677     let mut input_tarballs = format!("{},{},{},{},{},{}",
678                                      sanitize_sh(&rustc_installer),
679                                      sanitize_sh(&cargo_installer),
680                                      sanitize_sh(&rls_installer),
681                                      sanitize_sh(&analysis_installer),
682                                      sanitize_sh(&docs_installer),
683                                      sanitize_sh(&std_installer));
684     if target.contains("pc-windows-gnu") {
685         input_tarballs.push_str(",");
686         input_tarballs.push_str(&sanitize_sh(&mingw_installer));
687     }
688
689     let mut cmd = Command::new(SH_CMD);
690     cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/combine-installers.sh")))
691        .arg("--product-name=Rust")
692        .arg("--rel-manifest-dir=rustlib")
693        .arg("--success-message=Rust-is-ready-to-roll.")
694        .arg(format!("--work-dir={}", sanitize_sh(&work)))
695        .arg(format!("--output-dir={}", sanitize_sh(&distdir(build))))
696        .arg(format!("--package-name={}-{}", pkgname(build, "rust"), target))
697        .arg("--legacy-manifest-dirs=rustlib,cargo")
698        .arg(format!("--input-tarballs={}", input_tarballs))
699        .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay)));
700     build.run(&mut cmd);
701
702     let mut license = String::new();
703     t!(t!(File::open(build.src.join("COPYRIGHT"))).read_to_string(&mut license));
704     license.push_str("\n");
705     t!(t!(File::open(build.src.join("LICENSE-APACHE"))).read_to_string(&mut license));
706     license.push_str("\n");
707     t!(t!(File::open(build.src.join("LICENSE-MIT"))).read_to_string(&mut license));
708
709     let rtf = r"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Arial;}}\nowwrap\fs18";
710     let mut rtf = rtf.to_string();
711     rtf.push_str("\n");
712     for line in license.lines() {
713         rtf.push_str(line);
714         rtf.push_str("\\line ");
715     }
716     rtf.push_str("}");
717
718     if target.contains("apple-darwin") {
719         let pkg = tmp.join("pkg");
720         let _ = fs::remove_dir_all(&pkg);
721         t!(fs::create_dir_all(pkg.join("rustc")));
722         t!(fs::create_dir_all(pkg.join("cargo")));
723         t!(fs::create_dir_all(pkg.join("rls")));
724         t!(fs::create_dir_all(pkg.join("rust-analysis")));
725         t!(fs::create_dir_all(pkg.join("rust-docs")));
726         t!(fs::create_dir_all(pkg.join("rust-std")));
727
728         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rustc"), target)),
729              &pkg.join("rustc"));
730         cp_r(&work.join(&format!("{}-{}", pkgname(build, "cargo"), target)),
731              &pkg.join("cargo"));
732         cp_r(&work.join(pkgname(build, "rls")),
733              &pkg.join("rls"));
734         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-analysis"), target)),
735              &pkg.join("rust-analysis"));
736         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-docs"), target)),
737              &pkg.join("rust-docs"));
738         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-std"), target)),
739              &pkg.join("rust-std"));
740
741         install(&etc.join("pkg/postinstall"), &pkg.join("rustc"), 0o755);
742         install(&etc.join("pkg/postinstall"), &pkg.join("cargo"), 0o755);
743         install(&etc.join("pkg/postinstall"), &pkg.join("rls"), 0o755);
744         install(&etc.join("pkg/postinstall"), &pkg.join("rust-analysis"), 0o755);
745         install(&etc.join("pkg/postinstall"), &pkg.join("rust-docs"), 0o755);
746         install(&etc.join("pkg/postinstall"), &pkg.join("rust-std"), 0o755);
747
748         let pkgbuild = |component: &str| {
749             let mut cmd = Command::new("pkgbuild");
750             cmd.arg("--identifier").arg(format!("org.rust-lang.{}", component))
751                .arg("--scripts").arg(pkg.join(component))
752                .arg("--nopayload")
753                .arg(pkg.join(component).with_extension("pkg"));
754             build.run(&mut cmd);
755         };
756         pkgbuild("rustc");
757         pkgbuild("cargo");
758         pkgbuild("rls");
759         pkgbuild("rust-analysis");
760         pkgbuild("rust-docs");
761         pkgbuild("rust-std");
762
763         // create an 'uninstall' package
764         install(&etc.join("pkg/postinstall"), &pkg.join("uninstall"), 0o755);
765         pkgbuild("uninstall");
766
767         t!(fs::create_dir_all(pkg.join("res")));
768         t!(t!(File::create(pkg.join("res/LICENSE.txt"))).write_all(license.as_bytes()));
769         install(&etc.join("gfx/rust-logo.png"), &pkg.join("res"), 0o644);
770         let mut cmd = Command::new("productbuild");
771         cmd.arg("--distribution").arg(etc.join("pkg/Distribution.xml"))
772            .arg("--resources").arg(pkg.join("res"))
773            .arg(distdir(build).join(format!("{}-{}.pkg",
774                                              pkgname(build, "rust"),
775                                              target)))
776            .arg("--package-path").arg(&pkg);
777         build.run(&mut cmd);
778     }
779
780     if target.contains("windows") {
781         let exe = tmp.join("exe");
782         let _ = fs::remove_dir_all(&exe);
783         t!(fs::create_dir_all(exe.join("rustc")));
784         t!(fs::create_dir_all(exe.join("cargo")));
785         t!(fs::create_dir_all(exe.join("rls")));
786         t!(fs::create_dir_all(exe.join("rust-analysis")));
787         t!(fs::create_dir_all(exe.join("rust-docs")));
788         t!(fs::create_dir_all(exe.join("rust-std")));
789         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rustc"), target))
790                   .join("rustc"),
791              &exe.join("rustc"));
792         cp_r(&work.join(&format!("{}-{}", pkgname(build, "cargo"), target))
793                   .join("cargo"),
794              &exe.join("cargo"));
795         cp_r(&work.join(pkgname(build, "rls"))
796                   .join("rls"),
797              &exe.join("rls"));
798         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-analysis"), target))
799                   .join("rust-analysis"),
800              &exe.join("rust-analysis"));
801         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-docs"), target))
802                   .join("rust-docs"),
803              &exe.join("rust-docs"));
804         cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-std"), target))
805                   .join(format!("rust-std-{}", target)),
806              &exe.join("rust-std"));
807
808         t!(fs::remove_file(exe.join("rustc/manifest.in")));
809         t!(fs::remove_file(exe.join("cargo/manifest.in")));
810         t!(fs::remove_file(exe.join("rls/manifest.in")));
811         t!(fs::remove_file(exe.join("rust-analysis/manifest.in")));
812         t!(fs::remove_file(exe.join("rust-docs/manifest.in")));
813         t!(fs::remove_file(exe.join("rust-std/manifest.in")));
814
815         if target.contains("windows-gnu") {
816             t!(fs::create_dir_all(exe.join("rust-mingw")));
817             cp_r(&work.join(&format!("{}-{}", pkgname(build, "rust-mingw"), target))
818                       .join("rust-mingw"),
819                  &exe.join("rust-mingw"));
820             t!(fs::remove_file(exe.join("rust-mingw/manifest.in")));
821         }
822
823         install(&etc.join("exe/rust.iss"), &exe, 0o644);
824         install(&etc.join("exe/modpath.iss"), &exe, 0o644);
825         install(&etc.join("exe/upgrade.iss"), &exe, 0o644);
826         install(&etc.join("gfx/rust-logo.ico"), &exe, 0o644);
827         t!(t!(File::create(exe.join("LICENSE.txt"))).write_all(license.as_bytes()));
828
829         // Generate exe installer
830         let mut cmd = Command::new("iscc");
831         cmd.arg("rust.iss")
832            .current_dir(&exe);
833         if target.contains("windows-gnu") {
834             cmd.arg("/dMINGW");
835         }
836         add_env(build, &mut cmd, target);
837         build.run(&mut cmd);
838         install(&exe.join(format!("{}-{}.exe", pkgname(build, "rust"), target)),
839                 &distdir(build),
840                 0o755);
841
842         // Generate msi installer
843         let wix = PathBuf::from(env::var_os("WIX").unwrap());
844         let heat = wix.join("bin/heat.exe");
845         let candle = wix.join("bin/candle.exe");
846         let light = wix.join("bin/light.exe");
847
848         let heat_flags = ["-nologo", "-gg", "-sfrag", "-srd", "-sreg"];
849         build.run(Command::new(&heat)
850                         .current_dir(&exe)
851                         .arg("dir")
852                         .arg("rustc")
853                         .args(&heat_flags)
854                         .arg("-cg").arg("RustcGroup")
855                         .arg("-dr").arg("Rustc")
856                         .arg("-var").arg("var.RustcDir")
857                         .arg("-out").arg(exe.join("RustcGroup.wxs")));
858         build.run(Command::new(&heat)
859                         .current_dir(&exe)
860                         .arg("dir")
861                         .arg("rust-docs")
862                         .args(&heat_flags)
863                         .arg("-cg").arg("DocsGroup")
864                         .arg("-dr").arg("Docs")
865                         .arg("-var").arg("var.DocsDir")
866                         .arg("-out").arg(exe.join("DocsGroup.wxs"))
867                         .arg("-t").arg(etc.join("msi/squash-components.xsl")));
868         build.run(Command::new(&heat)
869                         .current_dir(&exe)
870                         .arg("dir")
871                         .arg("rls")
872                         .args(&heat_flags)
873                         .arg("-cg").arg("RlsGroup")
874                         .arg("-dr").arg("Rls")
875                         .arg("-var").arg("var.RlsDir")
876                         .arg("-out").arg(exe.join("RlsGroup.wxs"))
877                         .arg("-t").arg(etc.join("msi/squash-components.xsl")));
878         build.run(Command::new(&heat)
879                         .current_dir(&exe)
880                         .arg("dir")
881                         .arg("rust-analysis")
882                         .args(&heat_flags)
883                         .arg("-cg").arg("AnalysisGroup")
884                         .arg("-dr").arg("Analysis")
885                         .arg("-var").arg("var.AnalysisDir")
886                         .arg("-out").arg(exe.join("AnalysisGroup.wxs"))
887                         .arg("-t").arg(etc.join("msi/squash-components.xsl")));
888         build.run(Command::new(&heat)
889                         .current_dir(&exe)
890                         .arg("dir")
891                         .arg("cargo")
892                         .args(&heat_flags)
893                         .arg("-cg").arg("CargoGroup")
894                         .arg("-dr").arg("Cargo")
895                         .arg("-var").arg("var.CargoDir")
896                         .arg("-out").arg(exe.join("CargoGroup.wxs"))
897                         .arg("-t").arg(etc.join("msi/remove-duplicates.xsl")));
898         build.run(Command::new(&heat)
899                         .current_dir(&exe)
900                         .arg("dir")
901                         .arg("rust-std")
902                         .args(&heat_flags)
903                         .arg("-cg").arg("StdGroup")
904                         .arg("-dr").arg("Std")
905                         .arg("-var").arg("var.StdDir")
906                         .arg("-out").arg(exe.join("StdGroup.wxs")));
907         if target.contains("windows-gnu") {
908             build.run(Command::new(&heat)
909                             .current_dir(&exe)
910                             .arg("dir")
911                             .arg("rust-mingw")
912                             .args(&heat_flags)
913                             .arg("-cg").arg("GccGroup")
914                             .arg("-dr").arg("Gcc")
915                             .arg("-var").arg("var.GccDir")
916                             .arg("-out").arg(exe.join("GccGroup.wxs")));
917         }
918
919         let candle = |input: &Path| {
920             let output = exe.join(input.file_stem().unwrap())
921                             .with_extension("wixobj");
922             let arch = if target.contains("x86_64") {"x64"} else {"x86"};
923             let mut cmd = Command::new(&candle);
924             cmd.current_dir(&exe)
925                .arg("-nologo")
926                .arg("-dRustcDir=rustc")
927                .arg("-dDocsDir=rust-docs")
928                .arg("-dRlsDir=rls")
929                .arg("-dAnalysisDir=rust-analysis")
930                .arg("-dCargoDir=cargo")
931                .arg("-dStdDir=rust-std")
932                .arg("-arch").arg(&arch)
933                .arg("-out").arg(&output)
934                .arg(&input);
935             add_env(build, &mut cmd, target);
936
937             if target.contains("windows-gnu") {
938                cmd.arg("-dGccDir=rust-mingw");
939             }
940             build.run(&mut cmd);
941         };
942         candle(&etc.join("msi/rust.wxs"));
943         candle(&etc.join("msi/ui.wxs"));
944         candle(&etc.join("msi/rustwelcomedlg.wxs"));
945         candle("RustcGroup.wxs".as_ref());
946         candle("DocsGroup.wxs".as_ref());
947         candle("RlsGroup.wxs".as_ref());
948         candle("AnalysisGroup.wxs".as_ref());
949         candle("CargoGroup.wxs".as_ref());
950         candle("StdGroup.wxs".as_ref());
951
952         if target.contains("windows-gnu") {
953             candle("GccGroup.wxs".as_ref());
954         }
955
956         t!(t!(File::create(exe.join("LICENSE.rtf"))).write_all(rtf.as_bytes()));
957         install(&etc.join("gfx/banner.bmp"), &exe, 0o644);
958         install(&etc.join("gfx/dialogbg.bmp"), &exe, 0o644);
959
960         let filename = format!("{}-{}.msi", pkgname(build, "rust"), target);
961         let mut cmd = Command::new(&light);
962         cmd.arg("-nologo")
963            .arg("-ext").arg("WixUIExtension")
964            .arg("-ext").arg("WixUtilExtension")
965            .arg("-out").arg(exe.join(&filename))
966            .arg("rust.wixobj")
967            .arg("ui.wixobj")
968            .arg("rustwelcomedlg.wixobj")
969            .arg("RustcGroup.wixobj")
970            .arg("DocsGroup.wixobj")
971            .arg("RlsGroup.wixobj")
972            .arg("AnalysisGroup.wixobj")
973            .arg("CargoGroup.wixobj")
974            .arg("StdGroup.wixobj")
975            .current_dir(&exe);
976
977         if target.contains("windows-gnu") {
978            cmd.arg("GccGroup.wixobj");
979         }
980         // ICE57 wrongly complains about the shortcuts
981         cmd.arg("-sice:ICE57");
982
983         build.run(&mut cmd);
984
985         t!(fs::rename(exe.join(&filename), distdir(build).join(&filename)));
986     }
987 }
988
989 fn add_env(build: &Build, cmd: &mut Command, target: &str) {
990     let mut parts = channel::CFG_RELEASE_NUM.split('.');
991     cmd.env("CFG_RELEASE_INFO", build.rust_version())
992        .env("CFG_RELEASE_NUM", channel::CFG_RELEASE_NUM)
993        .env("CFG_RELEASE", build.rust_release())
994        .env("CFG_PRERELEASE_VERSION", channel::CFG_PRERELEASE_VERSION)
995        .env("CFG_VER_MAJOR", parts.next().unwrap())
996        .env("CFG_VER_MINOR", parts.next().unwrap())
997        .env("CFG_VER_PATCH", parts.next().unwrap())
998        .env("CFG_VER_BUILD", "0") // just needed to build
999        .env("CFG_PACKAGE_VERS", build.rust_package_vers())
1000        .env("CFG_PACKAGE_NAME", pkgname(build, "rust"))
1001        .env("CFG_BUILD", target)
1002        .env("CFG_CHANNEL", &build.config.channel);
1003
1004     if target.contains("windows-gnu") {
1005        cmd.env("CFG_MINGW", "1")
1006           .env("CFG_ABI", "GNU");
1007     } else {
1008        cmd.env("CFG_MINGW", "0")
1009           .env("CFG_ABI", "MSVC");
1010     }
1011
1012     if target.contains("x86_64") {
1013        cmd.env("CFG_PLATFORM", "x64");
1014     } else {
1015        cmd.env("CFG_PLATFORM", "x86");
1016     }
1017 }
1018
1019 pub fn hash_and_sign(build: &Build) {
1020     let compiler = Compiler::new(0, &build.config.build);
1021     let mut cmd = build.tool_cmd(&compiler, "build-manifest");
1022     let sign = build.config.dist_sign_folder.as_ref().unwrap_or_else(|| {
1023         panic!("\n\nfailed to specify `dist.sign-folder` in `config.toml`\n\n")
1024     });
1025     let addr = build.config.dist_upload_addr.as_ref().unwrap_or_else(|| {
1026         panic!("\n\nfailed to specify `dist.upload-addr` in `config.toml`\n\n")
1027     });
1028     let file = build.config.dist_gpg_password_file.as_ref().unwrap_or_else(|| {
1029         panic!("\n\nfailed to specify `dist.gpg-password-file` in `config.toml`\n\n")
1030     });
1031     let mut pass = String::new();
1032     t!(t!(File::open(&file)).read_to_string(&mut pass));
1033
1034     let today = output(Command::new("date").arg("+%Y-%m-%d"));
1035
1036     cmd.arg(sign);
1037     cmd.arg(distdir(build));
1038     cmd.arg(today.trim());
1039     cmd.arg(build.rust_package_vers());
1040     cmd.arg(build.package_vers(&build.cargo_release_num()));
1041     cmd.arg(addr);
1042
1043     t!(fs::create_dir_all(distdir(build)));
1044
1045     let mut child = t!(cmd.stdin(Stdio::piped()).spawn());
1046     t!(child.stdin.take().unwrap().write_all(pass.as_bytes()));
1047     let status = t!(child.wait());
1048     assert!(status.success());
1049 }