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 = prepare_tool_cargo(builder, compiler, target, tool);
97 build.run(&mut cargo);
98 build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
102 fn prepare_tool_cargo(
105 target: Interned<String>,
108 let build = builder.build;
109 let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
110 let dir = build.src.join("src/tools").join(tool);
111 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
113 // We don't want to build tools dynamically as they'll be running across
114 // stages and such and it's just easier if they're not dynamically linked.
115 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
117 if let Some(dir) = build.openssl_install_dir(target) {
118 cargo.env("OPENSSL_STATIC", "1");
119 cargo.env("OPENSSL_DIR", dir);
120 cargo.env("LIBZ_SYS_STATIC", "1");
123 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
125 let info = GitInfo::new(&build.config, &dir);
126 if let Some(sha) = info.sha() {
127 cargo.env("CFG_COMMIT_HASH", sha);
129 if let Some(sha_short) = info.sha_short() {
130 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
132 if let Some(date) = info.commit_date() {
133 cargo.env("CFG_COMMIT_DATE", date);
139 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
140 #[derive(Copy, Clone)]
147 impl<'a> Builder<'a> {
148 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
152 compiler: self.compiler(0, self.build.build),
153 target: self.build.build,
161 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
163 pub compiler: Compiler,
164 pub target: Interned<String>,
167 impl Step for $name {
168 type Output = PathBuf;
170 fn should_run(run: ShouldRun) -> ShouldRun {
174 fn make_run(run: RunConfig) {
175 run.builder.ensure($name {
176 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
181 fn run(self, builder: &Builder) -> PathBuf {
182 builder.ensure(ToolBuild {
183 compiler: self.compiler,
195 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
196 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
197 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
198 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
199 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
200 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
201 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
202 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
203 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
204 RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
207 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
208 pub struct RemoteTestServer {
209 pub compiler: Compiler,
210 pub target: Interned<String>,
213 impl Step for RemoteTestServer {
214 type Output = PathBuf;
216 fn should_run(run: ShouldRun) -> ShouldRun {
217 run.path("src/tools/remote-test-server")
220 fn make_run(run: RunConfig) {
221 run.builder.ensure(RemoteTestServer {
222 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
227 fn run(self, builder: &Builder) -> PathBuf {
228 builder.ensure(ToolBuild {
229 compiler: self.compiler,
231 tool: "remote-test-server",
237 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
239 pub target_compiler: Compiler,
242 impl Step for Rustdoc {
243 type Output = PathBuf;
244 const DEFAULT: bool = true;
245 const ONLY_HOSTS: bool = true;
247 fn should_run(run: ShouldRun) -> ShouldRun {
248 run.path("src/tools/rustdoc")
251 fn make_run(run: RunConfig) {
252 run.builder.ensure(Rustdoc {
253 target_compiler: run.builder.compiler(run.builder.top_stage, run.host),
257 fn run(self, builder: &Builder) -> PathBuf {
258 let build = builder.build;
259 let target_compiler = self.target_compiler;
260 let target = target_compiler.host;
261 let build_compiler = if target_compiler.stage == 0 {
262 builder.compiler(0, builder.build.build)
264 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
265 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
266 // compilers, which isn't what we want.
267 builder.compiler(target_compiler.stage - 1, builder.build.build)
270 builder.ensure(CleanTools { compiler: build_compiler, target, mode: Mode::Librustc });
271 builder.ensure(compile::Rustc { compiler: build_compiler, target });
273 let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
274 println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
276 let mut cargo = prepare_tool_cargo(builder, build_compiler, target, "rustdoc");
277 build.run(&mut cargo);
278 let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
279 .join(exe("rustdoc", &target_compiler.host));
281 // don't create a stage0-sysroot/bin directory.
282 if target_compiler.stage > 0 {
283 let sysroot = builder.sysroot(target_compiler);
284 let bindir = sysroot.join("bin");
285 t!(fs::create_dir_all(&bindir));
286 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
287 let _ = fs::remove_file(&bin_rustdoc);
288 copy(&tool_rustdoc, &bin_rustdoc);
296 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
298 pub compiler: Compiler,
299 pub target: Interned<String>,
302 impl Step for Cargo {
303 type Output = PathBuf;
304 const DEFAULT: bool = true;
305 const ONLY_HOSTS: bool = true;
307 fn should_run(run: ShouldRun) -> ShouldRun {
308 let builder = run.builder;
309 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
312 fn make_run(run: RunConfig) {
313 run.builder.ensure(Cargo {
314 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
319 fn run(self, builder: &Builder) -> PathBuf {
320 builder.ensure(native::Openssl {
323 // Cargo depends on procedural macros, which requires a full host
324 // compiler to be available, so we need to depend on that.
325 builder.ensure(compile::Rustc {
326 compiler: self.compiler,
327 target: builder.build.build,
329 builder.ensure(ToolBuild {
330 compiler: self.compiler,
333 mode: Mode::Librustc,
338 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
340 pub compiler: Compiler,
341 pub target: Interned<String>,
345 type Output = PathBuf;
346 const DEFAULT: bool = true;
347 const ONLY_HOSTS: bool = true;
349 fn should_run(run: ShouldRun) -> ShouldRun {
350 let builder = run.builder;
351 run.path("src/tools/rls").default_condition(builder.build.config.extended)
354 fn make_run(run: RunConfig) {
355 run.builder.ensure(Rls {
356 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
361 fn run(self, builder: &Builder) -> PathBuf {
362 builder.ensure(native::Openssl {
365 // RLS depends on procedural macros, which requires a full host
366 // compiler to be available, so we need to depend on that.
367 builder.ensure(compile::Rustc {
368 compiler: self.compiler,
369 target: builder.build.build,
371 builder.ensure(ToolBuild {
372 compiler: self.compiler,
375 mode: Mode::Librustc,
380 impl<'a> Builder<'a> {
381 /// Get a `Command` which is ready to run `tool` in `stage` built for
383 pub fn tool_cmd(&self, tool: Tool) -> Command {
384 let mut cmd = Command::new(self.tool_exe(tool));
385 let compiler = self.compiler(0, self.build.build);
386 self.prepare_tool_cmd(compiler, &mut cmd);
390 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
392 /// Notably this munges the dynamic library lookup path to point to the
393 /// right location to run `compiler`.
394 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
395 let host = &compiler.host;
396 let mut paths: Vec<PathBuf> = vec![
397 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
398 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
401 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
402 // mode) and that C compiler may need some extra PATH modification. Do
404 if compiler.host.contains("msvc") {
405 let curpaths = env::var_os("PATH").unwrap_or_default();
406 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
407 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
411 for path in env::split_paths(v) {
412 if !curpaths.contains(&path) {
418 add_lib_path(paths, cmd);