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;
17 use std::borrow::ToOwned;
18 use std::dynamic_lib::DynamicLibrary;
21 use std::path::PathBuf;
23 use syntax::codemap::{Span, COMMAND_LINE_SP};
25 use syntax::attr::AttrMetaMethods;
27 /// Pointer to a registrar function.
28 pub type PluginRegistrarFun =
31 pub struct PluginRegistrar {
32 pub fun: PluginRegistrarFun,
33 pub args: Vec<P<ast::MetaItem>>,
36 struct PluginLoader<'a> {
38 reader: CrateReader<'a>,
39 plugins: Vec<PluginRegistrar>,
42 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
43 span_err!(a, b, E0498, "malformed plugin attribute");
46 /// Read plugin metadata and dynamically load registrar functions.
47 pub fn load_plugins(sess: &Session, krate: &ast::Crate,
48 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
49 let mut loader = PluginLoader::new(sess);
51 for attr in &krate.attrs {
52 if !attr.check_name("plugin") {
56 let plugins = match attr.meta_item_list() {
59 call_malformed_plugin_attribute(sess, attr.span);
64 for plugin in plugins {
65 if plugin.value_str().is_some() {
66 call_malformed_plugin_attribute(sess, attr.span);
70 let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
71 loader.load_plugin(plugin.span, &*plugin.name(), args);
75 if let Some(plugins) = addl_plugins {
76 for plugin in plugins {
77 loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
84 impl<'a> PluginLoader<'a> {
85 fn new(sess: &'a Session) -> PluginLoader<'a> {
88 reader: CrateReader::new(sess),
93 fn load_plugin(&mut self, span: Span, name: &str, args: Vec<P<ast::MetaItem>>) {
94 let registrar = self.reader.find_plugin_registrar(span, name);
96 if let Some((lib, symbol)) = registrar {
97 let fun = self.dylink_registrar(span, lib, symbol);
98 self.plugins.push(PluginRegistrar {
105 // Dynamically link a registrar function into the compiler process.
106 fn dylink_registrar(&mut self,
109 symbol: String) -> PluginRegistrarFun {
110 // Make sure the path contains a / or the linker will search for it.
111 let path = env::current_dir().unwrap().join(&path);
113 let lib = match DynamicLibrary::open(Some(&path)) {
115 // this is fatal: there are almost certainly macros we need
116 // inside this crate, so continue would spew "macro undefined"
119 self.sess.span_fatal(span, &err[..])
125 match lib.symbol(&symbol[..]) {
127 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
129 // again fatal if we can't register macros
131 self.sess.span_fatal(span, &err[..])
135 // Intentionally leak the dynamic library. We can't ever unload it
136 // since the library can make things that will live arbitrarily long
137 // (e.g. an @-box cycle or a thread).