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, "build", path);
98 build.run(&mut cargo);
99 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
103 pub fn prepare_tool_cargo(
106 target: Interned<String>,
107 command: &'static str,
110 let build = builder.build;
111 let mut cargo = builder.cargo(compiler, Mode::Tool, target, command);
112 let dir = build.src.join(path);
113 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
115 // We don't want to build tools dynamically as they'll be running across
116 // stages and such and it's just easier if they're not dynamically linked.
117 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
119 if let Some(dir) = build.openssl_install_dir(target) {
120 cargo.env("OPENSSL_STATIC", "1");
121 cargo.env("OPENSSL_DIR", dir);
122 cargo.env("LIBZ_SYS_STATIC", "1");
125 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
126 cargo.env("CFG_VERSION", build.rust_version());
128 let info = GitInfo::new(&build.config, &dir);
129 if let Some(sha) = info.sha() {
130 cargo.env("CFG_COMMIT_HASH", sha);
132 if let Some(sha_short) = info.sha_short() {
133 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
135 if let Some(date) = info.commit_date() {
136 cargo.env("CFG_COMMIT_DATE", date);
142 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
143 #[derive(Copy, Clone)]
150 impl<'a> Builder<'a> {
151 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
155 compiler: self.compiler(0, self.build.build),
156 target: self.build.build,
164 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
166 pub compiler: Compiler,
167 pub target: Interned<String>,
170 impl Step for $name {
171 type Output = PathBuf;
173 fn should_run(run: ShouldRun) -> ShouldRun {
177 fn make_run(run: RunConfig) {
178 run.builder.ensure($name {
179 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
184 fn run(self, builder: &Builder) -> PathBuf {
185 builder.ensure(ToolBuild {
186 compiler: self.compiler,
199 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
200 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
201 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
202 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
203 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
204 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
205 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
206 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd;
207 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
208 RustInstaller, "src/tools/rust-installer", "fabricate", Mode::Libstd;
211 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
212 pub struct RemoteTestServer {
213 pub compiler: Compiler,
214 pub target: Interned<String>,
217 impl Step for RemoteTestServer {
218 type Output = PathBuf;
220 fn should_run(run: ShouldRun) -> ShouldRun {
221 run.path("src/tools/remote-test-server")
224 fn make_run(run: RunConfig) {
225 run.builder.ensure(RemoteTestServer {
226 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
231 fn run(self, builder: &Builder) -> PathBuf {
232 builder.ensure(ToolBuild {
233 compiler: self.compiler,
235 tool: "remote-test-server",
237 path: "src/tools/remote-test-server",
242 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
244 pub host: Interned<String>,
247 impl Step for Rustdoc {
248 type Output = PathBuf;
249 const DEFAULT: bool = true;
250 const ONLY_HOSTS: bool = true;
252 fn should_run(run: ShouldRun) -> ShouldRun {
253 run.path("src/tools/rustdoc")
256 fn make_run(run: RunConfig) {
257 run.builder.ensure(Rustdoc {
262 fn run(self, builder: &Builder) -> PathBuf {
263 let build = builder.build;
264 let target_compiler = builder.compiler(builder.top_stage, self.host);
265 let target = target_compiler.host;
266 let build_compiler = if target_compiler.stage == 0 {
267 builder.compiler(0, builder.build.build)
268 } else if target_compiler.stage >= 2 {
269 // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
270 // building rustdoc itself.
271 builder.compiler(target_compiler.stage, builder.build.build)
273 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
274 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
275 // compilers, which isn't what we want.
276 builder.compiler(target_compiler.stage - 1, builder.build.build)
279 builder.ensure(compile::Rustc { compiler: build_compiler, target });
281 let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
282 println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
284 let mut cargo = prepare_tool_cargo(builder,
288 "src/tools/rustdoc");
289 build.run(&mut cargo);
290 // Cargo adds a number of paths to the dylib search path on windows, which results in
291 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
292 // rustdoc a different name.
293 let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
294 .join(exe("rustdoc-tool-binary", &target_compiler.host));
296 // don't create a stage0-sysroot/bin directory.
297 if target_compiler.stage > 0 {
298 let sysroot = builder.sysroot(target_compiler);
299 let bindir = sysroot.join("bin");
300 t!(fs::create_dir_all(&bindir));
301 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
302 let _ = fs::remove_file(&bin_rustdoc);
303 copy(&tool_rustdoc, &bin_rustdoc);
311 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
313 pub compiler: Compiler,
314 pub target: Interned<String>,
317 impl Step for Cargo {
318 type Output = PathBuf;
319 const DEFAULT: bool = true;
320 const ONLY_HOSTS: bool = true;
322 fn should_run(run: ShouldRun) -> ShouldRun {
323 let builder = run.builder;
324 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
327 fn make_run(run: RunConfig) {
328 run.builder.ensure(Cargo {
329 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
334 fn run(self, builder: &Builder) -> PathBuf {
335 builder.ensure(native::Openssl {
338 // Cargo depends on procedural macros, which requires a full host
339 // compiler to be available, so we need to depend on that.
340 builder.ensure(compile::Rustc {
341 compiler: self.compiler,
342 target: builder.build.build,
344 builder.ensure(ToolBuild {
345 compiler: self.compiler,
348 mode: Mode::Librustc,
349 path: "src/tools/cargo",
354 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
356 pub compiler: Compiler,
357 pub target: Interned<String>,
360 impl Step for Clippy {
361 type Output = PathBuf;
362 const DEFAULT: bool = false;
363 const ONLY_HOSTS: bool = true;
365 fn should_run(run: ShouldRun) -> ShouldRun {
366 run.path("src/tools/clippy")
369 fn make_run(run: RunConfig) {
370 run.builder.ensure(Clippy {
371 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
376 fn run(self, builder: &Builder) -> PathBuf {
377 // Clippy depends on procedural macros (serde), which requires a full host
378 // compiler to be available, so we need to depend on that.
379 builder.ensure(compile::Rustc {
380 compiler: self.compiler,
381 target: builder.build.build,
383 builder.ensure(ToolBuild {
384 compiler: self.compiler,
387 mode: Mode::Librustc,
388 path: "src/tools/clippy",
393 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
395 pub compiler: Compiler,
396 pub target: Interned<String>,
400 type Output = PathBuf;
401 const DEFAULT: bool = true;
402 const ONLY_HOSTS: bool = true;
404 fn should_run(run: ShouldRun) -> ShouldRun {
405 let builder = run.builder;
406 run.path("src/tools/rls").default_condition(builder.build.config.extended)
409 fn make_run(run: RunConfig) {
410 run.builder.ensure(Rls {
411 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
416 fn run(self, builder: &Builder) -> PathBuf {
417 builder.ensure(native::Openssl {
420 // RLS depends on procedural macros, which requires a full host
421 // compiler to be available, so we need to depend on that.
422 builder.ensure(compile::Rustc {
423 compiler: self.compiler,
424 target: builder.build.build,
426 builder.ensure(ToolBuild {
427 compiler: self.compiler,
430 mode: Mode::Librustc,
431 path: "src/tools/rls",
436 impl<'a> Builder<'a> {
437 /// Get a `Command` which is ready to run `tool` in `stage` built for
439 pub fn tool_cmd(&self, tool: Tool) -> Command {
440 let mut cmd = Command::new(self.tool_exe(tool));
441 let compiler = self.compiler(0, self.build.build);
442 self.prepare_tool_cmd(compiler, &mut cmd);
446 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
448 /// Notably this munges the dynamic library lookup path to point to the
449 /// right location to run `compiler`.
450 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
451 let host = &compiler.host;
452 let mut paths: Vec<PathBuf> = vec![
453 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
454 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
457 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
458 // mode) and that C compiler may need some extra PATH modification. Do
460 if compiler.host.contains("msvc") {
461 let curpaths = env::var_os("PATH").unwrap_or_default();
462 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
463 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
467 for path in env::split_paths(v) {
468 if !curpaths.contains(&path) {
474 add_lib_path(paths, cmd);