2015-02-11 19:53:10 -08:00
|
|
|
//! Used by `rustc` when loading a plugin.
|
2014-05-26 14:48:54 -07:00
|
|
|
|
2019-10-16 19:57:10 +03:00
|
|
|
use rustc::middle::cstore::MetadataLoader;
|
2015-11-22 22:14:09 +02:00
|
|
|
use rustc::session::Session;
|
2019-10-17 19:08:06 +03:00
|
|
|
use rustc_metadata::locator;
|
2019-02-06 23:56:39 +09:00
|
|
|
use crate::registry::Registry;
|
2014-05-24 16:16:10 -07:00
|
|
|
|
2015-02-06 13:56:38 -08:00
|
|
|
use std::borrow::ToOwned;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::env;
|
|
|
|
use std::mem;
|
|
|
|
use std::path::PathBuf;
|
2019-11-30 01:20:06 +03:00
|
|
|
use syntax::ast::{Crate, Ident};
|
2019-05-21 17:47:23 -07:00
|
|
|
use syntax::struct_span_err;
|
2019-11-30 01:20:06 +03:00
|
|
|
use syntax::symbol::sym;
|
|
|
|
use syntax_pos::Span;
|
2014-05-24 16:16:10 -07:00
|
|
|
|
2019-11-11 22:46:56 +01:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2014-05-26 14:48:54 -07:00
|
|
|
/// Pointer to a registrar function.
|
2019-11-30 01:20:06 +03:00
|
|
|
type PluginRegistrarFn = fn(&mut Registry<'_>);
|
2014-05-24 16:16:10 -07:00
|
|
|
|
2019-05-21 17:47:23 -07: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 21:25:04 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 14:48:54 -07:00
|
|
|
/// Read plugin metadata and dynamically load registrar functions.
|
2016-02-26 16:25:25 -05:00
|
|
|
pub fn load_plugins(sess: &Session,
|
2019-10-16 19:57:10 +03:00
|
|
|
metadata_loader: &dyn MetadataLoader,
|
2019-11-30 11:31:25 +03:00
|
|
|
krate: &Crate) -> Vec<PluginRegistrarFn> {
|
2019-11-30 01:20:06 +03:00
|
|
|
let mut plugins = Vec::new();
|
|
|
|
|
|
|
|
for attr in &krate.attrs {
|
|
|
|
if !attr.check_name(sym::plugin) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-07 00:43:03 +02:00
|
|
|
|
2019-11-30 01:20:06 +03:00
|
|
|
for plugin in attr.meta_item_list().unwrap_or_default() {
|
|
|
|
match plugin.ident() {
|
2019-11-30 11:31:25 +03:00
|
|
|
Some(ident) if plugin.is_word() =>
|
|
|
|
load_plugin(&mut plugins, sess, metadata_loader, ident),
|
2019-11-30 01:20:06 +03:00
|
|
|
_ => call_malformed_plugin_attribute(sess, plugin.span()),
|
2016-04-07 00:43:03 +02:00
|
|
|
}
|
2015-02-06 13:56:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 01:20:06 +03:00
|
|
|
plugins
|
2014-12-09 22:14:08 +05:30
|
|
|
}
|
|
|
|
|
2019-11-30 01:20:06 +03:00
|
|
|
fn load_plugin(plugins: &mut Vec<PluginRegistrarFn>,
|
|
|
|
sess: &Session,
|
|
|
|
metadata_loader: &dyn MetadataLoader,
|
|
|
|
ident: Ident) {
|
|
|
|
let registrar = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
|
|
|
|
|
|
|
|
if let Some((lib, disambiguator)) = registrar {
|
|
|
|
let symbol = sess.generate_plugin_registrar_symbol(disambiguator);
|
|
|
|
let fun = dylink_registrar(sess, ident.span, lib, symbol);
|
|
|
|
plugins.push(fun);
|
2014-05-24 16:16:10 -07:00
|
|
|
}
|
2019-11-30 01:20:06 +03:00
|
|
|
}
|
2014-12-31 19:48:39 -08:00
|
|
|
|
2019-11-30 01:20:06 +03:00
|
|
|
// Dynamically link a registrar function into the compiler process.
|
|
|
|
fn dylink_registrar(sess: &Session,
|
|
|
|
span: Span,
|
|
|
|
path: PathBuf,
|
|
|
|
symbol: String) -> PluginRegistrarFn {
|
|
|
|
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);
|
|
|
|
|
|
|
|
let lib = match DynamicLibrary::open(Some(&path)) {
|
|
|
|
Ok(lib) => lib,
|
|
|
|
// this is fatal: there are almost certainly macros we need
|
|
|
|
// inside this crate, so continue would spew "macro undefined"
|
|
|
|
// errors
|
|
|
|
Err(err) => {
|
|
|
|
sess.span_fatal(span, &err)
|
2014-05-24 16:16:10 -07:00
|
|
|
}
|
2019-11-30 01:20:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let registrar =
|
|
|
|
match lib.symbol(&symbol) {
|
|
|
|
Ok(registrar) => {
|
|
|
|
mem::transmute::<*mut u8, PluginRegistrarFn>(registrar)
|
|
|
|
}
|
|
|
|
// again fatal if we can't register macros
|
|
|
|
Err(err) => {
|
|
|
|
sess.span_fatal(span, &err)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 @-box cycle or a thread).
|
|
|
|
mem::forget(lib);
|
|
|
|
|
|
|
|
registrar
|
2014-05-24 16:16:10 -07:00
|
|
|
}
|
|
|
|
}
|