]> git.lizzy.rs Git - rust.git/blobdiff - src/bootstrap/tool.rs
Clean tools after building libstd/libtest/librustc.
[rust.git] / src / bootstrap / tool.rs
index f32cddbafc3e70909e9e2ec7c7b0691f78d7962f..7ccd527b33874ab0c5de6af03e6f02f727544bd5 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::fs;
 use std::env;
 use std::path::PathBuf;
 use std::process::Command;
@@ -15,7 +16,7 @@
 use Mode;
 use Compiler;
 use builder::{Step, RunConfig, ShouldRun, Builder};
-use util::{exe, add_lib_path};
+use util::{copy, exe, add_lib_path};
 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
 use native;
 use channel::GitInfo;
@@ -23,7 +24,7 @@
 
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
 pub struct CleanTools {
-    pub stage: u32,
+    pub compiler: Compiler,
     pub target: Interned<String>,
     pub mode: Mode,
 }
@@ -41,12 +42,10 @@ fn should_run(run: ShouldRun) -> ShouldRun {
     /// `stage` into the normal cargo output directory.
     fn run(self, builder: &Builder) {
         let build = builder.build;
-        let stage = self.stage;
+        let compiler = self.compiler;
         let target = self.target;
         let mode = self.mode;
 
-        let compiler = builder.compiler(stage, build.build);
-
         let stamp = match mode {
             Mode::Libstd => libstd_stamp(build, compiler, target),
             Mode::Libtest => libtest_stamp(build, compiler, target),
@@ -59,11 +58,11 @@ fn run(self, builder: &Builder) {
 }
 
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
-pub struct ToolBuild {
-    pub stage: u32,
-    pub target: Interned<String>,
-    pub tool: &'static str,
-    pub mode: Mode,
+struct ToolBuild {
+    compiler: Compiler,
+    target: Interned<String>,
+    tool: &'static str,
+    mode: Mode,
 }
 
 impl Step for ToolBuild {
@@ -79,12 +78,10 @@ fn should_run(run: ShouldRun) -> ShouldRun {
     /// `stage` into the normal cargo output directory.
     fn run(self, builder: &Builder) -> PathBuf {
         let build = builder.build;
-        let stage = self.stage;
+        let compiler = self.compiler;
         let target = self.target;
         let tool = self.tool;
 
-        let compiler = builder.compiler(stage, build.build);
-        builder.ensure(CleanTools { stage, target, mode: self.mode });
         match self.mode {
             Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
             Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
@@ -92,39 +89,49 @@ fn run(self, builder: &Builder) -> PathBuf {
             Mode::Tool => panic!("unexpected Mode::Tool for tool build")
         }
 
-        let _folder = build.fold_output(|| format!("stage{}-{}", stage, tool));
-        println!("Building stage{} tool {} ({})", stage, tool, target);
+        let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
+        println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
 
-        let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
-        let dir = build.src.join("src/tools").join(tool);
-        cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
-
-        // We don't want to build tools dynamically as they'll be running across
-        // stages and such and it's just easier if they're not dynamically linked.
-        cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
-
-        if let Some(dir) = build.openssl_install_dir(target) {
-            cargo.env("OPENSSL_STATIC", "1");
-            cargo.env("OPENSSL_DIR", dir);
-            cargo.env("LIBZ_SYS_STATIC", "1");
-        }
+        let mut cargo = prepare_tool_cargo(builder, compiler, target, tool);
+        build.run(&mut cargo);
+        build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
+    }
+}
 
-        cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
+fn prepare_tool_cargo(
+    builder: &Builder,
+    compiler: Compiler,
+    target: Interned<String>,
+    tool: &'static str,
+) -> Command {
+    let build = builder.build;
+    let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
+    let dir = build.src.join("src/tools").join(tool);
+    cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
+
+    // We don't want to build tools dynamically as they'll be running across
+    // stages and such and it's just easier if they're not dynamically linked.
+    cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
+
+    if let Some(dir) = build.openssl_install_dir(target) {
+        cargo.env("OPENSSL_STATIC", "1");
+        cargo.env("OPENSSL_DIR", dir);
+        cargo.env("LIBZ_SYS_STATIC", "1");
+    }
 
-        let info = GitInfo::new(&dir);
-        if let Some(sha) = info.sha() {
-            cargo.env("CFG_COMMIT_HASH", sha);
-        }
-        if let Some(sha_short) = info.sha_short() {
-            cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
-        }
-        if let Some(date) = info.commit_date() {
-            cargo.env("CFG_COMMIT_DATE", date);
-        }
+    cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
 
-        build.run(&mut cargo);
-        build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
+    let info = GitInfo::new(&build.config, &dir);
+    if let Some(sha) = info.sha() {
+        cargo.env("CFG_COMMIT_HASH", sha);
+    }
+    if let Some(sha_short) = info.sha_short() {
+        cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
     }
+    if let Some(date) = info.commit_date() {
+        cargo.env("CFG_COMMIT_DATE", date);
+    }
+    cargo
 }
 
 macro_rules! tool {
@@ -141,7 +148,7 @@ pub fn tool_exe(&self, tool: Tool) -> PathBuf {
                 match tool {
                     $(Tool::$name =>
                         self.ensure($name {
-                            stage: 0,
+                            compiler: self.compiler(0, self.build.build),
                             target: self.build.build,
                         }),
                     )+
@@ -152,7 +159,7 @@ pub fn tool_exe(&self, tool: Tool) -> PathBuf {
         $(
             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
         pub struct $name {
-            pub stage: u32,
+            pub compiler: Compiler,
             pub target: Interned<String>,
         }
 
@@ -165,14 +172,14 @@ fn should_run(run: ShouldRun) -> ShouldRun {
 
             fn make_run(run: RunConfig) {
                 run.builder.ensure($name {
-                    stage: run.builder.top_stage,
+                    compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
                     target: run.target,
                 });
             }
 
             fn run(self, builder: &Builder) -> PathBuf {
                 builder.ensure(ToolBuild {
-                    stage: self.stage,
+                    compiler: self.compiler,
                     target: self.target,
                     tool: $tool_name,
                     mode: $mode,
@@ -198,7 +205,7 @@ fn run(self, builder: &Builder) -> PathBuf {
 
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
 pub struct RemoteTestServer {
-    pub stage: u32,
+    pub compiler: Compiler,
     pub target: Interned<String>,
 }
 
@@ -211,14 +218,14 @@ fn should_run(run: ShouldRun) -> ShouldRun {
 
     fn make_run(run: RunConfig) {
         run.builder.ensure(RemoteTestServer {
-            stage: run.builder.top_stage,
+            compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
             target: run.target,
         });
     }
 
     fn run(self, builder: &Builder) -> PathBuf {
         builder.ensure(ToolBuild {
-            stage: self.stage,
+            compiler: self.compiler,
             target: self.target,
             tool: "remote-test-server",
             mode: Mode::Libstd,
@@ -226,9 +233,71 @@ fn run(self, builder: &Builder) -> PathBuf {
     }
 }
 
+#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
+pub struct Rustdoc {
+    pub host: Interned<String>,
+}
+
+impl Step for Rustdoc {
+    type Output = PathBuf;
+    const DEFAULT: bool = true;
+    const ONLY_HOSTS: bool = true;
+
+    fn should_run(run: ShouldRun) -> ShouldRun {
+        run.path("src/tools/rustdoc")
+    }
+
+    fn make_run(run: RunConfig) {
+        run.builder.ensure(Rustdoc {
+            host: run.host,
+        });
+    }
+
+    fn run(self, builder: &Builder) -> PathBuf {
+        let build = builder.build;
+        let target_compiler = builder.compiler(builder.top_stage, self.host);
+        let target = target_compiler.host;
+        let build_compiler = if target_compiler.stage == 0 {
+            builder.compiler(0, builder.build.build)
+        } else if target_compiler.stage >= 2 {
+            // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
+            // building rustdoc itself.
+            target_compiler
+        } else {
+            // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
+            // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
+            // compilers, which isn't what we want.
+            builder.compiler(target_compiler.stage - 1, builder.build.build)
+        };
+
+        builder.ensure(compile::Rustc { compiler: build_compiler, target });
+
+        let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
+        println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
+
+        let mut cargo = prepare_tool_cargo(builder, build_compiler, target, "rustdoc");
+        build.run(&mut cargo);
+        let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
+            .join(exe("rustdoc", &target_compiler.host));
+
+        // don't create a stage0-sysroot/bin directory.
+        if target_compiler.stage > 0 {
+            let sysroot = builder.sysroot(target_compiler);
+            let bindir = sysroot.join("bin");
+            t!(fs::create_dir_all(&bindir));
+            let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
+            let _ = fs::remove_file(&bin_rustdoc);
+            copy(&tool_rustdoc, &bin_rustdoc);
+            bin_rustdoc
+        } else {
+            tool_rustdoc
+        }
+    }
+}
+
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
 pub struct Cargo {
-    pub stage: u32,
+    pub compiler: Compiler,
     pub target: Interned<String>,
 }
 
@@ -244,7 +313,7 @@ fn should_run(run: ShouldRun) -> ShouldRun {
 
     fn make_run(run: RunConfig) {
         run.builder.ensure(Cargo {
-            stage: run.builder.top_stage,
+            compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
             target: run.target,
         });
     }
@@ -256,11 +325,11 @@ fn run(self, builder: &Builder) -> PathBuf {
         // Cargo depends on procedural macros, which requires a full host
         // compiler to be available, so we need to depend on that.
         builder.ensure(compile::Rustc {
-            compiler: builder.compiler(self.stage, builder.build.build),
+            compiler: self.compiler,
             target: builder.build.build,
         });
         builder.ensure(ToolBuild {
-            stage: self.stage,
+            compiler: self.compiler,
             target: self.target,
             tool: "cargo",
             mode: Mode::Librustc,
@@ -270,7 +339,7 @@ fn run(self, builder: &Builder) -> PathBuf {
 
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
 pub struct Rls {
-    pub stage: u32,
+    pub compiler: Compiler,
     pub target: Interned<String>,
 }
 
@@ -286,7 +355,7 @@ fn should_run(run: ShouldRun) -> ShouldRun {
 
     fn make_run(run: RunConfig) {
         run.builder.ensure(Rls {
-            stage: run.builder.top_stage,
+            compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
             target: run.target,
         });
     }
@@ -298,11 +367,11 @@ fn run(self, builder: &Builder) -> PathBuf {
         // RLS depends on procedural macros, which requires a full host
         // compiler to be available, so we need to depend on that.
         builder.ensure(compile::Rustc {
-            compiler: builder.compiler(self.stage, builder.build.build),
+            compiler: self.compiler,
             target: builder.build.build,
         });
         builder.ensure(ToolBuild {
-            stage: self.stage,
+            compiler: self.compiler,
             target: self.target,
             tool: "rls",
             mode: Mode::Librustc,