@@ -6,6 +6,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
6
6
use rustc_errors:: { ErrorGuaranteed , Handler } ;
7
7
use rustc_fs_util:: fix_windows_verbatim_for_gcc;
8
8
use rustc_hir:: def_id:: CrateNum ;
9
+ use rustc_metadata:: find_native_static_library;
9
10
use rustc_metadata:: fs:: { emit_metadata, METADATA_FILENAME } ;
10
11
use rustc_middle:: middle:: dependency_format:: Linkage ;
11
12
use rustc_middle:: middle:: exported_symbols:: SymbolExportKind ;
@@ -24,7 +25,7 @@ use rustc_target::spec::crt_objects::{CrtObjects, CrtObjectsFallback};
24
25
use rustc_target:: spec:: { LinkOutputKind , LinkerFlavor , LldFlavor , SplitDebuginfo } ;
25
26
use rustc_target:: spec:: { PanicStrategy , RelocModel , RelroLevel , SanitizerSet , Target } ;
26
27
27
- use super :: archive:: { find_library , ArchiveBuilder , ArchiveBuilderBuilder } ;
28
+ use super :: archive:: { ArchiveBuilder , ArchiveBuilderBuilder } ;
28
29
use super :: command:: Command ;
29
30
use super :: linker:: { self , Linker } ;
30
31
use super :: metadata:: { create_rmeta_file, MetadataPosition } ;
@@ -307,6 +308,9 @@ fn link_rlib<'a>(
307
308
}
308
309
}
309
310
311
+ // Used if packed_bundled_libs flag enabled.
312
+ let mut packed_bundled_libs = Vec :: new ( ) ;
313
+
310
314
// Note that in this loop we are ignoring the value of `lib.cfg`. That is,
311
315
// we may not be configured to actually include a static library if we're
312
316
// adding it here. That's because later when we consume this rlib we'll
@@ -325,6 +329,8 @@ fn link_rlib<'a>(
325
329
// metadata of the rlib we're generating somehow.
326
330
for lib in codegen_results. crate_info . used_libraries . iter ( ) {
327
331
match lib. kind {
332
+ NativeLibKind :: Static { bundle : None | Some ( true ) , whole_archive : Some ( true ) }
333
+ if flavor == RlibFlavor :: Normal && sess. opts . unstable_opts . packed_bundled_libs => { }
328
334
NativeLibKind :: Static { bundle : None | Some ( true ) , whole_archive : Some ( true ) }
329
335
if flavor == RlibFlavor :: Normal =>
330
336
{
@@ -348,7 +354,16 @@ fn link_rlib<'a>(
348
354
}
349
355
if let Some ( name) = lib. name {
350
356
let location =
351
- find_library ( name. as_str ( ) , lib. verbatim . unwrap_or ( false ) , & lib_search_paths, sess) ;
357
+ find_native_static_library ( name. as_str ( ) , lib. verbatim , & lib_search_paths, sess) ;
358
+ if sess. opts . unstable_opts . packed_bundled_libs && flavor == RlibFlavor :: Normal {
359
+ packed_bundled_libs. push ( find_native_static_library (
360
+ lib. filename . unwrap ( ) . as_str ( ) ,
361
+ Some ( true ) ,
362
+ & lib_search_paths,
363
+ sess,
364
+ ) ) ;
365
+ continue ;
366
+ }
352
367
ab. add_archive ( & location, Box :: new ( |_| false ) ) . unwrap_or_else ( |e| {
353
368
sess. fatal ( & format ! (
354
369
"failed to add native library {}: {}" ,
@@ -403,6 +418,12 @@ fn link_rlib<'a>(
403
418
ab. add_file ( & trailing_metadata) ;
404
419
}
405
420
421
+ // Add all bundled static native library dependencies.
422
+ // Archives added to the end of .rlib archive, see comment above for the reason.
423
+ for lib in packed_bundled_libs {
424
+ ab. add_file ( & lib)
425
+ }
426
+
406
427
return Ok ( ab) ;
407
428
}
408
429
@@ -2384,7 +2405,15 @@ fn add_upstream_rust_crates<'a>(
2384
2405
let src = & codegen_results. crate_info . used_crate_source [ & cnum] ;
2385
2406
match data[ cnum. as_usize ( ) - 1 ] {
2386
2407
_ if codegen_results. crate_info . profiler_runtime == Some ( cnum) => {
2387
- add_static_crate ( cmd, sess, archive_builder_builder, codegen_results, tmpdir, cnum) ;
2408
+ add_static_crate (
2409
+ cmd,
2410
+ sess,
2411
+ archive_builder_builder,
2412
+ codegen_results,
2413
+ tmpdir,
2414
+ cnum,
2415
+ & Default :: default ( ) ,
2416
+ ) ;
2388
2417
}
2389
2418
// compiler-builtins are always placed last to ensure that they're
2390
2419
// linked correctly.
@@ -2394,7 +2423,23 @@ fn add_upstream_rust_crates<'a>(
2394
2423
}
2395
2424
Linkage :: NotLinked | Linkage :: IncludedFromDylib => { }
2396
2425
Linkage :: Static => {
2397
- add_static_crate ( cmd, sess, archive_builder_builder, codegen_results, tmpdir, cnum) ;
2426
+ let bundled_libs = if sess. opts . unstable_opts . packed_bundled_libs {
2427
+ codegen_results. crate_info . native_libraries [ & cnum]
2428
+ . iter ( )
2429
+ . filter_map ( |lib| lib. filename )
2430
+ . collect :: < FxHashSet < _ > > ( )
2431
+ } else {
2432
+ Default :: default ( )
2433
+ } ;
2434
+ add_static_crate (
2435
+ cmd,
2436
+ sess,
2437
+ archive_builder_builder,
2438
+ codegen_results,
2439
+ tmpdir,
2440
+ cnum,
2441
+ & bundled_libs,
2442
+ ) ;
2398
2443
2399
2444
// Link static native libs with "-bundle" modifier only if the crate they originate from
2400
2445
// is being linked statically to the current crate. If it's linked dynamically
@@ -2405,6 +2450,14 @@ fn add_upstream_rust_crates<'a>(
2405
2450
// external build system already has the native dependencies defined, and it
2406
2451
// will provide them to the linker itself.
2407
2452
if sess. opts . unstable_opts . link_native_libraries {
2453
+ if sess. opts . unstable_opts . packed_bundled_libs {
2454
+ // If rlib contains native libs as archives, unpack them to tmpdir.
2455
+ let rlib = & src. rlib . as_ref ( ) . unwrap ( ) . 0 ;
2456
+ archive_builder_builder
2457
+ . extract_bundled_libs ( rlib, tmpdir, & bundled_libs)
2458
+ . unwrap_or_else ( |e| sess. fatal ( e) ) ;
2459
+ }
2460
+
2408
2461
let mut last = ( None , NativeLibKind :: Unspecified , None ) ;
2409
2462
for lib in & codegen_results. crate_info . native_libraries [ & cnum] {
2410
2463
let Some ( name) = lib. name else {
@@ -2446,10 +2499,17 @@ fn add_upstream_rust_crates<'a>(
2446
2499
| NativeLibKind :: Framework { .. }
2447
2500
| NativeLibKind :: Unspecified
2448
2501
| NativeLibKind :: RawDylib => { }
2449
- NativeLibKind :: Static {
2450
- bundle : Some ( true ) | None ,
2451
- whole_archive : _,
2452
- } => { }
2502
+ NativeLibKind :: Static { bundle : Some ( true ) | None , whole_archive } => {
2503
+ if sess. opts . unstable_opts . packed_bundled_libs {
2504
+ // If rlib contains native libs as archives, they are unpacked to tmpdir.
2505
+ let path = tmpdir. join ( lib. filename . unwrap ( ) . as_str ( ) ) ;
2506
+ if whole_archive == Some ( true ) {
2507
+ cmd. link_whole_rlib ( & path) ;
2508
+ } else {
2509
+ cmd. link_rlib ( & path) ;
2510
+ }
2511
+ }
2512
+ }
2453
2513
}
2454
2514
}
2455
2515
}
@@ -2468,7 +2528,15 @@ fn add_upstream_rust_crates<'a>(
2468
2528
// was already "included" in a dylib (e.g., `libstd` when `-C prefer-dynamic`
2469
2529
// is used)
2470
2530
if let Some ( cnum) = compiler_builtins {
2471
- add_static_crate ( cmd, sess, archive_builder_builder, codegen_results, tmpdir, cnum) ;
2531
+ add_static_crate (
2532
+ cmd,
2533
+ sess,
2534
+ archive_builder_builder,
2535
+ codegen_results,
2536
+ tmpdir,
2537
+ cnum,
2538
+ & Default :: default ( ) ,
2539
+ ) ;
2472
2540
}
2473
2541
2474
2542
// Converts a library file-stem into a cc -l argument
@@ -2501,6 +2569,7 @@ fn add_upstream_rust_crates<'a>(
2501
2569
codegen_results : & CodegenResults ,
2502
2570
tmpdir : & Path ,
2503
2571
cnum : CrateNum ,
2572
+ bundled_lib_file_names : & FxHashSet < Symbol > ,
2504
2573
) {
2505
2574
let src = & codegen_results. crate_info . used_crate_source [ & cnum] ;
2506
2575
let cratepath = & src. rlib . as_ref ( ) . unwrap ( ) . 0 ;
@@ -2529,6 +2598,7 @@ fn add_upstream_rust_crates<'a>(
2529
2598
let dst = tmpdir. join ( cratepath. file_name ( ) . unwrap ( ) ) ;
2530
2599
let name = cratepath. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
2531
2600
let name = & name[ 3 ..name. len ( ) - 5 ] ; // chop off lib/.rlib
2601
+ let bundled_lib_file_names = bundled_lib_file_names. clone ( ) ;
2532
2602
2533
2603
sess. prof . generic_activity_with_arg ( "link_altering_rlib" , name) . run ( || {
2534
2604
let canonical_name = name. replace ( '-' , "_" ) ;
@@ -2562,6 +2632,15 @@ fn add_upstream_rust_crates<'a>(
2562
2632
let skip_because_lto =
2563
2633
upstream_rust_objects_already_included && is_rust_object && is_builtins;
2564
2634
2635
+ // We skip native libraries because:
2636
+ // 1. This native libraries won't be used from the generated rlib,
2637
+ // so we can throw them away to avoid the copying work.
2638
+ // 2. We can't allow it to be a single remaining entry in archive
2639
+ // as some linkers may complain on that.
2640
+ if bundled_lib_file_names. contains ( & Symbol :: intern ( f) ) {
2641
+ return true ;
2642
+ }
2643
+
2565
2644
if skip_because_cfg_say_so || skip_because_lto {
2566
2645
return true ;
2567
2646
}
0 commit comments