2015-02-12 03:53:10 +00:00
|
|
|
//! Used by `rustc` when loading a plugin.
|
2014-05-26 21:48:54 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::Registry;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::Crate;
|
2019-12-31 20:25:16 +00:00
|
|
|
use rustc_errors::struct_span_err;
|
2019-10-17 16:08:06 +00:00
|
|
|
use rustc_metadata::locator;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::middle::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 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2019-12-31 20:25:16 +00:00
|
|
|
|
2015-02-06 21:56:38 +00:00
|
|
|
use std::borrow::ToOwned;
|
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
|
|
|
|
2019-05-22 00:47:23 +00:00
|
|
|
fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
|
|
|
|
struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
|
|
|
|
.span_label(span, "malformed attribute")
|
|
|
|
.emit();
|
2015-09-14 19:25:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 21:48:54 +00:00
|
|
|
/// Read plugin metadata and dynamically load registrar functions.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn load_plugins(
|
|
|
|
sess: &Session,
|
|
|
|
metadata_loader: &dyn MetadataLoader,
|
|
|
|
krate: &Crate,
|
|
|
|
) -> Vec<PluginRegistrarFn> {
|
2019-11-29 22:20:06 +00:00
|
|
|
let mut plugins = Vec::new();
|
|
|
|
|
|
|
|
for attr in &krate.attrs {
|
2020-07-30 01:27:50 +00:00
|
|
|
if !sess.check_name(attr, 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-12-22 22:42:04 +00:00
|
|
|
Some(ident) if plugin.is_word() => {
|
|
|
|
load_plugin(&mut plugins, sess, metadata_loader, ident)
|
|
|
|
}
|
2019-11-29 22:20:06 +00:00
|
|
|
_ => call_malformed_plugin_attribute(sess, 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-12-22 22:42:04 +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);
|
|
|
|
let fun = dylink_registrar(sess, ident.span, lib);
|
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
|
|
|
|
2019-11-29 22:20:06 +00:00
|
|
|
// Dynamically link a registrar function into the compiler process.
|
2021-05-14 13:37:53 +00:00
|
|
|
fn dylink_registrar(sess: &Session, span: Span, path: PathBuf) -> PluginRegistrarFn {
|
2019-11-29 22:20:06 +00:00
|
|
|
use rustc_metadata::dynamic_lib::DynamicLibrary;
|
|
|
|
|
|
|
|
// Make sure the path contains a / or the linker will search for it.
|
|
|
|
let path = env::current_dir().unwrap().join(&path);
|
|
|
|
|
2020-04-24 22:31:42 +00:00
|
|
|
let lib = match DynamicLibrary::open(&path) {
|
2019-11-29 22:20:06 +00:00
|
|
|
Ok(lib) => lib,
|
|
|
|
// this is fatal: there are almost certainly macros we need
|
|
|
|
// inside this crate, so continue would spew "macro undefined"
|
|
|
|
// errors
|
2019-12-22 22:42:04 +00:00
|
|
|
Err(err) => sess.span_fatal(span, &err),
|
2019-11-29 22:20:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
2021-05-14 13:37:53 +00:00
|
|
|
let registrar = match lib.symbol("__rustc_plugin_registrar") {
|
2019-12-22 22:42:04 +00:00
|
|
|
Ok(registrar) => mem::transmute::<*mut u8, PluginRegistrarFn>(registrar),
|
|
|
|
// again fatal if we can't register macros
|
|
|
|
Err(err) => sess.span_fatal(span, &err),
|
|
|
|
};
|
2019-11-29 22:20:06 +00:00
|
|
|
|
|
|
|
// Intentionally leak the dynamic library. We can't ever unload it
|
|
|
|
// since the library can make things that will live arbitrarily long
|
2021-05-14 13:37:53 +00:00
|
|
|
// (e.g., an Rc cycle or a thread).
|
2019-11-29 22:20:06 +00:00
|
|
|
mem::forget(lib);
|
|
|
|
|
|
|
|
registrar
|
2014-05-24 23:16:10 +00:00
|
|
|
}
|
|
|
|
}
|