]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_plugin_impl/src/load.rs
Merge commit 'e8dca3e87d164d2806098c462c6ce41301341f68' into sync_from_cg_gcc
[rust.git] / compiler / rustc_plugin_impl / src / load.rs
1 //! Used by `rustc` when loading a plugin.
2
3 use crate::Registry;
4 use libloading::Library;
5 use rustc_ast::Crate;
6 use rustc_errors::struct_span_err;
7 use rustc_metadata::locator;
8 use rustc_session::cstore::MetadataLoader;
9 use rustc_session::Session;
10 use rustc_span::symbol::{sym, Ident};
11 use rustc_span::Span;
12
13 use std::borrow::ToOwned;
14 use std::env;
15 use std::mem;
16 use std::path::PathBuf;
17
18 /// Pointer to a registrar function.
19 type PluginRegistrarFn = fn(&mut Registry<'_>);
20
21 fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
22     struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
23         .span_label(span, "malformed attribute")
24         .emit();
25 }
26
27 /// Read plugin metadata and dynamically load registrar functions.
28 pub fn load_plugins(
29     sess: &Session,
30     metadata_loader: &dyn MetadataLoader,
31     krate: &Crate,
32 ) -> Vec<PluginRegistrarFn> {
33     let mut plugins = Vec::new();
34
35     for attr in &krate.attrs {
36         if !attr.has_name(sym::plugin) {
37             continue;
38         }
39
40         for plugin in attr.meta_item_list().unwrap_or_default() {
41             match plugin.ident() {
42                 Some(ident) if plugin.is_word() => {
43                     load_plugin(&mut plugins, sess, metadata_loader, ident)
44                 }
45                 _ => call_malformed_plugin_attribute(sess, plugin.span()),
46             }
47         }
48     }
49
50     plugins
51 }
52
53 fn load_plugin(
54     plugins: &mut Vec<PluginRegistrarFn>,
55     sess: &Session,
56     metadata_loader: &dyn MetadataLoader,
57     ident: Ident,
58 ) {
59     let lib = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
60     let fun = dylink_registrar(lib).unwrap_or_else(|err| {
61         // This is fatal: there are almost certainly macros we need inside this crate, so
62         // continuing would spew "macro undefined" errors.
63         sess.span_fatal(ident.span, &err.to_string());
64     });
65     plugins.push(fun);
66 }
67
68 /// Dynamically link a registrar function into the compiler process.
69 fn dylink_registrar(lib_path: PathBuf) -> Result<PluginRegistrarFn, libloading::Error> {
70     // Make sure the path contains a / or the linker will search for it.
71     let lib_path = env::current_dir().unwrap().join(&lib_path);
72
73     let lib = unsafe { Library::new(&lib_path) }?;
74
75     let registrar_sym = unsafe { lib.get::<PluginRegistrarFn>(b"__rustc_plugin_registrar") }?;
76
77     // Intentionally leak the dynamic library. We can't ever unload it
78     // since the library can make things that will live arbitrarily long
79     // (e.g., an Rc cycle or a thread).
80     let registrar_sym = unsafe { registrar_sym.into_raw() };
81     mem::forget(lib);
82
83     Ok(*registrar_sym)
84 }