diff --git a/Cargo.lock b/Cargo.lock index 2f143b097b7f4..9ef45681e7119 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3624,6 +3624,7 @@ dependencies = [ "itertools", "jobserver", "libc", + "libloading", "object 0.28.4", "pathdiff", "regex", diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml index 93b10a07e449d..a913e4d18d72e 100644 --- a/compiler/rustc_codegen_ssa/Cargo.toml +++ b/compiler/rustc_codegen_ssa/Cargo.toml @@ -19,6 +19,7 @@ pathdiff = "0.2.0" snap = "1" smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } regex = "1.4" +libloading = "0.7.1" rustc_serialize = { path = "../rustc_serialize" } rustc_arena = { path = "../rustc_arena" } diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index a2f74b9421468..aa503987eb213 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -4,6 +4,7 @@ use rustc_session::Session; use rustc_span::symbol::Symbol; use std::io; +use std::ffi::OsStr; use std::path::{Path, PathBuf}; pub(super) fn find_library( @@ -32,6 +33,17 @@ pub(super) fn find_library( if test.exists() { return test; } + if cfg!(target_os = "macos") { + // This is a bit unintuitive: On macOS, a shared library may not be present on the file + // system yet still be loadable. This is due to the fact that there's a global cache of shared libraries being + // maintained by the system on BigSur and newer [1]. Therefore, as a last resort, try loading the library + // instead of just checking for its existence as a file. + // [1]: https://web.archive.org/web/20220317152936/https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11-beta-release-notes + let path: &OsStr = test.as_ref(); + if let Ok(_) = unsafe { libloading::Library::new(path) } { + return test; + } + } } } sess.fatal(&format!(