1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Used by `rustc` when loading a plugin.
14 use metadata::creader::CrateReader;
15 use plugin::registry::Registry;
19 use std::dynamic_lib::DynamicLibrary;
20 use std::borrow::ToOwned;
22 use syntax::codemap::{Span, COMMAND_LINE_SP};
24 use syntax::attr::AttrMetaMethods;
26 /// Pointer to a registrar function.
27 pub type PluginRegistrarFun =
30 pub struct PluginRegistrar {
31 pub fun: PluginRegistrarFun,
32 pub args: Vec<P<ast::MetaItem>>,
35 struct PluginLoader<'a> {
37 reader: CrateReader<'a>,
38 plugins: Vec<PluginRegistrar>,
41 /// Read plugin metadata and dynamically load registrar functions.
42 pub fn load_plugins(sess: &Session, krate: &ast::Crate,
43 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
44 let mut loader = PluginLoader::new(sess);
46 for attr in &krate.attrs {
47 if !attr.check_name("plugin") {
51 let plugins = match attr.meta_item_list() {
54 sess.span_err(attr.span, "malformed plugin attribute");
59 for plugin in plugins {
60 if plugin.value_str().is_some() {
61 sess.span_err(attr.span, "malformed plugin attribute");
65 let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
66 loader.load_plugin(plugin.span, &*plugin.name(), args);
70 if let Some(plugins) = addl_plugins {
71 for plugin in plugins {
72 loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
79 impl<'a> PluginLoader<'a> {
80 fn new(sess: &'a Session) -> PluginLoader<'a> {
83 reader: CrateReader::new(sess),
88 fn load_plugin(&mut self, span: Span, name: &str, args: Vec<P<ast::MetaItem>>) {
89 let registrar = self.reader.find_plugin_registrar(span, name);
91 if let Some((lib, symbol)) = registrar {
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 // Make sure the path contains a / or the linker will search for it.
106 let path = env::current_dir().unwrap().join(&path);
108 let lib = match DynamicLibrary::open(Some(&path)) {
110 // this is fatal: there are almost certainly macros we need
111 // inside this crate, so continue would spew "macro undefined"
114 self.sess.span_fatal(span, &err[..])
120 match lib.symbol(&symbol[..]) {
122 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
124 // again fatal if we can't register macros
126 self.sess.span_fatal(span, &err[..])
130 // Intentionally leak the dynamic library. We can't ever unload it
131 // since the library can make things that will live arbitrarily long
132 // (e.g. an @-box cycle or a task).