]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/load.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[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 session::Session;
14 use metadata::creader::CrateReader;
15 use plugin::registry::Registry;
16
17 use std::mem;
18 use std::env;
19 use std::dynamic_lib::DynamicLibrary;
20 use std::borrow::ToOwned;
21 use syntax::ast;
22 use syntax::codemap::{Span, COMMAND_LINE_SP};
23 use syntax::ptr::P;
24 use syntax::attr::AttrMetaMethods;
25
26 /// Pointer to a registrar function.
27 pub type PluginRegistrarFun =
28     fn(&mut Registry);
29
30 pub struct PluginRegistrar {
31     pub fun: PluginRegistrarFun,
32     pub args: Vec<P<ast::MetaItem>>,
33 }
34
35 struct PluginLoader<'a> {
36     sess: &'a Session,
37     reader: CrateReader<'a>,
38     plugins: Vec<PluginRegistrar>,
39 }
40
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);
45
46     for attr in &krate.attrs {
47         if !attr.check_name("plugin") {
48             continue;
49         }
50
51         let plugins = match attr.meta_item_list() {
52             Some(xs) => xs,
53             None => {
54                 sess.span_err(attr.span, "malformed plugin attribute");
55                 continue;
56             }
57         };
58
59         for plugin in plugins {
60             if plugin.value_str().is_some() {
61                 sess.span_err(attr.span, "malformed plugin attribute");
62                 continue;
63             }
64
65             let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
66             loader.load_plugin(plugin.span, &*plugin.name(), args);
67         }
68     }
69
70     if let Some(plugins) = addl_plugins {
71         for plugin in plugins {
72             loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
73         }
74     }
75
76     loader.plugins
77 }
78
79 impl<'a> PluginLoader<'a> {
80     fn new(sess: &'a Session) -> PluginLoader<'a> {
81         PluginLoader {
82             sess: sess,
83             reader: CrateReader::new(sess),
84             plugins: vec![],
85         }
86     }
87
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);
90
91         if let Some((lib, symbol)) = registrar {
92             let fun = self.dylink_registrar(span, lib, symbol);
93             self.plugins.push(PluginRegistrar {
94                 fun: fun,
95                 args: args,
96             });
97         }
98     }
99
100     // Dynamically link a registrar function into the compiler process.
101     fn dylink_registrar(&mut self,
102                         span: Span,
103                         path: Path,
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);
107
108         let lib = match DynamicLibrary::open(Some(&path)) {
109             Ok(lib) => lib,
110             // this is fatal: there are almost certainly macros we need
111             // inside this crate, so continue would spew "macro undefined"
112             // errors
113             Err(err) => {
114                 self.sess.span_fatal(span, &err[])
115             }
116         };
117
118         unsafe {
119             let registrar =
120                 match lib.symbol(&symbol[]) {
121                     Ok(registrar) => {
122                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
123                     }
124                     // again fatal if we can't register macros
125                     Err(err) => {
126                         self.sess.span_fatal(span, &err[])
127                     }
128                 };
129
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).
133             mem::forget(lib);
134
135             registrar
136         }
137     }
138 }