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.
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.
14 use dl = std::unstable::dynamic_lib;
16 pub type PluginJson = Option<(~str, json::Json)>;
17 pub type PluginResult = (clean::Crate, PluginJson);
18 pub type PluginCallback = fn (clean::Crate) -> PluginResult;
20 /// Manages loading and running of plugins
21 pub struct PluginManager {
22 priv dylibs: ~[dl::DynamicLibrary],
23 priv callbacks: ~[PluginCallback],
24 /// The directory plugins will be loaded from
29 /// Create a new plugin manager
30 pub fn new(prefix: Path) -> PluginManager {
38 /// Load a plugin with the given name.
40 /// Turns `name` into the proper dynamic library filename for the given
41 /// platform. On windows, it turns into name.dll, on OS X, name.dylib, and
42 /// elsewhere, libname.so.
43 pub fn load_plugin(&mut self, name: ~str) {
44 let x = self.prefix.join(libname(name));
45 let lib_result = dl::DynamicLibrary::open(Some(&x));
46 let lib = lib_result.unwrap();
47 let plugin = unsafe { lib.symbol("rustdoc_plugin_entrypoint") }.unwrap();
48 self.dylibs.push(lib);
49 self.callbacks.push(plugin);
52 /// Load a normal Rust function as a plugin.
54 /// This is to run passes over the cleaned crate. Plugins run this way
55 /// correspond to the A-aux tag on Github.
56 pub fn add_plugin(&mut self, plugin: PluginCallback) {
57 self.callbacks.push(plugin);
59 /// Run all the loaded plugins over the crate, returning their results
60 pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, ~[PluginJson]) {
61 let mut out_json = ~[];
62 let mut krate = krate;
63 for &callback in self.callbacks.iter() {
64 let (c, res) = callback(krate);
72 #[cfg(target_os="win32")]
73 fn libname(mut n: ~str) -> ~str {
78 #[cfg(target_os="macos")]
79 fn libname(mut n: ~str) -> ~str {
84 #[cfg(not(target_os="win32"), not(target_os="macos"))]
85 fn libname(n: ~str) -> ~str {