@@ -2197,28 +2197,32 @@ pub fn register_fn_fuller(ccx: @CrateContext,
21972197 ccx.item_symbols.insert(node_id, ps);
21982198
21992199 // FIXME #4404 android JNI hacks
2200- let is_main = is_main_fn (&ccx.sess, node_id) &&
2200+ let is_entry = is_entry_fn (&ccx.sess, node_id) &&
22012201 (!*ccx.sess.building_library ||
22022202 (*ccx.sess.building_library &&
22032203 ccx.sess.targ_cfg.os == session::os_android));
2204- if is_main { create_main_wrapper (ccx, sp, llfn); }
2204+ if is_entry { create_entry_wrapper (ccx, sp, llfn); }
22052205 llfn
22062206}
22072207
2208- pub fn is_main_fn (sess: &Session, node_id: ast::node_id) -> bool {
2209- match *sess.main_fn {
2210- Some((main_id , _)) => node_id == main_id ,
2208+ pub fn is_entry_fn (sess: &Session, node_id: ast::node_id) -> bool {
2209+ match *sess.entry_fn {
2210+ Some((entry_id , _)) => node_id == entry_id ,
22112211 None => false
22122212 }
22132213}
22142214
22152215// Create a _rust_main(args: ~[str]) function which will be called from the
22162216// runtime rust_start function
2217- pub fn create_main_wrapper (ccx: @CrateContext,
2217+ pub fn create_entry_wrapper (ccx: @CrateContext,
22182218 _sp: span, main_llfn: ValueRef) {
2219-
2220- let llfn = create_main(ccx, main_llfn);
2221- create_entry_fn(ccx, llfn);
2219+ let et = ccx.sess.entry_type.unwrap();
2220+ if et == session::EntryMain {
2221+ let llfn = create_main(ccx, main_llfn);
2222+ create_entry_fn(ccx, llfn, true);
2223+ } else {
2224+ create_entry_fn(ccx, main_llfn, false);
2225+ }
22222226
22232227 fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef {
22242228 let nt = ty::mk_nil(ccx.tcx);
@@ -2242,7 +2246,7 @@ pub fn create_main_wrapper(ccx: @CrateContext,
22422246 return llfdecl;
22432247 }
22442248
2245- fn create_entry_fn(ccx: @CrateContext, rust_main: ValueRef) {
2249+ fn create_entry_fn(ccx: @CrateContext, rust_main: ValueRef, use_start_lang_item:bool ) {
22462250 let llfty = T_fn(~[ccx.int_type, T_ptr(T_ptr(T_i8()))], ccx.int_type);
22472251
22482252 // FIXME #4404 android JNI hacks
@@ -2264,34 +2268,51 @@ pub fn create_main_wrapper(ccx: @CrateContext,
22642268 unsafe {
22652269 llvm::LLVMPositionBuilderAtEnd(bld, llbb);
22662270 }
2267- let crate_map = ccx.crate_map;
2268- let start_def_id = ccx.tcx.lang_items.start_fn();
2269- let start_fn = if start_def_id.crate == ast::local_crate {
2270- ccx.sess.bug(~" start lang item is never in the local crate ")
2271- } else {
2272- let start_fn_type = csearch::get_type(ccx.tcx,
2273- start_def_id).ty;
2274- trans_external_path(ccx, start_def_id, start_fn_type)
2275- };
22762271
22772272 let retptr = unsafe {
22782273 llvm::LLVMBuildAlloca(bld, ccx.int_type, noname())
22792274 };
22802275
2281- let args = unsafe {
2282- let opaque_rust_main = llvm::LLVMBuildPointerCast(
2283- bld, rust_main, T_ptr(T_i8()), noname());
2284- let opaque_crate_map = llvm::LLVMBuildPointerCast(
2285- bld, crate_map, T_ptr(T_i8()), noname());
2286-
2287- ~[
2288- retptr,
2289- C_null(T_opaque_box_ptr(ccx)),
2290- opaque_rust_main,
2291- llvm::LLVMGetParam(llfn, 0 as c_uint),
2292- llvm::LLVMGetParam(llfn, 1 as c_uint),
2293- opaque_crate_map
2294- ]
2276+ let crate_map = ccx.crate_map;
2277+ let opaque_crate_map = unsafe {llvm::LLVMBuildPointerCast(
2278+ bld, crate_map, T_ptr(T_i8()), noname())};
2279+
2280+ let (start_fn, args) = if use_start_lang_item {
2281+ let start_def_id = ccx.tcx.lang_items.start_fn();
2282+ let start_fn = if start_def_id.crate == ast::local_crate {
2283+ ccx.sess.bug(~" start lang item is never in the local crate ")
2284+ } else {
2285+ let start_fn_type = csearch::get_type(ccx.tcx,
2286+ start_def_id).ty;
2287+ trans_external_path(ccx, start_def_id, start_fn_type)
2288+ };
2289+
2290+ let args = unsafe {
2291+ let opaque_rust_main = llvm::LLVMBuildPointerCast(
2292+ bld, rust_main, T_ptr(T_i8()), noname());
2293+
2294+ ~[
2295+ retptr,
2296+ C_null(T_opaque_box_ptr(ccx)),
2297+ opaque_rust_main,
2298+ llvm::LLVMGetParam(llfn, 0 as c_uint),
2299+ llvm::LLVMGetParam(llfn, 1 as c_uint),
2300+ opaque_crate_map
2301+ ]
2302+ };
2303+ (start_fn, args)
2304+ } else {
2305+ debug!(" using user-defined start fn ");
2306+ let args = unsafe {
2307+ ~[ retptr,
2308+ C_null(T_opaque_box_ptr(ccx)),
2309+ llvm::LLVMGetParam(llfn, 0 as c_uint),
2310+ llvm::LLVMGetParam(llfn, 1 as c_uint),
2311+ opaque_crate_map
2312+ ]
2313+ };
2314+
2315+ (rust_main, args)
22952316 };
22962317
22972318 unsafe {
0 commit comments