@@ -2197,28 +2197,32 @@ pub fn register_fn_fuller(ccx: @CrateContext,
2197
2197
ccx.item_symbols.insert(node_id, ps);
2198
2198
2199
2199
// 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) &&
2201
2201
(!*ccx.sess.building_library ||
2202
2202
(*ccx.sess.building_library &&
2203
2203
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); }
2205
2205
llfn
2206
2206
}
2207
2207
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 ,
2211
2211
None => false
2212
2212
}
2213
2213
}
2214
2214
2215
2215
// Create a _rust_main(args: ~[str]) function which will be called from the
2216
2216
// runtime rust_start function
2217
- pub fn create_main_wrapper (ccx: @CrateContext,
2217
+ pub fn create_entry_wrapper (ccx: @CrateContext,
2218
2218
_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
+ }
2222
2226
2223
2227
fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef {
2224
2228
let nt = ty::mk_nil(ccx.tcx);
@@ -2242,7 +2246,7 @@ pub fn create_main_wrapper(ccx: @CrateContext,
2242
2246
return llfdecl;
2243
2247
}
2244
2248
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 ) {
2246
2250
let llfty = T_fn(~[ccx.int_type, T_ptr(T_ptr(T_i8()))], ccx.int_type);
2247
2251
2248
2252
// FIXME #4404 android JNI hacks
@@ -2264,34 +2268,51 @@ pub fn create_main_wrapper(ccx: @CrateContext,
2264
2268
unsafe {
2265
2269
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
2266
2270
}
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
- };
2276
2271
2277
2272
let retptr = unsafe {
2278
2273
llvm::LLVMBuildAlloca(bld, ccx.int_type, noname())
2279
2274
};
2280
2275
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)
2295
2316
};
2296
2317
2297
2318
unsafe {
0 commit comments