@@ -36,6 +36,7 @@ use regex::Regex;
3636use tempfile:: Builder as TempFileBuilder ;
3737
3838use std:: ffi:: OsString ;
39+ use std:: lazy:: OnceCell ;
3940use std:: path:: { Path , PathBuf } ;
4041use std:: process:: { ExitStatus , Output , Stdio } ;
4142use std:: { ascii, char, env, fmt, fs, io, mem, str} ;
@@ -2001,7 +2002,7 @@ fn add_local_native_libraries(
20012002 let relevant_libs =
20022003 codegen_results. crate_info . used_libraries . iter ( ) . filter ( |l| relevant_lib ( sess, l) ) ;
20032004
2004- let search_path = archive_search_paths ( sess ) ;
2005+ let search_path = OnceCell :: new ( ) ;
20052006 let mut last = ( NativeLibKind :: Unspecified , None ) ;
20062007 for lib in relevant_libs {
20072008 let name = match lib. name {
@@ -2023,7 +2024,11 @@ fn add_local_native_libraries(
20232024 }
20242025 NativeLibKind :: Static { bundle : None | Some ( true ) , .. }
20252026 | 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+ ) ;
20272032 }
20282033 NativeLibKind :: Static { .. } => cmd. link_staticlib ( name, verbatim) ,
20292034 NativeLibKind :: RawDylib => {
@@ -2116,6 +2121,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21162121 }
21172122
21182123 let mut compiler_builtins = None ;
2124+ let search_path = OnceCell :: new ( ) ;
21192125
21202126 for & cnum in deps. iter ( ) {
21212127 if group_start == Some ( cnum) {
@@ -2149,16 +2155,35 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21492155 // external build system already has the native dependencies defined, and it
21502156 // will provide them to the linker itself.
21512157 if sess. opts . debugging_opts . link_native_libraries {
2152- // Skip if this library is the same as the last.
21532158 let mut last = None ;
21542159 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+ }
21622187 }
21632188 }
21642189 }
0 commit comments