1010
1111use std:: fs;
1212use std:: env;
13+ use std:: iter;
1314use std:: path:: PathBuf ;
1415use std:: process:: { Command , exit} ;
1516
@@ -593,7 +594,7 @@ impl<'a> Builder<'a> {
593594 /// right location to run `compiler`.
594595 fn prepare_tool_cmd ( & self , compiler : Compiler , cmd : & mut Command ) {
595596 let host = & compiler. host ;
596- let mut paths : Vec < PathBuf > = vec ! [
597+ let mut lib_paths : Vec < PathBuf > = vec ! [
597598 PathBuf :: from( & self . sysroot_libdir( compiler, compiler. host) ) ,
598599 self . cargo_out( compiler, Mode :: Tool , * host) . join( "deps" ) ,
599600 ] ;
@@ -610,11 +611,46 @@ impl<'a> Builder<'a> {
610611 }
611612 for path in env:: split_paths ( v) {
612613 if !curpaths. contains ( & path) {
613- paths . push ( path) ;
614+ lib_paths . push ( path) ;
614615 }
615616 }
616617 }
617618 }
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+ }
619655 }
620656}
0 commit comments