@@ -2202,28 +2202,32 @@ pub fn register_fn_fuller(ccx: @CrateContext,
2202
2202
ccx.item_symbols.insert(node_id, ps);
2203
2203
2204
2204
// 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) &&
2206
2206
(!*ccx.sess.building_library ||
2207
2207
(*ccx.sess.building_library &&
2208
2208
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); }
2210
2210
llfn
2211
2211
}
2212
2212
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 ,
2216
2216
None => false
2217
2217
}
2218
2218
}
2219
2219
2220
2220
// Create a _rust_main(args: ~[str]) function which will be called from the
2221
2221
// runtime rust_start function
2222
- pub fn create_main_wrapper (ccx: @CrateContext,
2222
+ pub fn create_entry_wrapper (ccx: @CrateContext,
2223
2223
_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
+ }
2227
2231
2228
2232
fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef {
2229
2233
let nt = ty::mk_nil(ccx.tcx);
@@ -2247,7 +2251,7 @@ pub fn create_main_wrapper(ccx: @CrateContext,
2247
2251
return llfdecl;
2248
2252
}
2249
2253
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 ) {
2251
2255
let llfty = T_fn(~[ccx.int_type, T_ptr(T_ptr(T_i8()))], ccx.int_type);
2252
2256
2253
2257
// FIXME #4404 android JNI hacks
@@ -2269,34 +2273,51 @@ pub fn create_main_wrapper(ccx: @CrateContext,
2269
2273
unsafe {
2270
2274
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
2271
2275
}
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
- };
2281
2276
2282
2277
let retptr = unsafe {
2283
2278
llvm::LLVMBuildAlloca(bld, ccx.int_type, noname())
2284
2279
};
2285
2280
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)
2300
2321
};
2301
2322
2302
2323
unsafe {
0 commit comments