@@ -2202,28 +2202,32 @@ pub fn register_fn_fuller(ccx: @CrateContext,
22022202 ccx.item_symbols.insert(node_id, ps);
22032203
22042204 // FIXME #4404 android JNI hacks
2205- let is_main = is_main_fn (&ccx.sess, node_id) &&
2205+ let is_entry = is_entry_fn (&ccx.sess, node_id) &&
22062206 (!*ccx.sess.building_library ||
22072207 (*ccx.sess.building_library &&
22082208 ccx.sess.targ_cfg.os == session::os_android));
2209- if is_main { create_main_wrapper (ccx, sp, llfn); }
2209+ if is_entry { create_entry_wrapper (ccx, sp, llfn); }
22102210 llfn
22112211}
22122212
2213- pub fn is_main_fn (sess: &Session, node_id: ast::node_id) -> bool {
2214- match *sess.main_fn {
2215- Some((main_id , _)) => node_id == main_id ,
2213+ pub fn is_entry_fn (sess: &Session, node_id: ast::node_id) -> bool {
2214+ match *sess.entry_fn {
2215+ Some((entry_id , _)) => node_id == entry_id ,
22162216 None => false
22172217 }
22182218}
22192219
22202220// Create a _rust_main(args: ~[str]) function which will be called from the
22212221// runtime rust_start function
2222- pub fn create_main_wrapper (ccx: @CrateContext,
2222+ pub fn create_entry_wrapper (ccx: @CrateContext,
22232223 _sp: span, main_llfn: ValueRef) {
2224-
2225- let llfn = create_main(ccx, main_llfn);
2226- create_entry_fn(ccx, llfn);
2224+ let et = ccx.sess.entry_type.unwrap();
2225+ if et == session::EntryMain {
2226+ let llfn = create_main(ccx, main_llfn);
2227+ create_entry_fn(ccx, llfn, true);
2228+ } else {
2229+ create_entry_fn(ccx, main_llfn, false);
2230+ }
22272231
22282232 fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef {
22292233 let nt = ty::mk_nil(ccx.tcx);
@@ -2247,7 +2251,7 @@ pub fn create_main_wrapper(ccx: @CrateContext,
22472251 return llfdecl;
22482252 }
22492253
2250- fn create_entry_fn(ccx: @CrateContext, rust_main: ValueRef) {
2254+ fn create_entry_fn(ccx: @CrateContext, rust_main: ValueRef, use_start_lang_item:bool ) {
22512255 let llfty = T_fn(~[ccx.int_type, T_ptr(T_ptr(T_i8()))], ccx.int_type);
22522256
22532257 // FIXME #4404 android JNI hacks
@@ -2269,34 +2273,51 @@ pub fn create_main_wrapper(ccx: @CrateContext,
22692273 unsafe {
22702274 llvm::LLVMPositionBuilderAtEnd(bld, llbb);
22712275 }
2272- let crate_map = ccx.crate_map;
2273- let start_def_id = ccx.tcx.lang_items.start_fn();
2274- let start_fn = if start_def_id.crate == ast::local_crate {
2275- ccx.sess.bug(~" start lang item is never in the local crate ")
2276- } else {
2277- let start_fn_type = csearch::get_type(ccx.tcx,
2278- start_def_id).ty;
2279- trans_external_path(ccx, start_def_id, start_fn_type)
2280- };
22812276
22822277 let retptr = unsafe {
22832278 llvm::LLVMBuildAlloca(bld, ccx.int_type, noname())
22842279 };
22852280
2286- let args = unsafe {
2287- let opaque_rust_main = llvm::LLVMBuildPointerCast(
2288- bld, rust_main, T_ptr(T_i8()), noname());
2289- let opaque_crate_map = llvm::LLVMBuildPointerCast(
2290- bld, crate_map, T_ptr(T_i8()), noname());
2291-
2292- ~[
2293- retptr,
2294- C_null(T_opaque_box_ptr(ccx)),
2295- opaque_rust_main,
2296- llvm::LLVMGetParam(llfn, 0 as c_uint),
2297- llvm::LLVMGetParam(llfn, 1 as c_uint),
2298- opaque_crate_map
2299- ]
2281+ let crate_map = ccx.crate_map;
2282+ let opaque_crate_map = unsafe {llvm::LLVMBuildPointerCast(
2283+ bld, crate_map, T_ptr(T_i8()), noname())};
2284+
2285+ let (start_fn, args) = if use_start_lang_item {
2286+ let start_def_id = ccx.tcx.lang_items.start_fn();
2287+ let start_fn = if start_def_id.crate == ast::local_crate {
2288+ ccx.sess.bug(~" start lang item is never in the local crate ")
2289+ } else {
2290+ let start_fn_type = csearch::get_type(ccx.tcx,
2291+ start_def_id).ty;
2292+ trans_external_path(ccx, start_def_id, start_fn_type)
2293+ };
2294+
2295+ let args = unsafe {
2296+ let opaque_rust_main = llvm::LLVMBuildPointerCast(
2297+ bld, rust_main, T_ptr(T_i8()), noname());
2298+
2299+ ~[
2300+ retptr,
2301+ C_null(T_opaque_box_ptr(ccx)),
2302+ opaque_rust_main,
2303+ llvm::LLVMGetParam(llfn, 0 as c_uint),
2304+ llvm::LLVMGetParam(llfn, 1 as c_uint),
2305+ opaque_crate_map
2306+ ]
2307+ };
2308+ (start_fn, args)
2309+ } else {
2310+ debug!(" using user-defined start fn ");
2311+ let args = unsafe {
2312+ ~[ retptr,
2313+ C_null(T_opaque_box_ptr(ccx)),
2314+ llvm::LLVMGetParam(llfn, 0 as c_uint),
2315+ llvm::LLVMGetParam(llfn, 1 as c_uint),
2316+ opaque_crate_map
2317+ ]
2318+ };
2319+
2320+ (rust_main, args)
23002321 };
23012322
23022323 unsafe {
0 commit comments