rust/src/librustc/metadata/loader.rs

249 lines
8.3 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Finds crate binaries and loads their metadata
2012-09-04 20:54:36 +02:00
use syntax::diagnostic::span_handler;
use syntax::{ast, attr};
use syntax::print::pprust;
use syntax::codemap::span;
use lib::llvm::{False, llvm, mk_object_file, mk_section_iter};
use metadata::filesearch::FileSearch;
2012-09-04 20:54:36 +02:00
use io::WriterUtil;
use syntax::parse::token::ident_interner;
export os;
export os_macos, os_win32, os_linux, os_freebsd;
export ctxt;
export load_library_crate;
export list_file_metadata;
export note_linkage_attrs;
export crate_name_from_metas;
export metadata_matches;
export meta_section_name;
enum os {
os_macos,
os_win32,
os_linux,
os_freebsd
}
type ctxt = {
diag: span_handler,
filesearch: FileSearch,
span: span,
ident: ast::ident,
metas: ~[@ast::meta_item],
hash: ~str,
os: os,
2012-07-19 01:18:02 +02:00
static: bool,
intr: @ident_interner
};
fn load_library_crate(cx: ctxt) -> {ident: ~str, data: @~[u8]} {
2012-08-06 21:34:08 +02:00
match find_library_crate(cx) {
Some(ref t) => return (*t),
2012-08-20 21:23:37 +02:00
None => {
cx.diag.span_fatal(
2012-08-23 02:24:52 +02:00
cx.span, fmt!("can't find crate for `%s`",
*cx.intr.get(cx.ident)));
}
}
}
2012-08-20 21:23:37 +02:00
fn find_library_crate(cx: ctxt) -> Option<{ident: ~str, data: @~[u8]}> {
attr::require_unique_names(cx.diag, cx.metas);
find_library_crate_aux(cx, libname(cx), cx.filesearch)
}
fn libname(cx: ctxt) -> {prefix: ~str, suffix: ~str} {
2012-08-02 02:30:05 +02:00
if cx.static { return {prefix: ~"lib", suffix: ~".rlib"}; }
2012-08-06 21:34:08 +02:00
match cx.os {
2012-08-04 04:59:04 +02:00
os_win32 => return {prefix: ~"", suffix: ~".dll"},
os_macos => return {prefix: ~"lib", suffix: ~".dylib"},
os_linux => return {prefix: ~"lib", suffix: ~".so"},
os_freebsd => return {prefix: ~"lib", suffix: ~".so"}
}
}
fn find_library_crate_aux(cx: ctxt,
nn: {prefix: ~str, suffix: ~str},
filesearch: filesearch::FileSearch) ->
2012-08-20 21:23:37 +02:00
Option<{ident: ~str, data: @~[u8]}> {
let crate_name = crate_name_from_metas(cx.metas);
2012-07-19 01:18:02 +02:00
let prefix: ~str = nn.prefix + crate_name + ~"-";
let suffix: ~str = nn.suffix;
let mut matches = ~[];
2012-07-01 01:19:07 +02:00
filesearch::search(filesearch, |path| {
debug!("inspecting file %s", path.to_str());
2012-09-22 04:37:57 +02:00
let f: ~str = path.filename().get();
if !(str::starts_with(f, prefix) && str::ends_with(f, suffix)) {
debug!("skipping %s, doesn't look like %s*%s", path.to_str(),
prefix, suffix);
2012-08-20 21:23:37 +02:00
option::None::<()>
} else {
debug!("%s is a candidate", path.to_str());
2012-08-06 21:34:08 +02:00
match get_metadata_section(cx.os, path) {
2012-08-20 21:23:37 +02:00
option::Some(cvec) => {
if !crate_matches(cvec, cx.metas, cx.hash) {
debug!("skipping %s, metadata doesn't match",
path.to_str());
2012-08-20 21:23:37 +02:00
option::None::<()>
} else {
debug!("found %s with matching metadata", path.to_str());
matches.push({ident: path.to_str(), data: cvec});
2012-08-20 21:23:37 +02:00
option::None::<()>
}
}
2012-08-04 04:59:04 +02:00
_ => {
debug!("could not load metadata for %s", path.to_str());
2012-08-20 21:23:37 +02:00
option::None::<()>
}
}
}
});
if matches.is_empty() {
2012-08-20 21:23:37 +02:00
None
} else if matches.len() == 1u {
2012-08-20 21:23:37 +02:00
Some(matches[0])
} else {
cx.diag.span_err(
2012-08-23 02:24:52 +02:00
cx.span, fmt!("multiple matching crates for `%s`", crate_name));
cx.diag.handler().note(~"candidates:");
for matches.each |match_| {
2012-08-23 02:24:52 +02:00
cx.diag.handler().note(fmt!("path: %s", match_.ident));
let attrs = decoder::get_crate_attributes(match_.data);
2012-07-19 01:18:02 +02:00
note_linkage_attrs(cx.intr, cx.diag, attrs);
}
cx.diag.handler().abort_if_errors();
2012-08-20 21:23:37 +02:00
None
}
}
fn crate_name_from_metas(metas: ~[@ast::meta_item]) -> ~str {
let name_items = attr::find_meta_items_by_name(metas, ~"name");
2012-08-06 21:34:08 +02:00
match vec::last_opt(name_items) {
2012-08-20 21:23:37 +02:00
Some(i) => {
match attr::get_meta_item_value_str(i) {
Some(ref n) => (*n),
// FIXME (#2406): Probably want a warning here since the user
// is using the wrong type of meta item.
2012-08-04 04:59:04 +02:00
_ => fail
}
}
2012-08-20 21:23:37 +02:00
None => fail ~"expected to find the crate name"
}
}
fn note_linkage_attrs(intr: @ident_interner, diag: span_handler,
2012-07-19 01:18:02 +02:00
attrs: ~[ast::attribute]) {
for attr::find_linkage_metas(attrs).each |mi| {
2012-08-23 02:24:52 +02:00
diag.handler().note(fmt!("meta: %s",
pprust::meta_item_to_str(*mi,intr)));
}
}
fn crate_matches(crate_data: @~[u8], metas: ~[@ast::meta_item],
hash: ~str) -> bool {
let attrs = decoder::get_crate_attributes(crate_data);
let linkage_metas = attr::find_linkage_metas(attrs);
if hash.is_not_empty() {
let chash = decoder::get_crate_hash(crate_data);
2012-07-19 01:18:02 +02:00
if chash != hash { return false; }
}
metadata_matches(linkage_metas, metas)
}
fn metadata_matches(extern_metas: ~[@ast::meta_item],
local_metas: ~[@ast::meta_item]) -> bool {
2012-08-23 02:24:52 +02:00
debug!("matching %u metadata requirements against %u items",
vec::len(local_metas), vec::len(extern_metas));
2012-07-01 01:19:07 +02:00
for local_metas.each |needed| {
if !attr::contains(extern_metas, *needed) {
2012-08-02 02:30:05 +02:00
return false;
}
}
2012-08-02 02:30:05 +02:00
return true;
}
fn get_metadata_section(os: os,
2012-08-20 21:23:37 +02:00
filename: &Path) -> Option<@~[u8]> unsafe {
let mb = str::as_c_str(filename.to_str(), |buf| {
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
});
2012-08-20 21:23:37 +02:00
if mb as int == 0 { return option::None::<@~[u8]>; }
2012-08-06 21:34:08 +02:00
let of = match mk_object_file(mb) {
2012-08-20 21:23:37 +02:00
option::Some(of) => of,
_ => return option::None::<@~[u8]>
};
let si = mk_section_iter(of.llof);
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
let name_buf = llvm::LLVMGetSectionName(si.llsi);
2012-09-13 04:55:05 +02:00
let name = unsafe { str::raw::from_c_str(name_buf) };
if name == meta_section_name(os) {
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
let mut found = None;
unsafe {
2012-09-19 02:34:08 +02:00
let cvbuf: *u8 = cast::reinterpret_cast(&cbuf);
let vlen = vec::len(encoder::metadata_encoding_version);
debug!("checking %u bytes of metadata-version stamp",
vlen);
let minsz = uint::min(vlen, csz);
let mut version_ok = false;
do vec::raw::buf_as_slice(cvbuf, minsz) |buf0| {
version_ok = (buf0 ==
encoder::metadata_encoding_version);
}
if !version_ok { return None; }
let cvbuf1 = ptr::offset(cvbuf, vlen);
debug!("inflating %u bytes of compressed metadata",
csz - vlen);
do vec::raw::buf_as_slice(cvbuf1, csz-vlen) |bytes| {
let inflated = flate::inflate_bytes(bytes);
found = move Some(@(move inflated));
}
if found != None {
return found;
}
}
}
llvm::LLVMMoveToNextSection(si.llsi);
}
2012-08-20 21:23:37 +02:00
return option::None::<@~[u8]>;
}
fn meta_section_name(os: os) -> ~str {
2012-08-06 21:34:08 +02:00
match os {
2012-08-04 04:59:04 +02:00
os_macos => ~"__DATA,__note.rustc",
os_win32 => ~".note.rustc",
os_linux => ~".note.rustc",
os_freebsd => ~".note.rustc"
}
}
// A diagnostic function for dumping crate metadata to an output stream
fn list_file_metadata(intr: @ident_interner,
os: os, path: &Path, out: io::Writer) {
2012-08-06 21:34:08 +02:00
match get_metadata_section(os, path) {
2012-08-20 21:23:37 +02:00
option::Some(bytes) => decoder::list_crate_metadata(intr, bytes, out),
option::None => {
out.write_str(~"could not find metadata in "
+ path.to_str() + ~".\n");
}
}
}