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