rustc: Eliminate some session deps from metadata::loader

This commit is contained in:
Brian Anderson 2012-05-22 17:16:26 -07:00
parent c90a047016
commit 98b93b6c86
6 changed files with 84 additions and 55 deletions

View File

@ -1,15 +1,12 @@
import driver::session;
import session::sess_os_to_meta_os;
import metadata::loader::meta_section_name;
fn get_target_strs(target_os: session::os) -> target_strs::t {
ret {
module_asm: "",
meta_sect_name: alt target_os {
session::os_macos { "__DATA,__note.rustc" }
session::os_win32 { ".note.rustc" }
session::os_linux { ".note.rustc" }
session::os_freebsd { ".note.rustc" }
},
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)),
data_layout: alt target_os {
session::os_macos {

View File

@ -1,15 +1,12 @@
import driver::session;
import session::sess_os_to_meta_os;
import metadata::loader::meta_section_name;
fn get_target_strs(target_os: session::os) -> target_strs::t {
ret {
module_asm: "",
meta_sect_name: alt target_os {
session::os_macos { "__DATA,__note.rustc" }
session::os_win32 { ".note.rustc" }
session::os_linux { ".note.rustc" }
session::os_freebsd { ".note.rustc" }
},
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)),
data_layout: alt target_os {
session::os_macos {

View File

@ -676,7 +676,8 @@ fn early_error(emitter: diagnostic::emitter, msg: str) -> ! {
}
fn list_metadata(sess: session, path: str, out: io::writer) {
metadata::loader::list_file_metadata(sess, path, out);
metadata::loader::list_file_metadata(
session::sess_os_to_meta_os(sess.targ_cfg.os), path, out);
}
#[cfg(test)]

View File

@ -194,6 +194,17 @@ fn building_library(req_crate_type: crate_type, crate: @ast::crate,
}
}
fn sess_os_to_meta_os(os: os) -> metadata::loader::os {
import metadata::loader;
alt os {
os_win32 { loader::os_win32 }
os_linux { loader::os_linux }
os_macos { loader::os_macos }
os_freebsd { loader::os_freebsd }
}
}
#[cfg(test)]
mod test {
import syntax::ast_util;

View File

@ -179,8 +179,16 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
alt existing_match(e, metas, hash) {
none {
let cinfo =
loader::load_library_crate(e.sess, ident, span, metas, hash);
let load_ctxt: loader::ctxt = {
sess: e.sess,
span: span,
ident: ident,
metas: metas,
hash: hash,
os: session::sess_os_to_meta_os(e.sess.targ_cfg.os),
static: e.sess.opts.static
};
let cinfo = loader::load_library_crate(load_ctxt);
let cfilename = cinfo.ident;
let cdata = cinfo.data;

View File

@ -13,56 +13,62 @@ import lib::llvm::{False, llvm, mk_object_file, mk_section_iter};
import util::{filesearch};
import io::writer_util;
export os;
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;
fn load_library_crate(sess: session::session, ident: ast::ident, span: span,
metas: [@ast::meta_item], hash: str)
-> {ident: str, data: @[u8]} {
enum os {
os_macos,
os_win32,
os_linux,
os_freebsd
}
type ctxt = {
sess: session,
span: span,
ident: ast::ident,
metas: [@ast::meta_item],
hash: str,
os: os,
static: bool
};
alt find_library_crate(sess, span, metas, hash) {
fn load_library_crate(cx: ctxt) -> {ident: str, data: @[u8]} {
alt find_library_crate(cx) {
some(t) { ret t; }
none {
sess.span_fatal(span, #fmt["can't find crate for '%s'", ident]);
cx.sess.span_fatal(
cx.span, #fmt["can't find crate for '%s'", cx.ident]);
}
}
}
fn find_library_crate(sess: session::session, span: span,
metas: [@ast::meta_item], hash: str)
-> option<{ident: str, data: @[u8]}> {
attr::require_unique_names(sess.diagnostic(), metas);
let metas = metas;
let nn = default_native_lib_naming(sess, sess.opts.static);
find_library_crate_aux(sess, span, nn,
metas, hash, sess.filesearch)
fn find_library_crate(cx: ctxt) -> option<{ident: str, data: @[u8]}> {
attr::require_unique_names(cx.sess.diagnostic(), cx.metas);
find_library_crate_aux(cx, libname(cx), cx.sess.filesearch)
}
fn default_native_lib_naming(sess: session::session, static: bool) ->
{prefix: str, suffix: str} {
if static { ret {prefix: "lib", suffix: ".rlib"}; }
alt sess.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"}; }
session::os_freebsd { ret {prefix: "lib", suffix: ".so"}; }
fn libname(cx: ctxt) -> {prefix: str, suffix: str} {
if cx.static { ret {prefix: "lib", suffix: ".rlib"}; }
alt cx.os {
os_win32 { ret {prefix: "", suffix: ".dll"}; }
os_macos { ret {prefix: "lib", suffix: ".dylib"}; }
os_linux { ret {prefix: "lib", suffix: ".so"}; }
os_freebsd { ret {prefix: "lib", suffix: ".so"}; }
}
}
fn find_library_crate_aux(sess: session::session,
span: span,
fn find_library_crate_aux(cx: ctxt,
nn: {prefix: str, suffix: str},
metas: [@ast::meta_item],
hash: str,
filesearch: filesearch::filesearch) ->
option<{ident: str, data: @[u8]}> {
let crate_name = crate_name_from_metas(metas);
let crate_name = crate_name_from_metas(cx.metas);
let prefix: str = nn.prefix + crate_name + "-";
let suffix: str = nn.suffix;
@ -76,9 +82,9 @@ fn find_library_crate_aux(sess: session::session,
option::none::<()>
} else {
#debug("%s is a candidate", path);
alt get_metadata_section(sess, path) {
alt get_metadata_section(cx.os, path) {
option::some(cvec) {
if !crate_matches(cvec, metas, hash) {
if !crate_matches(cvec, cx.metas, cx.hash) {
#debug("skipping %s, metadata doesn't match", path);
option::none::<()>
} else {
@ -100,15 +106,15 @@ fn find_library_crate_aux(sess: session::session,
} else if matches.len() == 1u {
some(matches[0])
} else {
sess.span_err(
span, #fmt("multiple matching crates for `%s`", crate_name));
sess.note("candidates:");
cx.sess.span_err(
cx.span, #fmt("multiple matching crates for `%s`", crate_name));
cx.sess.note("candidates:");
for matches.each {|match|
sess.note(#fmt("path: %s", match.ident));
cx.sess.note(#fmt("path: %s", match.ident));
let attrs = decoder::get_crate_attributes(match.data);
note_linkage_attrs(sess, attrs);
note_linkage_attrs(cx.sess, attrs);
}
sess.abort_if_errors();
cx.sess.abort_if_errors();
none
}
}
@ -166,7 +172,7 @@ fn metadata_matches(extern_metas: [@ast::meta_item],
ret true;
}
fn get_metadata_section(sess: session::session,
fn get_metadata_section(os: os,
filename: str) -> option<@[u8]> unsafe {
let mb = str::as_c_str(filename, {|buf|
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
@ -180,7 +186,7 @@ fn get_metadata_section(sess: session::session,
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
let name_buf = llvm::LLVMGetSectionName(si.llsi);
let name = unsafe { str::unsafe::from_c_str(name_buf) };
if str::eq(name, sess.targ_cfg.target_strs.meta_sect_name) {
if str::eq(name, meta_section_name(os)) {
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
unsafe {
@ -193,9 +199,18 @@ fn get_metadata_section(sess: session::session,
ret option::none::<@[u8]>;
}
fn meta_section_name(os: os) -> str {
alt os {
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(sess: session::session, path: str, out: io::writer) {
alt get_metadata_section(sess, path) {
fn list_file_metadata(os: os, path: str, out: io::writer) {
alt get_metadata_section(os, path) {
option::some(bytes) { decoder::list_crate_metadata(bytes, out); }
option::none {
out.write_str("could not find metadata in " + path + ".\n");