1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use std::path::PathBuf;
14 use std::process::Command;
18 use builder::{Step, RunConfig, ShouldRun, Builder};
19 use util::{copy, exe, add_lib_path};
20 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
25 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
26 pub struct CleanTools {
27 pub compiler: Compiler,
28 pub target: Interned<String>,
32 impl Step for CleanTools {
35 fn should_run(run: ShouldRun) -> ShouldRun {
39 /// Build a tool in `src/tools`
41 /// This will build the specified tool with the specified `host` compiler in
42 /// `stage` into the normal cargo output directory.
43 fn run(self, builder: &Builder) {
44 let build = builder.build;
45 let compiler = self.compiler;
46 let target = self.target;
49 let stamp = match mode {
50 Mode::Libstd => libstd_stamp(build, compiler, target),
51 Mode::Libtest => libtest_stamp(build, compiler, target),
52 Mode::Librustc => librustc_stamp(build, compiler, target),
55 let out_dir = build.cargo_out(compiler, Mode::Tool, target);
56 build.clear_if_dirty(&out_dir, &stamp);
60 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
63 target: Interned<String>,
69 impl Step for ToolBuild {
70 type Output = PathBuf;
72 fn should_run(run: ShouldRun) -> ShouldRun {
76 /// Build a tool in `src/tools`
78 /// This will build the specified tool with the specified `host` compiler in
79 /// `stage` into the normal cargo output directory.
80 fn run(self, builder: &Builder) -> PathBuf {
81 let build = builder.build;
82 let compiler = self.compiler;
83 let target = self.target;
88 Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
89 Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
90 Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
91 Mode::Tool => panic!("unexpected Mode::Tool for tool build")
94 let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
95 println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
97 let mut cargo = prepare_tool_cargo(builder, compiler, target, path);
98 build.run(&mut cargo);
99 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
103 fn prepare_tool_cargo(
106 target: Interned<String>,
109 let build = builder.build;
110 let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
111 let dir = build.src.join(path);
112 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
114 // We don't want to build tools dynamically as they'll be running across
115 // stages and such and it's just easier if they're not dynamically linked.
116 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
118 if let Some(dir) = build.openssl_install_dir(target) {
119 cargo.env("OPENSSL_STATIC", "1");
120 cargo.env("OPENSSL_DIR", dir);
121 cargo.env("LIBZ_SYS_STATIC", "1");
124 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
125 cargo.env("CFG_VERSION", build.rust_version());
127 let info = GitInfo::new(&build.config, &dir);
128 if let Some(sha) = info.sha() {
129 cargo.env("CFG_COMMIT_HASH", sha);
131 if let Some(sha_short) = info.sha_short() {
132 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
134 if let Some(date) = info.commit_date() {
135 cargo.env("CFG_COMMIT_DATE", date);
141 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
142 #[derive(Copy, Clone)]
149 impl<'a> Builder<'a> {
150 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
151 let stage = self.tool_default_stage(tool);
155 compiler: self.compiler(stage, self.build.build),
156 target: self.build.build,
162 pub fn tool_default_stage(&self, tool: Tool) -> u32 {
163 // Compile the error-index in the top stage as it depends on
164 // rustdoc, so we want to avoid recompiling rustdoc twice if we
165 // can. Otherwise compile everything else in stage0 as there's
166 // no need to rebootstrap everything
168 Tool::ErrorIndex => self.top_stage,
175 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
177 pub compiler: Compiler,
178 pub target: Interned<String>,
181 impl Step for $name {
182 type Output = PathBuf;
184 fn should_run(run: ShouldRun) -> ShouldRun {
188 fn make_run(run: RunConfig) {
189 run.builder.ensure($name {
190 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
195 fn run(self, builder: &Builder) -> PathBuf {
196 builder.ensure(ToolBuild {
197 compiler: self.compiler,
210 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
211 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
212 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
213 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
214 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
215 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
216 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
217 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd;
218 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
219 RustInstaller, "src/tools/rust-installer", "fabricate", Mode::Libstd;
222 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
223 pub struct RemoteTestServer {
224 pub compiler: Compiler,
225 pub target: Interned<String>,
228 impl Step for RemoteTestServer {
229 type Output = PathBuf;
231 fn should_run(run: ShouldRun) -> ShouldRun {
232 run.path("src/tools/remote-test-server")
235 fn make_run(run: RunConfig) {
236 run.builder.ensure(RemoteTestServer {
237 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
242 fn run(self, builder: &Builder) -> PathBuf {
243 builder.ensure(ToolBuild {
244 compiler: self.compiler,
246 tool: "remote-test-server",
248 path: "src/tools/remote-test-server",
253 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
255 pub host: Interned<String>,
258 impl Step for Rustdoc {
259 type Output = PathBuf;
260 const DEFAULT: bool = true;
261 const ONLY_HOSTS: bool = true;
263 fn should_run(run: ShouldRun) -> ShouldRun {
264 run.path("src/tools/rustdoc")
267 fn make_run(run: RunConfig) {
268 run.builder.ensure(Rustdoc {
273 fn run(self, builder: &Builder) -> PathBuf {
274 let build = builder.build;
275 let target_compiler = builder.compiler(builder.top_stage, self.host);
276 let target = target_compiler.host;
277 let build_compiler = if target_compiler.stage == 0 {
278 builder.compiler(0, builder.build.build)
279 } else if target_compiler.stage >= 2 {
280 // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
281 // building rustdoc itself.
282 builder.compiler(target_compiler.stage, builder.build.build)
284 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
285 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
286 // compilers, which isn't what we want.
287 builder.compiler(target_compiler.stage - 1, builder.build.build)
290 builder.ensure(compile::Rustc { compiler: build_compiler, target });
292 let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
293 println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
295 let mut cargo = prepare_tool_cargo(builder,
298 "src/tools/rustdoc");
299 build.run(&mut cargo);
300 // Cargo adds a number of paths to the dylib search path on windows, which results in
301 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
302 // rustdoc a different name.
303 let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
304 .join(exe("rustdoc-tool-binary", &target_compiler.host));
306 // don't create a stage0-sysroot/bin directory.
307 if target_compiler.stage > 0 {
308 let sysroot = builder.sysroot(target_compiler);
309 let bindir = sysroot.join("bin");
310 t!(fs::create_dir_all(&bindir));
311 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
312 let _ = fs::remove_file(&bin_rustdoc);
313 copy(&tool_rustdoc, &bin_rustdoc);
321 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
323 pub compiler: Compiler,
324 pub target: Interned<String>,
327 impl Step for Cargo {
328 type Output = PathBuf;
329 const DEFAULT: bool = true;
330 const ONLY_HOSTS: bool = true;
332 fn should_run(run: ShouldRun) -> ShouldRun {
333 let builder = run.builder;
334 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
337 fn make_run(run: RunConfig) {
338 run.builder.ensure(Cargo {
339 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
344 fn run(self, builder: &Builder) -> PathBuf {
345 builder.ensure(native::Openssl {
348 // Cargo depends on procedural macros, which requires a full host
349 // compiler to be available, so we need to depend on that.
350 builder.ensure(compile::Rustc {
351 compiler: self.compiler,
352 target: builder.build.build,
354 builder.ensure(ToolBuild {
355 compiler: self.compiler,
358 mode: Mode::Librustc,
359 path: "src/tools/cargo",
364 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
366 pub compiler: Compiler,
367 pub target: Interned<String>,
370 impl Step for Clippy {
371 type Output = PathBuf;
372 const DEFAULT: bool = false;
373 const ONLY_HOSTS: bool = true;
375 fn should_run(run: ShouldRun) -> ShouldRun {
376 run.path("src/tools/clippy")
379 fn make_run(run: RunConfig) {
380 run.builder.ensure(Clippy {
381 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
386 fn run(self, builder: &Builder) -> PathBuf {
387 // Clippy depends on procedural macros (serde), which requires a full host
388 // compiler to be available, so we need to depend on that.
389 builder.ensure(compile::Rustc {
390 compiler: self.compiler,
391 target: builder.build.build,
393 builder.ensure(ToolBuild {
394 compiler: self.compiler,
397 mode: Mode::Librustc,
398 path: "src/tools/clippy",
403 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
405 pub compiler: Compiler,
406 pub target: Interned<String>,
410 type Output = PathBuf;
411 const DEFAULT: bool = true;
412 const ONLY_HOSTS: bool = true;
414 fn should_run(run: ShouldRun) -> ShouldRun {
415 let builder = run.builder;
416 run.path("src/tools/rls").default_condition(builder.build.config.extended)
419 fn make_run(run: RunConfig) {
420 run.builder.ensure(Rls {
421 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
426 fn run(self, builder: &Builder) -> PathBuf {
427 builder.ensure(native::Openssl {
430 // RLS depends on procedural macros, which requires a full host
431 // compiler to be available, so we need to depend on that.
432 builder.ensure(compile::Rustc {
433 compiler: self.compiler,
434 target: builder.build.build,
436 builder.ensure(ToolBuild {
437 compiler: self.compiler,
440 mode: Mode::Librustc,
441 path: "src/tools/rls",
446 impl<'a> Builder<'a> {
447 /// Get a `Command` which is ready to run `tool` in `stage` built for
449 pub fn tool_cmd(&self, tool: Tool) -> Command {
450 let mut cmd = Command::new(self.tool_exe(tool));
451 let compiler = self.compiler(self.tool_default_stage(tool), self.build.build);
452 self.prepare_tool_cmd(compiler, &mut cmd);
456 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
458 /// Notably this munges the dynamic library lookup path to point to the
459 /// right location to run `compiler`.
460 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
461 let host = &compiler.host;
462 let mut paths: Vec<PathBuf> = vec![
463 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
464 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
467 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
468 // mode) and that C compiler may need some extra PATH modification. Do
470 if compiler.host.contains("msvc") {
471 let curpaths = env::var_os("PATH").unwrap_or_default();
472 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
473 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
477 for path in env::split_paths(v) {
478 if !curpaths.contains(&path) {
484 add_lib_path(paths, cmd);