@@ -36,6 +36,7 @@ use regex::Regex;
36
36
use tempfile:: Builder as TempFileBuilder ;
37
37
38
38
use std:: ffi:: OsString ;
39
+ use std:: lazy:: OnceCell ;
39
40
use std:: path:: { Path , PathBuf } ;
40
41
use std:: process:: { ExitStatus , Output , Stdio } ;
41
42
use std:: { ascii, char, env, fmt, fs, io, mem, str} ;
@@ -2001,7 +2002,7 @@ fn add_local_native_libraries(
2001
2002
let relevant_libs =
2002
2003
codegen_results. crate_info . used_libraries . iter ( ) . filter ( |l| relevant_lib ( sess, l) ) ;
2003
2004
2004
- let search_path = archive_search_paths ( sess ) ;
2005
+ let search_path = OnceCell :: new ( ) ;
2005
2006
let mut last = ( NativeLibKind :: Unspecified , None ) ;
2006
2007
for lib in relevant_libs {
2007
2008
let name = match lib. name {
@@ -2023,7 +2024,11 @@ fn add_local_native_libraries(
2023
2024
}
2024
2025
NativeLibKind :: Static { bundle : None | Some ( true ) , .. }
2025
2026
| NativeLibKind :: Static { whole_archive : Some ( true ) , .. } => {
2026
- cmd. link_whole_staticlib ( name, verbatim, & search_path) ;
2027
+ cmd. link_whole_staticlib (
2028
+ name,
2029
+ verbatim,
2030
+ & search_path. get_or_init ( || archive_search_paths ( sess) ) ,
2031
+ ) ;
2027
2032
}
2028
2033
NativeLibKind :: Static { .. } => cmd. link_staticlib ( name, verbatim) ,
2029
2034
NativeLibKind :: RawDylib => {
@@ -2116,6 +2121,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
2116
2121
}
2117
2122
2118
2123
let mut compiler_builtins = None ;
2124
+ let search_path = OnceCell :: new ( ) ;
2119
2125
2120
2126
for & cnum in deps. iter ( ) {
2121
2127
if group_start == Some ( cnum) {
@@ -2149,16 +2155,35 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
2149
2155
// external build system already has the native dependencies defined, and it
2150
2156
// will provide them to the linker itself.
2151
2157
if sess. opts . debugging_opts . link_native_libraries {
2152
- // Skip if this library is the same as the last.
2153
2158
let mut last = None ;
2154
2159
for lib in & codegen_results. crate_info . native_libraries [ & cnum] {
2155
- if lib. name . is_some ( )
2156
- && relevant_lib ( sess, lib)
2157
- && matches ! ( lib. kind, NativeLibKind :: Static { bundle: Some ( false ) , .. } )
2158
- && last != lib. name
2159
- {
2160
- cmd. link_staticlib ( lib. name . unwrap ( ) , lib. verbatim . unwrap_or ( false ) ) ;
2161
- last = lib. name ;
2160
+ if !relevant_lib ( sess, lib) {
2161
+ // Skip libraries if they are disabled by `#[link(cfg=...)]`
2162
+ continue ;
2163
+ }
2164
+
2165
+ // Skip if this library is the same as the last.
2166
+ if last == lib. name {
2167
+ continue ;
2168
+ }
2169
+
2170
+ if let Some ( static_lib_name) = lib. name {
2171
+ if let NativeLibKind :: Static { bundle : Some ( false ) , whole_archive } =
2172
+ lib. kind
2173
+ {
2174
+ let verbatim = lib. verbatim . unwrap_or ( false ) ;
2175
+ if whole_archive == Some ( true ) {
2176
+ cmd. link_whole_staticlib (
2177
+ static_lib_name,
2178
+ verbatim,
2179
+ search_path. get_or_init ( || archive_search_paths ( sess) ) ,
2180
+ ) ;
2181
+ } else {
2182
+ cmd. link_staticlib ( static_lib_name, verbatim) ;
2183
+ }
2184
+
2185
+ last = lib. name ;
2186
+ }
2162
2187
}
2163
2188
}
2164
2189
}
0 commit comments