@@ -2408,8 +2408,14 @@ fn add_upstream_rust_crates<'a>(
2408
2408
Linkage :: Dynamic => add_dynamic_crate ( cmd, sess, & src. dylib . as_ref ( ) . unwrap ( ) . 0 ) ,
2409
2409
}
2410
2410
2411
- if sess. opts . unstable_opts . link_native_libraries {
2412
- if linkage == Linkage :: Static && sess. opts . unstable_opts . packed_bundled_libs {
2411
+ if sess. opts . unstable_opts . link_native_libraries
2412
+ && codegen_results. crate_info . compiler_builtins != Some ( cnum)
2413
+ {
2414
+ if ( linkage == Linkage :: Static
2415
+ || codegen_results. crate_info . compiler_builtins == Some ( cnum)
2416
+ || codegen_results. crate_info . profiler_runtime == Some ( cnum) )
2417
+ && sess. opts . unstable_opts . packed_bundled_libs
2418
+ {
2413
2419
let bundled_libs = if sess. opts . unstable_opts . packed_bundled_libs {
2414
2420
codegen_results. crate_info . native_libraries [ & cnum]
2415
2421
. iter ( )
@@ -2468,7 +2474,10 @@ fn add_upstream_rust_crates<'a>(
2468
2474
}
2469
2475
}
2470
2476
NativeLibKind :: Static { bundle : Some ( true ) | None , whole_archive } => {
2471
- if linkage == Linkage :: Static && sess. opts . unstable_opts . packed_bundled_libs
2477
+ if ( linkage == Linkage :: Static
2478
+ || codegen_results. crate_info . compiler_builtins == Some ( cnum)
2479
+ || codegen_results. crate_info . profiler_runtime == Some ( cnum) )
2480
+ && sess. opts . unstable_opts . packed_bundled_libs
2472
2481
{
2473
2482
// If rlib contains native libs as archives, they are unpacked to tmpdir.
2474
2483
let path = tmpdir. join ( lib. filename . unwrap ( ) . as_str ( ) ) ;
@@ -2512,6 +2521,101 @@ fn add_upstream_rust_crates<'a>(
2512
2521
cnum,
2513
2522
& Default :: default ( ) ,
2514
2523
) ;
2524
+
2525
+ let src = & codegen_results. crate_info . used_crate_source [ & cnum] ;
2526
+ let linkage = data[ cnum. as_usize ( ) - 1 ] ;
2527
+ if sess. opts . unstable_opts . link_native_libraries {
2528
+ if ( linkage == Linkage :: Static
2529
+ || codegen_results. crate_info . compiler_builtins == Some ( cnum) )
2530
+ && sess. opts . unstable_opts . packed_bundled_libs
2531
+ {
2532
+ let bundled_libs = if sess. opts . unstable_opts . packed_bundled_libs {
2533
+ codegen_results. crate_info . native_libraries [ & cnum]
2534
+ . iter ( )
2535
+ . filter_map ( |lib| lib. filename )
2536
+ . collect :: < FxHashSet < _ > > ( )
2537
+ } else {
2538
+ Default :: default ( )
2539
+ } ;
2540
+ // If rlib contains native libs as archives, unpack them to tmpdir.
2541
+ let rlib = & src. rlib . as_ref ( ) . unwrap ( ) . 0 ;
2542
+ archive_builder_builder
2543
+ . extract_bundled_libs ( rlib, tmpdir, & bundled_libs)
2544
+ . unwrap_or_else ( |e| sess. fatal ( e) ) ;
2545
+ }
2546
+
2547
+ let mut last = ( None , NativeLibKind :: Unspecified , None ) ;
2548
+ for lib in & codegen_results. crate_info . native_libraries [ & cnum] {
2549
+ let Some ( name) = lib. name else {
2550
+ continue ;
2551
+ } ;
2552
+ let name = name. as_str ( ) ;
2553
+ if !relevant_lib ( sess, lib) {
2554
+ continue ;
2555
+ }
2556
+
2557
+ // Skip if this library is the same as the last.
2558
+ last = if ( lib. name , lib. kind , lib. verbatim ) == last {
2559
+ continue ;
2560
+ } else {
2561
+ ( lib. name , lib. kind , lib. verbatim )
2562
+ } ;
2563
+
2564
+ let verbatim = lib. verbatim . unwrap_or ( false ) ;
2565
+ match lib. kind {
2566
+ NativeLibKind :: Static { bundle : Some ( false ) , whole_archive : Some ( true ) } => {
2567
+ if linkage == Linkage :: Static {
2568
+ cmd. link_whole_staticlib (
2569
+ name,
2570
+ verbatim,
2571
+ search_path. get_or_init ( || archive_search_paths ( sess) ) ,
2572
+ ) ;
2573
+ }
2574
+ }
2575
+ NativeLibKind :: Static {
2576
+ bundle : Some ( false ) ,
2577
+ whole_archive : Some ( false ) | None ,
2578
+ } => {
2579
+ if linkage == Linkage :: Static {
2580
+ // HACK/FIXME: Fixup a circular dependency between libgcc and libc
2581
+ // with glibc. This logic should be moved to the libc crate.
2582
+ if sess. target . os == "linux" && sess. target . env == "gnu" && name == "c"
2583
+ {
2584
+ cmd. link_staticlib ( "gcc" , false ) ;
2585
+ }
2586
+ cmd. link_staticlib ( name, verbatim) ;
2587
+ }
2588
+ }
2589
+ NativeLibKind :: Static { bundle : Some ( true ) | None , whole_archive } => {
2590
+ if ( linkage == Linkage :: Static
2591
+ || codegen_results. crate_info . compiler_builtins == Some ( cnum) )
2592
+ && sess. opts . unstable_opts . packed_bundled_libs
2593
+ {
2594
+ // If rlib contains native libs as archives, they are unpacked to tmpdir.
2595
+ let path = tmpdir. join ( lib. filename . unwrap ( ) . as_str ( ) ) ;
2596
+ if whole_archive == Some ( true ) {
2597
+ cmd. link_whole_rlib ( & path) ;
2598
+ } else {
2599
+ cmd. link_rlib ( & path) ;
2600
+ }
2601
+ }
2602
+ }
2603
+ NativeLibKind :: Dylib { as_needed } => {
2604
+ cmd. link_dylib ( name, verbatim, as_needed. unwrap_or ( true ) )
2605
+ }
2606
+ NativeLibKind :: RawDylib => { }
2607
+ NativeLibKind :: Framework { as_needed } => {
2608
+ cmd. link_framework ( name, as_needed. unwrap_or ( true ) )
2609
+ }
2610
+ NativeLibKind :: LinkArg => {
2611
+ if linkage == Linkage :: Static {
2612
+ cmd. arg ( name) ;
2613
+ }
2614
+ }
2615
+ NativeLibKind :: Unspecified => cmd. link_dylib ( name, verbatim, true ) ,
2616
+ }
2617
+ }
2618
+ }
2515
2619
}
2516
2620
2517
2621
// Converts a library file-stem into a cc -l argument
0 commit comments