@@ -476,6 +476,7 @@ fn link_rlib<'a>(sess: &'a Session,
476476 for lib in sess. cstore . used_libraries ( ) {
477477 match lib. kind {
478478 NativeLibraryKind :: NativeStatic => { }
479+ NativeLibraryKind :: NativeStaticNobundle |
479480 NativeLibraryKind :: NativeFramework |
480481 NativeLibraryKind :: NativeUnknown => continue ,
481482 }
@@ -674,6 +675,7 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path,
674675
675676 for lib in all_native_libs. iter ( ) . filter ( |l| relevant_lib ( sess, l) ) {
676677 let name = match lib. kind {
678+ NativeLibraryKind :: NativeStaticNobundle |
677679 NativeLibraryKind :: NativeUnknown => "library" ,
678680 NativeLibraryKind :: NativeFramework => "framework" ,
679681 // These are included, no need to print them
@@ -894,7 +896,7 @@ fn link_args(cmd: &mut Linker,
894896 // on other dylibs (e.g. other native deps).
895897 add_local_native_libraries ( cmd, sess) ;
896898 add_upstream_rust_crates ( cmd, sess, crate_type, tmpdir) ;
897- add_upstream_native_libraries ( cmd, sess) ;
899+ add_upstream_native_libraries ( cmd, sess, crate_type ) ;
898900
899901 // # Telling the linker what we're doing
900902
@@ -985,6 +987,7 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
985987 match lib. kind {
986988 NativeLibraryKind :: NativeUnknown => cmd. link_dylib ( & lib. name . as_str ( ) ) ,
987989 NativeLibraryKind :: NativeFramework => cmd. link_framework ( & lib. name . as_str ( ) ) ,
990+ NativeLibraryKind :: NativeStaticNobundle => cmd. link_staticlib ( & lib. name . as_str ( ) ) ,
988991 NativeLibraryKind :: NativeStatic => bug ! ( ) ,
989992 }
990993 }
@@ -1210,7 +1213,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
12101213// generic function calls a native function, then the generic function must
12111214// be instantiated in the target crate, meaning that the native symbol must
12121215// also be resolved in the target crate.
1213- fn add_upstream_native_libraries ( cmd : & mut Linker , sess : & Session ) {
1216+ fn add_upstream_native_libraries ( cmd : & mut Linker , sess : & Session , crate_type : config :: CrateType ) {
12141217 // Be sure to use a topological sorting of crates because there may be
12151218 // interdependencies between native libraries. When passing -nodefaultlibs,
12161219 // for example, almost all native libraries depend on libc, so we have to
@@ -1220,6 +1223,9 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
12201223 // This passes RequireStatic, but the actual requirement doesn't matter,
12211224 // we're just getting an ordering of crate numbers, we're not worried about
12221225 // the paths.
1226+ let formats = sess. dependency_formats . borrow ( ) ;
1227+ let data = formats. get ( & crate_type) . unwrap ( ) ;
1228+
12231229 let crates = sess. cstore . used_crates ( LinkagePreference :: RequireStatic ) ;
12241230 for ( cnum, _) in crates {
12251231 for lib in sess. cstore . native_libraries ( cnum) {
@@ -1229,7 +1235,15 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
12291235 match lib. kind {
12301236 NativeLibraryKind :: NativeUnknown => cmd. link_dylib ( & lib. name . as_str ( ) ) ,
12311237 NativeLibraryKind :: NativeFramework => cmd. link_framework ( & lib. name . as_str ( ) ) ,
1232-
1238+ NativeLibraryKind :: NativeStaticNobundle => {
1239+ // Link "static-nobundle" native libs only if the crate they originate from
1240+ // is being linked statically to the current crate. If it's linked dynamically
1241+ // or is an rlib already included via some other dylib crate, the symbols from
1242+ // native libs will have already been included in that dylib.
1243+ if data[ cnum. as_usize ( ) - 1 ] == Linkage :: Static {
1244+ cmd. link_staticlib ( & lib. name . as_str ( ) )
1245+ }
1246+ } ,
12331247 // ignore statically included native libraries here as we've
12341248 // already included them when we included the rust library
12351249 // previously
0 commit comments