2015-02-12 03:53:10 +00:00
|
|
|
//! Used by `rustc` when loading a plugin.
|
2014-05-26 21:48:54 +00:00
|
|
|
|
2022-08-19 17:29:33 +00:00
|
|
|
use crate::errors::{LoadPluginError, MalformedPluginAttribute};
|
2019-11-30 12:35:25 +00:00
|
|
|
use crate::Registry;
|
2021-11-08 23:03:55 +00:00
|
|
|
use libloading::Library;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::Crate;
|
2019-10-17 16:08:06 +00:00
|
|
|
use rustc_metadata::locator;
|
2020-11-14 02:02:03 +00:00
|
|
|
use rustc_session::cstore::MetadataLoader;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::Session;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{sym, Ident};
|
2019-12-31 20:25:16 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::env;
|
|
|
|
use std::mem;
|
|
|
|
use std::path::PathBuf;
|
2019-11-11 21:46:56 +00:00
|
|
|
|
2014-05-26 21:48:54 +00:00
|
|
|
/// Pointer to a registrar function.
|
2019-11-29 22:20:06 +00:00
|
|
|
type PluginRegistrarFn = fn(&mut Registry<'_>);
|
2014-05-24 23:16:10 +00:00
|
|
|
|
2014-05-26 21:48:54 +00:00
|
|
|
/// Read plugin metadata and dynamically load registrar functions.
|
2016-02-26 21:25:25 +00:00
|
|
|
pub fn load_plugins(
|
|
|
|
sess: &Session,
|
2019-10-16 16:57:10 +00:00
|
|
|
metadata_loader: &dyn MetadataLoader,
|
2019-11-30 08:31:25 +00:00
|
|
|
krate: &Crate,
|
|
|
|
) -> Vec<PluginRegistrarFn> {
|
2019-11-29 22:20:06 +00:00
|
|
|
let mut plugins = Vec::new();
|
|
|
|
|
|
|
|
for attr in &krate.attrs {
|
2021-07-29 17:00:41 +00:00
|
|
|
if !attr.has_name(sym::plugin) {
|
2019-11-29 22:20:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-06 22:43:03 +00:00
|
|
|
|
2019-11-29 22:20:06 +00:00
|
|
|
for plugin in attr.meta_item_list().unwrap_or_default() {
|
|
|
|
match plugin.ident() {
|
2019-11-30 08:31:25 +00:00
|
|
|
Some(ident) if plugin.is_word() => {
|
|
|
|
load_plugin(&mut plugins, sess, metadata_loader, ident)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2022-08-19 17:29:33 +00:00
|
|
|
_ => {
|
|
|
|
sess.emit_err(MalformedPluginAttribute { span: plugin.span() });
|
|
|
|
}
|
2016-04-06 22:43:03 +00:00
|
|
|
}
|
2015-02-06 21:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 22:20:06 +00:00
|
|
|
plugins
|
2014-12-09 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 22:20:06 +00:00
|
|
|
fn load_plugin(
|
|
|
|
plugins: &mut Vec<PluginRegistrarFn>,
|
|
|
|
sess: &Session,
|
|
|
|
metadata_loader: &dyn MetadataLoader,
|
|
|
|
ident: Ident,
|
|
|
|
) {
|
2021-05-14 13:37:53 +00:00
|
|
|
let lib = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
|
2021-11-08 23:03:55 +00:00
|
|
|
let fun = dylink_registrar(lib).unwrap_or_else(|err| {
|
|
|
|
// This is fatal: there are almost certainly macros we need inside this crate, so
|
|
|
|
// continuing would spew "macro undefined" errors.
|
2022-08-19 17:29:33 +00:00
|
|
|
sess.emit_fatal(LoadPluginError { span: ident.span, msg: err.to_string() });
|
2021-11-08 23:03:55 +00:00
|
|
|
});
|
2020-07-05 07:39:15 +00:00
|
|
|
plugins.push(fun);
|
2019-11-29 22:20:06 +00:00
|
|
|
}
|
2015-01-01 03:48:39 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
/// Dynamically link a registrar function into the compiler process.
|
|
|
|
fn dylink_registrar(lib_path: PathBuf) -> Result<PluginRegistrarFn, libloading::Error> {
|
2019-11-29 22:20:06 +00:00
|
|
|
// Make sure the path contains a / or the linker will search for it.
|
2021-11-08 23:03:55 +00:00
|
|
|
let lib_path = env::current_dir().unwrap().join(&lib_path);
|
2019-11-29 22:20:06 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
let lib = unsafe { Library::new(&lib_path) }?;
|
2019-11-29 22:20:06 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
let registrar_sym = unsafe { lib.get::<PluginRegistrarFn>(b"__rustc_plugin_registrar") }?;
|
2019-11-29 22:20:06 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
// Intentionally leak the dynamic library. We can't ever unload it
|
|
|
|
// since the library can make things that will live arbitrarily long
|
|
|
|
// (e.g., an Rc cycle or a thread).
|
|
|
|
let registrar_sym = unsafe { registrar_sym.into_raw() };
|
|
|
|
mem::forget(lib);
|
2019-11-29 22:20:06 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
Ok(*registrar_sym)
|
2014-05-24 23:16:10 +00:00
|
|
|
}
|