]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/plugins.rs
Removed some unnecessary RefCells from resolve
[rust.git] / src / librustdoc / plugins.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 use clean;
12
13 use std::dynamic_lib as dl;
14 use serialize::json;
15 use std::mem;
16 use std::string::String;
17
18 pub type PluginJson = Option<(String, json::Json)>;
19 pub type PluginResult = (clean::Crate, PluginJson);
20 pub type PluginCallback = fn (clean::Crate) -> PluginResult;
21
22 /// Manages loading and running of plugins
23 pub struct PluginManager {
24     dylibs: Vec<dl::DynamicLibrary> ,
25     callbacks: Vec<PluginCallback> ,
26     /// The directory plugins will be loaded from
27     pub prefix: Path,
28 }
29
30 impl PluginManager {
31     /// Create a new plugin manager
32     pub fn new(prefix: Path) -> PluginManager {
33         PluginManager {
34             dylibs: Vec::new(),
35             callbacks: Vec::new(),
36             prefix: prefix,
37         }
38     }
39
40     /// Load a plugin with the given name.
41     ///
42     /// Turns `name` into the proper dynamic library filename for the given
43     /// platform. On windows, it turns into name.dll, on OS X, name.dylib, and
44     /// elsewhere, libname.so.
45     pub fn load_plugin(&mut self, name: String) {
46         let x = self.prefix.join(libname(name));
47         let lib_result = dl::DynamicLibrary::open(Some(&x));
48         let lib = lib_result.unwrap();
49         unsafe {
50             let plugin = lib.symbol("rustdoc_plugin_entrypoint").unwrap();
51             self.callbacks.push(mem::transmute::<*mut u8,PluginCallback>(plugin));
52         }
53         self.dylibs.push(lib);
54     }
55
56     /// Load a normal Rust function as a plugin.
57     ///
58     /// This is to run passes over the cleaned crate. Plugins run this way
59     /// correspond to the A-aux tag on Github.
60     pub fn add_plugin(&mut self, plugin: PluginCallback) {
61         self.callbacks.push(plugin);
62     }
63     /// Run all the loaded plugins over the crate, returning their results
64     pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, Vec<PluginJson> ) {
65         let mut out_json = Vec::new();
66         let mut krate = krate;
67         for &callback in self.callbacks.iter() {
68             let (c, res) = callback(krate);
69             krate = c;
70             out_json.push(res);
71         }
72         (krate, out_json)
73     }
74 }
75
76 #[cfg(target_os = "windows")]
77 fn libname(mut n: String) -> String {
78     n.push_str(".dll");
79     n
80 }
81
82 #[cfg(target_os="macos")]
83 fn libname(mut n: String) -> String {
84     n.push_str(".dylib");
85     n
86 }
87
88 #[cfg(not(target_os="windows"), not(target_os="macos"))]
89 fn libname(n: String) -> String {
90     let mut i = String::from_str("lib");
91     i.push_str(n.as_slice());
92     i.push_str(".so");
93     i
94 }