@@ -1149,13 +1149,12 @@ def construct_arguments(
11491149
11501150 return args , env
11511151
1152- def _get_dynamic_libraries_for_runtime (cc_linking_context , is_linking_statically ):
1152+ def _get_dynamic_libraries_for_runtime (cc_infos , is_linking_statically ):
11531153 libraries = []
1154- if not cc_linking_context :
1155- return libraries
11561154
1157- for linker_input in cc_linking_context .linker_inputs .to_list ():
1158- libraries .extend (linker_input .libraries )
1155+ for cc_info in cc_infos :
1156+ for linker_input in cc_info .linking_context .linker_inputs .to_list ():
1157+ libraries .extend (linker_input .libraries )
11591158
11601159 dynamic_libraries_for_runtime = []
11611160 for library in libraries :
@@ -1528,9 +1527,6 @@ def rustc_compile_action(
15281527
15291528 experimental_use_coverage_metadata_files = toolchain ._experimental_use_coverage_metadata_files
15301529
1531- cc_info = establish_cc_info (ctx , attr , crate_info , toolchain , cc_toolchain , feature_configuration , interface_library )
1532- linking_context = cc_info [0 ].linking_context if cc_info else None
1533-
15341530 runfiles_list = []
15351531 for data_dep in getattr (ctx .attr , "data" , []):
15361532 if data_dep [DefaultInfo ].data_runfiles .files :
@@ -1552,8 +1548,10 @@ def rustc_compile_action(
15521548
15531549 common_runfiles = ctx .runfiles ().merge_all (runfiles_list )
15541550
1551+ cc_infos_from_deps = _collect_cc_infos_from_deps (ctx , toolchain , crate_info )
1552+
15551553 dynamic_libraries = ctx .runfiles (
1556- files = _get_dynamic_libraries_for_runtime (linking_context , True ),
1554+ files = _get_dynamic_libraries_for_runtime (cc_infos_from_deps , True ),
15571555 )
15581556 library_default_runfiles = None
15591557 library_data_runfiles = None
@@ -1564,7 +1562,7 @@ def rustc_compile_action(
15641562 library_default_runfiles = common_runfiles .merge (dynamic_libraries )
15651563
15661564 data_runfiles = ctx .runfiles (
1567- files = _get_dynamic_libraries_for_runtime (linking_context , False ),
1565+ files = _get_dynamic_libraries_for_runtime (cc_infos_from_deps , False ),
15681566 )
15691567
15701568 # Data runfiles for library include dynamic libraries even when they have
@@ -1608,7 +1606,7 @@ def rustc_compile_action(
16081606 runfiles = binary_runfiles ,
16091607 executable = executable ,
16101608 ),
1611- ] + cc_info
1609+ ]
16121610
16131611 # When invoked by aspects (and when running `bazel coverage`), the
16141612 # baseline_coverage.dat created here will conflict with the baseline_coverage.dat of the
@@ -1637,6 +1635,8 @@ def rustc_compile_action(
16371635 else :
16381636 providers .extend ([crate_info , dep_info ])
16391637
1638+ providers += establish_cc_info (ctx , attr , crate_info , toolchain , cc_toolchain , feature_configuration , interface_library )
1639+
16401640 output_group_info = {}
16411641
16421642 if pdb_file :
@@ -1764,6 +1764,31 @@ def _add_codegen_units_flags(toolchain, emit, args):
17641764
17651765 args .add ("-Ccodegen-units={}" .format (toolchain ._codegen_units ))
17661766
1767+ def _collect_cc_infos_from_deps (ctx , toolchain , crate_info ):
1768+ cc_infos = []
1769+
1770+ # Flattening is okay since crate_info.deps only records direct deps.
1771+ for dep in crate_info .deps .to_list ():
1772+ if dep .cc_info :
1773+ # A Rust staticlib or shared library doesn't need to propagate linker inputs
1774+ # of its dependencies, except for shared libraries.
1775+ if crate_info .type in ["cdylib" , "staticlib" ]:
1776+ shared_linker_inputs = _collect_nonstatic_linker_inputs (dep .cc_info )
1777+ if shared_linker_inputs :
1778+ linking_context = cc_common .create_linking_context (
1779+ linker_inputs = depset (shared_linker_inputs ),
1780+ )
1781+ cc_infos .append (CcInfo (linking_context = linking_context ))
1782+ else :
1783+ cc_infos .append (dep .cc_info )
1784+
1785+ if crate_info .type in ("rlib" , "lib" ):
1786+ libstd_and_allocator_cc_info = _get_std_and_alloc_info (ctx , toolchain , crate_info )
1787+ if libstd_and_allocator_cc_info :
1788+ # TODO: if we already have an rlib in our deps, we could skip this
1789+ cc_infos .append (libstd_and_allocator_cc_info )
1790+ return cc_infos
1791+
17671792def establish_cc_info (ctx , attr , crate_info , toolchain , cc_toolchain , feature_configuration , interface_library ):
17681793 """If the produced crate is suitable yield a CcInfo to allow for interop with cc rules
17691794
@@ -1846,28 +1871,7 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co
18461871 cc_infos = [
18471872 CcInfo (linking_context = linking_context ),
18481873 toolchain .stdlib_linkflags ,
1849- ]
1850-
1851- # Flattening is okay since crate_info.deps only records direct deps.
1852- for dep in crate_info .deps .to_list ():
1853- if dep .cc_info :
1854- # A Rust staticlib or shared library doesn't need to propagate linker inputs
1855- # of its dependencies, except for shared libraries.
1856- if crate_info .type in ["cdylib" , "staticlib" ]:
1857- shared_linker_inputs = _collect_nonstatic_linker_inputs (dep .cc_info )
1858- if shared_linker_inputs :
1859- linking_context = cc_common .create_linking_context (
1860- linker_inputs = depset (shared_linker_inputs ),
1861- )
1862- cc_infos .append (CcInfo (linking_context = linking_context ))
1863- else :
1864- cc_infos .append (dep .cc_info )
1865-
1866- if crate_info .type in ("rlib" , "lib" ):
1867- libstd_and_allocator_cc_info = _get_std_and_alloc_info (ctx , toolchain , crate_info )
1868- if libstd_and_allocator_cc_info :
1869- # TODO: if we already have an rlib in our deps, we could skip this
1870- cc_infos .append (libstd_and_allocator_cc_info )
1874+ ] + _collect_cc_infos_from_deps (ctx , toolchain , crate_info )
18711875
18721876 providers = [cc_common .merge_cc_infos (cc_infos = cc_infos )]
18731877 if dot_a :
0 commit comments