]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/tool.rs
Use assert_eq! in copy_from_slice
[rust.git] / src / bootstrap / tool.rs
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.
4 //
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.
10
11 use std::fs;
12 use std::env;
13 use std::iter;
14 use std::path::PathBuf;
15 use std::process::{Command, exit};
16
17 use Mode;
18 use Compiler;
19 use builder::{Step, RunConfig, ShouldRun, Builder};
20 use util::{exe, add_lib_path};
21 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
22 use native;
23 use channel::GitInfo;
24 use cache::Interned;
25 use toolstate::ToolState;
26
27 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
28 pub struct CleanTools {
29     pub compiler: Compiler,
30     pub target: Interned<String>,
31     pub cause: Mode,
32 }
33
34 impl Step for CleanTools {
35     type Output = ();
36
37     fn should_run(run: ShouldRun) -> ShouldRun {
38         run.never()
39     }
40
41     fn run(self, builder: &Builder) {
42         let compiler = self.compiler;
43         let target = self.target;
44         let cause = self.cause;
45
46         // This is for the original compiler, but if we're forced to use stage 1, then
47         // std/test/rustc stamps won't exist in stage 2, so we need to get those from stage 1, since
48         // we copy the libs forward.
49         let tools_dir = builder.stage_out(compiler, Mode::ToolRustc);
50         let compiler = if builder.force_use_stage1(compiler, target) {
51             builder.compiler(1, compiler.host)
52         } else {
53             compiler
54         };
55
56         for &cur_mode in &[Mode::Std, Mode::Test, Mode::Rustc] {
57             let stamp = match cur_mode {
58                 Mode::Std => libstd_stamp(builder, compiler, target),
59                 Mode::Test => libtest_stamp(builder, compiler, target),
60                 Mode::Rustc => librustc_stamp(builder, compiler, target),
61                 _ => panic!(),
62             };
63
64             if builder.clear_if_dirty(&tools_dir, &stamp) {
65                 break;
66             }
67
68             // If we are a rustc tool, and std changed, we also need to clear ourselves out -- our
69             // dependencies depend on std. Therefore, we iterate up until our own mode.
70             if cause == cur_mode {
71                 break;
72             }
73         }
74     }
75 }
76
77 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
78 struct ToolBuild {
79     compiler: Compiler,
80     target: Interned<String>,
81     tool: &'static str,
82     path: &'static str,
83     mode: Mode,
84     is_ext_tool: bool,
85     extra_features: Vec<String>,
86 }
87
88 impl Step for ToolBuild {
89     type Output = Option<PathBuf>;
90
91     fn should_run(run: ShouldRun) -> ShouldRun {
92         run.never()
93     }
94
95     /// Build a tool in `src/tools`
96     ///
97     /// This will build the specified tool with the specified `host` compiler in
98     /// `stage` into the normal cargo output directory.
99     fn run(self, builder: &Builder) -> Option<PathBuf> {
100         let compiler = self.compiler;
101         let target = self.target;
102         let tool = self.tool;
103         let path = self.path;
104         let is_ext_tool = self.is_ext_tool;
105
106         match self.mode {
107             Mode::ToolStd => builder.ensure(compile::Std { compiler, target }),
108             Mode::ToolTest => builder.ensure(compile::Test { compiler, target }),
109             Mode::ToolRustc => builder.ensure(compile::Rustc { compiler, target }),
110             _ => panic!("unexpected Mode for tool build")
111         }
112
113         let mut cargo = prepare_tool_cargo(builder, compiler, self.mode, target, "build", path);
114         cargo.arg("--features").arg(self.extra_features.join(" "));
115
116         let _folder = builder.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
117         builder.info(&format!("Building stage{} tool {} ({})", compiler.stage, tool, target));
118         let mut duplicates = Vec::new();
119         let is_expected = compile::stream_cargo(builder, &mut cargo, &mut |msg| {
120             // Only care about big things like the RLS/Cargo for now
121             if tool != "rls" && tool != "cargo" {
122                 return
123             }
124             let (id, features, filenames) = match msg {
125                 compile::CargoMessage::CompilerArtifact {
126                     package_id,
127                     features,
128                     filenames
129                 } => {
130                     (package_id, features, filenames)
131                 }
132                 _ => return,
133             };
134             let features = features.iter().map(|s| s.to_string()).collect::<Vec<_>>();
135
136             for path in filenames {
137                 let val = (tool, PathBuf::from(&*path), features.clone());
138                 // we're only interested in deduplicating rlibs for now
139                 if val.1.extension().and_then(|s| s.to_str()) != Some("rlib") {
140                     continue
141                 }
142
143                 // Don't worry about libs that turn out to be host dependencies
144                 // or build scripts, we only care about target dependencies that
145                 // are in `deps`.
146                 if let Some(maybe_target) = val.1
147                     .parent()                   // chop off file name
148                     .and_then(|p| p.parent())   // chop off `deps`
149                     .and_then(|p| p.parent())   // chop off `release`
150                     .and_then(|p| p.file_name())
151                     .and_then(|p| p.to_str())
152                 {
153                     if maybe_target != &*target {
154                         continue
155                     }
156                 }
157
158                 let mut artifacts = builder.tool_artifacts.borrow_mut();
159                 let prev_artifacts = artifacts
160                     .entry(target)
161                     .or_insert_with(Default::default);
162                 if let Some(prev) = prev_artifacts.get(&*id) {
163                     if prev.1 != val.1 {
164                         duplicates.push((
165                             id.to_string(),
166                             val,
167                             prev.clone(),
168                         ));
169                     }
170                     return
171                 }
172                 prev_artifacts.insert(id.to_string(), val);
173             }
174         });
175
176         if is_expected && duplicates.len() != 0 {
177             println!("duplicate artfacts found when compiling a tool, this \
178                       typically means that something was recompiled because \
179                       a transitive dependency has different features activated \
180                       than in a previous build:\n");
181             for (id, cur, prev) in duplicates {
182                 println!("  {}", id);
183                 println!("    `{}` enabled features {:?} at {:?}",
184                          cur.0, cur.2, cur.1);
185                 println!("    `{}` enabled features {:?} at {:?}",
186                          prev.0, prev.2, prev.1);
187             }
188             println!("");
189             panic!("tools should not compile multiple copies of the same crate");
190         }
191
192         builder.save_toolstate(tool, if is_expected {
193             ToolState::TestFail
194         } else {
195             ToolState::BuildFail
196         });
197
198         if !is_expected {
199             if !is_ext_tool {
200                 exit(1);
201             } else {
202                 return None;
203             }
204         } else {
205             let cargo_out = builder.cargo_out(compiler, self.mode, target)
206                 .join(exe(tool, &compiler.host));
207             let bin = builder.tools_dir(compiler).join(exe(tool, &compiler.host));
208             builder.copy(&cargo_out, &bin);
209             Some(bin)
210         }
211     }
212 }
213
214 pub fn prepare_tool_cargo(
215     builder: &Builder,
216     compiler: Compiler,
217     mode: Mode,
218     target: Interned<String>,
219     command: &'static str,
220     path: &'static str,
221 ) -> Command {
222     let mut cargo = builder.cargo(compiler, mode, target, command);
223     let dir = builder.src.join(path);
224     cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
225
226     // We don't want to build tools dynamically as they'll be running across
227     // stages and such and it's just easier if they're not dynamically linked.
228     cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
229
230     if let Some(dir) = builder.openssl_install_dir(target) {
231         cargo.env("OPENSSL_STATIC", "1");
232         cargo.env("OPENSSL_DIR", dir);
233         cargo.env("LIBZ_SYS_STATIC", "1");
234     }
235
236     // if tools are using lzma we want to force the build script to build its
237     // own copy
238     cargo.env("LZMA_API_STATIC", "1");
239
240     cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
241     cargo.env("CFG_VERSION", builder.rust_version());
242
243     let info = GitInfo::new(&builder.config, &dir);
244     if let Some(sha) = info.sha() {
245         cargo.env("CFG_COMMIT_HASH", sha);
246     }
247     if let Some(sha_short) = info.sha_short() {
248         cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
249     }
250     if let Some(date) = info.commit_date() {
251         cargo.env("CFG_COMMIT_DATE", date);
252     }
253     cargo
254 }
255
256 macro_rules! tool {
257     ($($name:ident, $path:expr, $tool_name:expr, $mode:expr $(,llvm_tools = $llvm:expr)*;)+) => {
258         #[derive(Copy, Clone)]
259         pub enum Tool {
260             $(
261                 $name,
262             )+
263         }
264
265         impl Tool {
266             pub fn get_mode(&self) -> Mode {
267                 let mode = match self {
268                     $(Tool::$name => $mode,)+
269                 };
270                 mode
271             }
272
273             /// Whether this tool requires LLVM to run
274             pub fn uses_llvm_tools(&self) -> bool {
275                 match self {
276                     $(Tool::$name => true $(&& $llvm)*,)+
277                 }
278             }
279         }
280
281         impl<'a> Builder<'a> {
282             pub fn tool_exe(&self, tool: Tool) -> PathBuf {
283                 let stage = self.tool_default_stage(tool);
284                 match tool {
285                     $(Tool::$name =>
286                         self.ensure($name {
287                             compiler: self.compiler(stage, self.config.build),
288                             target: self.config.build,
289                         }),
290                     )+
291                 }
292             }
293
294             pub fn tool_default_stage(&self, tool: Tool) -> u32 {
295                 // Compile the error-index in the same stage as rustdoc to avoid
296                 // recompiling rustdoc twice if we can. Otherwise compile
297                 // everything else in stage0 as there's no need to rebootstrap
298                 // everything.
299                 match tool {
300                     Tool::ErrorIndex if self.top_stage >= 2 => self.top_stage,
301                     _ => 0,
302                 }
303             }
304         }
305
306         $(
307             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
308         pub struct $name {
309             pub compiler: Compiler,
310             pub target: Interned<String>,
311         }
312
313         impl Step for $name {
314             type Output = PathBuf;
315
316             fn should_run(run: ShouldRun) -> ShouldRun {
317                 run.path($path)
318             }
319
320             fn make_run(run: RunConfig) {
321                 run.builder.ensure($name {
322                     compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
323                     target: run.target,
324                 });
325             }
326
327             fn run(self, builder: &Builder) -> PathBuf {
328                 builder.ensure(ToolBuild {
329                     compiler: self.compiler,
330                     target: self.target,
331                     tool: $tool_name,
332                     mode: $mode,
333                     path: $path,
334                     is_ext_tool: false,
335                     extra_features: Vec::new(),
336                 }).expect("expected to build -- essential tool")
337             }
338         }
339         )+
340     }
341 }
342
343 // FIXME(#51459): We have only checked that RustInstaller does not require
344 // the LLVM binaries when running. We should go through all tools to determine
345 // if they really need LLVM binaries, and make `llvm_tools` a required argument.
346 tool!(
347     Rustbook, "src/tools/rustbook", "rustbook", Mode::ToolRustc;
348     ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::ToolRustc;
349     UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::ToolStd;
350     Tidy, "src/tools/tidy", "tidy", Mode::ToolStd;
351     Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::ToolStd;
352     CargoTest, "src/tools/cargotest", "cargotest", Mode::ToolStd;
353     Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTest;
354     BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::ToolStd;
355     RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::ToolStd;
356     RustInstaller, "src/tools/rust-installer", "fabricate", Mode::ToolStd, llvm_tools = false;
357     RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes", Mode::ToolStd;
358 );
359
360 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
361 pub struct RemoteTestServer {
362     pub compiler: Compiler,
363     pub target: Interned<String>,
364 }
365
366 impl Step for RemoteTestServer {
367     type Output = PathBuf;
368
369     fn should_run(run: ShouldRun) -> ShouldRun {
370         run.path("src/tools/remote-test-server")
371     }
372
373     fn make_run(run: RunConfig) {
374         run.builder.ensure(RemoteTestServer {
375             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
376             target: run.target,
377         });
378     }
379
380     fn run(self, builder: &Builder) -> PathBuf {
381         builder.ensure(ToolBuild {
382             compiler: self.compiler,
383             target: self.target,
384             tool: "remote-test-server",
385             mode: Mode::ToolStd,
386             path: "src/tools/remote-test-server",
387             is_ext_tool: false,
388             extra_features: Vec::new(),
389         }).expect("expected to build -- essential tool")
390     }
391 }
392
393 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
394 pub struct Rustdoc {
395     pub host: Interned<String>,
396 }
397
398 impl Step for Rustdoc {
399     type Output = PathBuf;
400     const DEFAULT: bool = true;
401     const ONLY_HOSTS: bool = true;
402
403     fn should_run(run: ShouldRun) -> ShouldRun {
404         run.path("src/tools/rustdoc")
405     }
406
407     fn make_run(run: RunConfig) {
408         run.builder.ensure(Rustdoc {
409             host: run.host,
410         });
411     }
412
413     fn run(self, builder: &Builder) -> PathBuf {
414         let target_compiler = builder.compiler(builder.top_stage, self.host);
415         let target = target_compiler.host;
416         let build_compiler = if target_compiler.stage == 0 {
417             builder.compiler(0, builder.config.build)
418         } else if target_compiler.stage >= 2 {
419             // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
420             // building rustdoc itself.
421             builder.compiler(target_compiler.stage, builder.config.build)
422         } else {
423             // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
424             // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
425             // compilers, which isn't what we want.
426             builder.compiler(target_compiler.stage - 1, builder.config.build)
427         };
428
429         builder.ensure(compile::Rustc { compiler: build_compiler, target });
430         builder.ensure(compile::Rustc {
431             compiler: build_compiler,
432             target: builder.config.build,
433         });
434
435         let mut cargo = prepare_tool_cargo(builder,
436                                            build_compiler,
437                                            Mode::ToolRustc,
438                                            target,
439                                            "build",
440                                            "src/tools/rustdoc");
441
442         // Most tools don't get debuginfo, but rustdoc should.
443         cargo.env("RUSTC_DEBUGINFO", builder.config.rust_debuginfo.to_string())
444              .env("RUSTC_DEBUGINFO_LINES", builder.config.rust_debuginfo_lines.to_string());
445
446         let _folder = builder.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
447         builder.info(&format!("Building rustdoc for stage{} ({})",
448             target_compiler.stage, target_compiler.host));
449         builder.run(&mut cargo);
450
451         // Cargo adds a number of paths to the dylib search path on windows, which results in
452         // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
453         // rustdoc a different name.
454         let tool_rustdoc = builder.cargo_out(build_compiler, Mode::ToolRustc, target)
455             .join(exe("rustdoc_tool_binary", &target_compiler.host));
456
457         // don't create a stage0-sysroot/bin directory.
458         if target_compiler.stage > 0 {
459             let sysroot = builder.sysroot(target_compiler);
460             let bindir = sysroot.join("bin");
461             t!(fs::create_dir_all(&bindir));
462             let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
463             let _ = fs::remove_file(&bin_rustdoc);
464             builder.copy(&tool_rustdoc, &bin_rustdoc);
465             bin_rustdoc
466         } else {
467             tool_rustdoc
468         }
469     }
470 }
471
472 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
473 pub struct Cargo {
474     pub compiler: Compiler,
475     pub target: Interned<String>,
476 }
477
478 impl Step for Cargo {
479     type Output = PathBuf;
480     const DEFAULT: bool = true;
481     const ONLY_HOSTS: bool = true;
482
483     fn should_run(run: ShouldRun) -> ShouldRun {
484         let builder = run.builder;
485         run.path("src/tools/cargo").default_condition(builder.config.extended)
486     }
487
488     fn make_run(run: RunConfig) {
489         run.builder.ensure(Cargo {
490             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
491             target: run.target,
492         });
493     }
494
495     fn run(self, builder: &Builder) -> PathBuf {
496         builder.ensure(native::Openssl {
497             target: self.target,
498         });
499         // Cargo depends on procedural macros, which requires a full host
500         // compiler to be available, so we need to depend on that.
501         builder.ensure(compile::Rustc {
502             compiler: self.compiler,
503             target: builder.config.build,
504         });
505         builder.ensure(ToolBuild {
506             compiler: self.compiler,
507             target: self.target,
508             tool: "cargo",
509             mode: Mode::ToolRustc,
510             path: "src/tools/cargo",
511             is_ext_tool: false,
512             extra_features: Vec::new(),
513         }).expect("expected to build -- essential tool")
514     }
515 }
516
517 macro_rules! tool_extended {
518     (($sel:ident, $builder:ident),
519        $($name:ident,
520        $toolstate:ident,
521        $path:expr,
522        $tool_name:expr,
523        $extra_deps:block;)+) => {
524         $(
525             #[derive(Debug, Clone, Hash, PartialEq, Eq)]
526         pub struct $name {
527             pub compiler: Compiler,
528             pub target: Interned<String>,
529             pub extra_features: Vec<String>,
530         }
531
532         impl Step for $name {
533             type Output = Option<PathBuf>;
534             const DEFAULT: bool = true;
535             const ONLY_HOSTS: bool = true;
536
537             fn should_run(run: ShouldRun) -> ShouldRun {
538                 let builder = run.builder;
539                 run.path($path).default_condition(builder.config.extended)
540             }
541
542             fn make_run(run: RunConfig) {
543                 run.builder.ensure($name {
544                     compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
545                     target: run.target,
546                     extra_features: Vec::new(),
547                 });
548             }
549
550             #[allow(unused_mut)]
551             fn run(mut $sel, $builder: &Builder) -> Option<PathBuf> {
552                 $extra_deps
553                 $builder.ensure(ToolBuild {
554                     compiler: $sel.compiler,
555                     target: $sel.target,
556                     tool: $tool_name,
557                     mode: Mode::ToolRustc,
558                     path: $path,
559                     extra_features: $sel.extra_features,
560                     is_ext_tool: true,
561                 })
562             }
563         }
564         )+
565     }
566 }
567
568 tool_extended!((self, builder),
569     Cargofmt, rustfmt, "src/tools/rustfmt", "cargo-fmt", {};
570     Clippy, clippy, "src/tools/clippy", "clippy-driver", {
571         // Clippy depends on procedural macros (serde), which requires a full host
572         // compiler to be available, so we need to depend on that.
573         builder.ensure(compile::Rustc {
574             compiler: self.compiler,
575             target: builder.config.build,
576         });
577     };
578     Miri, miri, "src/tools/miri", "miri", {};
579     Rls, rls, "src/tools/rls", "rls", {
580         builder.ensure(native::Openssl {
581             target: self.target,
582         });
583         // RLS depends on procedural macros, which requires a full host
584         // compiler to be available, so we need to depend on that.
585         builder.ensure(compile::Rustc {
586             compiler: self.compiler,
587             target: builder.config.build,
588         });
589     };
590     Rustfmt, rustfmt, "src/tools/rustfmt", "rustfmt", {};
591 );
592
593 impl<'a> Builder<'a> {
594     /// Get a `Command` which is ready to run `tool` in `stage` built for
595     /// `host`.
596     pub fn tool_cmd(&self, tool: Tool) -> Command {
597         let mut cmd = Command::new(self.tool_exe(tool));
598         let compiler = self.compiler(self.tool_default_stage(tool), self.config.build);
599         self.prepare_tool_cmd(compiler, tool, &mut cmd);
600         cmd
601     }
602
603     /// Prepares the `cmd` provided to be able to run the `compiler` provided.
604     ///
605     /// Notably this munges the dynamic library lookup path to point to the
606     /// right location to run `compiler`.
607     fn prepare_tool_cmd(&self, compiler: Compiler, tool: Tool, cmd: &mut Command) {
608         let host = &compiler.host;
609         let mut lib_paths: Vec<PathBuf> = vec![
610             PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
611             self.cargo_out(compiler, tool.get_mode(), *host).join("deps"),
612         ];
613
614         // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
615         // mode) and that C compiler may need some extra PATH modification. Do
616         // so here.
617         if compiler.host.contains("msvc") {
618             let curpaths = env::var_os("PATH").unwrap_or_default();
619             let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
620             for &(ref k, ref v) in self.cc[&compiler.host].env() {
621                 if k != "PATH" {
622                     continue
623                 }
624                 for path in env::split_paths(v) {
625                     if !curpaths.contains(&path) {
626                         lib_paths.push(path);
627                     }
628                 }
629             }
630         }
631
632         // Add the llvm/bin directory to PATH since it contains lots of
633         // useful, platform-independent tools
634         if tool.uses_llvm_tools() {
635             if let Some(llvm_bin_path) = self.llvm_bin_path() {
636                 if host.contains("windows") {
637                     // On Windows, PATH and the dynamic library path are the same,
638                     // so we just add the LLVM bin path to lib_path
639                     lib_paths.push(llvm_bin_path);
640                 } else {
641                     let old_path = env::var_os("PATH").unwrap_or_default();
642                     let new_path = env::join_paths(iter::once(llvm_bin_path)
643                             .chain(env::split_paths(&old_path)))
644                         .expect("Could not add LLVM bin path to PATH");
645                     cmd.env("PATH", new_path);
646                 }
647             }
648         }
649
650         add_lib_path(lib_paths, cmd);
651     }
652
653     fn llvm_bin_path(&self) -> Option<PathBuf> {
654         if self.config.llvm_enabled && !self.config.dry_run {
655             let llvm_config = self.ensure(native::Llvm {
656                 target: self.config.build,
657                 emscripten: false,
658             });
659
660             // Add the llvm/bin directory to PATH since it contains lots of
661             // useful, platform-independent tools
662             let llvm_bin_path = llvm_config.parent()
663                 .expect("Expected llvm-config to be contained in directory");
664             assert!(llvm_bin_path.is_dir());
665             Some(llvm_bin_path.to_path_buf())
666         } else {
667             None
668         }
669     }
670 }