10
10
11
11
use std:: fs;
12
12
use std:: env;
13
+ use std:: iter;
13
14
use std:: path:: PathBuf ;
14
15
use std:: process:: { Command , exit} ;
15
16
@@ -593,7 +594,7 @@ impl<'a> Builder<'a> {
593
594
/// right location to run `compiler`.
594
595
fn prepare_tool_cmd ( & self , compiler : Compiler , cmd : & mut Command ) {
595
596
let host = & compiler. host ;
596
- let mut paths : Vec < PathBuf > = vec ! [
597
+ let mut lib_paths : Vec < PathBuf > = vec ! [
597
598
PathBuf :: from( & self . sysroot_libdir( compiler, compiler. host) ) ,
598
599
self . cargo_out( compiler, Mode :: Tool , * host) . join( "deps" ) ,
599
600
] ;
@@ -610,11 +611,46 @@ impl<'a> Builder<'a> {
610
611
}
611
612
for path in env:: split_paths ( v) {
612
613
if !curpaths. contains ( & path) {
613
- paths . push ( path) ;
614
+ lib_paths . push ( path) ;
614
615
}
615
616
}
616
617
}
617
618
}
618
- add_lib_path ( paths, cmd) ;
619
+
620
+ // Add the llvm/bin directory to PATH since it contains lots of
621
+ // useful, platform-independent tools
622
+ if let Some ( llvm_bin_path) = self . llvm_bin_path ( ) {
623
+ if host. contains ( "windows" ) {
624
+ // On Windows, PATH and the dynamic library path are the same,
625
+ // so we just add the LLVM bin path to lib_path
626
+ lib_paths. push ( llvm_bin_path) ;
627
+ } else {
628
+ let old_path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
629
+ let new_path = env:: join_paths ( iter:: once ( llvm_bin_path)
630
+ . chain ( env:: split_paths ( & old_path) ) )
631
+ . expect ( "Could not add LLVM bin path to PATH" ) ;
632
+ cmd. env ( "PATH" , new_path) ;
633
+ }
634
+ }
635
+
636
+ add_lib_path ( lib_paths, cmd) ;
637
+ }
638
+
639
+ fn llvm_bin_path ( & self ) -> Option < PathBuf > {
640
+ if self . config . llvm_enabled && !self . config . dry_run {
641
+ let llvm_config = self . ensure ( native:: Llvm {
642
+ target : self . config . build ,
643
+ emscripten : false ,
644
+ } ) ;
645
+
646
+ // Add the llvm/bin directory to PATH since it contains lots of
647
+ // useful, platform-independent tools
648
+ let llvm_bin_path = llvm_config. parent ( )
649
+ . expect ( "Expected llvm-config to be contained in directory" ) ;
650
+ assert ! ( llvm_bin_path. is_dir( ) ) ;
651
+ Some ( llvm_bin_path. to_path_buf ( ) )
652
+ } else {
653
+ None
654
+ }
619
655
}
620
656
}
0 commit comments