Skip to content

Commit

Permalink
Default to llvm-ar when compiling with clang --target=wasm32.
Browse files Browse the repository at this point in the history
Generalization of the method would facilitate 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.
  • Loading branch information
dot-asm committed Mar 8, 2022
1 parent fba7fed commit a1faddd
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2533,6 +2533,21 @@ impl Build {
}
None => default_ar,
}
} else if target.starts_with("wasm32") {
// Formally speaking one should be able to use this approach,
// parsing -print-search-dirs output, to cover all clang targets,
// including Android SDKs and other cross-compilation scenarios...
// And even extend it to gcc targets by seaching for "ar" instead
// of "llvm-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
}
} else {
default_ar
};
Expand Down Expand Up @@ -3280,7 +3295,7 @@ fn map_darwin_target_from_rust_to_compiler_architecture(target: &str) -> Option<
}
}

fn which(tool: &Path) -> Option<PathBuf> {
fn which(tool: &Path, path_entries: Option<OsString>) -> Option<PathBuf> {
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())
Expand All @@ -3293,9 +3308,23 @@ fn which(tool: &Path) -> Option<PathBuf> {
}

// 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<PathBuf> {
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
}

0 comments on commit a1faddd

Please sign in to comment.