]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/load.rs
std: Clean out deprecated APIs
[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     fn dylink_registrar(&mut self,
107                         span: Span,
108                         path: PathBuf,
109                         symbol: String) -> PluginRegistrarFun {
110         use rustc_back::dynamic_lib::DynamicLibrary;
111
112         // Make sure the path contains a / or the linker will search for it.
113         let path = env::current_dir().unwrap().join(&path);
114
115         let lib = match DynamicLibrary::open(Some(&path)) {
116             Ok(lib) => lib,
117             // this is fatal: there are almost certainly macros we need
118             // inside this crate, so continue would spew "macro undefined"
119             // errors
120             Err(err) => {
121                 self.sess.span_fatal(span, &err[..])
122             }
123         };
124
125         unsafe {
126             let registrar =
127                 match lib.symbol(&symbol[..]) {
128                     Ok(registrar) => {
129                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
130                     }
131                     // again fatal if we can't register macros
132                     Err(err) => {
133                         self.sess.span_fatal(span, &err[..])
134                     }
135                 };
136
137             // Intentionally leak the dynamic library. We can't ever unload it
138             // since the library can make things that will live arbitrarily long
139             // (e.g. an @-box cycle or a thread).
140             mem::forget(lib);
141
142             registrar
143         }
144     }
145 }