|
1 | | -"""This file implements rust_proto_library.""" |
| 1 | +"""This file implements rust_proto_library (frontend macro).""" |
| 2 | + |
| 3 | +# go/for-macros-only |
2 | 4 |
|
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 | 5 | 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", |
15 | 10 | ) |
16 | 11 |
|
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 |
24 | 15 |
|
25 | 16 | def rust_proto_library(name, deps, **args): |
26 | 17 | """Declares all the boilerplate needed to use Rust protobufs conveniently. |
@@ -63,104 +54,3 @@ def rust_proto_library(name, deps, **args): |
63 | 54 | visibility = ["//rust/test:__subpackages__"], |
64 | 55 | **args |
65 | 56 | ) |
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) |
0 commit comments