Skip to content

Commit 4923492

Browse files
committed
---
yaml --- r: 3009 b: refs/heads/master c: ab3635e h: refs/heads/master i: 3007: 28d49db v: v3
1 parent 906dca4 commit 4923492

File tree

7 files changed

+148
-37
lines changed

7 files changed

+148
-37
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c9c1d860f14d2d422106d21a30beb2ea237d9de3
2+
refs/heads/master: ab3635eebef2b8cf0e19cdbc5b4e8dd7a49a4658

trunk/src/comp/back/link.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ mod write {
279279
*/
280280

281281

282-
iter crate_export_metas(ast::crate c) -> @ast::meta_item {
282+
iter crate_export_metas(&ast::crate c) -> @ast::meta_item {
283283
for (@ast::crate_directive cdir in c.node.directives) {
284284
alt (cdir.node) {
285285
case (ast::cdir_meta(?v, ?mis)) {
@@ -293,12 +293,30 @@ iter crate_export_metas(ast::crate c) -> @ast::meta_item {
293293
}
294294
}
295295
}
296-
fn get_crate_meta(&session::session sess,
297-
&ast::crate c, str k, str default,
298-
bool warn_default) -> str {
296+
297+
298+
iter crate_local_metas(&ast::crate c) -> @ast::meta_item {
299+
for (@ast::crate_directive cdir in c.node.directives) {
300+
alt (cdir.node) {
301+
case (ast::cdir_meta(?v, ?mis)) {
302+
if (v == ast::local_meta) {
303+
for (@ast::meta_item mi in mis) {
304+
put mi;
305+
}
306+
}
307+
}
308+
case (_) {}
309+
}
310+
}
311+
}
312+
313+
314+
fn get_crate_meta_export(&session::session sess,
315+
&ast::crate c, str k, str default,
316+
bool warn_default) -> str {
299317
let vec[@ast::meta_item] v = [];
300318
for each (@ast::meta_item mi in crate_export_metas(c)) {
301-
if (mi.node.name == k) {
319+
if (mi.node.key == k) {
302320
v += [mi];
303321
}
304322
}
@@ -323,7 +341,7 @@ fn get_crate_meta(&session::session sess,
323341
fn crate_meta_extras_hash(sha1 sha, &ast::crate crate) -> str {
324342
fn lteq(&@ast::meta_item ma,
325343
&@ast::meta_item mb) -> bool {
326-
ret ma.node.name <= mb.node.name;
344+
ret ma.node.key <= mb.node.key;
327345
}
328346

329347
fn len_and_str(&str s) -> str {
@@ -332,16 +350,16 @@ fn crate_meta_extras_hash(sha1 sha, &ast::crate crate) -> str {
332350

333351
let vec[mutable @ast::meta_item] v = [mutable];
334352
for each (@ast::meta_item mi in crate_export_metas(crate)) {
335-
if (mi.node.name != "name" &&
336-
mi.node.name != "vers") {
353+
if (mi.node.key != "name" &&
354+
mi.node.key != "vers") {
337355
v += [mutable mi];
338356
}
339357
}
340358
sort::quick_sort(lteq, v);
341359
sha.reset();
342360
for (@ast::meta_item m_ in v) {
343361
auto m = m_;
344-
sha.input_str(len_and_str(m.node.name));
362+
sha.input_str(len_and_str(m.node.key));
345363
sha.input_str(len_and_str(m.node.value));
346364
}
347365
ret truncated_sha1_result(sha);
@@ -352,13 +370,13 @@ fn crate_meta_name(&session::session sess, &ast::crate crate,
352370
auto os = str::split(fs::basename(output), '.' as u8);
353371
assert vec::len(os) >= 2u;
354372
vec::pop(os);
355-
ret get_crate_meta(sess, crate, "name", str::connect(os, "."),
356-
sess.get_opts().shared);
373+
ret get_crate_meta_export(sess, crate, "name", str::connect(os, "."),
374+
sess.get_opts().shared);
357375
}
358376

359377
fn crate_meta_vers(&session::session sess, &ast::crate crate) -> str {
360-
ret get_crate_meta(sess, crate, "vers", "0.0",
361-
sess.get_opts().shared);
378+
ret get_crate_meta_export(sess, crate, "vers", "0.0",
379+
sess.get_opts().shared);
362380
}
363381

364382
fn truncated_sha1_result(sha1 sha) -> str {

trunk/src/comp/front/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type crate_directive = spanned[crate_directive_];
9797

9898

9999
type meta_item = spanned[meta_item_];
100-
type meta_item_ = rec(ident name, str value);
100+
type meta_item_ = rec(ident key, str value);
101101

102102
type block = spanned[block_];
103103
type block_ = rec(vec[@stmt] stmts,

trunk/src/comp/front/creader.rs

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -500,24 +500,75 @@ fn get_metadata_section(str filename) -> option::t[vec[u8]] {
500500
}
501501

502502

503-
fn load_crate(session::session sess,
504-
int cnum,
505-
ast::ident ident,
506-
vec[str] library_search_paths) {
507-
auto filename = parser::default_native_name(sess, ident);
503+
fn metadata_matches(&vec[u8] data,
504+
&vec[@ast::meta_item] metas) -> bool {
505+
ret true;
506+
}
507+
508+
fn find_library_crate(&session::session sess,
509+
&ast::ident ident,
510+
&vec[@ast::meta_item] metas,
511+
&vec[str] library_search_paths)
512+
-> option::t[tup(str, vec[u8])] {
513+
514+
let str crate_name = ident;
515+
for (@ast::meta_item mi in metas) {
516+
if (mi.node.key == "name") {
517+
crate_name = mi.node.value;
518+
break;
519+
}
520+
}
521+
auto nn = parser::default_native_lib_naming(sess);
522+
let str prefix = nn.prefix + crate_name;
523+
524+
// FIXME: we could probably use a 'glob' function in std::fs but it will
525+
// be much easier to write once the unsafe module knows more about FFI
526+
// tricks. Currently the glob(3) interface is a bit more than we can
527+
// stomach from here, and writing a C++ wrapper is more work than just
528+
// manually filtering fs::list_dir here.
529+
508530
for (str library_search_path in library_search_paths) {
509-
auto path = fs::connect(library_search_path, filename);
510-
alt (get_metadata_section(path)) {
511-
case (option::some(?cvec)) {
512-
sess.set_external_crate(cnum, rec(name=ident, data=cvec));
513-
ret;
531+
532+
for (str path in fs::list_dir(library_search_path)) {
533+
534+
let str f = fs::basename(path);
535+
if (! (str::starts_with(f, prefix) &&
536+
str::ends_with(f, nn.suffix))) {
537+
log #fmt("skipping %s, doesn't look like %s*%s",
538+
path, prefix, nn.suffix);
539+
cont;
540+
}
541+
542+
alt (get_metadata_section(path)) {
543+
case (option::some(?cvec)) {
544+
if (!metadata_matches(cvec, metas)) {
545+
log #fmt("skipping %s, metadata doesn't match", path);
546+
cont;
547+
}
548+
log #fmt("found %s with matching metadata", path);
549+
ret some(tup(path, cvec));
550+
}
551+
case (_) {}
514552
}
515-
case (_) {}
516553
}
517554
}
518-
519-
log_err #fmt("can't open crate '%s' (looked for '%s' in lib search path)",
520-
ident, filename);
555+
ret none;
556+
}
557+
558+
fn load_library_crate(&session::session sess,
559+
&int cnum,
560+
&ast::ident ident,
561+
&vec[@ast::meta_item] metas,
562+
&vec[str] library_search_paths) {
563+
alt (find_library_crate(sess, ident, metas, library_search_paths)) {
564+
case (some(?t)) {
565+
sess.set_external_crate(cnum, rec(name=ident,
566+
data=t._1));
567+
ret;
568+
}
569+
case (_) {}
570+
}
571+
log_err #fmt("can't find crate for '%s'", ident);
521572
fail;
522573
}
523574

@@ -535,8 +586,8 @@ fn visit_view_item(env e, &@ast::view_item i) {
535586
auto cnum;
536587
if (!e.crate_cache.contains_key(ident)) {
537588
cnum = e.next_crate_num;
538-
load_crate(e.sess, cnum, ident,
539-
e.library_search_paths);
589+
load_library_crate(e.sess, cnum, ident, meta_items,
590+
e.library_search_paths);
540591
e.crate_cache.insert(ident, e.next_crate_num);
541592
e.next_crate_num += 1;
542593
} else {

trunk/src/comp/front/parser.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,20 +1992,26 @@ fn parse_native_mod_items(&parser p, &str native_name,
19921992
items=items);
19931993
}
19941994

1995-
fn default_native_name(session::session sess, str id) -> str {
1995+
fn default_native_lib_naming(session::session sess)
1996+
-> rec(str prefix, str suffix) {
19961997
alt (sess.get_targ_cfg().os) {
19971998
case (session::os_win32) {
1998-
ret id + ".dll";
1999+
ret rec(prefix="", suffix=".dll");
19992000
}
20002001
case (session::os_macos) {
2001-
ret "lib" + id + ".dylib";
2002+
ret rec(prefix="lib", suffix=".dylib");
20022003
}
20032004
case (session::os_linux) {
2004-
ret "lib" + id + ".so";
2005+
ret rec(prefix="lib", suffix=".so");
20052006
}
20062007
}
20072008
}
20082009

2010+
fn default_native_name(session::session sess, str id) -> str {
2011+
auto n = default_native_lib_naming(sess);
2012+
ret n.prefix + id + n.suffix;
2013+
}
2014+
20092015
fn parse_item_native_mod(&parser p) -> @ast::item {
20102016
auto lo = p.get_last_lo_pos();
20112017
auto abi = ast::native_abi_cdecl;
@@ -2194,7 +2200,7 @@ fn parse_meta_item(&parser p) -> @ast::meta_item {
21942200
case (token::LIT_STR(?s)) {
21952201
auto hi = p.get_hi_pos();
21962202
p.bump();
2197-
ret @spanned(lo, hi, rec(name = ident,
2203+
ret @spanned(lo, hi, rec(key = ident,
21982204
value = p.get_str(s)));
21992205
}
22002206
case (_) {

trunk/src/comp/middle/metadata.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import middle::trans;
1414
import middle::ty;
1515
import middle::ty::path_to_str;
1616
import back::x86;
17+
import back::link;
1718
import util::common;
1819
import pretty::ppaux::lit_to_str;
1920

@@ -46,6 +47,12 @@ const uint tag_index_buckets_bucket = 0x13u;
4647
const uint tag_index_buckets_bucket_elt = 0x14u;
4748
const uint tag_index_table = 0x15u;
4849

50+
const uint tag_meta_export = 0x16u;
51+
const uint tag_meta_local = 0x17u;
52+
const uint tag_meta_item = 0x18u;
53+
const uint tag_meta_item_key = 0x19u;
54+
const uint tag_meta_item_value = 0x20u;
55+
4956
// Type encoding
5057

5158
// Compact string representation for ty.t values. API ty_str & parse_from_str.
@@ -709,12 +716,41 @@ fn write_int(&io::writer writer, &int n) {
709716
}
710717

711718

719+
fn encode_meta_items(&ebml::writer ebml_w, &ast::crate crate) {
720+
721+
fn encode_meta_item(&ebml::writer ebml_w, &ast::meta_item mi) {
722+
ebml::start_tag(ebml_w, tag_meta_item);
723+
ebml::start_tag(ebml_w, tag_meta_item_key);
724+
ebml_w.writer.write(str::bytes(mi.node.key));
725+
ebml::end_tag(ebml_w);
726+
ebml::start_tag(ebml_w, tag_meta_item_value);
727+
ebml_w.writer.write(str::bytes(mi.node.value));
728+
ebml::end_tag(ebml_w);
729+
ebml::end_tag(ebml_w);
730+
}
731+
732+
ebml::start_tag(ebml_w, tag_meta_export);
733+
for each (@ast::meta_item mi in link::crate_export_metas(crate)) {
734+
encode_meta_item(ebml_w, *mi);
735+
}
736+
ebml::end_tag(ebml_w);
737+
738+
ebml::start_tag(ebml_w, tag_meta_local);
739+
for each (@ast::meta_item mi in link::crate_local_metas(crate)) {
740+
encode_meta_item(ebml_w, *mi);
741+
}
742+
ebml::end_tag(ebml_w);
743+
}
744+
712745
fn encode_metadata(&@trans::crate_ctxt cx, &@ast::crate crate)
713746
-> ValueRef {
714747
auto string_w = io::string_writer();
715748
auto buf_w = string_w.get_writer().get_buf_writer();
716749
auto ebml_w = ebml::create_writer(buf_w);
717750

751+
// Encode the meta items
752+
encode_meta_items(ebml_w, *crate);
753+
718754
// Encode and index the paths.
719755
ebml::start_tag(ebml_w, tag_paths);
720756
auto paths_index = encode_item_paths(ebml_w, crate);

trunk/src/comp/pretty/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ fn print_view_item(&ps s, &@ast::view_item item) {
10001000
popen(s);
10011001
fn print_meta(&ps s, &@ast::meta_item item) {
10021002
ibox(s, indent_unit);
1003-
word_space(s, item.node.name);
1003+
word_space(s, item.node.key);
10041004
word_space(s, "=");
10051005
print_string(s, item.node.value);
10061006
end(s);

0 commit comments

Comments
 (0)