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.
12 use std::path::{Path, PathBuf};
13 use std::process::Command;
17 use builder::{Step, ShouldRun, Builder};
18 use util::{exe, add_lib_path};
19 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
24 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
25 pub struct CleanTools {
27 pub target: Interned<String>,
31 impl Step for CleanTools {
34 fn should_run(run: ShouldRun) -> ShouldRun {
38 /// Build a tool in `src/tools`
40 /// This will build the specified tool with the specified `host` compiler in
41 /// `stage` into the normal cargo output directory.
42 fn run(self, builder: &Builder) {
43 let build = builder.build;
44 let stage = self.stage;
45 let target = self.target;
48 let compiler = builder.compiler(stage, build.build);
50 let stamp = match mode {
51 Mode::Libstd => libstd_stamp(build, compiler, target),
52 Mode::Libtest => libtest_stamp(build, compiler, target),
53 Mode::Librustc => librustc_stamp(build, compiler, target),
56 let out_dir = build.cargo_out(compiler, Mode::Tool, target);
57 build.clear_if_dirty(&out_dir, &stamp);
61 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
62 pub struct ToolBuild {
64 pub target: Interned<String>,
65 pub tool: &'static str,
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 stage = self.stage;
83 let target = self.target;
86 let compiler = builder.compiler(stage, build.build);
87 builder.ensure(CleanTools { stage, target, mode: self.mode });
89 Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
90 Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
91 Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
92 Mode::Tool => panic!("unexpected Mode::Tool for tool build")
95 let _folder = build.fold_output(|| format!("stage{}-{}", stage, tool));
96 println!("Building stage{} tool {} ({})", stage, tool, target);
98 let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
99 let dir = build.src.join("src/tools").join(tool);
100 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
102 // We don't want to build tools dynamically as they'll be running across
103 // stages and such and it's just easier if they're not dynamically linked.
104 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
106 if let Some(dir) = build.openssl_install_dir(target) {
107 cargo.env("OPENSSL_STATIC", "1");
108 cargo.env("OPENSSL_DIR", dir);
109 cargo.env("LIBZ_SYS_STATIC", "1");
112 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
114 let info = GitInfo::new(&dir);
115 if let Some(sha) = info.sha() {
116 cargo.env("CFG_COMMIT_HASH", sha);
118 if let Some(sha_short) = info.sha_short() {
119 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
121 if let Some(date) = info.commit_date() {
122 cargo.env("CFG_COMMIT_DATE", date);
125 build.run(&mut cargo);
126 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
131 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
132 #[derive(Copy, Clone)]
139 impl<'a> Builder<'a> {
140 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
145 target: self.build.build,
153 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
156 pub target: Interned<String>,
159 impl Step for $name {
160 type Output = PathBuf;
162 fn should_run(run: ShouldRun) -> ShouldRun {
168 _path: Option<&Path>,
169 _host: Interned<String>,
170 target: Interned<String>
172 builder.ensure($name {
173 stage: builder.top_stage,
178 fn run(self, builder: &Builder) -> PathBuf {
179 builder.ensure(ToolBuild {
192 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
193 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
194 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
195 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
196 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
197 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
198 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
199 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
200 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
201 RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
204 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
205 pub struct RemoteTestServer {
207 pub target: Interned<String>,
210 impl Step for RemoteTestServer {
211 type Output = PathBuf;
213 fn should_run(run: ShouldRun) -> ShouldRun {
214 run.path("src/tools/remote-test-server")
219 _path: Option<&Path>,
220 _host: Interned<String>,
221 target: Interned<String>
223 builder.ensure(RemoteTestServer {
224 stage: builder.top_stage,
229 fn run(self, builder: &Builder) -> PathBuf {
230 builder.ensure(ToolBuild {
233 tool: "remote-test-server",
239 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
242 pub target: Interned<String>,
245 impl Step for Cargo {
246 type Output = PathBuf;
247 const DEFAULT: bool = true;
248 const ONLY_HOSTS: bool = true;
250 fn should_run(run: ShouldRun) -> ShouldRun {
251 run.path("src/tools/cargo")
255 builder: &Builder, path: Option<&Path>, _host: Interned<String>, target: Interned<String>
257 if path.is_none() && !builder.build.config.extended {
260 builder.ensure(Cargo {
261 stage: builder.top_stage,
266 fn run(self, builder: &Builder) -> PathBuf {
267 builder.ensure(native::Openssl {
270 // Cargo depends on procedural macros, which requires a full host
271 // compiler to be available, so we need to depend on that.
272 builder.ensure(compile::Rustc {
273 compiler: builder.compiler(self.stage, builder.build.build),
274 target: builder.build.build,
276 builder.ensure(ToolBuild {
285 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
288 pub target: Interned<String>,
292 type Output = PathBuf;
293 const DEFAULT: bool = true;
294 const ONLY_HOSTS: bool = true;
296 fn should_run(run: ShouldRun) -> ShouldRun {
297 run.path("src/tools/rls")
301 builder: &Builder, path: Option<&Path>, _host: Interned<String>, target: Interned<String>
303 if path.is_none() && !builder.build.config.extended {
307 stage: builder.top_stage,
312 fn run(self, builder: &Builder) -> PathBuf {
313 builder.ensure(native::Openssl {
316 // RLS depends on procedural macros, which requires a full host
317 // compiler to be available, so we need to depend on that.
318 builder.ensure(compile::Rustc {
319 compiler: builder.compiler(self.stage, builder.build.build),
320 target: builder.build.build,
322 builder.ensure(ToolBuild {
326 mode: Mode::Librustc,
331 impl<'a> Builder<'a> {
332 /// Get a `Command` which is ready to run `tool` in `stage` built for
334 pub fn tool_cmd(&self, tool: Tool) -> Command {
335 let mut cmd = Command::new(self.tool_exe(tool));
336 let compiler = self.compiler(0, self.build.build);
337 self.prepare_tool_cmd(compiler, &mut cmd);
341 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
343 /// Notably this munges the dynamic library lookup path to point to the
344 /// right location to run `compiler`.
345 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
346 let host = &compiler.host;
347 let mut paths: Vec<PathBuf> = vec![
348 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
349 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
352 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
353 // mode) and that C compiler may need some extra PATH modification. Do
355 if compiler.host.contains("msvc") {
356 let curpaths = env::var_os("PATH").unwrap_or_default();
357 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
358 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
362 for path in env::split_paths(v) {
363 if !curpaths.contains(&path) {
369 add_lib_path(paths, cmd);