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-08-18 10:29:44 -05:00
|
|
|
use std::dynamic_lib as dl;
|
2014-04-02 18:54:22 -05:00
|
|
|
use serialize::json;
|
2014-06-12 16:08:44 -05:00
|
|
|
use std::mem;
|
2014-05-22 18:57:53 -05:00
|
|
|
use std::string::String;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub type PluginJson = Option<(String, 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 {
|
2014-03-28 12:27:24 -05:00
|
|
|
dylibs: Vec<dl::DynamicLibrary> ,
|
|
|
|
callbacks: Vec<PluginCallback> ,
|
2013-08-15 15:28:54 -05:00
|
|
|
/// The directory plugins will be loaded from
|
2014-03-28 12:27:24 -05:00
|
|
|
pub prefix: Path,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginManager {
|
|
|
|
/// Create a new plugin manager
|
|
|
|
pub fn new(prefix: Path) -> PluginManager {
|
|
|
|
PluginManager {
|
2014-03-05 17:28:08 -06:00
|
|
|
dylibs: Vec::new(),
|
|
|
|
callbacks: Vec::new(),
|
2013-08-15 15:28:54 -05:00
|
|
|
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.
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn load_plugin(&mut self, name: String) {
|
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();
|
2014-06-12 16:08:44 -05:00
|
|
|
unsafe {
|
|
|
|
let plugin = lib.symbol("rustdoc_plugin_entrypoint").unwrap();
|
2014-06-25 14:47:34 -05:00
|
|
|
self.callbacks.push(mem::transmute::<*mut u8,PluginCallback>(plugin));
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
self.dylibs.push(lib);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-03-05 17:28:08 -06:00
|
|
|
pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, Vec<PluginJson> ) {
|
|
|
|
let mut out_json = Vec::new();
|
2014-02-05 15:15:24 -06:00
|
|
|
let mut krate = krate;
|
2015-01-31 11:20:46 -06:00
|
|
|
for &callback in &self.callbacks {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-10 23:26:45 -05:00
|
|
|
#[cfg(target_os = "windows")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn libname(mut n: String) -> String {
|
2013-08-15 15:28:54 -05:00
|
|
|
n.push_str(".dll");
|
2014-05-12 15:44:59 -05:00
|
|
|
n
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os="macos")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn libname(mut n: String) -> String {
|
2013-08-15 15:28:54 -05:00
|
|
|
n.push_str(".dylib");
|
2014-05-12 15:44:59 -05:00
|
|
|
n
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 02:29:09 -05:00
|
|
|
#[cfg(all(not(target_os="windows"), not(target_os="macos")))]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn libname(n: String) -> String {
|
|
|
|
let mut i = String::from_str("lib");
|
2014-05-12 15:44:59 -05:00
|
|
|
i.push_str(n.as_slice());
|
2013-08-15 15:28:54 -05:00
|
|
|
i.push_str(".so");
|
2014-05-12 15:44:59 -05:00
|
|
|
i
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|