@@ -497,36 +497,12 @@ fn link_staticlib<'a>(
497
497
let mut all_native_libs = vec ! [ ] ;
498
498
499
499
let res = each_linked_rlib ( sess, & codegen_results. crate_info , & mut |cnum, path| {
500
- let name = codegen_results. crate_info . crate_name [ & cnum] ;
501
- let native_libs = & codegen_results. crate_info . native_libraries [ & cnum] ;
502
-
503
- // Here when we include the rlib into our staticlib we need to make a
504
- // decision whether to include the extra object files along the way.
505
- // These extra object files come from statically included native
506
- // libraries, but they may be cfg'd away with #[link(cfg(..))].
507
- //
508
- // This unstable feature, though, only needs liblibc to work. The only
509
- // use case there is where musl is statically included in liblibc.rlib,
510
- // so if we don't want the included version we just need to skip it. As
511
- // a result the logic here is that if *any* linked library is cfg'd away
512
- // we just skip all object files.
513
- //
514
- // Clearly this is not sufficient for a general purpose feature, and
515
- // we'd want to read from the library's metadata to determine which
516
- // object files come from where and selectively skip them.
517
- let skip_object_files = native_libs. iter ( ) . any ( |lib| {
518
- matches ! ( lib. kind, NativeLibKind :: Static { bundle: None | Some ( true ) , .. } )
519
- && !relevant_lib ( sess, lib)
520
- } ) ;
521
-
522
500
let lto = are_upstream_rust_objects_already_included ( sess)
523
501
&& !ignored_for_lto ( sess, & codegen_results. crate_info , cnum) ;
524
502
525
- // Ignoring obj file starting with the crate name
526
- // as simple comparison is not enough - there
527
- // might be also an extra name suffix
528
- let obj_start = name. as_str ( ) . to_owned ( ) ;
503
+ let native_libs = & codegen_results. crate_info . native_libraries [ & cnum] ;
529
504
505
+ let bundled: FxHashSet < _ > = native_libs. iter ( ) . filter_map ( |lib| lib. filename ) . collect ( ) ;
530
506
ab. add_archive (
531
507
path,
532
508
Box :: new ( move |fname : & str | {
@@ -540,17 +516,26 @@ fn link_staticlib<'a>(
540
516
return true ;
541
517
}
542
518
543
- // Otherwise if this is *not* a rust object and we're skipping
544
- // objects then skip this file
545
- if skip_object_files && ( !fname. starts_with ( & obj_start) || !fname. ends_with ( ".o" ) ) {
519
+ // Skip objects for bundled libs.
520
+ if bundled. iter ( ) . any ( |l| l. as_str ( ) == fname) {
546
521
return true ;
547
522
}
548
523
549
524
// ok, don't skip this
550
525
false
551
526
} ) ,
552
527
)
553
- . unwrap ( ) ;
528
+ . unwrap ( ) ; // It may be error?
529
+
530
+ let bundled: FxHashSet < _ > = native_libs. iter ( ) . filter_map ( |lib| lib. filename ) . collect ( ) ;
531
+ archive_builder_builder
532
+ . extract_bundled_libs ( path, tempdir. as_ref ( ) , & bundled)
533
+ . unwrap_or_else ( |e| sess. emit_fatal ( e) ) ;
534
+ for name in bundled {
535
+ let joined = tempdir. as_ref ( ) . join ( name. as_str ( ) ) ;
536
+ let path = joined. as_path ( ) ;
537
+ ab. add_archive ( path, Box :: new ( |_| false ) ) . unwrap ( ) ;
538
+ }
554
539
555
540
all_native_libs. extend ( codegen_results. crate_info . native_libraries [ & cnum] . iter ( ) . cloned ( ) ) ;
556
541
} ) ;
@@ -2570,6 +2555,8 @@ fn add_static_crate<'a>(
2570
2555
matches ! ( lib. kind, NativeLibKind :: Static { bundle: None | Some ( true ) , .. } )
2571
2556
&& !relevant_lib ( sess, lib)
2572
2557
} ) ;
2558
+ let _ = skip_native;
2559
+ let skip_native = false ;
2573
2560
2574
2561
if ( !are_upstream_rust_objects_already_included ( sess)
2575
2562
|| ignored_for_lto ( sess, & codegen_results. crate_info , cnum) )
0 commit comments