Find codegen backends in more locations

* Search in the sysroot passed using `--sysroot` in addition to the
  default sysroot.
* Search for `librustc_codegen_$name.so` in addition to
  `librustc_codegen_$name-$release.so`.
This commit is contained in:
bjorn3 2021-02-01 19:29:31 +01:00
parent 1fe1fa9122
commit 7f19a2d2de
2 changed files with 19 additions and 10 deletions

View File

@ -799,7 +799,7 @@ pub fn version(binary: &str, matches: &getopts::Matches) {
println!("host: {}", config::host_triple()); println!("host: {}", config::host_triple());
println!("release: {}", unw(util::release_str())); println!("release: {}", unw(util::release_str()));
if cfg!(feature = "llvm") { if cfg!(feature = "llvm") {
get_builtin_codegen_backend("llvm")().print_version(); get_builtin_codegen_backend(&None, "llvm")().print_version();
} }
} }
} }
@ -1088,7 +1088,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
if cg_flags.iter().any(|x| *x == "passes=list") { if cg_flags.iter().any(|x| *x == "passes=list") {
if cfg!(feature = "llvm") { if cfg!(feature = "llvm") {
get_builtin_codegen_backend("llvm")().print_passes(); get_builtin_codegen_backend(&None, "llvm")().print_passes();
} }
return None; return None;
} }

View File

@ -241,7 +241,7 @@ pub fn get_codegen_backend(sopts: &config::Options) -> Box<dyn CodegenBackend> {
let backend = match codegen_name { let backend = match codegen_name {
filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()), filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()),
codegen_name => get_builtin_codegen_backend(codegen_name), codegen_name => get_builtin_codegen_backend(&sopts.maybe_sysroot, codegen_name),
}; };
unsafe { unsafe {
@ -366,15 +366,21 @@ fn sysroot_candidates() -> Vec<PathBuf> {
} }
} }
pub fn get_builtin_codegen_backend(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> { pub fn get_builtin_codegen_backend(
maybe_sysroot: &Option<PathBuf>,
backend_name: &str,
) -> fn() -> Box<dyn CodegenBackend> {
match backend_name { match backend_name {
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new, "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
_ => get_codegen_sysroot(backend_name), _ => get_codegen_sysroot(maybe_sysroot, backend_name),
} }
} }
pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> { pub fn get_codegen_sysroot(
maybe_sysroot: &Option<PathBuf>,
backend_name: &str,
) -> fn() -> Box<dyn CodegenBackend> {
// For now we only allow this function to be called once as it'll dlopen a // For now we only allow this function to be called once as it'll dlopen a
// few things, which seems to work best if we only do that once. In // few things, which seems to work best if we only do that once. In
// general this assertion never trips due to the once guard in `get_codegen_backend`, // general this assertion never trips due to the once guard in `get_codegen_backend`,
@ -389,8 +395,9 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
let target = session::config::host_triple(); let target = session::config::host_triple();
let sysroot_candidates = sysroot_candidates(); let sysroot_candidates = sysroot_candidates();
let sysroot = sysroot_candidates let sysroot = maybe_sysroot
.iter() .iter()
.chain(sysroot_candidates.iter())
.map(|sysroot| { .map(|sysroot| {
let libdir = filesearch::relative_target_lib_path(&sysroot, &target); let libdir = filesearch::relative_target_lib_path(&sysroot, &target);
sysroot.join(libdir).with_file_name("codegen-backends") sysroot.join(libdir).with_file_name("codegen-backends")
@ -426,8 +433,10 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
let mut file: Option<PathBuf> = None; let mut file: Option<PathBuf> = None;
let expected_name = let expected_names = &[
format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")); format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")),
format!("rustc_codegen_{}", backend_name),
];
for entry in d.filter_map(|e| e.ok()) { for entry in d.filter_map(|e| e.ok()) {
let path = entry.path(); let path = entry.path();
let filename = match path.file_name().and_then(|s| s.to_str()) { let filename = match path.file_name().and_then(|s| s.to_str()) {
@ -438,7 +447,7 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
continue; continue;
} }
let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()]; let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
if name != expected_name { if !expected_names.iter().any(|expected| expected == name) {
continue; continue;
} }
if let Some(ref prev) = file { if let Some(ref prev) = file {