2013-09-12 20:10:51 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
use clean;
|
|
|
|
|
2014-02-21 16:18:39 -06:00
|
|
|
use serialize::json;
|
2013-08-15 15:28:54 -05:00
|
|
|
use dl = std::unstable::dynamic_lib;
|
|
|
|
|
2014-02-13 19:49:11 -06:00
|
|
|
pub type PluginJson = Option<(~str, json::Json)>;
|
2013-08-15 15:28:54 -05:00
|
|
|
pub type PluginResult = (clean::Crate, PluginJson);
|
2014-02-13 22:23:01 -06:00
|
|
|
pub type PluginCallback = fn (clean::Crate) -> PluginResult;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
/// Manages loading and running of plugins
|
|
|
|
pub struct PluginManager {
|
|
|
|
priv dylibs: ~[dl::DynamicLibrary],
|
2014-02-10 08:36:31 -06:00
|
|
|
priv callbacks: ~[PluginCallback],
|
2013-08-15 15:28:54 -05:00
|
|
|
/// The directory plugins will be loaded from
|
|
|
|
prefix: Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginManager {
|
|
|
|
/// Create a new plugin manager
|
|
|
|
pub fn new(prefix: Path) -> PluginManager {
|
|
|
|
PluginManager {
|
|
|
|
dylibs: ~[],
|
|
|
|
callbacks: ~[],
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load a plugin with the given name.
|
|
|
|
///
|
|
|
|
/// Turns `name` into the proper dynamic library filename for the given
|
|
|
|
/// platform. On windows, it turns into name.dll, on OS X, name.dylib, and
|
|
|
|
/// elsewhere, libname.so.
|
|
|
|
pub fn load_plugin(&mut self, name: ~str) {
|
2013-10-05 21:49:32 -05:00
|
|
|
let x = self.prefix.join(libname(name));
|
2013-08-15 15:28:54 -05:00
|
|
|
let lib_result = dl::DynamicLibrary::open(Some(&x));
|
|
|
|
let lib = lib_result.unwrap();
|
|
|
|
let plugin = unsafe { lib.symbol("rustdoc_plugin_entrypoint") }.unwrap();
|
|
|
|
self.dylibs.push(lib);
|
|
|
|
self.callbacks.push(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load a normal Rust function as a plugin.
|
|
|
|
///
|
|
|
|
/// This is to run passes over the cleaned crate. Plugins run this way
|
|
|
|
/// correspond to the A-aux tag on Github.
|
2014-02-10 08:36:31 -06:00
|
|
|
pub fn add_plugin(&mut self, plugin: PluginCallback) {
|
2013-08-15 15:28:54 -05:00
|
|
|
self.callbacks.push(plugin);
|
|
|
|
}
|
|
|
|
/// Run all the loaded plugins over the crate, returning their results
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, ~[PluginJson]) {
|
2013-08-15 15:28:54 -05:00
|
|
|
let mut out_json = ~[];
|
2014-02-05 15:15:24 -06:00
|
|
|
let mut krate = krate;
|
2013-08-15 15:28:54 -05:00
|
|
|
for &callback in self.callbacks.iter() {
|
2014-02-05 15:15:24 -06:00
|
|
|
let (c, res) = callback(krate);
|
|
|
|
krate = c;
|
2013-08-15 15:28:54 -05:00
|
|
|
out_json.push(res);
|
|
|
|
}
|
2014-02-05 15:15:24 -06:00
|
|
|
(krate, out_json)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os="win32")]
|
|
|
|
fn libname(mut n: ~str) -> ~str {
|
|
|
|
n.push_str(".dll");
|
|
|
|
n
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os="macos")]
|
|
|
|
fn libname(mut n: ~str) -> ~str {
|
|
|
|
n.push_str(".dylib");
|
|
|
|
n
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os="win32"), not(target_os="macos"))]
|
|
|
|
fn libname(n: ~str) -> ~str {
|
|
|
|
let mut i = ~"lib";
|
|
|
|
i.push_str(n);
|
|
|
|
i.push_str(".so");
|
|
|
|
i
|
|
|
|
}
|