Skip to content

Commit 1a24a59

Browse files
author
Robin Kruppe
committed
Remove rustc_llvm dependency from rustc_metadata
Move the code for loading metadata from rlibs and dylibs from rustc_metadata into rustc_trans, and introduce a trait to avoid introducing a direct dependency on rustc_trans. This means rustc_metadata is no longer rebuilt when LLVM changes.
1 parent 9f15631 commit 1a24a59

File tree

22 files changed

+276
-219
lines changed

22 files changed

+276
-219
lines changed

src/Cargo.lock

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ arena = { path = "../libarena" }
1313
fmt_macros = { path = "../libfmt_macros" }
1414
graphviz = { path = "../libgraphviz" }
1515
log = "0.3"
16+
owning_ref = "0.3.3"
1617
rustc_back = { path = "../librustc_back" }
1718
rustc_bitflags = { path = "../librustc_bitflags" }
1819
rustc_const_math = { path = "../librustc_const_math" }

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern crate fmt_macros;
5454
extern crate getopts;
5555
extern crate graphviz;
5656
extern crate libc;
57+
extern crate owning_ref;
5758
extern crate rustc_llvm as llvm;
5859
extern crate rustc_back;
5960
extern crate rustc_data_structures;

src/librustc/middle/cstore.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ use session::search_paths::PathKind;
3636
use util::nodemap::{NodeSet, DefIdMap};
3737

3838
use std::any::Any;
39-
use std::path::PathBuf;
39+
use std::path::{Path, PathBuf};
4040
use std::rc::Rc;
41+
use owning_ref::ErasedBoxRef;
4142
use syntax::ast;
4243
use syntax::ext::base::SyntaxExtension;
4344
use syntax::symbol::Symbol;
@@ -201,11 +202,33 @@ impl EncodedMetadataHashes {
201202
}
202203
}
203204

205+
/// The backend's way to give the crate store access to the metadata in a library.
206+
/// Note that it returns the raw metadata bytes stored in the library file, whether
207+
/// it is compressed, uncompressed, some weird mix, etc.
208+
/// rmeta files are backend independent and not handled here.
209+
///
210+
/// At the time of this writing, there is only one backend and one way to store
211+
/// metadata in library -- this trait just serves to decouple rustc_metadata from
212+
/// the archive reader, which depends on LLVM.
213+
pub trait MetadataLoader {
214+
fn get_rlib_metadata(&self,
215+
target: &Target,
216+
filename: &Path)
217+
-> Result<ErasedBoxRef<[u8]>, String>;
218+
fn get_dylib_metadata(&self,
219+
target: &Target,
220+
filename: &Path)
221+
-> Result<ErasedBoxRef<[u8]>, String>;
222+
}
223+
204224
/// A store of Rust crates, through with their metadata
205225
/// can be accessed.
206226
pub trait CrateStore {
207227
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
208228

229+
// access to the metadata loader
230+
fn metadata_loader(&self) -> &MetadataLoader;
231+
209232
// item info
210233
fn visibility(&self, def: DefId) -> ty::Visibility;
211234
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
@@ -275,8 +298,6 @@ pub trait CrateStore {
275298
fn used_link_args(&self) -> Vec<String>;
276299

277300
// utility functions
278-
fn metadata_filename(&self) -> &str;
279-
fn metadata_section_name(&self, target: &Target) -> &str;
280301
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
281302
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
282303
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
@@ -413,8 +434,6 @@ impl CrateStore for DummyCrateStore {
413434
fn used_link_args(&self) -> Vec<String> { vec![] }
414435

415436
// utility functions
416-
fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
417-
fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") }
418437
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
419438
{ vec![] }
420439
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
@@ -427,6 +446,9 @@ impl CrateStore for DummyCrateStore {
427446
bug!("encode_metadata")
428447
}
429448
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
449+
450+
// access to the metadata loader
451+
fn metadata_loader(&self) -> &MetadataLoader { bug!("metadata_loader") }
430452
}
431453

432454
pub trait CrateLoader {

src/librustc_driver/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub fn run_compiler<'a>(args: &[String],
204204
};
205205

206206
let dep_graph = DepGraph::new(sopts.build_dep_graph());
207-
let cstore = Rc::new(CStore::new(&dep_graph));
207+
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
208208

209209
let loader = file_loader.unwrap_or(box RealFileLoader);
210210
let codemap = Rc::new(CodeMap::with_file_loader(loader, sopts.file_path_mapping()));
@@ -409,7 +409,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
409409
return None;
410410
}
411411
let dep_graph = DepGraph::new(sopts.build_dep_graph());
412-
let cstore = Rc::new(CStore::new(&dep_graph));
412+
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
413413
let mut sess = build_session(sopts.clone(),
414414
&dep_graph,
415415
None,
@@ -558,7 +558,11 @@ impl RustcDefaultCalls {
558558
&Input::File(ref ifile) => {
559559
let path = &(*ifile);
560560
let mut v = Vec::new();
561-
locator::list_file_metadata(&sess.target.target, path, &mut v).unwrap();
561+
locator::list_file_metadata(&sess.target.target,
562+
path,
563+
sess.cstore.metadata_loader(),
564+
&mut v)
565+
.unwrap();
562566
println!("{}", String::from_utf8(v).unwrap());
563567
}
564568
&Input::Str { .. } => {

src/librustc_driver/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use driver;
1414
use rustc::dep_graph::DepGraph;
1515
use rustc_lint;
1616
use rustc_resolve::MakeGlobMap;
17+
use rustc_trans;
1718
use rustc::middle::lang_items;
1819
use rustc::middle::free_region::FreeRegionMap;
1920
use rustc::middle::region::{CodeExtent, RegionMaps};
@@ -104,7 +105,7 @@ fn test_env<F>(source_string: &str,
104105

105106
let dep_graph = DepGraph::new(false);
106107
let _ignore = dep_graph.in_ignore();
107-
let cstore = Rc::new(CStore::new(&dep_graph));
108+
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
108109
let sess = session::build_session_(options,
109110
&dep_graph,
110111
None,

src/librustc_metadata/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ crate-type = ["dylib"]
1111
[dependencies]
1212
flate = { path = "../libflate" }
1313
log = "0.3"
14+
owning_ref = "0.3.3"
1415
proc_macro = { path = "../libproc_macro" }
1516
rustc = { path = "../librustc" }
1617
rustc_back = { path = "../librustc_back" }
1718
rustc_const_math = { path = "../librustc_const_math" }
1819
rustc_data_structures = { path = "../librustc_data_structures" }
1920
rustc_errors = { path = "../librustc_errors" }
20-
rustc_llvm = { path = "../librustc_llvm" }
2121
serialize = { path = "../libserialize" }
2222
syntax = { path = "../libsyntax" }
2323
syntax_ext = { path = "../libsyntax_ext" }

src/librustc_metadata/creader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ impl<'a> CrateLoader<'a> {
393393
rejected_via_filename: vec![],
394394
should_match_name: true,
395395
is_proc_macro: Some(false),
396+
metadata_loader: &*self.cstore.metadata_loader,
396397
};
397398

398399
self.load(&mut locate_ctxt).or_else(|| {
@@ -554,6 +555,7 @@ impl<'a> CrateLoader<'a> {
554555
rejected_via_filename: vec![],
555556
should_match_name: true,
556557
is_proc_macro: None,
558+
metadata_loader: &*self.cstore.metadata_loader,
557559
};
558560
let library = self.load(&mut locate_ctxt).or_else(|| {
559561
if !is_cross {

src/librustc_metadata/cstore.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111
// The crate store - a central repo for information collected about external
1212
// crates and libraries
1313

14-
use locator;
1514
use schema::{self, Tracked};
1615

1716
use rustc::dep_graph::{DepGraph, DepNode, GlobalMetaDataKind};
1817
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefIndex, DefId};
1918
use rustc::hir::map::definitions::DefPathTable;
2019
use rustc::hir::svh::Svh;
21-
use rustc::middle::cstore::{DepKind, ExternCrate};
20+
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
2221
use rustc_back::PanicStrategy;
2322
use rustc_data_structures::indexed_vec::IndexVec;
2423
use rustc::util::nodemap::{FxHashMap, FxHashSet, NodeMap, DefIdMap};
2524

2625
use std::cell::{RefCell, Cell};
2726
use std::rc::Rc;
28-
use flate::Bytes;
27+
use owning_ref::ErasedBoxRef;
2928
use syntax::{ast, attr};
3029
use syntax::ext::base::SyntaxExtension;
3130
use syntax::symbol::Symbol;
@@ -43,11 +42,7 @@ pub use cstore_impl::provide;
4342
// own crate numbers.
4443
pub type CrateNumMap = IndexVec<CrateNum, CrateNum>;
4544

46-
pub enum MetadataBlob {
47-
Inflated(Bytes),
48-
Archive(locator::ArchiveMetadata),
49-
Raw(Vec<u8>),
50-
}
45+
pub struct MetadataBlob(pub ErasedBoxRef<[u8]>);
5146

5247
/// Holds information about a syntax_pos::FileMap imported from another crate.
5348
/// See `imported_filemaps()` for more information.
@@ -103,10 +98,11 @@ pub struct CStore {
10398
statically_included_foreign_items: RefCell<FxHashSet<DefIndex>>,
10499
pub dllimport_foreign_items: RefCell<FxHashSet<DefIndex>>,
105100
pub visible_parent_map: RefCell<DefIdMap<DefId>>,
101+
pub metadata_loader: Box<MetadataLoader>,
106102
}
107103

108104
impl CStore {
109-
pub fn new(dep_graph: &DepGraph) -> CStore {
105+
pub fn new(dep_graph: &DepGraph, metadata_loader: Box<MetadataLoader>) -> CStore {
110106
CStore {
111107
dep_graph: dep_graph.clone(),
112108
metas: RefCell::new(FxHashMap()),
@@ -116,6 +112,7 @@ impl CStore {
116112
statically_included_foreign_items: RefCell::new(FxHashSet()),
117113
dllimport_foreign_items: RefCell::new(FxHashSet()),
118114
visible_parent_map: RefCell::new(FxHashMap()),
115+
metadata_loader: metadata_loader,
119116
}
120117
}
121118

src/librustc_metadata/cstore_impl.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
use cstore;
1212
use encoder;
13-
use locator;
1413
use schema;
1514

1615
use rustc::dep_graph::DepTrackingMapConfig;
1716
use rustc::middle::cstore::{CrateStore, CrateSource, LibSource, DepKind,
18-
ExternCrate, NativeLibrary, LinkMeta,
17+
ExternCrate, NativeLibrary, MetadataLoader, LinkMeta,
1918
LinkagePreference, LoadedMacro, EncodedMetadata};
2019
use rustc::hir::def;
2120
use rustc::middle::lang_items;
@@ -38,7 +37,6 @@ use syntax::parse::filemap_to_stream;
3837
use syntax::symbol::Symbol;
3938
use syntax_pos::{Span, NO_EXPANSION};
4039
use rustc::hir::svh::Svh;
41-
use rustc_back::target::Target;
4240
use rustc::hir;
4341

4442
macro_rules! provide {
@@ -135,6 +133,10 @@ impl CrateStore for cstore::CStore {
135133
self.get_crate_data(krate)
136134
}
137135

136+
fn metadata_loader(&self) -> &MetadataLoader {
137+
&*self.metadata_loader
138+
}
139+
138140
fn visibility(&self, def: DefId) -> ty::Visibility {
139141
self.dep_graph.read(DepNode::MetaData(def));
140142
self.get_crate_data(def.krate).get_visibility(def.index)
@@ -420,17 +422,6 @@ impl CrateStore for cstore::CStore {
420422
{
421423
self.get_used_link_args().borrow().clone()
422424
}
423-
424-
fn metadata_filename(&self) -> &str
425-
{
426-
locator::METADATA_FILENAME
427-
}
428-
429-
fn metadata_section_name(&self, target: &Target) -> &str
430-
{
431-
locator::meta_section_name(target)
432-
}
433-
434425
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
435426
{
436427
self.do_get_used_crates(prefer)
@@ -522,4 +513,4 @@ impl CrateStore for cstore::CStore {
522513
drop(visible_parent_map);
523514
self.visible_parent_map.borrow()
524515
}
525-
}
516+
}

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ pub trait Metadata<'a, 'tcx>: Copy {
7777

7878
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
7979
fn raw_bytes(self) -> &'a [u8] {
80-
match *self {
81-
MetadataBlob::Inflated(ref vec) => vec,
82-
MetadataBlob::Archive(ref ar) => ar.as_slice(),
83-
MetadataBlob::Raw(ref vec) => vec,
84-
}
80+
&self.0
8581
}
8682
}
8783

src/librustc_metadata/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern crate syntax;
3737
extern crate syntax_pos;
3838
extern crate flate;
3939
extern crate serialize as rustc_serialize; // used by deriving
40+
extern crate owning_ref;
4041
extern crate rustc_errors as errors;
4142
extern crate syntax_ext;
4243
extern crate proc_macro;
@@ -46,7 +47,6 @@ extern crate rustc;
4647
extern crate rustc_back;
4748
extern crate rustc_const_math;
4849
extern crate rustc_data_structures;
49-
extern crate rustc_llvm;
5050

5151
mod diagnostics;
5252

0 commit comments

Comments
 (0)