@@ -727,10 +727,18 @@ def collect_inputs(
727727 if linker_script :
728728 nolinkstamp_compile_direct_inputs .append (linker_script )
729729
730+ if crate_info .type in ["dylib" , "cdylib" ]:
731+ # For shared libraries we want to link C++ runtime library dynamically
732+ # (for example libstdc++.so or libc++.so).
733+ runtime_libs = cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration )
734+ else :
735+ runtime_libs = cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration )
736+
730737 nolinkstamp_compile_inputs = depset (
731738 nolinkstamp_compile_direct_inputs +
732739 additional_transitive_inputs ,
733740 transitive = [
741+ runtime_libs ,
734742 linker_depset ,
735743 crate_info .srcs ,
736744 transitive_crate_outputs ,
@@ -2253,36 +2261,22 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
22532261
22542262 args .add_all (make_link_flags_args , map_each = make_link_flags )
22552263
2256- args .add_all (linkstamp_outs , before_each = "-C" , format_each = "link -args=%s" )
2264+ args .add_all (linkstamp_outs , format_each = "-Clink -args=%s" )
22572265
22582266 if crate_type in ["dylib" , "cdylib" ]:
22592267 # For shared libraries we want to link C++ runtime library dynamically
22602268 # (for example libstdc++.so or libc++.so).
2261- args .add_all (
2262- cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration ),
2263- map_each = _get_dirname ,
2264- format_each = "-Lnative=%s" ,
2265- )
2269+ runtime_libs = cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration )
2270+ args .add_all (runtime_libs , map_each = _get_dirname , format_each = "-Lnative=%s" )
22662271 if include_link_flags :
2267- args .add_all (
2268- cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration ),
2269- map_each = get_lib_name ,
2270- format_each = "-ldylib=%s" ,
2271- )
2272+ args .add_all (runtime_libs , map_each = get_lib_name , format_each = "-ldylib=%s" )
22722273 else :
22732274 # For all other crate types we want to link C++ runtime library statically
22742275 # (for example libstdc++.a or libc++.a).
2275- args .add_all (
2276- cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration ),
2277- map_each = _get_dirname ,
2278- format_each = "-Lnative=%s" ,
2279- )
2276+ runtime_libs = cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration )
2277+ args .add_all (runtime_libs , map_each = _get_dirname , format_each = "-Lnative=%s" )
22802278 if include_link_flags :
2281- args .add_all (
2282- cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration ),
2283- map_each = get_lib_name ,
2284- format_each = "-lstatic=%s" ,
2285- )
2279+ args .add_all (runtime_libs , map_each = get_lib_name , format_each = "-lstatic=%s" )
22862280
22872281def _get_dirname (file ):
22882282 """A helper function for `_add_native_link_flags`.
0 commit comments