]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/load.rs
c2f8d092b08fdb972e9d2f0342fbc4bf200dcd22
[rust.git] / src / librustc_plugin / load.rs
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.
4 //
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.
10
11 //! Used by `rustc` when loading a plugin.
12
13 use rustc::session::Session;
14 use rustc_metadata::creader::CrateReader;
15 use rustc_metadata::cstore::CStore;
16 use registry::Registry;
17
18 use std::borrow::ToOwned;
19 use std::env;
20 use std::mem;
21 use std::path::PathBuf;
22 use syntax::ast;
23 use syntax::codemap::{Span, COMMAND_LINE_SP};
24 use syntax::ptr::P;
25 use syntax::attr::AttrMetaMethods;
26
27 /// Pointer to a registrar function.
28 pub type PluginRegistrarFun =
29     fn(&mut Registry);
30
31 pub struct PluginRegistrar {
32     pub fun: PluginRegistrarFun,
33     pub args: Vec<P<ast::MetaItem>>,
34 }
35
36 struct PluginLoader<'a> {
37     sess: &'a Session,
38     reader: CrateReader<'a>,
39     plugins: Vec<PluginRegistrar>,
40 }
41
42 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
43     span_err!(a, b, E0498, "malformed plugin attribute");
44 }
45
46 /// Read plugin metadata and dynamically load registrar functions.
47 pub fn load_plugins(sess: &Session, cstore: &CStore, krate: &ast::Crate,
48                     addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
49     let mut loader = PluginLoader::new(sess, cstore);
50
51     for attr in &krate.attrs {
52         if !attr.check_name("plugin") {
53             continue;
54         }
55
56         let plugins = match attr.meta_item_list() {
57             Some(xs) => xs,
58             None => {
59                 call_malformed_plugin_attribute(sess, attr.span);
60                 continue;
61             }
62         };
63
64         for plugin in plugins {
65             if plugin.value_str().is_some() {
66                 call_malformed_plugin_attribute(sess, attr.span);
67                 continue;
68             }
69
70             let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
71             loader.load_plugin(plugin.span, &plugin.name(), args);
72         }
73     }
74
75     if let Some(plugins) = addl_plugins {
76         for plugin in plugins {
77             loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
78         }
79     }
80
81     loader.plugins
82 }
83
84 impl<'a> PluginLoader<'a> {
85     fn new(sess: &'a Session, cstore: &'a CStore) -> PluginLoader<'a> {
86         PluginLoader {
87             sess: sess,
88             reader: CrateReader::new(sess, cstore),
89             plugins: vec![],
90         }
91     }
92
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);
95
96         if let Some((lib, symbol)) = registrar {
97             let fun = self.dylink_registrar(span, lib, symbol);
98             self.plugins.push(PluginRegistrar {
99                 fun: fun,
100                 args: args,
101             });
102         }
103     }
104
105     // Dynamically link a registrar function into the compiler process.
106     #[allow(deprecated)]
107     fn dylink_registrar(&mut self,
108                         span: Span,
109                         path: PathBuf,
110                         symbol: String) -> PluginRegistrarFun {
111         use std::dynamic_lib::DynamicLibrary;
112
113         // Make sure the path contains a / or the linker will search for it.
114         let path = env::current_dir().unwrap().join(&path);
115
116         let lib = match DynamicLibrary::open(Some(&path)) {
117             Ok(lib) => lib,
118             // this is fatal: there are almost certainly macros we need
119             // inside this crate, so continue would spew "macro undefined"
120             // errors
121             Err(err) => {
122                 self.sess.span_fatal(span, &err[..])
123             }
124         };
125
126         unsafe {
127             let registrar =
128                 match lib.symbol(&symbol[..]) {
129                     Ok(registrar) => {
130                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
131                     }
132                     // again fatal if we can't register macros
133                     Err(err) => {
134                         self.sess.span_fatal(span, &err[..])
135                     }
136                 };
137
138             // Intentionally leak the dynamic library. We can't ever unload it
139             // since the library can make things that will live arbitrarily long
140             // (e.g. an @-box cycle or a thread).
141             mem::forget(lib);
142
143             registrar
144         }
145     }
146 }