@@ -2129,7 +2129,14 @@ fn linker_with_args<'a>(
21292129 cmd. add_as_needed ( ) ;
21302130
21312131 // Local native libraries of all kinds.
2132- add_local_native_libraries ( cmd, sess, archive_builder_builder, codegen_results, tmpdir) ;
2132+ add_local_native_libraries (
2133+ cmd,
2134+ sess,
2135+ archive_builder_builder,
2136+ codegen_results,
2137+ tmpdir,
2138+ link_output_kind,
2139+ ) ;
21332140
21342141 // Upstream rust crates and their non-dynamic native libraries.
21352142 add_upstream_rust_crates (
@@ -2139,10 +2146,18 @@ fn linker_with_args<'a>(
21392146 codegen_results,
21402147 crate_type,
21412148 tmpdir,
2149+ link_output_kind,
21422150 ) ;
21432151
21442152 // Dynamic native libraries from upstream crates.
2145- add_upstream_native_libraries ( cmd, sess, archive_builder_builder, codegen_results, tmpdir) ;
2153+ add_upstream_native_libraries (
2154+ cmd,
2155+ sess,
2156+ archive_builder_builder,
2157+ codegen_results,
2158+ tmpdir,
2159+ link_output_kind,
2160+ ) ;
21462161
21472162 // Link with the import library generated for any raw-dylib functions.
21482163 for ( raw_dylib_name, raw_dylib_imports) in
@@ -2397,6 +2412,7 @@ fn add_native_libs_from_crate(
23972412 cnum : CrateNum ,
23982413 link_static : bool ,
23992414 link_dynamic : bool ,
2415+ link_output_kind : LinkOutputKind ,
24002416) {
24012417 if !sess. opts . unstable_opts . link_native_libraries {
24022418 // If `-Zlink-native-libraries=false` is set, then the assumption is that an
@@ -2476,8 +2492,16 @@ fn add_native_libs_from_crate(
24762492 }
24772493 }
24782494 NativeLibKind :: Unspecified => {
2479- if link_dynamic {
2480- cmd. link_dylib ( name, verbatim, true ) ;
2495+ // If we are generating a static binary, prefer static library when the
2496+ // link kind is unspecified.
2497+ if !link_output_kind. can_link_dylib ( ) && !sess. target . crt_static_allows_dylibs {
2498+ if link_static {
2499+ cmd. link_staticlib ( name, verbatim)
2500+ }
2501+ } else {
2502+ if link_dynamic {
2503+ cmd. link_dylib ( name, verbatim, true ) ;
2504+ }
24812505 }
24822506 }
24832507 NativeLibKind :: Framework { as_needed } => {
@@ -2504,6 +2528,7 @@ fn add_local_native_libraries(
25042528 archive_builder_builder : & dyn ArchiveBuilderBuilder ,
25052529 codegen_results : & CodegenResults ,
25062530 tmpdir : & Path ,
2531+ link_output_kind : LinkOutputKind ,
25072532) {
25082533 if sess. opts . unstable_opts . link_native_libraries {
25092534 // User-supplied library search paths (-L on the command line). These are the same paths
@@ -2533,6 +2558,7 @@ fn add_local_native_libraries(
25332558 LOCAL_CRATE ,
25342559 link_static,
25352560 link_dynamic,
2561+ link_output_kind,
25362562 ) ;
25372563}
25382564
@@ -2543,6 +2569,7 @@ fn add_upstream_rust_crates<'a>(
25432569 codegen_results : & CodegenResults ,
25442570 crate_type : CrateType ,
25452571 tmpdir : & Path ,
2572+ link_output_kind : LinkOutputKind ,
25462573) {
25472574 // All of the heavy lifting has previously been accomplished by the
25482575 // dependency_format module of the compiler. This is just crawling the
@@ -2620,6 +2647,7 @@ fn add_upstream_rust_crates<'a>(
26202647 cnum,
26212648 link_static,
26222649 link_dynamic,
2650+ link_output_kind,
26232651 ) ;
26242652 }
26252653}
@@ -2630,6 +2658,7 @@ fn add_upstream_native_libraries(
26302658 archive_builder_builder : & dyn ArchiveBuilderBuilder ,
26312659 codegen_results : & CodegenResults ,
26322660 tmpdir : & Path ,
2661+ link_output_kind : LinkOutputKind ,
26332662) {
26342663 let search_path = OnceCell :: new ( ) ;
26352664 for & cnum in & codegen_results. crate_info . used_crates {
@@ -2658,6 +2687,7 @@ fn add_upstream_native_libraries(
26582687 cnum,
26592688 link_static,
26602689 link_dynamic,
2690+ link_output_kind,
26612691 ) ;
26622692 }
26632693}
0 commit comments