From 81bf2388a6ff65efde1416affef816197f27af8b Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 14 Feb 2022 16:50:53 +0100 Subject: [PATCH] Default to llvm-ar when compiling with clang. Defaulting to llvm-ar facilitates cases when system ar(1) doesn't support any given cross-compile target. This is because accompanying llvm-ar is known to match targets supported by the clang installation at hand. --- src/lib.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e3a2b98b0..ec1d5cdb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1029,7 +1029,7 @@ impl Build { None => "none", }; if cudart != "none" { - if let Some(nvcc) = which(&self.get_compiler().path) { + if let Some(nvcc) = which(&self.get_compiler().path, None) { // Try to figure out the -L search path. If it fails, // it's on user to specify one by passing it through // RUSTFLAGS environment variable. @@ -2534,7 +2534,15 @@ impl Build { None => default_ar, } } else { - default_ar + let compiler = self.get_base_compiler()?; + if compiler.family == ToolFamily::Clang { + match search_programs(&mut self.cmd(&compiler.path), "llvm-ar") { + Some(ar) => ar.to_str().unwrap().to_owned(), + None => default_ar, + } + } else { + default_ar + } }; Ok((self.cmd(&program), program)) } @@ -3280,7 +3288,7 @@ fn map_darwin_target_from_rust_to_compiler_architecture(target: &str) -> Option< } } -fn which(tool: &Path) -> Option { +fn which(tool: &Path, path_entries: Option) -> Option { fn check_exe(exe: &mut PathBuf) -> bool { let exe_ext = std::env::consts::EXE_EXTENSION; exe.exists() || (!exe_ext.is_empty() && exe.set_extension(exe_ext) && exe.exists()) @@ -3293,9 +3301,23 @@ fn which(tool: &Path) -> Option { } // Loop through PATH entries searching for the |tool|. - let path_entries = env::var_os("PATH")?; + let path_entries = path_entries.or(env::var_os("PATH"))?; env::split_paths(&path_entries).find_map(|path_entry| { let mut exe = path_entry.join(tool); return if check_exe(&mut exe) { Some(exe) } else { None }; }) } + +// search for |prog| on 'programs' path in '|cc| -print-search-dirs' output +fn search_programs(cc: &mut Command, prog: &str) -> Option { + let search_dirs = run_output(cc.arg("-print-search-dirs"), "cc").ok()?; + // clang driver appears to be forcing UTF-8 output even on Windows, + // hence from_utf8 is assumed to be usable in all cases. + let search_dirs = std::str::from_utf8(&search_dirs).ok()?; + for dirs in search_dirs.split(|c| c == '\r' || c == '\n') { + if dirs.starts_with("programs: =") { + return which(Path::new(prog), Some(OsString::from(&dirs[11..]))); + } + } + None +}