2011-06-27 18:03:01 -05:00
|
|
|
// Extracting metadata from crate files
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
import driver::session;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::ast;
|
2011-05-12 10:24:54 -05:00
|
|
|
import lib::llvm::False;
|
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::mk_object_file;
|
|
|
|
import lib::llvm::mk_section_iter;
|
2011-06-30 01:42:35 -05:00
|
|
|
import front::attr;
|
2011-05-26 19:16:54 -05:00
|
|
|
import middle::resolve;
|
2011-07-26 09:47:13 -05:00
|
|
|
import syntax::visit;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::codemap::span;
|
2011-05-12 10:24:54 -05:00
|
|
|
import back::x86;
|
|
|
|
import util::common;
|
2011-07-05 19:57:34 -05:00
|
|
|
import std::ivec;
|
2011-05-17 13:41:41 -05:00
|
|
|
import std::str;
|
2011-05-12 10:24:54 -05:00
|
|
|
import std::fs;
|
2011-07-12 12:59:18 -05:00
|
|
|
import std::ioivec;
|
2011-05-12 10:24:54 -05:00
|
|
|
import std::option;
|
|
|
|
import std::option::none;
|
|
|
|
import std::option::some;
|
|
|
|
import std::map::hashmap;
|
2011-07-08 14:08:43 -05:00
|
|
|
import std::map::new_int_hash;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::print::pprust;
|
2011-07-07 14:22:39 -05:00
|
|
|
import common::*;
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-06-22 21:04:04 -05:00
|
|
|
export read_crates;
|
|
|
|
export list_file_metadata;
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-07-07 23:03:09 -05:00
|
|
|
// Traverses an AST, reading all the information about use'd crates and native
|
|
|
|
// libraries necessary for later resolving, typechecking, linking, etc.
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_crates(sess: session::session, crate: &ast::crate) {
|
|
|
|
let e =
|
|
|
|
@{sess: sess,
|
|
|
|
crate_cache: @std::map::new_str_hash[int](),
|
|
|
|
library_search_paths: sess.get_opts().library_search_paths,
|
|
|
|
mutable next_crate_num: 1};
|
|
|
|
let v =
|
|
|
|
visit::mk_simple_visitor(@{visit_view_item:
|
|
|
|
bind visit_view_item(e, _),
|
|
|
|
visit_item: bind visit_item(e, _)
|
|
|
|
with *visit::default_simple_visitor()});
|
2011-07-26 09:47:13 -05:00
|
|
|
visit::visit_crate(crate, (), v);
|
2011-07-07 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-07-07 23:37:56 -05:00
|
|
|
type env =
|
2011-07-27 07:19:39 -05:00
|
|
|
@{sess: session::session,
|
|
|
|
crate_cache: @hashmap[str, int],
|
|
|
|
library_search_paths: str[],
|
|
|
|
mutable next_crate_num: ast::crate_num};
|
2011-07-07 23:37:56 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn visit_view_item(e: env, i: &@ast::view_item) {
|
|
|
|
alt i.node {
|
|
|
|
ast::view_item_use(ident, meta_items, id) {
|
|
|
|
let cnum = resolve_crate(e, ident, meta_items, i.span);
|
|
|
|
cstore::add_use_stmt_cnum(e.sess.get_cstore(), id, cnum);
|
|
|
|
}
|
|
|
|
_ { }
|
2011-07-07 23:37:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn visit_item(e: env, i: &@ast::item) {
|
|
|
|
alt i.node {
|
|
|
|
ast::item_native_mod(m) {
|
|
|
|
if m.abi != ast::native_abi_rust && m.abi != ast::native_abi_cdecl {
|
|
|
|
ret;
|
2011-07-07 23:37:56 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let cstore = e.sess.get_cstore();
|
|
|
|
if !cstore::add_used_library(cstore, m.native_name) { ret; }
|
|
|
|
for a: ast::attribute in
|
|
|
|
attr::find_attrs_by_name(i.attrs, "link_args") {
|
|
|
|
alt attr::get_meta_item_value_str(attr::attr_meta(a)) {
|
|
|
|
some(linkarg) { cstore::add_used_link_args(cstore, linkarg); }
|
|
|
|
none. {/* fallthrough */ }
|
|
|
|
}
|
2011-07-07 23:37:56 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-07 23:37:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 23:03:09 -05:00
|
|
|
// A diagnostic function for dumping crate metadata to an output stream
|
2011-07-27 07:19:39 -05:00
|
|
|
fn list_file_metadata(path: str, out: ioivec::writer) {
|
|
|
|
alt get_metadata_section(path) {
|
|
|
|
option::some(bytes) { decoder::list_crate_metadata(bytes, out); }
|
|
|
|
option::none. {
|
|
|
|
out.write_str("Could not find metadata in " + path + ".\n");
|
|
|
|
}
|
2011-07-07 23:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn metadata_matches(crate_data: &@u8[], metas: &(@ast::meta_item)[]) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(crate_data);
|
|
|
|
let linkage_metas = attr::find_linkage_metas(attrs);
|
2011-06-28 14:53:59 -05:00
|
|
|
|
|
|
|
log #fmt("matching %u metadata requirements against %u items",
|
2011-07-05 19:57:34 -05:00
|
|
|
ivec::len(metas), ivec::len(linkage_metas));
|
2011-06-28 14:53:59 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
for needed: @ast::meta_item in metas {
|
|
|
|
if !attr::contains(linkage_metas, needed) {
|
2011-06-28 14:53:59 -05:00
|
|
|
log #fmt("missing %s", pprust::meta_item_to_str(*needed));
|
|
|
|
ret false;
|
2011-06-10 17:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn default_native_lib_naming(sess: session::session, static: bool) ->
|
|
|
|
{prefix: str, suffix: str} {
|
|
|
|
if static { ret {prefix: "lib", suffix: ".rlib"}; }
|
|
|
|
alt sess.get_targ_cfg().os {
|
|
|
|
session::os_win32. { ret {prefix: "", suffix: ".dll"}; }
|
|
|
|
session::os_macos. { ret {prefix: "lib", suffix: ".dylib"}; }
|
|
|
|
session::os_linux. { ret {prefix: "lib", suffix: ".so"}; }
|
2011-06-27 14:24:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn find_library_crate(sess: &session::session, ident: &ast::ident,
|
|
|
|
metas: &(@ast::meta_item)[],
|
|
|
|
library_search_paths: &str[]) ->
|
|
|
|
option::t[{ident: str, data: @u8[]}] {
|
2011-06-28 18:52:11 -05:00
|
|
|
|
2011-07-05 17:58:48 -05:00
|
|
|
attr::require_unique_names(sess, metas);
|
|
|
|
|
2011-07-27 07:48:34 -05:00
|
|
|
// FIXME: Probably want a warning here since the user
|
|
|
|
// is using the wrong type of meta item
|
|
|
|
let crate_name = {
|
|
|
|
let name_items = attr::find_meta_items_by_name(metas, "name");
|
|
|
|
alt ivec::last(name_items) {
|
|
|
|
some(i) {
|
|
|
|
alt attr::get_meta_item_value_str(i) {
|
|
|
|
some(n) { n }
|
|
|
|
_ { ident }
|
2011-06-21 16:23:16 -05:00
|
|
|
}
|
2011-07-27 07:48:34 -05:00
|
|
|
}
|
|
|
|
none. { ident }
|
|
|
|
}
|
|
|
|
};
|
2011-06-28 18:52:11 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let nn = default_native_lib_naming(sess, sess.get_opts().static);
|
|
|
|
let x =
|
|
|
|
find_library_crate_aux(nn, crate_name, metas, library_search_paths);
|
|
|
|
if x != none || sess.get_opts().static { ret x; }
|
|
|
|
let nn2 = default_native_lib_naming(sess, true);
|
2011-07-08 14:33:00 -05:00
|
|
|
ret find_library_crate_aux(nn2, crate_name, metas, library_search_paths);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn find_library_crate_aux(nn: &{prefix: str, suffix: str}, crate_name: str,
|
|
|
|
metas: &(@ast::meta_item)[],
|
|
|
|
library_search_paths: &str[]) ->
|
|
|
|
option::t[{ident: str, data: @u8[]}] {
|
|
|
|
let prefix: str = nn.prefix + crate_name;
|
2011-06-10 14:56:42 -05:00
|
|
|
// FIXME: we could probably use a 'glob' function in std::fs but it will
|
|
|
|
// be much easier to write once the unsafe module knows more about FFI
|
|
|
|
// tricks. Currently the glob(3) interface is a bit more than we can
|
|
|
|
// stomach from here, and writing a C++ wrapper is more work than just
|
|
|
|
// manually filtering fs::list_dir here.
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
for library_search_path: str in library_search_paths {
|
2011-06-29 13:54:35 -05:00
|
|
|
log #fmt("searching %s", library_search_path);
|
2011-07-27 07:19:39 -05:00
|
|
|
for path: str in fs::list_dir(library_search_path) {
|
2011-06-29 13:54:35 -05:00
|
|
|
log #fmt("searching %s", path);
|
2011-07-27 07:19:39 -05:00
|
|
|
let f: str = fs::basename(path);
|
|
|
|
if !(str::starts_with(f, prefix) && str::ends_with(f, nn.suffix))
|
|
|
|
{
|
2011-06-15 13:19:50 -05:00
|
|
|
log #fmt("skipping %s, doesn't look like %s*%s", path, prefix,
|
|
|
|
nn.suffix);
|
2011-06-10 14:56:42 -05:00
|
|
|
cont;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
alt get_metadata_section(path) {
|
|
|
|
option::some(cvec) {
|
|
|
|
if !metadata_matches(cvec, metas) {
|
|
|
|
log #fmt("skipping %s, metadata doesn't match", path);
|
|
|
|
cont;
|
2011-06-10 14:56:42 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
log #fmt("found %s with matching metadata", path);
|
|
|
|
ret some({ident: path, data: cvec});
|
|
|
|
}
|
|
|
|
_ { }
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
ret none;
|
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get_metadata_section(filename: str) -> option::t[@u8[]] {
|
|
|
|
let b = str::buf(filename);
|
|
|
|
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(b);
|
|
|
|
if mb as int == 0 { ret option::none[@u8[]]; }
|
|
|
|
let of = mk_object_file(mb);
|
|
|
|
let si = mk_section_iter(of.llof);
|
|
|
|
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
|
|
|
let name_buf = llvm::LLVMGetSectionName(si.llsi);
|
|
|
|
let name = str::str_from_cstr(name_buf);
|
|
|
|
if str::eq(name, x86::get_meta_sect_name()) {
|
|
|
|
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
|
|
|
|
let csz = llvm::LLVMGetSectionSize(si.llsi);
|
|
|
|
let cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
|
2011-07-12 12:59:18 -05:00
|
|
|
ret option::some[@u8[]](@ivec::unsafe::from_buf(cvbuf, csz));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
llvm::LLVMMoveToNextSection(si.llsi);
|
|
|
|
}
|
2011-07-12 12:59:18 -05:00
|
|
|
ret option::none[@u8[]];
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn load_library_crate(sess: &session::session, span: span, ident: &ast::ident,
|
|
|
|
metas: &(@ast::meta_item)[],
|
|
|
|
library_search_paths: &str[]) ->
|
|
|
|
{ident: str, data: @u8[]} {
|
2011-07-08 14:40:16 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
alt find_library_crate(sess, ident, metas, library_search_paths) {
|
|
|
|
some(t) { ret t; }
|
|
|
|
none. {
|
|
|
|
sess.span_fatal(span, #fmt("can't find crate for '%s'", ident));
|
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn resolve_crate(e: env, ident: ast::ident, metas: (@ast::meta_item)[],
|
|
|
|
span: span) -> ast::crate_num {
|
|
|
|
if !e.crate_cache.contains_key(ident) {
|
|
|
|
let cinfo =
|
|
|
|
load_library_crate(e.sess, span, ident, metas,
|
|
|
|
e.library_search_paths);
|
2011-07-08 14:40:16 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let cfilename = cinfo.ident;
|
|
|
|
let cdata = cinfo.data;
|
2011-07-08 14:40:16 -05:00
|
|
|
|
2011-07-08 14:55:38 -05:00
|
|
|
// Claim this crate number and cache it
|
2011-07-27 07:19:39 -05:00
|
|
|
let cnum = e.next_crate_num;
|
2011-07-08 14:40:16 -05:00
|
|
|
e.crate_cache.insert(ident, cnum);
|
2011-07-08 14:04:52 -05:00
|
|
|
e.next_crate_num += 1;
|
2011-07-08 14:40:16 -05:00
|
|
|
|
2011-07-08 14:55:38 -05:00
|
|
|
// Now resolve the crates referenced by this crate
|
2011-07-27 07:19:39 -05:00
|
|
|
let cnum_map = resolve_crate_deps(e, cdata);
|
2011-07-08 14:55:38 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let cmeta = {name: ident, data: cdata, cnum_map: cnum_map};
|
2011-07-08 14:55:38 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let cstore = e.sess.get_cstore();
|
2011-07-08 14:40:16 -05:00
|
|
|
cstore::set_crate_data(cstore, cnum, cmeta);
|
|
|
|
cstore::add_used_crate_file(cstore, cfilename);
|
2011-07-08 14:04:52 -05:00
|
|
|
ret cnum;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { ret e.crate_cache.get(ident); }
|
2011-07-08 14:55:38 -05:00
|
|
|
}
|
2011-05-26 19:16:54 -05:00
|
|
|
|
2011-07-08 14:55:38 -05:00
|
|
|
// Go through the crate metadata and load any crates that it references
|
2011-07-27 07:19:39 -05:00
|
|
|
fn resolve_crate_deps(e: env, cdata: &@u8[]) -> cstore::cnum_map {
|
2011-07-08 14:55:38 -05:00
|
|
|
log "resolving deps of external crate";
|
|
|
|
// The map from crate numbers in the crate we're resolving to local crate
|
|
|
|
// numbers
|
2011-07-27 07:19:39 -05:00
|
|
|
let cnum_map = new_int_hash[ast::crate_num]();
|
|
|
|
for dep: decoder::crate_dep in decoder::get_crate_deps(cdata) {
|
|
|
|
let extrn_cnum = dep.cnum;
|
|
|
|
let cname = dep.ident;
|
2011-07-08 14:55:38 -05:00
|
|
|
log #fmt("resolving dep %s", cname);
|
2011-07-27 07:19:39 -05:00
|
|
|
if e.crate_cache.contains_key(cname) {
|
2011-07-08 14:55:38 -05:00
|
|
|
log "already have it";
|
|
|
|
// We've already seen this crate
|
2011-07-27 07:19:39 -05:00
|
|
|
let local_cnum = e.crate_cache.get(cname);
|
2011-07-08 14:55:38 -05:00
|
|
|
cnum_map.insert(extrn_cnum, local_cnum);
|
|
|
|
} else {
|
|
|
|
log "need to load it";
|
|
|
|
// This is a new one so we've got to load it
|
|
|
|
// FIXME: Need better error reporting than just a bogus span
|
2011-07-27 07:19:39 -05:00
|
|
|
let fake_span = {lo: 0u, hi: 0u};
|
|
|
|
let local_cnum = resolve_crate(e, cname, ~[], fake_span);
|
2011-07-08 14:55:38 -05:00
|
|
|
cnum_map.insert(extrn_cnum, local_cnum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret cnum_map;
|
|
|
|
}
|
2011-04-07 13:20:57 -05:00
|
|
|
|
2011-03-24 19:35:27 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 17:07:27 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-24 19:35:27 -05:00
|
|
|
// End:
|