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)]
28 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>,
68 impl Step for ToolBuild {
69 type Output = PathBuf;
71 fn should_run(run: ShouldRun) -> ShouldRun {
75 /// Build a tool in `src/tools`
77 /// This will build the specified tool with the specified `host` compiler in
78 /// `stage` into the normal cargo output directory.
79 fn run(self, builder: &Builder) -> PathBuf {
80 let build = builder.build;
81 let compiler = self.compiler;
82 let target = self.target;
85 builder.ensure(CleanTools { compiler, target, mode: self.mode });
87 Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
88 Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
89 Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
90 Mode::Tool => panic!("unexpected Mode::Tool for tool build")
93 let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
94 println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
96 let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
97 let dir = build.src.join("src/tools").join(tool);
98 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
100 // We don't want to build tools dynamically as they'll be running across
101 // stages and such and it's just easier if they're not dynamically linked.
102 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
104 if let Some(dir) = build.openssl_install_dir(target) {
105 cargo.env("OPENSSL_STATIC", "1");
106 cargo.env("OPENSSL_DIR", dir);
107 cargo.env("LIBZ_SYS_STATIC", "1");
110 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
112 let info = GitInfo::new(&dir);
113 if let Some(sha) = info.sha() {
114 cargo.env("CFG_COMMIT_HASH", sha);
116 if let Some(sha_short) = info.sha_short() {
117 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
119 if let Some(date) = info.commit_date() {
120 cargo.env("CFG_COMMIT_DATE", date);
123 build.run(&mut cargo);
124 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
129 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
130 #[derive(Copy, Clone)]
137 impl<'a> Builder<'a> {
138 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
142 compiler: self.compiler(0, self.build.build),
143 target: self.build.build,
151 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
153 pub compiler: Compiler,
154 pub target: Interned<String>,
157 impl Step for $name {
158 type Output = PathBuf;
160 fn should_run(run: ShouldRun) -> ShouldRun {
164 fn make_run(run: RunConfig) {
165 run.builder.ensure($name {
166 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
171 fn run(self, builder: &Builder) -> PathBuf {
172 builder.ensure(ToolBuild {
173 compiler: self.compiler,
185 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
186 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
187 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
188 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
189 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
190 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
191 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
192 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
193 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
194 RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
197 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
198 pub struct RemoteTestServer {
199 pub compiler: Compiler,
200 pub target: Interned<String>,
203 impl Step for RemoteTestServer {
204 type Output = PathBuf;
206 fn should_run(run: ShouldRun) -> ShouldRun {
207 run.path("src/tools/remote-test-server")
210 fn make_run(run: RunConfig) {
211 run.builder.ensure(RemoteTestServer {
212 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
217 fn run(self, builder: &Builder) -> PathBuf {
218 builder.ensure(ToolBuild {
219 compiler: self.compiler,
221 tool: "remote-test-server",
227 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
229 pub target_compiler: Compiler,
232 impl Step for Rustdoc {
233 type Output = PathBuf;
234 const DEFAULT: bool = true;
235 const ONLY_HOSTS: bool = true;
237 fn should_run(run: ShouldRun) -> ShouldRun {
238 run.path("src/tools/rustdoc")
241 fn make_run(run: RunConfig) {
242 run.builder.ensure(Rustdoc {
243 target_compiler: run.builder.compiler(run.builder.top_stage, run.host),
247 fn run(self, builder: &Builder) -> PathBuf {
248 let target_compiler = self.target_compiler;
249 let build_compiler = if target_compiler.stage == 0 {
252 builder.compiler(target_compiler.stage - 1, target_compiler.host)
255 let tool_rustdoc = builder.ensure(ToolBuild {
256 compiler: build_compiler,
257 target: build_compiler.host,
259 mode: Mode::Librustc,
262 // don't create a stage0-sysroot/bin directory.
263 if target_compiler.stage > 0 {
264 let sysroot = builder.sysroot(target_compiler);
265 let bindir = sysroot.join("bin");
266 t!(fs::create_dir_all(&bindir));
267 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
268 let _ = fs::remove_file(&bin_rustdoc);
269 copy(&tool_rustdoc, &bin_rustdoc);
277 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
279 pub compiler: Compiler,
280 pub target: Interned<String>,
283 impl Step for Cargo {
284 type Output = PathBuf;
285 const DEFAULT: bool = true;
286 const ONLY_HOSTS: bool = true;
288 fn should_run(run: ShouldRun) -> ShouldRun {
289 let builder = run.builder;
290 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
293 fn make_run(run: RunConfig) {
294 run.builder.ensure(Cargo {
295 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
300 fn run(self, builder: &Builder) -> PathBuf {
301 builder.ensure(native::Openssl {
304 // Cargo depends on procedural macros, which requires a full host
305 // compiler to be available, so we need to depend on that.
306 builder.ensure(compile::Rustc {
307 compiler: self.compiler,
308 target: builder.build.build,
310 builder.ensure(ToolBuild {
311 compiler: self.compiler,
314 mode: Mode::Librustc,
319 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
321 pub compiler: Compiler,
322 pub target: Interned<String>,
326 type Output = PathBuf;
327 const DEFAULT: bool = true;
328 const ONLY_HOSTS: bool = true;
330 fn should_run(run: ShouldRun) -> ShouldRun {
331 let builder = run.builder;
332 run.path("src/tools/rls").default_condition(builder.build.config.extended)
335 fn make_run(run: RunConfig) {
336 run.builder.ensure(Rls {
337 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
342 fn run(self, builder: &Builder) -> PathBuf {
343 builder.ensure(native::Openssl {
346 // RLS depends on procedural macros, which requires a full host
347 // compiler to be available, so we need to depend on that.
348 builder.ensure(compile::Rustc {
349 compiler: self.compiler,
350 target: builder.build.build,
352 builder.ensure(ToolBuild {
353 compiler: self.compiler,
356 mode: Mode::Librustc,
361 impl<'a> Builder<'a> {
362 /// Get a `Command` which is ready to run `tool` in `stage` built for
364 pub fn tool_cmd(&self, tool: Tool) -> Command {
365 let mut cmd = Command::new(self.tool_exe(tool));
366 let compiler = self.compiler(0, self.build.build);
367 self.prepare_tool_cmd(compiler, &mut cmd);
371 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
373 /// Notably this munges the dynamic library lookup path to point to the
374 /// right location to run `compiler`.
375 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
376 let host = &compiler.host;
377 let mut paths: Vec<PathBuf> = vec![
378 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
379 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
382 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
383 // mode) and that C compiler may need some extra PATH modification. Do
385 if compiler.host.contains("msvc") {
386 let curpaths = env::var_os("PATH").unwrap_or_default();
387 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
388 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
392 for path in env::split_paths(v) {
393 if !curpaths.contains(&path) {
399 add_lib_path(paths, cmd);