Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ def _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation
def _is_proc_macro(crate_info):
return "proc-macro" in (crate_info.type, crate_info.wrapped_crate_type)

def flatten_crate_groups(deps):
"""Given a list of DepVariantInfo (like crate_info.deps), flatten any crate_group_info into the rest of the list"""
crate_deps = []
for dep in deps:
crate_group = getattr(dep, "crate_group_info", None)
if crate_group:
crate_deps.extend(crate_group.dep_variant_infos.to_list())
else:
crate_deps.append(dep)
return crate_deps

def collect_deps(
deps,
proc_macro_deps,
Expand Down Expand Up @@ -260,13 +271,7 @@ def collect_deps(
direct_metadata_outputs = []
transitive_metadata_outputs = []

crate_deps = []
for dep in deps + proc_macro_deps:
crate_group = getattr(dep, "crate_group_info", None)
if crate_group:
crate_deps.extend(crate_group.dep_variant_infos.to_list())
else:
crate_deps.append(dep)
crate_deps = flatten_crate_groups(deps + proc_macro_deps)

aliases = {k.label: v for k, v in aliases.items()}
for dep in crate_deps:
Expand Down Expand Up @@ -1580,9 +1585,11 @@ def rustc_compile_action(
toolchain.stdlib_linkflags.linking_context,
]

for dep in crate_info.deps.to_list():
if dep.cc_info:
linking_contexts.append(dep.cc_info.linking_context)
linking_contexts += [
dep.cc_info.linking_context
for dep in flatten_crate_groups(crate_info.deps.to_list())
if dep.cc_info
]

# In the cc_common.link action we need to pass the name of the final
# binary (output) relative to the package of this target.
Expand Down Expand Up @@ -1958,7 +1965,7 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co
]

# Flattening is okay since crate_info.deps only records direct deps.
for dep in crate_info.deps.to_list():
for dep in flatten_crate_groups(crate_info.deps.to_list()):
if dep.cc_info:
# A Rust staticlib or shared library doesn't need to propagate linker inputs
# of its dependencies, except for shared libraries.
Expand Down
32 changes: 32 additions & 0 deletions test/integration/cc_common_link/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(
"rust_binary",
"rust_library",
"rust_shared_library",
"rust_library_group",
"rust_test",
)

Expand All @@ -12,6 +13,35 @@ cc_library(
linkstamp = "cclinkstampdep.cc",
)

rust_library(
name = "deep_dep",
srcs = ["deep_dep.rs"],
edition = "2021",
)

rust_library_group(
name = "dep_group",
deps = [":deep_dep"],
)

rust_library(
name = "very_nested_dep",
srcs = ["very_nested_dep.rs"],
edition = "2021",
)

rust_library_group(
name = "nested_dep_group",
deps = [":very_nested_dep"],
)

rust_library(
name = "nesting_dep",
srcs = ["nesting_dep.rs"],
deps = [":nested_dep_group"],
edition = "2021",
)

rust_library(
name = "rdep",
srcs = ["rdep.rs"],
Expand All @@ -25,6 +55,8 @@ rust_binary(
deps = [
":cclinkstampdep",
":rdep",
":dep_group",
":nesting_dep",
],
)

Expand Down
2 changes: 2 additions & 0 deletions test/integration/cc_common_link/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ extern "C" {

fn main() {
println!("bin rdep: {}", rdep::rdep());
println!("bin deep_dep: {}", deep_dep::deep_dep());
println!("bin nesting_dep: {}", nesting_dep::nesting_dep());
println!("cclinkstampdep: {}", unsafe { cclinkstampdep() });
}
3 changes: 3 additions & 0 deletions test/integration/cc_common_link/deep_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn deep_dep() -> i32 {
0xcf
}
3 changes: 3 additions & 0 deletions test/integration/cc_common_link/nesting_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn nesting_dep() -> i32 {
very_nested_dep::very_nested_dep()
}
3 changes: 3 additions & 0 deletions test/integration/cc_common_link/very_nested_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn very_nested_dep() -> i32 {
0xaf
}