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