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 registry::Registry;
8 use std::borrow::ToOwned;
11 use std::path::PathBuf;
13 use syntax_pos::{Span, DUMMY_SP};
15 /// Pointer to a registrar function.
16 pub type PluginRegistrarFun =
19 pub struct PluginRegistrar {
20 pub fun: PluginRegistrarFun,
21 pub args: Vec<ast::NestedMetaItem>,
24 struct PluginLoader<'a> {
26 reader: CrateLoader<'a>,
27 plugins: Vec<PluginRegistrar>,
30 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
31 span_err!(a, b, E0498, "malformed plugin attribute");
34 /// Read plugin metadata and dynamically load registrar functions.
35 pub fn load_plugins(sess: &Session,
39 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
40 let mut loader = PluginLoader::new(sess, cstore, crate_name);
42 // do not report any error now. since crate attributes are
43 // not touched by expansion, every use of plugin without
44 // the feature enabled will result in an error later...
45 if sess.features_untracked().plugin {
46 for attr in &krate.attrs {
47 if !attr.check_name("plugin") {
51 let plugins = match attr.meta_item_list() {
56 for plugin in plugins {
57 // plugins must have a name and can't be key = value
59 Some(name) if !plugin.is_value_str() => {
60 let args = plugin.meta_item_list().map(ToOwned::to_owned);
61 loader.load_plugin(plugin.span, &name.as_str(), args.unwrap_or_default());
63 _ => call_malformed_plugin_attribute(sess, attr.span),
69 if let Some(plugins) = addl_plugins {
70 for plugin in plugins {
71 loader.load_plugin(DUMMY_SP, &plugin, vec![]);
78 impl<'a> PluginLoader<'a> {
79 fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
82 reader: CrateLoader::new(sess, cstore, crate_name),
87 fn load_plugin(&mut self, span: Span, name: &str, args: Vec<ast::NestedMetaItem>) {
88 let registrar = self.reader.find_plugin_registrar(span, name);
90 if let Some((lib, disambiguator)) = registrar {
91 let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
92 let fun = self.dylink_registrar(span, lib, symbol);
93 self.plugins.push(PluginRegistrar {
100 // Dynamically link a registrar function into the compiler process.
101 fn dylink_registrar(&mut self,
104 symbol: String) -> PluginRegistrarFun {
105 use rustc_metadata::dynamic_lib::DynamicLibrary;
107 // Make sure the path contains a / or the linker will search for it.
108 let path = env::current_dir().unwrap().join(&path);
110 let lib = match DynamicLibrary::open(Some(&path)) {
112 // this is fatal: there are almost certainly macros we need
113 // inside this crate, so continue would spew "macro undefined"
116 self.sess.span_fatal(span, &err)
122 match lib.symbol(&symbol) {
124 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
126 // again fatal if we can't register macros
128 self.sess.span_fatal(span, &err)
132 // Intentionally leak the dynamic library. We can't ever unload it
133 // since the library can make things that will live arbitrarily long
134 // (e.g., an @-box cycle or a thread).