Skip to content

Commit 175d325

Browse files
committed
rustc_metadata: Move some code around
Plugin search doesn't need a crate loader, only crate locator
1 parent 2cda75c commit 175d325

File tree

3 files changed

+84
-98
lines changed

3 files changed

+84
-98
lines changed

src/librustc_metadata/creader.rs

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ use rustc::session::{Session, CrateDisambiguator};
1414
use rustc::session::config::{Sanitizer, self};
1515
use rustc_target::spec::{PanicStrategy, TargetTriple};
1616
use rustc::session::search_paths::PathKind;
17-
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource, MetadataLoader};
17+
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource};
1818
use rustc::util::common::record_time;
1919
use rustc::util::nodemap::FxHashSet;
2020
use rustc::hir::map::Definitions;
2121
use rustc::hir::def_id::LOCAL_CRATE;
2222

23-
use std::path::{Path, PathBuf};
23+
use std::path::Path;
2424
use std::{cmp, fs};
2525

2626
use syntax::ast;
2727
use syntax::attr;
2828
use syntax_expand::allocator::{global_allocator_spans, AllocatorKind};
2929
use syntax::symbol::{Symbol, sym};
30-
use syntax::{span_err, span_fatal};
30+
use syntax::span_fatal;
3131
use syntax_pos::{Span, DUMMY_SP};
3232
use log::{debug, info, log_enabled};
3333
use proc_macro::bridge::client::ProcMacro;
@@ -471,62 +471,7 @@ impl<'a> CrateLoader<'a> {
471471
self.resolve_crate(dep.name, span, dep_kind, Some((root, &dep))).0
472472
})).collect()
473473
}
474-
}
475-
476-
fn read_extension_crate(
477-
sess: &Session,
478-
metadata_loader: &dyn MetadataLoader,
479-
name: Symbol,
480-
span: Span,
481-
) -> (Library, bool) {
482-
info!("read extension crate `{}`", name);
483-
let target_triple = sess.opts.target_triple.clone();
484-
let host_triple = TargetTriple::from_triple(config::host_triple());
485-
let is_cross = target_triple != host_triple;
486-
let mut target_only = false;
487-
let mut locate_ctxt = locator::Context {
488-
sess,
489-
span,
490-
crate_name: name,
491-
hash: None,
492-
extra_filename: None,
493-
filesearch: sess.host_filesearch(PathKind::Crate),
494-
target: &sess.host,
495-
triple: host_triple,
496-
root: None,
497-
rejected_via_hash: vec![],
498-
rejected_via_triple: vec![],
499-
rejected_via_kind: vec![],
500-
rejected_via_version: vec![],
501-
rejected_via_filename: vec![],
502-
should_match_name: true,
503-
is_proc_macro: None,
504-
metadata_loader,
505-
};
506-
507-
let library = locate_ctxt.maybe_load_library_crate().or_else(|| {
508-
if !is_cross {
509-
return None
510-
}
511-
// Try loading from target crates. This will abort later if we
512-
// try to load a plugin registrar function,
513-
target_only = true;
514-
515-
locate_ctxt.target = &sess.target.target;
516-
locate_ctxt.triple = target_triple;
517-
locate_ctxt.filesearch = sess.target_filesearch(PathKind::Crate);
518-
519-
locate_ctxt.maybe_load_library_crate()
520-
});
521-
let library = match library {
522-
Some(l) => l,
523-
None => locate_ctxt.report_errs(),
524-
};
525474

526-
(library, target_only)
527-
}
528-
529-
impl<'a> CrateLoader<'a> {
530475
fn dlsym_proc_macros(&self,
531476
path: &Path,
532477
disambiguator: CrateDisambiguator,
@@ -557,44 +502,7 @@ impl<'a> CrateLoader<'a> {
557502

558503
decls
559504
}
560-
}
561-
562-
/// Look for a plugin registrar. Returns library path, crate
563-
/// SVH and DefIndex of the registrar function.
564-
pub fn find_plugin_registrar(sess: &Session,
565-
metadata_loader: &dyn MetadataLoader,
566-
span: Span,
567-
name: Symbol)
568-
-> Option<(PathBuf, CrateDisambiguator)> {
569-
let (library, target_only) = read_extension_crate(sess, metadata_loader, name, span);
570-
571-
if target_only {
572-
// Need to abort before syntax expansion.
573-
let message = format!("plugin `{}` is not available for triple `{}` \
574-
(only found {})",
575-
name,
576-
config::host_triple(),
577-
sess.opts.target_triple);
578-
span_fatal!(sess, span, E0456, "{}", &message);
579-
}
580-
581-
match library.source.dylib {
582-
Some(dylib) => {
583-
Some((dylib.0, library.metadata.get_root().disambiguator))
584-
}
585-
None => {
586-
span_err!(sess, span, E0457,
587-
"plugin `{}` only found in rlib format, but must be available \
588-
in dylib format",
589-
name);
590-
// No need to abort because the loading code will just ignore this
591-
// empty dylib.
592-
None
593-
}
594-
}
595-
}
596505

597-
impl<'a> CrateLoader<'a> {
598506
fn inject_panic_runtime(&self, krate: &ast::Crate) {
599507
// If we're only compiling an rlib, then there's no need to select a
600508
// panic runtime, so we just skip this section entirely.

src/librustc_metadata/locator.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ use rustc_data_structures::fx::FxHashSet;
220220
use rustc_data_structures::svh::Svh;
221221
use rustc_data_structures::sync::MetadataRef;
222222
use rustc::middle::cstore::{CrateSource, MetadataLoader};
223-
use rustc::session::{config, Session};
223+
use rustc::session::{config, Session, CrateDisambiguator};
224224
use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
225225
use rustc::session::search_paths::PathKind;
226226
use rustc::util::nodemap::FxHashMap;
227227

228228
use errors::DiagnosticBuilder;
229+
use syntax::{span_err, span_fatal};
229230
use syntax::symbol::{Symbol, sym};
230231
use syntax::struct_span_err;
231232
use syntax_pos::Span;
@@ -911,6 +912,83 @@ fn get_metadata_section_imp(target: &Target,
911912
}
912913
}
913914

915+
/// Look for a plugin registrar. Returns its library path and crate disambiguator.
916+
pub fn find_plugin_registrar(
917+
sess: &Session,
918+
metadata_loader: &dyn MetadataLoader,
919+
span: Span,
920+
name: Symbol,
921+
) -> Option<(PathBuf, CrateDisambiguator)> {
922+
info!("find plugin registrar `{}`", name);
923+
let target_triple = sess.opts.target_triple.clone();
924+
let host_triple = TargetTriple::from_triple(config::host_triple());
925+
let is_cross = target_triple != host_triple;
926+
let mut target_only = false;
927+
let mut locate_ctxt = Context {
928+
sess,
929+
span,
930+
crate_name: name,
931+
hash: None,
932+
extra_filename: None,
933+
filesearch: sess.host_filesearch(PathKind::Crate),
934+
target: &sess.host,
935+
triple: host_triple,
936+
root: None,
937+
rejected_via_hash: vec![],
938+
rejected_via_triple: vec![],
939+
rejected_via_kind: vec![],
940+
rejected_via_version: vec![],
941+
rejected_via_filename: vec![],
942+
should_match_name: true,
943+
is_proc_macro: None,
944+
metadata_loader,
945+
};
946+
947+
let library = locate_ctxt.maybe_load_library_crate().or_else(|| {
948+
if !is_cross {
949+
return None
950+
}
951+
// Try loading from target crates. This will abort later if we
952+
// try to load a plugin registrar function,
953+
target_only = true;
954+
955+
locate_ctxt.target = &sess.target.target;
956+
locate_ctxt.triple = target_triple;
957+
locate_ctxt.filesearch = sess.target_filesearch(PathKind::Crate);
958+
959+
locate_ctxt.maybe_load_library_crate()
960+
});
961+
let library = match library {
962+
Some(l) => l,
963+
None => locate_ctxt.report_errs(),
964+
};
965+
966+
if target_only {
967+
// Need to abort before syntax expansion.
968+
let message = format!("plugin `{}` is not available for triple `{}` \
969+
(only found {})",
970+
name,
971+
config::host_triple(),
972+
sess.opts.target_triple);
973+
span_fatal!(sess, span, E0456, "{}", &message);
974+
}
975+
976+
match library.source.dylib {
977+
Some(dylib) => {
978+
Some((dylib.0, library.metadata.get_root().disambiguator))
979+
}
980+
None => {
981+
span_err!(sess, span, E0457,
982+
"plugin `{}` only found in rlib format, but must be available \
983+
in dylib format",
984+
name);
985+
// No need to abort because the loading code will just ignore this
986+
// empty dylib.
987+
None
988+
}
989+
}
990+
}
991+
914992
/// A diagnostic function for dumping crate metadata to an output stream.
915993
pub fn list_file_metadata(target: &Target,
916994
path: &Path,

src/librustc_plugin/load.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc::middle::cstore::MetadataLoader;
44
use rustc::session::Session;
5-
use rustc_metadata::creader;
5+
use rustc_metadata::locator;
66
use crate::registry::Registry;
77

88
use std::borrow::ToOwned;
@@ -80,7 +80,7 @@ pub fn load_plugins(sess: &Session,
8080

8181
impl<'a> PluginLoader<'a> {
8282
fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
83-
let registrar = creader::find_plugin_registrar(self.sess, self.metadata_loader, span, name);
83+
let registrar = locator::find_plugin_registrar(self.sess, self.metadata_loader, span, name);
8484

8585
if let Some((lib, disambiguator)) = registrar {
8686
let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);

0 commit comments

Comments
 (0)