Skip to content

Commit ac7d5c0

Browse files
committed
Traverse CrateGroupInfo when gathering CcInfo linking_context entries for use_cc_common_link
Certain code written before crate groups were introduced wasn't adjusted to traverse groups, specifically code that traverses crate deps to discover cc_info. collect_deps properly does flatten crate_groups, so this moves the code for flattening a list of DepVariantInfo into a utility function, 'flatten_crate_groups'. This fixes rust_library_group and rust_prost_library for experimental_use_cc_common_link.
1 parent cdaf15f commit ac7d5c0

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

rust/private/rustc.bzl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ def _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation
218218
def _is_proc_macro(crate_info):
219219
return "proc-macro" in (crate_info.type, crate_info.wrapped_crate_type)
220220

221+
def flatten_crate_groups(deps):
222+
"""Given a list of DepVariantInfo (like crate_info.deps), flatten any crate_group_info into the rest of the list"""
223+
crate_deps = []
224+
for dep in deps:
225+
crate_group = getattr(dep, "crate_group_info", None)
226+
if crate_group:
227+
crate_deps.extend(crate_group.dep_variant_infos.to_list())
228+
else:
229+
crate_deps.append(dep)
230+
return crate_deps
231+
221232
def collect_deps(
222233
deps,
223234
proc_macro_deps,
@@ -260,13 +271,7 @@ def collect_deps(
260271
direct_metadata_outputs = []
261272
transitive_metadata_outputs = []
262273

263-
crate_deps = []
264-
for dep in deps + proc_macro_deps:
265-
crate_group = getattr(dep, "crate_group_info", None)
266-
if crate_group:
267-
crate_deps.extend(crate_group.dep_variant_infos.to_list())
268-
else:
269-
crate_deps.append(dep)
274+
crate_deps = flatten_crate_groups(deps + proc_macro_deps)
270275

271276
aliases = {k.label: v for k, v in aliases.items()}
272277
for dep in crate_deps:
@@ -1580,9 +1585,11 @@ def rustc_compile_action(
15801585
toolchain.stdlib_linkflags.linking_context,
15811586
]
15821587

1583-
for dep in crate_info.deps.to_list():
1584-
if dep.cc_info:
1585-
linking_contexts.append(dep.cc_info.linking_context)
1588+
linking_contexts += [
1589+
dep.cc_info.linking_context
1590+
for dep in flatten_crate_groups(crate_info.deps.to_list())
1591+
if dep.cc_info
1592+
]
15861593

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

19601967
# Flattening is okay since crate_info.deps only records direct deps.
1961-
for dep in crate_info.deps.to_list():
1968+
for dep in flatten_crate_groups(crate_info.deps.to_list()):
19621969
if dep.cc_info:
19631970
# A Rust staticlib or shared library doesn't need to propagate linker inputs
19641971
# of its dependencies, except for shared libraries.

test/integration/cc_common_link/BUILD.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load(
44
"rust_binary",
55
"rust_library",
66
"rust_shared_library",
7+
"rust_library_group",
78
"rust_test",
89
)
910

@@ -12,6 +13,35 @@ cc_library(
1213
linkstamp = "cclinkstampdep.cc",
1314
)
1415

16+
rust_library(
17+
name = "deep_dep",
18+
srcs = ["deep_dep.rs"],
19+
edition = "2021",
20+
)
21+
22+
rust_library_group(
23+
name = "dep_group",
24+
deps = [":deep_dep"],
25+
)
26+
27+
rust_library(
28+
name = "very_nested_dep",
29+
srcs = ["very_nested_dep.rs"],
30+
edition = "2021",
31+
)
32+
33+
rust_library_group(
34+
name = "nested_dep_group",
35+
deps = [":very_nested_dep"],
36+
)
37+
38+
rust_library(
39+
name = "nesting_dep",
40+
srcs = ["nesting_dep.rs"],
41+
deps = [":nested_dep_group"],
42+
edition = "2021",
43+
)
44+
1545
rust_library(
1646
name = "rdep",
1747
srcs = ["rdep.rs"],
@@ -25,6 +55,8 @@ rust_binary(
2555
deps = [
2656
":cclinkstampdep",
2757
":rdep",
58+
":dep_group",
59+
":nesting_dep",
2860
],
2961
)
3062

test/integration/cc_common_link/bin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ extern "C" {
66

77
fn main() {
88
println!("bin rdep: {}", rdep::rdep());
9+
println!("bin deep_dep: {}", deep_dep::deep_dep());
10+
println!("bin nesting_dep: {}", nesting_dep::nesting_dep());
911
println!("cclinkstampdep: {}", unsafe { cclinkstampdep() });
1012
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn deep_dep() -> i32 {
2+
0xcf
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn nesting_dep() -> i32 {
2+
very_nested_dep::very_nested_dep()
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn very_nested_dep() -> i32 {
2+
0xaf
3+
}

0 commit comments

Comments
 (0)