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 {
154 compiler: self.compiler(0, self.build.build),
155 target: self.build.build,
163 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
165 pub compiler: Compiler,
166 pub target: Interned<String>,
169 impl Step for $name {
170 type Output = PathBuf;
172 fn should_run(run: ShouldRun) -> ShouldRun {
176 fn make_run(run: RunConfig) {
177 run.builder.ensure($name {
178 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
183 fn run(self, builder: &Builder) -> PathBuf {
184 builder.ensure(ToolBuild {
185 compiler: self.compiler,
198 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
199 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
200 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
201 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
202 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
203 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
204 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
205 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd;
206 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
207 RustInstaller, "src/tools/rust-installer", "fabricate", Mode::Libstd;
210 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
211 pub struct RemoteTestServer {
212 pub compiler: Compiler,
213 pub target: Interned<String>,
216 impl Step for RemoteTestServer {
217 type Output = PathBuf;
219 fn should_run(run: ShouldRun) -> ShouldRun {
220 run.path("src/tools/remote-test-server")
223 fn make_run(run: RunConfig) {
224 run.builder.ensure(RemoteTestServer {
225 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
230 fn run(self, builder: &Builder) -> PathBuf {
231 builder.ensure(ToolBuild {
232 compiler: self.compiler,
234 tool: "remote-test-server",
236 path: "src/tools/remote-test-server",
241 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
243 pub host: Interned<String>,
246 impl Step for Rustdoc {
247 type Output = PathBuf;
248 const DEFAULT: bool = true;
249 const ONLY_HOSTS: bool = true;
251 fn should_run(run: ShouldRun) -> ShouldRun {
252 run.path("src/tools/rustdoc")
255 fn make_run(run: RunConfig) {
256 run.builder.ensure(Rustdoc {
261 fn run(self, builder: &Builder) -> PathBuf {
262 let build = builder.build;
263 let target_compiler = builder.compiler(builder.top_stage, self.host);
264 let target = target_compiler.host;
265 let build_compiler = if target_compiler.stage == 0 {
266 builder.compiler(0, builder.build.build)
267 } else if target_compiler.stage >= 2 {
268 // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
269 // building rustdoc itself.
270 builder.compiler(target_compiler.stage, builder.build.build)
272 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
273 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
274 // compilers, which isn't what we want.
275 builder.compiler(target_compiler.stage - 1, builder.build.build)
278 builder.ensure(compile::Rustc { compiler: build_compiler, target });
280 let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
281 println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
283 let mut cargo = prepare_tool_cargo(builder,
286 "src/tools/rustdoc");
287 build.run(&mut cargo);
288 // Cargo adds a number of paths to the dylib search path on windows, which results in
289 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
290 // rustdoc a different name.
291 let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
292 .join(exe("rustdoc-tool-binary", &target_compiler.host));
294 // don't create a stage0-sysroot/bin directory.
295 if target_compiler.stage > 0 {
296 let sysroot = builder.sysroot(target_compiler);
297 let bindir = sysroot.join("bin");
298 t!(fs::create_dir_all(&bindir));
299 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
300 let _ = fs::remove_file(&bin_rustdoc);
301 copy(&tool_rustdoc, &bin_rustdoc);
309 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
311 pub compiler: Compiler,
312 pub target: Interned<String>,
315 impl Step for Cargo {
316 type Output = PathBuf;
317 const DEFAULT: bool = true;
318 const ONLY_HOSTS: bool = true;
320 fn should_run(run: ShouldRun) -> ShouldRun {
321 let builder = run.builder;
322 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
325 fn make_run(run: RunConfig) {
326 run.builder.ensure(Cargo {
327 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
332 fn run(self, builder: &Builder) -> PathBuf {
333 builder.ensure(native::Openssl {
336 // Cargo depends on procedural macros, which requires a full host
337 // compiler to be available, so we need to depend on that.
338 builder.ensure(compile::Rustc {
339 compiler: self.compiler,
340 target: builder.build.build,
342 builder.ensure(ToolBuild {
343 compiler: self.compiler,
346 mode: Mode::Librustc,
347 path: "src/tools/cargo",
352 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
354 pub compiler: Compiler,
355 pub target: Interned<String>,
358 impl Step for Clippy {
359 type Output = PathBuf;
360 const DEFAULT: bool = false;
361 const ONLY_HOSTS: bool = true;
363 fn should_run(run: ShouldRun) -> ShouldRun {
364 run.path("src/tools/clippy")
367 fn make_run(run: RunConfig) {
368 run.builder.ensure(Clippy {
369 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
374 fn run(self, builder: &Builder) -> PathBuf {
375 // Clippy depends on procedural macros (serde), which requires a full host
376 // compiler to be available, so we need to depend on that.
377 builder.ensure(compile::Rustc {
378 compiler: self.compiler,
379 target: builder.build.build,
381 builder.ensure(ToolBuild {
382 compiler: self.compiler,
385 mode: Mode::Librustc,
386 path: "src/tools/clippy",
391 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
393 pub compiler: Compiler,
394 pub target: Interned<String>,
398 type Output = PathBuf;
399 const DEFAULT: bool = true;
400 const ONLY_HOSTS: bool = true;
402 fn should_run(run: ShouldRun) -> ShouldRun {
403 let builder = run.builder;
404 run.path("src/tools/rls").default_condition(builder.build.config.extended)
407 fn make_run(run: RunConfig) {
408 run.builder.ensure(Rls {
409 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
414 fn run(self, builder: &Builder) -> PathBuf {
415 builder.ensure(native::Openssl {
418 // RLS depends on procedural macros, which requires a full host
419 // compiler to be available, so we need to depend on that.
420 builder.ensure(compile::Rustc {
421 compiler: self.compiler,
422 target: builder.build.build,
424 builder.ensure(ToolBuild {
425 compiler: self.compiler,
428 mode: Mode::Librustc,
429 path: "src/tools/rls",
434 impl<'a> Builder<'a> {
435 /// Get a `Command` which is ready to run `tool` in `stage` built for
437 pub fn tool_cmd(&self, tool: Tool) -> Command {
438 let mut cmd = Command::new(self.tool_exe(tool));
439 let compiler = self.compiler(0, self.build.build);
440 self.prepare_tool_cmd(compiler, &mut cmd);
444 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
446 /// Notably this munges the dynamic library lookup path to point to the
447 /// right location to run `compiler`.
448 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
449 let host = &compiler.host;
450 let mut paths: Vec<PathBuf> = vec![
451 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
452 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
455 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
456 // mode) and that C compiler may need some extra PATH modification. Do
458 if compiler.host.contains("msvc") {
459 let curpaths = env::var_os("PATH").unwrap_or_default();
460 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
461 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
465 for path in env::split_paths(v) {
466 if !curpaths.contains(&path) {
472 add_lib_path(paths, cmd);