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