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::PathBuf;
13 use std::process::Command;
17 use builder::{Step, RunConfig, 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 {
166 fn make_run(run: RunConfig) {
167 run.builder.ensure($name {
168 stage: run.builder.top_stage,
173 fn run(self, builder: &Builder) -> PathBuf {
174 builder.ensure(ToolBuild {
187 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
188 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
189 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
190 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
191 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
192 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
193 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
194 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
195 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
196 RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
199 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
200 pub struct RemoteTestServer {
202 pub target: Interned<String>,
205 impl Step for RemoteTestServer {
206 type Output = PathBuf;
208 fn should_run(run: ShouldRun) -> ShouldRun {
209 run.path("src/tools/remote-test-server")
212 fn make_run(run: RunConfig) {
213 run.builder.ensure(RemoteTestServer {
214 stage: run.builder.top_stage,
219 fn run(self, builder: &Builder) -> PathBuf {
220 builder.ensure(ToolBuild {
223 tool: "remote-test-server",
229 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
232 pub target: Interned<String>,
235 impl Step for Cargo {
236 type Output = PathBuf;
237 const DEFAULT: bool = true;
238 const ONLY_HOSTS: bool = true;
240 fn should_run(run: ShouldRun) -> ShouldRun {
241 let builder = run.builder;
242 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
245 fn make_run(run: RunConfig) {
246 run.builder.ensure(Cargo {
247 stage: run.builder.top_stage,
252 fn run(self, builder: &Builder) -> PathBuf {
253 builder.ensure(native::Openssl {
256 // Cargo depends on procedural macros, which requires a full host
257 // compiler to be available, so we need to depend on that.
258 builder.ensure(compile::Rustc {
259 compiler: builder.compiler(self.stage, builder.build.build),
260 target: builder.build.build,
262 builder.ensure(ToolBuild {
271 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
274 pub target: Interned<String>,
278 type Output = PathBuf;
279 const DEFAULT: bool = true;
280 const ONLY_HOSTS: bool = true;
282 fn should_run(run: ShouldRun) -> ShouldRun {
283 let builder = run.builder;
284 run.path("src/tools/rls").default_condition(builder.build.config.extended)
287 fn make_run(run: RunConfig) {
288 run.builder.ensure(Rls {
289 stage: run.builder.top_stage,
294 fn run(self, builder: &Builder) -> PathBuf {
295 builder.ensure(native::Openssl {
298 // RLS depends on procedural macros, which requires a full host
299 // compiler to be available, so we need to depend on that.
300 builder.ensure(compile::Rustc {
301 compiler: builder.compiler(self.stage, builder.build.build),
302 target: builder.build.build,
304 builder.ensure(ToolBuild {
308 mode: Mode::Librustc,
313 impl<'a> Builder<'a> {
314 /// Get a `Command` which is ready to run `tool` in `stage` built for
316 pub fn tool_cmd(&self, tool: Tool) -> Command {
317 let mut cmd = Command::new(self.tool_exe(tool));
318 let compiler = self.compiler(0, self.build.build);
319 self.prepare_tool_cmd(compiler, &mut cmd);
323 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
325 /// Notably this munges the dynamic library lookup path to point to the
326 /// right location to run `compiler`.
327 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
328 let host = &compiler.host;
329 let mut paths: Vec<PathBuf> = vec![
330 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
331 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
334 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
335 // mode) and that C compiler may need some extra PATH modification. Do
337 if compiler.host.contains("msvc") {
338 let curpaths = env::var_os("PATH").unwrap_or_default();
339 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
340 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
344 for path in env::split_paths(v) {
345 if !curpaths.contains(&path) {
351 add_lib_path(paths, cmd);