about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-12 17:28:52 +0000
committerbors <bors@rust-lang.org>2021-12-12 17:28:52 +0000
commit6bda5b331cfe7e04e1fe348c58a928fc2b650f4f (patch)
treefa337d4fa5d7143dfe8fc6aed1b3c77fd287af74 /compiler/rustc_codegen_llvm/src
parent753e569c9c2a4e3ef394ef7abd0802bf57f66bce (diff)
parent923f939791a08d3f58566b0fc755381de031f43e (diff)
downloadrust-6bda5b331cfe7e04e1fe348c58a928fc2b650f4f.tar.gz
rust-6bda5b331cfe7e04e1fe348c58a928fc2b650f4f.zip
Auto merge of #90716 - euclio:libloading, r=cjgillot
replace dynamic library module with libloading

This PR deletes the `rustc_metadata::dynamic_lib` module in favor of the popular and better tested [`libloading` crate](https://github.com/nagisa/rust_libloading/).

We don't benefit from `libloading`'s symbol lifetimes since we end up leaking the loaded library in all cases, but the call-sites look much nicer by improving error handling and abstracting away some transmutes. We also can remove `rustc_metadata`'s direct dependencies on `libc` and `winapi`.

This PR also adds an exception for `libloading` (and its license) to tidy, so this will need sign-off from the compiler team.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
-rw-r--r--compiler/rustc_codegen_llvm/src/llvm_util.rs17
1 files changed, 8 insertions, 9 deletions
diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs
index 3393c9baa28..79a261244d3 100644
--- a/compiler/rustc_codegen_llvm/src/llvm_util.rs
+++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs
@@ -1,9 +1,9 @@
 use crate::back::write::create_informational_target_machine;
 use crate::{llvm, llvm_util};
 use libc::c_int;
+use libloading::Library;
 use rustc_codegen_ssa::target_features::supported_target_features;
 use rustc_data_structures::fx::FxHashSet;
-use rustc_metadata::dynamic_lib::DynamicLibrary;
 use rustc_middle::bug;
 use rustc_session::config::PrintRequest;
 use rustc_session::Session;
@@ -13,7 +13,6 @@ use std::ffi::{CStr, CString};
 use tracing::debug;
 
 use std::mem;
-use std::path::Path;
 use std::ptr;
 use std::slice;
 use std::str;
@@ -120,14 +119,14 @@ unsafe fn configure_llvm(sess: &Session) {
 
     llvm::LLVMInitializePasses();
 
+    // Register LLVM plugins by loading them into the compiler process.
     for plugin in &sess.opts.debugging_opts.llvm_plugins {
-        let path = Path::new(plugin);
-        let res = DynamicLibrary::open(path);
-        match res {
-            Ok(_) => debug!("LLVM plugin loaded succesfully {} ({})", path.display(), plugin),
-            Err(e) => bug!("couldn't load plugin: {}", e),
-        }
-        mem::forget(res);
+        let lib = Library::new(plugin).unwrap_or_else(|e| bug!("couldn't load plugin: {}", e));
+        debug!("LLVM plugin loaded successfully {:?} ({})", lib, plugin);
+
+        // Intentionally leak the dynamic library. We can't ever unload it
+        // since the library can make things that will live arbitrarily long.
+        mem::forget(lib);
     }
 
     rustc_llvm::initialize_available_targets();