@@ -2425,10 +2425,6 @@ fn add_local_native_libraries(
2425
2425
) ;
2426
2426
}
2427
2427
2428
- /// # Linking Rust crates and their non-bundled static libraries
2429
- ///
2430
- /// Rust crates are not considered at all when creating an rlib output. All dependencies will be
2431
- /// linked when producing the final output (instead of the intermediate rlib version).
2432
2428
fn add_upstream_rust_crates < ' a > (
2433
2429
cmd : & mut dyn Linker ,
2434
2430
sess : & ' a Session ,
@@ -2444,67 +2440,52 @@ fn add_upstream_rust_crates<'a>(
2444
2440
// Linking to a rlib involves just passing it to the linker (the linker
2445
2441
// will slurp up the object files inside), and linking to a dynamic library
2446
2442
// involves just passing the right -l flag.
2447
-
2448
2443
let ( _, data) = codegen_results
2449
2444
. crate_info
2450
2445
. dependency_formats
2451
2446
. iter ( )
2452
2447
. find ( |( ty, _) | * ty == crate_type)
2453
2448
. expect ( "failed to find crate type in dependency format list" ) ;
2454
2449
2455
- // Invoke get_used_crates to ensure that we get a topological sorting of
2456
- // crates.
2457
- let deps = & codegen_results. crate_info . used_crates ;
2458
-
2459
- let mut compiler_builtins = None ;
2460
2450
let search_paths = OnceCell :: new ( ) ;
2461
-
2462
- for & cnum in deps. iter ( ) {
2463
- // We may not pass all crates through to the linker. Some crates may
2464
- // appear statically in an existing dylib, meaning we'll pick up all the
2465
- // symbols from the dylib.
2451
+ for & cnum in & codegen_results. crate_info . used_crates {
2452
+ // We may not pass all crates through to the linker. Some crates may appear statically in
2453
+ // an existing dylib, meaning we'll pick up all the symbols from the dylib.
2454
+ // We must always link crates `compiler_builtins` and `profiler_builtins` statically.
2455
+ // Even if they were already included into a dylib
2456
+ // (e.g. `libstd` when `-C prefer-dynamic` is used).
2466
2457
let linkage = data[ cnum. as_usize ( ) - 1 ] ;
2467
- let bundled_libs =
2468
- if sess. opts . unstable_opts . packed_bundled_libs && linkage == Linkage :: Static {
2469
- codegen_results. crate_info . native_libraries [ & cnum]
2470
- . iter ( )
2471
- . filter_map ( |lib| lib. filename )
2472
- . collect :: < FxHashSet < _ > > ( )
2473
- } else {
2474
- Default :: default ( )
2475
- } ;
2458
+ let link_static_crate = linkage == Linkage :: Static
2459
+ || linkage == Linkage :: IncludedFromDylib
2460
+ && ( codegen_results. crate_info . compiler_builtins == Some ( cnum)
2461
+ || codegen_results. crate_info . profiler_runtime == Some ( cnum) ) ;
2462
+
2463
+ let mut bundled_libs = Default :: default ( ) ;
2476
2464
match linkage {
2477
- _ if codegen_results. crate_info . profiler_runtime == Some ( cnum) => {
2478
- add_static_crate (
2479
- cmd,
2480
- sess,
2481
- archive_builder_builder,
2482
- codegen_results,
2483
- tmpdir,
2484
- cnum,
2485
- & Default :: default ( ) ,
2486
- ) ;
2487
- }
2488
- // compiler-builtins are always placed last to ensure that they're
2489
- // linked correctly.
2490
- _ if codegen_results. crate_info . compiler_builtins == Some ( cnum) => {
2491
- assert ! ( compiler_builtins. is_none( ) ) ;
2492
- compiler_builtins = Some ( cnum) ;
2465
+ Linkage :: Static | Linkage :: IncludedFromDylib => {
2466
+ if link_static_crate {
2467
+ if sess. opts . unstable_opts . packed_bundled_libs {
2468
+ bundled_libs = codegen_results. crate_info . native_libraries [ & cnum]
2469
+ . iter ( )
2470
+ . filter_map ( |lib| lib. filename )
2471
+ . collect ( ) ;
2472
+ }
2473
+ add_static_crate (
2474
+ cmd,
2475
+ sess,
2476
+ archive_builder_builder,
2477
+ codegen_results,
2478
+ tmpdir,
2479
+ cnum,
2480
+ & bundled_libs,
2481
+ ) ;
2482
+ }
2493
2483
}
2494
- Linkage :: NotLinked | Linkage :: IncludedFromDylib => { }
2495
- Linkage :: Static => add_static_crate (
2496
- cmd,
2497
- sess,
2498
- archive_builder_builder,
2499
- codegen_results,
2500
- tmpdir,
2501
- cnum,
2502
- & bundled_libs,
2503
- ) ,
2504
2484
Linkage :: Dynamic => {
2505
2485
let src = & codegen_results. crate_info . used_crate_source [ & cnum] ;
2506
2486
add_dynamic_crate ( cmd, sess, & src. dylib . as_ref ( ) . unwrap ( ) . 0 ) ;
2507
2487
}
2488
+ Linkage :: NotLinked => { }
2508
2489
}
2509
2490
2510
2491
// Static libraries are linked for a subset of linked upstream crates.
@@ -2514,7 +2495,8 @@ fn add_upstream_rust_crates<'a>(
2514
2495
// the native library because it is already linked into the dylib, and even if
2515
2496
// inline/const/generic functions from the dylib can refer to symbols from the native
2516
2497
// library, those symbols should be exported and available from the dylib anyway.
2517
- let link_static = linkage == Linkage :: Static ;
2498
+ // 3. Libraries bundled into `(compiler,profiler)_builtins` are special, see above.
2499
+ let link_static = link_static_crate;
2518
2500
// Dynamic libraries are not linked here, see the FIXME in `add_upstream_native_libraries`.
2519
2501
let link_dynamic = false ;
2520
2502
add_native_libs_from_crate (
@@ -2531,23 +2513,6 @@ fn add_upstream_rust_crates<'a>(
2531
2513
) ;
2532
2514
}
2533
2515
2534
- // compiler-builtins are always placed last to ensure that they're
2535
- // linked correctly.
2536
- // We must always link the `compiler_builtins` crate statically. Even if it
2537
- // was already "included" in a dylib (e.g., `libstd` when `-C prefer-dynamic`
2538
- // is used)
2539
- if let Some ( cnum) = compiler_builtins {
2540
- add_static_crate (
2541
- cmd,
2542
- sess,
2543
- archive_builder_builder,
2544
- codegen_results,
2545
- tmpdir,
2546
- cnum,
2547
- & Default :: default ( ) ,
2548
- ) ;
2549
- }
2550
-
2551
2516
// Converts a library file-stem into a cc -l argument
2552
2517
fn unlib < ' a > ( target : & Target , stem : & ' a str ) -> & ' a str {
2553
2518
if stem. starts_with ( "lib" ) && !target. is_like_windows { & stem[ 3 ..] } else { stem }
0 commit comments