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, Builder};
18 use util::{exe, add_lib_path};
19 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp, Rustc};
23 //// ========================================================================
26 //// Tools used during the build system but not shipped
27 //// "pseudo rule" which represents completely cleaning out the tools dir in
28 //// one stage. This needs to happen whenever a dependency changes (e.g.
29 //// libstd, libtest, librustc) and all of the tool compilations above will
30 //// be sequenced after this rule.
31 //rules.build("maybe-clean-tools", "path/to/nowhere")
32 // .after("librustc-tool")
33 // .after("libtest-tool")
34 // .after("libstd-tool");
36 //rules.build("librustc-tool", "path/to/nowhere")
37 // .dep(|s| s.name("librustc"))
38 // .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Librustc));
39 //rules.build("libtest-tool", "path/to/nowhere")
40 // .dep(|s| s.name("libtest"))
41 // .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libtest));
42 //rules.build("libstd-tool", "path/to/nowhere")
43 // .dep(|s| s.name("libstd"))
44 // .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libstd));
48 pub struct CleanTools<'a> {
54 impl<'a> Step<'a> for CleanTools<'a> {
57 /// Build a tool in `src/tools`
59 /// This will build the specified tool with the specified `host` compiler in
60 /// `stage` into the normal cargo output directory.
61 fn run(self, builder: &Builder) {
62 let build = builder.build;
63 let stage = self.stage;
64 let target = self.target;
67 let compiler = builder.compiler(stage, &build.build);
69 let stamp = match mode {
70 Mode::Libstd => libstd_stamp(build, compiler, target),
71 Mode::Libtest => libtest_stamp(build, compiler, target),
72 Mode::Librustc => librustc_stamp(build, compiler, target),
75 let out_dir = build.cargo_out(compiler, Mode::Tool, target);
76 build.clear_if_dirty(&out_dir, &stamp);
81 pub struct ToolBuild<'a> {
88 impl<'a> Step<'a> for ToolBuild<'a> {
89 type Output = PathBuf;
91 /// Build a tool in `src/tools`
93 /// This will build the specified tool with the specified `host` compiler in
94 /// `stage` into the normal cargo output directory.
95 fn run(self, builder: &Builder) -> PathBuf {
96 let build = builder.build;
97 let stage = self.stage;
98 let target = self.target;
101 let compiler = builder.compiler(stage, &build.build);
102 builder.ensure(CleanTools { stage, target, mode: self.mode });
104 Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
105 Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
106 Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
107 Mode::Tool => panic!("unexpected Mode::Tool for tool build")
110 let _folder = build.fold_output(|| format!("stage{}-{}", stage, tool));
111 println!("Building stage{} tool {} ({})", stage, tool, target);
113 let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
114 let dir = build.src.join("src/tools").join(tool);
115 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
117 // We don't want to build tools dynamically as they'll be running across
118 // stages and such and it's just easier if they're not dynamically linked.
119 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
121 if let Some(dir) = build.openssl_install_dir(target) {
122 cargo.env("OPENSSL_STATIC", "1");
123 cargo.env("OPENSSL_DIR", dir);
124 cargo.env("LIBZ_SYS_STATIC", "1");
127 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
129 let info = GitInfo::new(&dir);
130 if let Some(sha) = info.sha() {
131 cargo.env("CFG_COMMIT_HASH", sha);
133 if let Some(sha_short) = info.sha_short() {
134 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
136 if let Some(date) = info.commit_date() {
137 cargo.env("CFG_COMMIT_DATE", date);
140 build.run(&mut cargo);
141 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, compiler.host))
146 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
147 #[derive(Copy, Clone)]
154 impl<'a> Builder<'a> {
155 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
160 target: &self.build.build,
169 pub struct $name<'a> {
174 impl<'a> Step<'a> for $name<'a> {
175 type Output = PathBuf;
177 fn should_run(_builder: &Builder, path: &Path) -> bool {
178 path.ends_with($path)
181 fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, target: &str) {
182 builder.ensure($name {
183 stage: builder.top_stage,
188 fn run(self, builder: &Builder) -> PathBuf {
189 builder.ensure(ToolBuild {
202 // rules.build("tool-rustbook", "src/tools/rustbook")
203 // .dep(|s| s.name("maybe-clean-tools"))
204 // .dep(|s| s.name("librustc-tool"))
205 // .run(move |s| compile::tool(build, s.stage, s.target, "rustbook"));
206 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
207 // rules.build("tool-error-index", "src/tools/error_index_generator")
208 // .dep(|s| s.name("maybe-clean-tools"))
209 // .dep(|s| s.name("librustc-tool"))
210 // .run(move |s| compile::tool(build, s.stage, s.target, "error_index_generator"));
211 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
212 // rules.build("tool-unstable-book-gen", "src/tools/unstable-book-gen")
213 // .dep(|s| s.name("maybe-clean-tools"))
214 // .dep(|s| s.name("libstd-tool"))
215 // .run(move |s| compile::tool(build, s.stage, s.target, "unstable-book-gen"));
216 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
217 // rules.build("tool-tidy", "src/tools/tidy")
218 // .dep(|s| s.name("maybe-clean-tools"))
219 // .dep(|s| s.name("libstd-tool"))
220 // .run(move |s| compile::tool(build, s.stage, s.target, "tidy"));
221 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
222 // rules.build("tool-linkchecker", "src/tools/linkchecker")
223 // .dep(|s| s.name("maybe-clean-tools"))
224 // .dep(|s| s.name("libstd-tool"))
225 // .run(move |s| compile::tool(build, s.stage, s.target, "linkchecker"));
226 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
227 // rules.build("tool-cargotest", "src/tools/cargotest")
228 // .dep(|s| s.name("maybe-clean-tools"))
229 // .dep(|s| s.name("libstd-tool"))
230 // .run(move |s| compile::tool(build, s.stage, s.target, "cargotest"));
231 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
232 // rules.build("tool-compiletest", "src/tools/compiletest")
233 // .dep(|s| s.name("maybe-clean-tools"))
234 // .dep(|s| s.name("libtest-tool"))
235 // .run(move |s| compile::tool(build, s.stage, s.target, "compiletest"));
236 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
237 // rules.build("tool-build-manifest", "src/tools/build-manifest")
238 // .dep(|s| s.name("maybe-clean-tools"))
239 // .dep(|s| s.name("libstd-tool"))
240 // .run(move |s| compile::tool(build, s.stage, s.target, "build-manifest"));
241 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
242 // rules.build("tool-remote-test-client", "src/tools/remote-test-client")
243 // .dep(|s| s.name("maybe-clean-tools"))
244 // .dep(|s| s.name("libstd-tool"))
245 // .run(move |s| compile::tool(build, s.stage, s.target, "remote-test-client"));
246 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
247 // rules.build("tool-rust-installer", "src/tools/rust-installer")
248 // .dep(|s| s.name("maybe-clean-tools"))
249 // .dep(|s| s.name("libstd-tool"))
250 // .run(move |s| compile::tool(build, s.stage, s.target, "rust-installer"));
251 RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
255 pub struct RemoteTestServer<'a> {
260 impl<'a> Step<'a> for RemoteTestServer<'a> {
261 type Output = PathBuf;
263 fn should_run(_builder: &Builder, path: &Path) -> bool {
264 path.ends_with("src/tools/remote-test-server")
267 fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, target: &str) {
268 builder.ensure(RemoteTestServer {
269 stage: builder.top_stage,
274 fn run(self, builder: &Builder) -> PathBuf {
275 builder.ensure(ToolBuild {
278 tool: "remote-test-server",
284 // rules.build("tool-cargo", "src/tools/cargo")
286 // .default(build.config.extended)
287 // .dep(|s| s.name("maybe-clean-tools"))
288 // .dep(|s| s.name("libstd-tool"))
289 // .dep(|s| s.stage(0).host(s.target).name("openssl"))
291 // // Cargo depends on procedural macros, which requires a full host
292 // // compiler to be available, so we need to depend on that.
293 // s.name("librustc-link")
294 // .target(&build.build)
295 // .host(&build.build)
297 // .run(move |s| compile::tool(build, s.stage, s.target, "cargo"));
299 pub struct Cargo<'a> {
304 impl<'a> Step<'a> for Cargo<'a> {
305 type Output = PathBuf;
306 const DEFAULT: bool = true;
307 const ONLY_HOSTS: bool = true;
309 fn should_run(_builder: &Builder, path: &Path) -> bool {
310 path.ends_with("src/tools/cargo")
313 fn make_run(builder: &Builder, path: Option<&Path>, _host: &str, target: &str) {
314 if path.is_none() && !builder.build.config.extended {
317 builder.ensure(Cargo {
318 stage: builder.top_stage,
323 fn run(self, builder: &Builder) -> PathBuf {
324 builder.ensure(native::Openssl {
327 // Cargo depends on procedural macros, which requires a full host
328 // compiler to be available, so we need to depend on that.
329 builder.ensure(Rustc {
330 compiler: builder.compiler(builder.top_stage, &builder.build.build),
331 target: &builder.build.build,
333 builder.ensure(ToolBuild {
342 // rules.build("tool-rls", "src/tools/rls")
344 // .default(build.config.extended)
345 // .dep(|s| s.name("librustc-tool"))
346 // .dep(|s| s.stage(0).host(s.target).name("openssl"))
348 // // rls, like cargo, uses procedural macros
349 // s.name("librustc-link")
350 // .target(&build.build)
351 // .host(&build.build)
353 // .run(move |s| compile::tool(build, s.stage, s.target, "rls"));
361 impl<'a> Step<'a> for Rls<'a> {
362 type Output = PathBuf;
363 const DEFAULT: bool = true;
364 const ONLY_HOSTS: bool = true;
366 fn should_run(_builder: &Builder, path: &Path) -> bool {
367 path.ends_with("src/tools/rls")
370 fn make_run(builder: &Builder, path: Option<&Path>, _host: &str, target: &str) {
371 if path.is_none() && !builder.build.config.extended {
374 builder.ensure(Cargo {
375 stage: builder.top_stage,
380 fn run(self, builder: &Builder) -> PathBuf {
381 builder.ensure(native::Openssl {
384 // RLS depends on procedural macros, which requires a full host
385 // compiler to be available, so we need to depend on that.
386 builder.ensure(Rustc {
387 compiler: builder.compiler(builder.top_stage, &builder.build.build),
388 target: &builder.build.build,
390 builder.ensure(ToolBuild {
394 mode: Mode::Librustc,
399 impl<'a> Builder<'a> {
400 /// Get a `Command` which is ready to run `tool` in `stage` built for
402 pub fn tool_cmd(&self, tool: Tool) -> Command {
403 let mut cmd = Command::new(self.tool_exe(tool));
404 let compiler = self.compiler(0, &self.build.build);
405 self.prepare_tool_cmd(compiler, &mut cmd);
409 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
411 /// Notably this munges the dynamic library lookup path to point to the
412 /// right location to run `compiler`.
413 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
414 let host = compiler.host;
415 let mut paths = vec![
416 self.sysroot_libdir(compiler, compiler.host),
417 self.cargo_out(compiler, Mode::Tool, host).join("deps"),
420 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
421 // mode) and that C compiler may need some extra PATH modification. Do
423 if compiler.host.contains("msvc") {
424 let curpaths = env::var_os("PATH").unwrap_or_default();
425 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
426 for &(ref k, ref v) in self.cc[compiler.host].0.env() {
430 for path in env::split_paths(v) {
431 if !curpaths.contains(&path) {
437 add_lib_path(paths, cmd);