Skip to content

Commit 354cc75

Browse files
committed
Auto merge of #81641 - bjorn3:find_codegen_backend, r=davidtwco
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 combined would allow putting `librustc_codegen_cranelift.so` in the right location of a sysroot passed using `--sysroot`.
2 parents a6e7a5a + 7f19a2d commit 354cc75

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

compiler/rustc_driver/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ pub fn version(binary: &str, matches: &getopts::Matches) {
795795
println!("host: {}", config::host_triple());
796796
println!("release: {}", unw(util::release_str()));
797797
if cfg!(feature = "llvm") {
798-
get_builtin_codegen_backend("llvm")().print_version();
798+
get_builtin_codegen_backend(&None, "llvm")().print_version();
799799
}
800800
}
801801
}
@@ -1089,7 +1089,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
10891089

10901090
if cg_flags.iter().any(|x| *x == "passes=list") {
10911091
if cfg!(feature = "llvm") {
1092-
get_builtin_codegen_backend("llvm")().print_passes();
1092+
get_builtin_codegen_backend(&None, "llvm")().print_passes();
10931093
}
10941094
return None;
10951095
}

compiler/rustc_interface/src/util.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub fn get_codegen_backend(sopts: &config::Options) -> Box<dyn CodegenBackend> {
265265

266266
let backend = match codegen_name {
267267
filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()),
268-
codegen_name => get_builtin_codegen_backend(codegen_name),
268+
codegen_name => get_builtin_codegen_backend(&sopts.maybe_sysroot, codegen_name),
269269
};
270270

271271
unsafe {
@@ -390,15 +390,21 @@ fn sysroot_candidates() -> Vec<PathBuf> {
390390
}
391391
}
392392

393-
pub fn get_builtin_codegen_backend(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> {
393+
pub fn get_builtin_codegen_backend(
394+
maybe_sysroot: &Option<PathBuf>,
395+
backend_name: &str,
396+
) -> fn() -> Box<dyn CodegenBackend> {
394397
match backend_name {
395398
#[cfg(feature = "llvm")]
396399
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
397-
_ => get_codegen_sysroot(backend_name),
400+
_ => get_codegen_sysroot(maybe_sysroot, backend_name),
398401
}
399402
}
400403

401-
pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> {
404+
pub fn get_codegen_sysroot(
405+
maybe_sysroot: &Option<PathBuf>,
406+
backend_name: &str,
407+
) -> fn() -> Box<dyn CodegenBackend> {
402408
// For now we only allow this function to be called once as it'll dlopen a
403409
// few things, which seems to work best if we only do that once. In
404410
// general this assertion never trips due to the once guard in `get_codegen_backend`,
@@ -413,8 +419,9 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
413419
let target = session::config::host_triple();
414420
let sysroot_candidates = sysroot_candidates();
415421

416-
let sysroot = sysroot_candidates
422+
let sysroot = maybe_sysroot
417423
.iter()
424+
.chain(sysroot_candidates.iter())
418425
.map(|sysroot| {
419426
let libdir = filesearch::relative_target_lib_path(&sysroot, &target);
420427
sysroot.join(libdir).with_file_name("codegen-backends")
@@ -450,8 +457,10 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
450457

451458
let mut file: Option<PathBuf> = None;
452459

453-
let expected_name =
454-
format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE"));
460+
let expected_names = &[
461+
format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")),
462+
format!("rustc_codegen_{}", backend_name),
463+
];
455464
for entry in d.filter_map(|e| e.ok()) {
456465
let path = entry.path();
457466
let filename = match path.file_name().and_then(|s| s.to_str()) {
@@ -462,7 +471,7 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
462471
continue;
463472
}
464473
let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
465-
if name != expected_name {
474+
if !expected_names.iter().any(|expected| expected == name) {
466475
continue;
467476
}
468477
if let Some(ref prev) = file {

0 commit comments

Comments
 (0)