Skip to content

Commit b834067

Browse files
dzbarskyUebelAndre
andauthored
Handle rust_library_group dependencies in more places (#3617)
This gives downstream rules more flexibility in how deps are supplied to various attributes Co-authored-by: UebelAndre <[email protected]>
1 parent 202d7fb commit b834067

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
66
load("@rules_cc//cc:find_cc_toolchain.bzl", find_cpp_toolchain = "find_cc_toolchain")
77
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
88
load("//rust:defs.bzl", "rust_common")
9-
load("//rust:rust_common.bzl", "BuildInfo")
9+
load("//rust:rust_common.bzl", "BuildInfo", "CrateGroupInfo", "DepInfo")
1010

1111
# buildifier: disable=bzl-visibility
1212
load(
@@ -545,8 +545,19 @@ def _cargo_build_script_impl(ctx):
545545
build_script_inputs.append(dep_build_info.out_dir)
546546

547547
for dep in ctx.attr.deps:
548-
for dep_build_info in dep[rust_common.dep_info].transitive_build_infos.to_list():
549-
build_script_inputs.append(dep_build_info.out_dir)
548+
dep_infos = []
549+
if DepInfo in dep:
550+
dep_infos = [dep[DepInfo]]
551+
else:
552+
dep_infos = [
553+
dep_variant_info.dep_info
554+
for dep_variant_info in dep[CrateGroupInfo].dep_variant_infos.to_list()
555+
if dep_variant_info.dep_info
556+
]
557+
558+
for dep_info in dep_infos:
559+
for dep_build_info in dep_info.transitive_build_infos.to_list():
560+
build_script_inputs.append(dep_build_info.out_dir)
550561

551562
experimental_symlink_execroot = ctx.attr._experimental_symlink_execroot[BuildSettingInfo].value or \
552563
_feature_enabled(ctx, "symlink-exec-root")
@@ -608,7 +619,7 @@ cargo_build_script = rule(
608619
),
609620
"deps": attr.label_list(
610621
doc = "The Rust build-dependencies of the crate",
611-
providers = [rust_common.dep_info],
622+
providers = [[DepInfo], [CrateGroupInfo]],
612623
cfg = "exec",
613624
),
614625
"link_deps": attr.label_list(
@@ -617,7 +628,6 @@ cargo_build_script = rule(
617628
have the links attribute and therefore provide environment
618629
variables to this build script.
619630
"""),
620-
providers = [rust_common.dep_info],
621631
),
622632
"links": attr.string(
623633
doc = "The name of the native library this crate links against.",

rust/private/rust.bzl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ load(
2323
"AllocatorLibrariesImplInfo",
2424
"AllocatorLibrariesInfo",
2525
"BuildInfo",
26+
"CrateGroupInfo",
27+
"CrateInfo",
2628
"LintsInfo",
2729
)
2830
load("//rust/private:rustc.bzl", "collect_extra_rustc_flags", "is_no_std", "rustc_compile_action")
@@ -70,15 +72,24 @@ def _assert_correct_dep_mapping(ctx):
7072
),
7173
)
7274
for dep in ctx.attr.proc_macro_deps:
73-
type = dep[rust_common.crate_info].type
74-
if type != "proc-macro":
75-
fail(
76-
"{} listed {} in its proc_macro_deps, but it is not proc-macro, it is a {}. It should probably instead be listed in deps.".format(
77-
ctx.label,
78-
dep.label,
79-
type,
80-
),
81-
)
75+
if CrateInfo in dep:
76+
types = [dep[CrateInfo].type]
77+
else:
78+
types = [
79+
dep_variant_info.crate_info.type
80+
for dep_variant_info in dep[CrateGroupInfo].dep_variant_infos.to_list()
81+
if dep_variant_info.crate_info
82+
]
83+
84+
for type in types:
85+
if type != "proc-macro":
86+
fail(
87+
"{} listed {} in its proc_macro_deps, but it is not proc-macro, it is a {}. It should probably instead be listed in deps.".format(
88+
ctx.label,
89+
dep.label,
90+
type,
91+
),
92+
)
8293

8394
def _rust_library_impl(ctx):
8495
"""The implementation of the `rust_library` rule.
@@ -732,7 +743,7 @@ _common_attrs = {
732743
List of `rust_proc_macro` targets used to help build this library target.
733744
"""),
734745
cfg = "exec",
735-
providers = [rust_common.crate_info],
746+
providers = [[CrateInfo], [CrateGroupInfo]],
736747
),
737748
"rustc_env": attr.string_dict(
738749
doc = dedent("""\

test/dep_env/BUILD.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"""Tests for passing configuration to cargo_build_script rules"""
22

3+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
34
load("//cargo:defs.bzl", "cargo_build_script", "cargo_dep_env")
4-
load("//rust:defs.bzl", "rust_library", "rust_test")
5+
load("//rust:defs.bzl", "rust_library", "rust_library_group", "rust_test")
56
load(":dep_env.bzl", "create_dep_dir")
67

8+
cc_library(
9+
name = "ignored",
10+
)
11+
712
cargo_build_script(
813
name = "set_a_build",
914
srcs = ["set_a.rs"],
1015
edition = "2018",
16+
link_deps = [":ignored"],
1117
links = "X",
1218
)
1319

@@ -26,6 +32,11 @@ rust_library(
2632
deps = [":set_a_build"],
2733
)
2834

35+
rust_library_group(
36+
name = "set_a_group",
37+
deps = [":set_a"],
38+
)
39+
2940
cargo_dep_env(
3041
name = "set_b",
3142
src = "set_b.env",
@@ -63,6 +74,14 @@ cargo_build_script(
6374
deps = [":set_a"],
6475
)
6576

77+
cargo_build_script(
78+
name = "read_c_group",
79+
srcs = ["read_c.rs"],
80+
edition = "2018",
81+
link_deps = [":set_c"],
82+
deps = [":set_a_group"],
83+
)
84+
6685
cargo_build_script(
6786
name = "read_dep_dir",
6887
srcs = ["read_dep_dir.rs"],

test/rust_crate_group/BUILD.bazel

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_library_group", "rust_test")
1+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_library_group", "rust_proc_macro", "rust_test")
2+
3+
rust_proc_macro(
4+
name = "proc_dep1",
5+
srcs = ["proc_dep1.rs"],
6+
edition = "2021",
7+
)
8+
9+
rust_library_group(
10+
name = "proc_group",
11+
deps = [":proc_dep1"],
12+
)
213

314
rust_library(
415
name = "dep1",
@@ -24,12 +35,14 @@ rust_library(
2435
name = "library",
2536
srcs = ["lib.rs"],
2637
edition = "2021",
38+
proc_macro_deps = [":proc_group"],
2739
deps = [":dep1_and_2"],
2840
)
2941

3042
rust_test(
3143
name = "test",
3244
srcs = ["test.rs"],
3345
edition = "2021",
46+
proc_macro_deps = [":proc_group"],
3447
deps = [":dep1_and_2"],
3548
)

test/rust_crate_group/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn _test() {
2-
dep1::dep1();
2+
proc_dep1::id!(dep1::dep1());
33
dep2::dep2();
44
}

test/rust_crate_group/proc_dep1.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use proc_macro::TokenStream;
2+
3+
#[proc_macro]
4+
pub fn id(input: TokenStream) -> TokenStream {
5+
input
6+
}

test/rust_crate_group/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[test]
22
fn test() {
3-
dep1::dep1();
3+
proc_dep1::id!(dep1::dep1());
44
dep2::dep2();
55
}

0 commit comments

Comments
 (0)