@@ -2097,7 +2097,14 @@ fn linker_with_args<'a>(
2097
2097
cmd. add_as_needed ( ) ;
2098
2098
2099
2099
// Local native libraries of all kinds.
2100
- add_local_native_libraries ( cmd, sess, archive_builder_builder, codegen_results, tmpdir) ;
2100
+ add_local_native_libraries (
2101
+ cmd,
2102
+ sess,
2103
+ archive_builder_builder,
2104
+ codegen_results,
2105
+ tmpdir,
2106
+ link_output_kind,
2107
+ ) ;
2101
2108
2102
2109
// Upstream rust crates and their non-dynamic native libraries.
2103
2110
add_upstream_rust_crates (
@@ -2107,10 +2114,18 @@ fn linker_with_args<'a>(
2107
2114
codegen_results,
2108
2115
crate_type,
2109
2116
tmpdir,
2117
+ link_output_kind,
2110
2118
) ;
2111
2119
2112
2120
// Dynamic native libraries from upstream crates.
2113
- add_upstream_native_libraries ( cmd, sess, archive_builder_builder, codegen_results, tmpdir) ;
2121
+ add_upstream_native_libraries (
2122
+ cmd,
2123
+ sess,
2124
+ archive_builder_builder,
2125
+ codegen_results,
2126
+ tmpdir,
2127
+ link_output_kind,
2128
+ ) ;
2114
2129
2115
2130
// Link with the import library generated for any raw-dylib functions.
2116
2131
for ( raw_dylib_name, raw_dylib_imports) in
@@ -2365,6 +2380,7 @@ fn add_native_libs_from_crate(
2365
2380
cnum : CrateNum ,
2366
2381
link_static : bool ,
2367
2382
link_dynamic : bool ,
2383
+ link_output_kind : LinkOutputKind ,
2368
2384
) {
2369
2385
if !sess. opts . unstable_opts . link_native_libraries {
2370
2386
// If `-Zlink-native-libraries=false` is set, then the assumption is that an
@@ -2444,8 +2460,16 @@ fn add_native_libs_from_crate(
2444
2460
}
2445
2461
}
2446
2462
NativeLibKind :: Unspecified => {
2447
- if link_dynamic {
2448
- cmd. link_dylib ( name, verbatim, true ) ;
2463
+ // If we are generating a static binary, prefer static library when the
2464
+ // link kind is unspecified.
2465
+ if !link_output_kind. can_link_dylib ( ) && !sess. target . crt_static_allows_dylibs {
2466
+ if link_static {
2467
+ cmd. link_staticlib ( name, verbatim)
2468
+ }
2469
+ } else {
2470
+ if link_dynamic {
2471
+ cmd. link_dylib ( name, verbatim, true ) ;
2472
+ }
2449
2473
}
2450
2474
}
2451
2475
NativeLibKind :: Framework { as_needed } => {
@@ -2472,6 +2496,7 @@ fn add_local_native_libraries(
2472
2496
archive_builder_builder : & dyn ArchiveBuilderBuilder ,
2473
2497
codegen_results : & CodegenResults ,
2474
2498
tmpdir : & Path ,
2499
+ link_output_kind : LinkOutputKind ,
2475
2500
) {
2476
2501
if sess. opts . unstable_opts . link_native_libraries {
2477
2502
// User-supplied library search paths (-L on the command line). These are the same paths
@@ -2501,6 +2526,7 @@ fn add_local_native_libraries(
2501
2526
LOCAL_CRATE ,
2502
2527
link_static,
2503
2528
link_dynamic,
2529
+ link_output_kind,
2504
2530
) ;
2505
2531
}
2506
2532
@@ -2511,6 +2537,7 @@ fn add_upstream_rust_crates<'a>(
2511
2537
codegen_results : & CodegenResults ,
2512
2538
crate_type : CrateType ,
2513
2539
tmpdir : & Path ,
2540
+ link_output_kind : LinkOutputKind ,
2514
2541
) {
2515
2542
// All of the heavy lifting has previously been accomplished by the
2516
2543
// dependency_format module of the compiler. This is just crawling the
@@ -2588,6 +2615,7 @@ fn add_upstream_rust_crates<'a>(
2588
2615
cnum,
2589
2616
link_static,
2590
2617
link_dynamic,
2618
+ link_output_kind,
2591
2619
) ;
2592
2620
}
2593
2621
}
@@ -2598,6 +2626,7 @@ fn add_upstream_native_libraries(
2598
2626
archive_builder_builder : & dyn ArchiveBuilderBuilder ,
2599
2627
codegen_results : & CodegenResults ,
2600
2628
tmpdir : & Path ,
2629
+ link_output_kind : LinkOutputKind ,
2601
2630
) {
2602
2631
let search_path = OnceCell :: new ( ) ;
2603
2632
for & cnum in & codegen_results. crate_info . used_crates {
@@ -2626,6 +2655,7 @@ fn add_upstream_native_libraries(
2626
2655
cnum,
2627
2656
link_static,
2628
2657
link_dynamic,
2658
+ link_output_kind,
2629
2659
) ;
2630
2660
}
2631
2661
}
0 commit comments