1 //! Used by `rustc` when loading a plugin.
3 use rustc::session::Session;
4 use rustc_metadata::creader::CrateLoader;
5 use rustc_metadata::cstore::CStore;
6 use crate::registry::Registry;
8 use std::borrow::ToOwned;
11 use std::path::PathBuf;
14 use syntax_pos::{Span, DUMMY_SP};
16 /// Pointer to a registrar function.
17 pub type PluginRegistrarFun =
18 fn(&mut Registry<'_>);
20 pub struct PluginRegistrar {
21 pub fun: PluginRegistrarFun,
22 pub args: Vec<ast::NestedMetaItem>,
25 struct PluginLoader<'a> {
27 reader: CrateLoader<'a>,
28 plugins: Vec<PluginRegistrar>,
31 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
32 span_err!(a, b, E0498, "malformed plugin attribute");
35 /// Read plugin metadata and dynamically load registrar functions.
36 pub fn load_plugins(sess: &Session,
40 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
41 let mut loader = PluginLoader::new(sess, cstore, crate_name);
43 // do not report any error now. since crate attributes are
44 // not touched by expansion, every use of plugin without
45 // the feature enabled will result in an error later...
46 if sess.features_untracked().plugin {
47 for attr in &krate.attrs {
48 if !attr.check_name("plugin") {
52 let plugins = match attr.meta_item_list() {
57 for plugin in plugins {
58 // plugins must have a name and can't be key = value
59 match plugin.ident_str() {
60 Some(name) if !plugin.is_value_str() => {
61 let args = plugin.meta_item_list().map(ToOwned::to_owned);
62 loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
64 _ => call_malformed_plugin_attribute(sess, attr.span),
70 if let Some(plugins) = addl_plugins {
71 for plugin in plugins {
72 loader.load_plugin(DUMMY_SP, &plugin, vec![]);
79 impl<'a> PluginLoader<'a> {
80 fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
83 reader: CrateLoader::new(sess, cstore, crate_name),
88 fn load_plugin(&mut self, span: Span, name: &str, args: Vec<ast::NestedMetaItem>) {
89 let registrar = self.reader.find_plugin_registrar(span, name);
91 if let Some((lib, disambiguator)) = registrar {
92 let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
93 let fun = self.dylink_registrar(span, lib, symbol);
94 self.plugins.push(PluginRegistrar {
101 // Dynamically link a registrar function into the compiler process.
102 fn dylink_registrar(&mut self,
105 symbol: String) -> PluginRegistrarFun {
106 use rustc_metadata::dynamic_lib::DynamicLibrary;
108 // Make sure the path contains a / or the linker will search for it.
109 let path = env::current_dir().unwrap().join(&path);
111 let lib = match DynamicLibrary::open(Some(&path)) {
113 // this is fatal: there are almost certainly macros we need
114 // inside this crate, so continue would spew "macro undefined"
117 self.sess.span_fatal(span, &err)
123 match lib.symbol(&symbol) {
125 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
127 // again fatal if we can't register macros
129 self.sess.span_fatal(span, &err)
133 // Intentionally leak the dynamic library. We can't ever unload it
134 // since the library can make things that will live arbitrarily long
135 // (e.g., an @-box cycle or a thread).