Skip to content

Commit

Permalink
Read in rmeta crates
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Nov 20, 2016
1 parent b286a2f commit 534556a
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 50 deletions.
39 changes: 26 additions & 13 deletions src/librustc/middle/cstore.rs
Expand Up @@ -56,24 +56,37 @@ pub struct LinkMeta {
pub crate_hash: Svh,
}

// Where a crate came from on the local filesystem. One of these two options
// Where a crate came from on the local filesystem. One of these three options
// must be non-None.
#[derive(PartialEq, Clone, Debug)]
pub struct CrateSource {
pub dylib: Option<(PathBuf, PathKind)>,
pub rlib: Option<(PathBuf, PathKind)>,
pub rmeta: Option<(PathBuf, PathKind)>,
}

#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
pub enum DepKind {
/// A dependency that is only used for its macros.
MacrosOnly,
/// A dependency that is always injected into the dependency list and so
/// doesn't need to be linked to an rlib, e.g. the injected allocator.
Implicit,
/// A dependency that is required by an rlib version of this crate.
/// Ordinary `extern crate`s result in `Explicit` dependencies.
Explicit,
#[derive(PartialEq, Clone, Debug)]
pub enum LibSource {
Some(PathBuf),
MetadataOnly,
None,
}

impl LibSource {
pub fn is_some(&self) -> bool {
if let LibSource::Some(_) = *self {
true
} else {
false
}
}

pub fn option(&self) -> Option<PathBuf> {
match *self {
LibSource::Some(ref p) => Some(p.clone()),
LibSource::MetadataOnly | LibSource::None => None,
}
}
}

#[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
Expand Down Expand Up @@ -244,7 +257,7 @@ pub trait CrateStore<'tcx> {
// utility functions
fn metadata_filename(&self) -> &str;
fn metadata_section_name(&self, target: &Target) -> &str;
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>;
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down Expand Up @@ -427,7 +440,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
// utility functions
fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") }
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
{ vec![] }
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/dependency_format.rs
Expand Up @@ -192,7 +192,7 @@ fn calculate_type(sess: &session::Session,
if src.dylib.is_none() &&
!formats.contains_key(&cnum) &&
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
assert!(src.rlib.is_some());
assert!(src.rlib.is_some() || src.rmeta.is_some());
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
add_library(sess, cnum, RequireStatic, &mut formats);
ret[cnum.as_usize() - 1] = Linkage::Static;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_driver/driver.rs
Expand Up @@ -1093,7 +1093,6 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
"serialize work products",
move || rustc_incremental::save_work_products(sess));

println!("finish phase 5: {}", sess.err_count());
if sess.err_count() > 0 {
Err(sess.err_count())
} else {
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_metadata/creader.rs
Expand Up @@ -44,6 +44,7 @@ use log;
pub struct Library {
pub dylib: Option<(PathBuf, PathKind)>,
pub rlib: Option<(PathBuf, PathKind)>,
pub rmeta: Option<(PathBuf, PathKind)>,
pub metadata: MetadataBlob,
}

Expand All @@ -62,9 +63,11 @@ fn dump_crates(cstore: &CStore) {
info!(" cnum: {}", data.cnum);
info!(" hash: {}", data.hash());
info!(" reqd: {:?}", data.dep_kind.get());
let CrateSource { dylib, rlib } = data.source.clone();
let CrateSource { dylib, rlib, rmeta } = data.source.clone();
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
rmeta.map(|rl| info!(" rmeta: {}", rl.0.display()));
});
})
}

Expand Down Expand Up @@ -278,14 +281,15 @@ impl<'a> CrateLoader<'a> {
ident: ident.to_string(),
dylib: lib.dylib.clone().map(|p| p.0),
rlib: lib.rlib.clone().map(|p| p.0),
rmeta: lib.rmeta.clone().map(|p| p.0),
})
} else {
None
};
// Maintain a reference to the top most crate.
let root = if root.is_some() { root } else { &crate_paths };

let Library { dylib, rlib, metadata } = lib;
let Library { dylib, rlib, rmeta, metadata } = lib;

let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);

Expand All @@ -305,6 +309,7 @@ impl<'a> CrateLoader<'a> {
source: cstore::CrateSource {
dylib: dylib,
rlib: rlib,
rmeta: rmeta,
},
});

Expand Down
15 changes: 12 additions & 3 deletions src/librustc_metadata/cstore.rs
Expand Up @@ -25,15 +25,14 @@ use rustc::util::nodemap::{FxHashMap, NodeMap, NodeSet, DefIdMap};

use std::cell::{RefCell, Cell};
use std::rc::Rc;
use std::path::PathBuf;
use flate::Bytes;
use syntax::{ast, attr};
use syntax::ext::base::SyntaxExtension;
use syntax_pos;

pub use rustc::middle::cstore::{NativeLibrary, LinkagePreference};
pub use rustc::middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
pub use rustc::middle::cstore::{CrateSource, LinkMeta};
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};

// A map from external crate numbers (as decoded from some crate file) to
// local crate numbers (as generated during this session). Each external
Expand Down Expand Up @@ -185,7 +184,7 @@ impl CStore {
// positions.
pub fn do_get_used_crates(&self,
prefer: LinkagePreference)
-> Vec<(CrateNum, Option<PathBuf>)> {
-> Vec<(CrateNum, LibSource)> {
let mut ordering = Vec::new();
for (&num, _) in self.metas.borrow().iter() {
self.push_dependencies_in_postorder(&mut ordering, num);
Expand All @@ -201,6 +200,16 @@ impl CStore {
LinkagePreference::RequireDynamic => data.source.dylib.clone().map(|p| p.0),
LinkagePreference::RequireStatic => data.source.rlib.clone().map(|p| p.0),
};
let path = match path {
Some(p) => LibSource::Some(p),
None => {
if data.rmeta.is_some() {
LibSource::MetadataOnly
} else {
LibSource::None
}
}
};
Some((cnum, path))
})
.collect::<Vec<_>>();
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -13,7 +13,7 @@ use encoder;
use locator;
use schema;

use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, DepKind, ExternCrate};
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, LibSource, DepKind, ExternCrate};
use rustc::middle::cstore::{NativeLibrary, LinkMeta, LinkagePreference, LoadedMacro};
use rustc::hir::def::{self, Def};
use rustc::middle::lang_items;
Expand All @@ -28,7 +28,6 @@ use rustc::mir::Mir;
use rustc::util::nodemap::{NodeSet, DefIdMap};
use rustc_back::PanicStrategy;

use std::path::PathBuf;
use syntax::ast;
use syntax::attr;
use syntax::parse::{token, new_parser_from_source_str};
Expand Down Expand Up @@ -544,7 +543,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
locator::meta_section_name(target)
}

fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
{
self.do_get_used_crates(prefer)
}
Expand Down
52 changes: 34 additions & 18 deletions src/librustc_metadata/locator.rs
Expand Up @@ -53,6 +53,13 @@
//! is a platform-defined dynamic library. Each library has a metadata somewhere
//! inside of it.
//!
//! A third kind of dependency is an rmeta file. These are rlibs, which contain
//! metadata, but no code. To a first approximation, these are treated in the
//! same way as rlibs. Where there is both an rlib and an rmeta file, the rlib
//! gets priority (even if the rmeta file is newer). An rmeta file is only
//! useful for checking a downstream crate, attempting to link one will cause an
//! error.
//!
//! When translating a crate name to a crate on the filesystem, we all of a
//! sudden need to take into account both rlibs and dylibs! Linkage later on may
//! use either one of these files, as each has their pros/cons. The job of crate
Expand Down Expand Up @@ -275,33 +282,31 @@ pub struct CratePaths {
pub ident: String,
pub dylib: Option<PathBuf>,
pub rlib: Option<PathBuf>,
pub rmeta: Option<PathBuf>,
}

pub const METADATA_FILENAME: &'static str = "rust.metadata.bin";

#[derive(Copy, Clone, PartialEq)]
enum CrateFlavor {
Rlib,
Rmeta,
Dylib,
}

impl fmt::Display for CrateFlavor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
CrateFlavor::Rlib => "rlib",
CrateFlavor::Rmeta => "rmeta",
CrateFlavor::Dylib => "dylib",
})
}
}

impl CratePaths {
fn paths(&self) -> Vec<PathBuf> {
match (&self.dylib, &self.rlib) {
(&None, &None) => vec![],
(&Some(ref p), &None) |
(&None, &Some(ref p)) => vec![p.clone()],
(&Some(ref p1), &Some(ref p2)) => vec![p1.clone(), p2.clone()],
}
self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).cloned().collect()
}
}

Expand Down Expand Up @@ -457,11 +462,13 @@ impl<'a> Context<'a> {
None => return FileDoesntMatch,
Some(file) => file,
};
let (hash, rlib) = if file.starts_with(&rlib_prefix[..]) && file.ends_with(".rlib") {
(&file[(rlib_prefix.len())..(file.len() - ".rlib".len())], true)
let (hash, found_kind) = if file.starts_with(&rlib_prefix[..]) && file.ends_with(".rlib") {
(&file[(rlib_prefix.len())..(file.len() - ".rlib".len())], CrateFlavor::Rlib)
} else if file.starts_with(&rlib_prefix[..]) && file.ends_with(".rmeta") {
(&file[(rlib_prefix.len())..(file.len() - ".rmeta".len())], CrateFlavor::Rmeta)
} else if file.starts_with(&dylib_prefix) &&
file.ends_with(&dypair.1) {
(&file[(dylib_prefix.len())..(file.len() - dypair.1.len())], false)
(&file[(dylib_prefix.len())..(file.len() - dypair.1.len())], CrateFlavor::Dylib)
} else {
if file.starts_with(&staticlib_prefix[..]) && file.ends_with(&staticpair.1) {
staticlibs.push(CrateMismatch {
Expand All @@ -475,14 +482,14 @@ impl<'a> Context<'a> {

let hash_str = hash.to_string();
let slot = candidates.entry(hash_str)
.or_insert_with(|| (FxHashMap(), FxHashMap()));
let (ref mut rlibs, ref mut dylibs) = *slot;
.or_insert_with(|| (FxHashMap(), FxHashMap(), FxHashMap()));
let (ref mut rlibs, ref mut rmetas, ref mut dylibs) = *slot;
fs::canonicalize(path)
.map(|p| {
if rlib {
rlibs.insert(p, kind);
} else {
dylibs.insert(p, kind);
match found_kind {
CrateFlavor::Rlib => { rlibs.insert(p, kind); }
CrateFlavor::Rmeta => { rmetas.insert(p, kind); }
CrateFlavor::Dylib => { dylibs.insert(p, kind); }
}
FileMatches
})
Expand All @@ -499,15 +506,17 @@ impl<'a> Context<'a> {
// libraries corresponds to the crate id and hash criteria that this
// search is being performed for.
let mut libraries = FxHashMap();
for (_hash, (rlibs, dylibs)) in candidates {
for (_hash, (rlibs, rmetas, dylibs)) in candidates {
let mut slot = None;
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot);
let rmeta = self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot);
let dylib = self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot);
if let Some((h, m)) = slot {
libraries.insert(h,
Library {
dylib: dylib,
rlib: rlib,
rmeta: rmeta,
metadata: m,
});
}
Expand Down Expand Up @@ -703,6 +712,7 @@ impl<'a> Context<'a> {
let sess = self.sess;
let dylibname = self.dylibname();
let mut rlibs = FxHashMap();
let mut rmetas = FxHashMap();
let mut dylibs = FxHashMap();
{
let locs = locs.map(|l| PathBuf::from(l)).filter(|loc| {
Expand Down Expand Up @@ -744,6 +754,8 @@ impl<'a> Context<'a> {
for loc in locs {
if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") {
rlibs.insert(fs::canonicalize(&loc).unwrap(), PathKind::ExternFlag);
} else if loc.file_name().unwrap().to_str().unwrap().ends_with(".rmeta") {
rmetas.insert(fs::canonicalize(&loc).unwrap(), PathKind::ExternFlag);
} else {
dylibs.insert(fs::canonicalize(&loc).unwrap(), PathKind::ExternFlag);
}
Expand All @@ -753,16 +765,18 @@ impl<'a> Context<'a> {
// Extract the rlib/dylib pair.
let mut slot = None;
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot);
let rmeta = self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot);
let dylib = self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot);

if rlib.is_none() && dylib.is_none() {
if rlib.is_none() && rmeta.is_none() && dylib.is_none() {
return None;
}
match slot {
Some((_, metadata)) => {
Some(Library {
dylib: dylib,
rlib: rlib,
rmeta: rmeta,
metadata: metadata,
})
}
Expand Down Expand Up @@ -832,7 +846,7 @@ fn get_metadata_section_imp(target: &Target,
if !filename.exists() {
return Err(format!("no such file: '{}'", filename.display()));
}
if flavor == CrateFlavor::Rlib {
if flavor == CrateFlavor::Rlib || flavor == CrateFlavor::Rmeta {
// Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
// internally to read the file. We also avoid even using a memcpy by
// just keeping the archive along while the metadata is in use.
Expand Down Expand Up @@ -933,6 +947,8 @@ pub fn list_file_metadata(target: &Target, path: &Path, out: &mut io::Write) ->
let filename = path.file_name().unwrap().to_str().unwrap();
let flavor = if filename.ends_with(".rlib") {
CrateFlavor::Rlib
} else if filename.ends_with(".rmeta") {
CrateFlavor::Rmeta
} else {
CrateFlavor::Dylib
};
Expand Down

0 comments on commit 534556a

Please sign in to comment.