Skip to content

Commit c1e287a

Browse files
committed
Make -Z gen-crate-map usable for I/O
In #10422, I didn't actually test to make sure that the '-Z gen-crate-map' option was usable before I implemented it. The crate map was indeed generated when '-Z gen-crate-map' was specified, but the I/O factory slot was empty because of an extra check in trans about filling in that location. This commit both fixes that location, and checks in a "fancy test" which does lots of fun stuff. The test will use the rustc library to compile a rust crate, and then compile a C program to link against that crate and run the C program. To my knowledge this is the first test of its kind, so it's a little ad-hoc, but it seems to get the job done. We could perhaps generalize running tests like this, but for now I think it's fine to have this sort of functionality tucked away in a test.
1 parent 6d6ccb7 commit c1e287a

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

src/librustc/middle/trans/base.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use extra::time;
7777
use extra::sort;
7878
use syntax::ast::Name;
7979
use syntax::ast_map::{path, path_elt_to_str, path_name, path_pretty_name};
80-
use syntax::ast_util::{local_def};
80+
use syntax::ast_util::{local_def, is_local};
8181
use syntax::attr;
8282
use syntax::codemap::Span;
8383
use syntax::parse::token;
@@ -2996,7 +2996,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
29962996
return map;
29972997
}
29982998

2999-
pub fn fill_crate_map(ccx: &mut CrateContext, map: ValueRef) {
2999+
pub fn fill_crate_map(ccx: @mut CrateContext, map: ValueRef) {
30003000
let mut subcrates: ~[ValueRef] = ~[];
30013001
let mut i = 1;
30023002
let cstore = ccx.sess.cstore;
@@ -3014,19 +3014,20 @@ pub fn fill_crate_map(ccx: &mut CrateContext, map: ValueRef) {
30143014
subcrates.push(p2i(ccx, cr));
30153015
i += 1;
30163016
}
3017-
let event_loop_factory = if !*ccx.sess.building_library {
3018-
match ccx.tcx.lang_items.event_loop_factory() {
3019-
Some(did) => unsafe {
3017+
let event_loop_factory = match ccx.tcx.lang_items.event_loop_factory() {
3018+
Some(did) => unsafe {
3019+
if is_local(did) {
3020+
llvm::LLVMConstPointerCast(get_item_val(ccx, did.node),
3021+
ccx.int_type.ptr_to().to_ref())
3022+
} else {
30203023
let name = csearch::get_symbol(ccx.sess.cstore, did);
30213024
let global = name.with_c_str(|buf| {
30223025
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
30233026
});
30243027
global
3025-
},
3026-
None => C_null(ccx.int_type.ptr_to())
3027-
}
3028-
} else {
3029-
C_null(ccx.int_type.ptr_to())
3028+
}
3029+
},
3030+
None => C_null(ccx.int_type.ptr_to())
30303031
};
30313032
unsafe {
30323033
let maptype = Type::array(&ccx.int_type, subcrates.len() as u64);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) lib.rs -Z gen-crate-map
5+
ln -nsf $(call DYLIB,boot-*) $(call DYLIB,boot)
6+
$(CC) main.c -o $(call RUN,main) -lboot -Wl,-rpath,$(TMPDIR)
7+
$(call RUN,main)
8+
rm $(call DYLIB,boot)
9+
$(call FAIL,main)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[link(package_id = "boot", name = "boot", vers = "0.1")];
12+
#[crate_type = "lib"];
13+
14+
extern mod rustuv; // pull in uvio
15+
16+
use std::rt;
17+
18+
#[no_mangle] // this needs to get called from C
19+
pub extern "C" fn foo(argc: int, argv: **u8) -> int {
20+
do rt::start(argc, argv) {
21+
do spawn {
22+
println!("hello");
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// this is the rust entry point that we're going to call.
12+
int foo(int argc, char *argv[]);
13+
14+
int main(int argc, char *argv[]) {
15+
return foo(argc, argv);
16+
}

0 commit comments

Comments
 (0)