Skip to content

Commit 3cc5e07

Browse files
tkoeppecopybara-github
authored andcommitted
Internal change only.
PiperOrigin-RevId: 819969994
1 parent 592607b commit 3cc5e07

File tree

2 files changed

+134
-120
lines changed

2 files changed

+134
-120
lines changed

rust/defs.bzl

Lines changed: 10 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
"""This file implements rust_proto_library."""
1+
"""This file implements rust_proto_library (frontend macro)."""
2+
3+
# go/for-macros-only
24

3-
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
4-
load("@rules_rust//rust:defs.bzl", "rust_common")
5-
load("@rules_rust//rust:rust_common.bzl", "CrateInfo", "DepInfo")
6-
load("//bazel/common:proto_common.bzl", "proto_common")
7-
load("//bazel/common:proto_info.bzl", "ProtoInfo")
85
load(
9-
"//rust/bazel:aspects.bzl",
10-
"RustProtoInfo",
11-
"label_to_crate_name",
12-
"proto_rust_toolchain_label",
13-
"rust_cc_proto_library_aspect",
14-
"rust_upb_proto_library_aspect",
6+
":rules.bzl",
7+
_ProtoCrateNamesInfo = "ProtoCrateNamesInfo",
8+
_rust_cc_proto_library = "rust_cc_proto_library",
9+
_rust_upb_proto_library = "rust_upb_proto_library",
1510
)
1611

17-
ProtoCrateNamesInfo = provider(
18-
doc = """A provider that contains both names a Protobuf crate has throughout the build.""",
19-
fields = {
20-
"crate_name": "The name of rust_proto_library.",
21-
"old_crate_name": "The name of the proto_library.",
22-
},
23-
)
12+
ProtoCrateNamesInfo = _ProtoCrateNamesInfo
13+
rust_upb_proto_library = _rust_upb_proto_library
14+
rust_cc_proto_library = _rust_cc_proto_library
2415

2516
def rust_proto_library(name, deps, **args):
2617
"""Declares all the boilerplate needed to use Rust protobufs conveniently.
@@ -63,104 +54,3 @@ def rust_proto_library(name, deps, **args):
6354
visibility = ["//rust/test:__subpackages__"],
6455
**args
6556
)
66-
67-
def _user_visible_label(ctx):
68-
label = str(ctx.label)
69-
label = label.removesuffix("_cpp_rust_proto")
70-
label = label.removesuffix("_upb_rust_proto")
71-
return label + "_rust_proto"
72-
73-
def _rust_proto_library_impl(ctx):
74-
if not ctx.label.name.endswith("_rust_proto"):
75-
fail(
76-
"{}: Name of rust_proto_library target should end with `_rust_proto`."
77-
.format(_user_visible_label(ctx)),
78-
)
79-
deps = ctx.attr.deps
80-
if not deps:
81-
fail(
82-
"{}: Exactly 1 dependency in `deps` attribute expected, none were provided."
83-
.format(_user_visible_label(ctx)),
84-
)
85-
if len(deps) > 1:
86-
fail(
87-
"{}: Exactly 1 dependency in `deps` attribute expected, too many were provided."
88-
.format(_user_visible_label(ctx)),
89-
)
90-
91-
dep = deps[0]
92-
rust_proto_info = dep[RustProtoInfo]
93-
94-
proto_common.check_collocated(
95-
ctx.label,
96-
dep[ProtoInfo],
97-
ctx.attr._proto_lang_toolchain[proto_common.ProtoLangToolchainInfo],
98-
)
99-
100-
if len(rust_proto_info.dep_variant_infos) != 1:
101-
fail(
102-
"{}: rust_proto_library does not support src-less proto_library targets."
103-
.format(_user_visible_label(ctx)),
104-
)
105-
dep_variant_info = rust_proto_info.dep_variant_infos[0]
106-
crate_info = dep_variant_info.crate_info
107-
108-
# Change the crate name from the name of the proto_library to the name of the rust_proto_library.
109-
#
110-
# When the aspect visits proto_libraries, it doesn't know and cannot deduce the name of the
111-
# rust_proto_library (although the name of rust_proto_libraries is consistently ending with
112-
# _rust_proto, we can't rely on all proto_libraries to have a name consistently ending with
113-
# _proto), therefore we have to modify it after the fact here.
114-
#
115-
# Since Starlark providers are frozen once they leave the _impl function that defines them,
116-
# we have to create a shallow copy.
117-
toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"]
118-
fields = {field: getattr(crate_info, field) for field in dir(crate_info)}
119-
120-
# Construct a label and compute the crate name.
121-
# The label's package and workspace root are only relevant when 1P crate renaming is enabled.
122-
# The current implementation of crate renaming supports only monorepos which
123-
# means that it will only rename when label.workspace_root is empty.
124-
label_str = _user_visible_label(ctx)
125-
label = Label(label_str)
126-
fields["name"] = label_to_crate_name(ctx, label, toolchain)
127-
128-
# These two fields present on the dir(crate_info) but break on some versions of Bazel when
129-
# passed back in to crate_info. Strip them for now.
130-
fields.pop("to_json", None)
131-
fields.pop("to_proto", None)
132-
133-
crate_info_with_rust_proto_name = rust_common.crate_info(**fields)
134-
135-
return [
136-
ProtoCrateNamesInfo(
137-
crate_name = crate_info_with_rust_proto_name.name,
138-
old_crate_name = crate_info.name,
139-
),
140-
crate_info_with_rust_proto_name,
141-
dep_variant_info.dep_info,
142-
dep_variant_info.cc_info,
143-
DefaultInfo(files = dep_variant_info.crate_info.srcs),
144-
]
145-
146-
def _make_rust_proto_library(is_upb):
147-
return rule(
148-
implementation = _rust_proto_library_impl,
149-
attrs = {
150-
"deps": attr.label_list(
151-
mandatory = True,
152-
providers = [ProtoInfo],
153-
aspects = [rust_upb_proto_library_aspect if is_upb else rust_cc_proto_library_aspect],
154-
),
155-
"_proto_lang_toolchain": attr.label(
156-
default = Label(proto_rust_toolchain_label(is_upb)),
157-
),
158-
},
159-
toolchains = [
160-
"@rules_rust//rust:toolchain_type",
161-
],
162-
provides = [ProtoCrateNamesInfo, CrateInfo, DepInfo, CcInfo],
163-
)
164-
165-
rust_upb_proto_library = _make_rust_proto_library(is_upb = True)
166-
rust_cc_proto_library = _make_rust_proto_library(is_upb = False)

rust/rules.bzl

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""This file implements rust_proto_library (rule implementations)."""
2+
3+
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
4+
load("@rules_rust//rust:defs.bzl", "rust_common")
5+
load("@rules_rust//rust:rust_common.bzl", "CrateInfo", "DepInfo")
6+
load("//bazel/common:proto_common.bzl", "proto_common")
7+
load("//bazel/common:proto_info.bzl", "ProtoInfo")
8+
load(
9+
"//rust/bazel:aspects.bzl",
10+
"RustProtoInfo",
11+
"label_to_crate_name",
12+
"proto_rust_toolchain_label",
13+
"rust_cc_proto_library_aspect",
14+
"rust_upb_proto_library_aspect",
15+
)
16+
17+
ProtoCrateNamesInfo = provider(
18+
doc = """A provider that contains both names a Protobuf crate has throughout the build.""",
19+
fields = {
20+
"crate_name": "The name of rust_proto_library.",
21+
"old_crate_name": "The name of the proto_library.",
22+
},
23+
)
24+
25+
def _user_visible_label(ctx):
26+
label = str(ctx.label)
27+
label = label.removesuffix("_cpp_rust_proto")
28+
label = label.removesuffix("_upb_rust_proto")
29+
return label + "_rust_proto"
30+
31+
def _rust_proto_library_impl(ctx):
32+
if not ctx.label.name.endswith("_rust_proto"):
33+
fail(
34+
"{}: Name of rust_proto_library target should end with `_rust_proto`."
35+
.format(_user_visible_label(ctx)),
36+
)
37+
deps = ctx.attr.deps
38+
if not deps:
39+
fail(
40+
"{}: Exactly 1 dependency in `deps` attribute expected, none were provided."
41+
.format(_user_visible_label(ctx)),
42+
)
43+
if len(deps) > 1:
44+
fail(
45+
"{}: Exactly 1 dependency in `deps` attribute expected, too many were provided."
46+
.format(_user_visible_label(ctx)),
47+
)
48+
49+
dep = deps[0]
50+
rust_proto_info = dep[RustProtoInfo]
51+
52+
proto_common.check_collocated(
53+
ctx.label,
54+
dep[ProtoInfo],
55+
ctx.attr._proto_lang_toolchain[proto_common.ProtoLangToolchainInfo],
56+
)
57+
58+
if len(rust_proto_info.dep_variant_infos) != 1:
59+
fail(
60+
"{}: rust_proto_library does not support src-less proto_library targets."
61+
.format(_user_visible_label(ctx)),
62+
)
63+
dep_variant_info = rust_proto_info.dep_variant_infos[0]
64+
crate_info = dep_variant_info.crate_info
65+
66+
# Change the crate name from the name of the proto_library to the name of the rust_proto_library.
67+
#
68+
# When the aspect visits proto_libraries, it doesn't know and cannot deduce the name of the
69+
# rust_proto_library (although the name of rust_proto_libraries is consistently ending with
70+
# _rust_proto, we can't rely on all proto_libraries to have a name consistently ending with
71+
# _proto), therefore we have to modify it after the fact here.
72+
#
73+
# Since Starlark providers are frozen once they leave the _impl function that defines them,
74+
# we have to create a shallow copy.
75+
toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"]
76+
fields = {field: getattr(crate_info, field) for field in dir(crate_info)}
77+
78+
# Construct a label and compute the crate name.
79+
# The label's package and workspace root are only relevant when 1P crate renaming is enabled.
80+
# The current implementation of crate renaming supports only monorepos which
81+
# means that it will only rename when label.workspace_root is empty.
82+
label_str = _user_visible_label(ctx)
83+
label = Label(label_str)
84+
fields["name"] = label_to_crate_name(ctx, label, toolchain)
85+
86+
# These two fields present on the dir(crate_info) but break on some versions of Bazel when
87+
# passed back in to crate_info. Strip them for now.
88+
fields.pop("to_json", None)
89+
fields.pop("to_proto", None)
90+
91+
crate_info_with_rust_proto_name = rust_common.crate_info(**fields)
92+
93+
return [
94+
ProtoCrateNamesInfo(
95+
crate_name = crate_info_with_rust_proto_name.name,
96+
old_crate_name = crate_info.name,
97+
),
98+
crate_info_with_rust_proto_name,
99+
dep_variant_info.dep_info,
100+
dep_variant_info.cc_info,
101+
DefaultInfo(files = dep_variant_info.crate_info.srcs),
102+
]
103+
104+
def _make_rust_proto_library(is_upb):
105+
return rule(
106+
implementation = _rust_proto_library_impl,
107+
attrs = {
108+
"deps": attr.label_list(
109+
mandatory = True,
110+
providers = [ProtoInfo],
111+
aspects = [rust_upb_proto_library_aspect if is_upb else rust_cc_proto_library_aspect],
112+
),
113+
"_proto_lang_toolchain": attr.label(
114+
default = Label(proto_rust_toolchain_label(is_upb)),
115+
),
116+
},
117+
toolchains = [
118+
"@rules_rust//rust:toolchain_type",
119+
],
120+
provides = [ProtoCrateNamesInfo, CrateInfo, DepInfo, CcInfo],
121+
)
122+
123+
rust_upb_proto_library = _make_rust_proto_library(is_upb = True)
124+
rust_cc_proto_library = _make_rust_proto_library(is_upb = False)

0 commit comments

Comments
 (0)