Skip to content

Commit efc9c9c

Browse files
kotlajacopybara-github
authored andcommitted
Add proto_compiler attribute to proto_lang_toolchain
Added a macro for proto_lang_toolchain which removes proto_compiler/_proto_compiler dependency. I've added a unit test for the attribute. I've also added proto_compiler attribute in ProtoLangToolchainRule in order to have automatic description of the attribute. PiperOrigin-RevId: 444803364
1 parent 38c586f commit efc9c9c

File tree

3 files changed

+134
-36
lines changed

3 files changed

+134
-36
lines changed

src/main/java/com/google/devtools/build/lib/rules/proto/ProtoLangToolchainRule.java

+10
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
100100
attr("blacklisted_protos", LABEL_LIST)
101101
.allowedFileTypes()
102102
.mandatoryProviders(StarlarkProviderIdentifier.forKey(ProtoInfo.PROVIDER.getKey())))
103+
104+
/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(proto_compiler) -->
105+
The proto compiler executable.
106+
If provided, this target will be used as a proto-compiler to generate the code.
107+
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
108+
.add(
109+
attr("proto_compiler", LABEL)
110+
.allowedFileTypes()
111+
.cfg(ExecutionTransitionFactory.create())
112+
.exec())
103113
.add(
104114
attr(":proto_compiler", LABEL)
105115
.cfg(ExecutionTransitionFactory.create())

src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl

+68-26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load(":common/proto/providers.bzl", "ProtoLangToolchainInfo")
1818
load(":common/proto/proto_semantics.bzl", "semantics")
19+
load(":common/rule_util.bzl", "merge_attrs")
1920

2021
ProtoInfo = _builtins.toplevel.ProtoInfo
2122
proto_common = _builtins.toplevel.proto_common
@@ -36,6 +37,9 @@ def _rule_impl(ctx):
3637
if ctx.attr.plugin != None:
3738
plugin = ctx.attr.plugin[DefaultInfo].files_to_run
3839

40+
proto_compiler = getattr(ctx.attr, "proto_compiler", None)
41+
proto_compiler = getattr(ctx.attr, "_proto_compiler", proto_compiler)
42+
3943
return [
4044
DefaultInfo(
4145
files = depset(),
@@ -47,39 +51,77 @@ def _rule_impl(ctx):
4751
plugin = plugin,
4852
runtime = ctx.attr.runtime,
4953
provided_proto_sources = provided_proto_sources,
50-
proto_compiler = ctx.attr._proto_compiler.files_to_run,
54+
proto_compiler = proto_compiler.files_to_run,
5155
protoc_opts = ctx.fragments.proto.experimental_protoc_opts,
5256
progress_message = ctx.attr.progress_message,
5357
mnemonic = ctx.attr.mnemonic,
5458
),
5559
]
5660

57-
proto_lang_toolchain = rule(
61+
proto_lang_toolchain_attrs = {
62+
"progress_message": attr.string(default = "Generating proto_library %{label}"),
63+
"mnemonic": attr.string(default = "GenProto"),
64+
"command_line": attr.string(mandatory = True),
65+
"plugin_format_flag": attr.string(),
66+
"plugin": attr.label(
67+
executable = True,
68+
cfg = "exec",
69+
allow_files = True,
70+
),
71+
"runtime": attr.label(
72+
allow_files = True,
73+
),
74+
"blacklisted_protos": attr.label_list(
75+
allow_files = True,
76+
providers = [ProtoInfo],
77+
),
78+
}
79+
80+
proto_lang_toolchain_custom_protoc = rule(
5881
implementation = _rule_impl,
59-
attrs = {
60-
"progress_message": attr.string(default = "Generating proto_library %{label}"),
61-
"mnemonic": attr.string(default = "GenProto"),
62-
"command_line": attr.string(mandatory = True),
63-
"plugin_format_flag": attr.string(),
64-
"plugin": attr.label(
65-
executable = True,
66-
cfg = "exec",
67-
allow_files = True,
68-
),
69-
"runtime": attr.label(
70-
allow_files = True,
71-
),
72-
"blacklisted_protos": attr.label_list(
73-
allow_files = True,
74-
providers = [ProtoInfo],
75-
),
76-
"_proto_compiler": attr.label(
77-
cfg = "exec",
78-
executable = True,
79-
allow_files = True,
80-
default = configuration_field("proto", "proto_compiler"),
81-
),
82-
},
82+
attrs = merge_attrs(
83+
proto_lang_toolchain_attrs,
84+
{
85+
"proto_compiler": attr.label(
86+
cfg = "exec",
87+
executable = True,
88+
allow_files = True,
89+
),
90+
},
91+
),
8392
provides = [ProtoLangToolchainInfo],
8493
fragments = ["proto"] + semantics.EXTRA_FRAGMENTS,
8594
)
95+
96+
proto_lang_toolchain_default_protoc = rule(
97+
implementation = _rule_impl,
98+
attrs = merge_attrs(
99+
proto_lang_toolchain_attrs,
100+
{
101+
"_proto_compiler": attr.label(
102+
cfg = "exec",
103+
executable = True,
104+
allow_files = True,
105+
default = configuration_field("proto", "proto_compiler"),
106+
),
107+
},
108+
),
109+
provides = [ProtoLangToolchainInfo],
110+
fragments = ["proto"] + semantics.EXTRA_FRAGMENTS,
111+
)
112+
113+
def proto_lang_toolchain(
114+
name = None,
115+
proto_compiler = None,
116+
**kwargs):
117+
if proto_compiler != None:
118+
proto_lang_toolchain_custom_protoc(
119+
name = name,
120+
proto_compiler = proto_compiler,
121+
**kwargs
122+
)
123+
else:
124+
proto_lang_toolchain_default_protoc(
125+
name = name,
126+
**kwargs
127+
)

src/test/java/com/google/devtools/build/lib/rules/proto/StarlarkProtoLangToolchainTest.java

+56-10
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ private void validateStarlarkProtoLangToolchain(StarlarkInfo toolchain) throws E
6767
TransitiveInfoCollection runtimes = (TransitiveInfoCollection) toolchain.getValue("runtime");
6868
assertThat(runtimes.getLabel())
6969
.isEqualTo(Label.parseAbsolute("//third_party/x:runtime", ImmutableMap.of()));
70+
}
7071

71-
Label protoc = Label.parseAbsoluteUnchecked(ProtoConstants.DEFAULT_PROTOC_LABEL);
72+
private void validateProtoCompiler(StarlarkInfo toolchain, Label protoCompiler) {
7273
assertThat(
7374
((FilesToRunProvider) toolchain.getValue("proto_compiler"))
7475
.getExecutable()
7576
.prettyPrint())
76-
.isEqualTo(protoc.toPathFragment().getPathString());
77+
.isEqualTo(protoCompiler.toPathFragment().getPathString());
7778
}
7879

7980
@Override
@@ -103,10 +104,49 @@ public void protoToolchain() throws Exception {
103104
")");
104105

105106
update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
107+
StarlarkInfo toolchain =
108+
(StarlarkInfo)
109+
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey());
110+
Label protoc = Label.parseAbsoluteUnchecked(ProtoConstants.DEFAULT_PROTOC_LABEL);
111+
112+
validateStarlarkProtoLangToolchain(toolchain);
113+
validateProtoCompiler(toolchain, protoc);
114+
}
115+
116+
@Test
117+
public void protoToolchain_setProtoCompiler() throws Exception {
118+
scratch.file(
119+
"third_party/x/BUILD",
120+
"licenses(['unencumbered'])",
121+
"cc_binary(name = 'plugin', srcs = ['plugin.cc'])",
122+
"cc_library(name = 'runtime', srcs = ['runtime.cc'])",
123+
"filegroup(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])",
124+
"filegroup(name = 'any', srcs = ['any.proto'])",
125+
"proto_library(name = 'denied', srcs = [':descriptors', ':any'])",
126+
"cc_binary(name = 'compiler')");
127+
128+
scratch.file(
129+
"foo/BUILD",
130+
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
131+
"licenses(['unencumbered'])",
132+
"proto_lang_toolchain(",
133+
" name = 'toolchain',",
134+
" command_line = 'cmd-line:$(OUT)',",
135+
" plugin_format_flag = '--plugin=%s',",
136+
" plugin = '//third_party/x:plugin',",
137+
" runtime = '//third_party/x:runtime',",
138+
" progress_message = 'Progress Message %{label}',",
139+
" mnemonic = 'MyMnemonic',",
140+
" proto_compiler = '//third_party/x:compiler',",
141+
")");
106142

107-
validateStarlarkProtoLangToolchain(
143+
StarlarkInfo toolchain =
108144
(StarlarkInfo)
109-
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey()));
145+
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey());
146+
Label protoc = Label.parseAbsoluteUnchecked("//third_party/x:compiler");
147+
148+
validateStarlarkProtoLangToolchain(toolchain);
149+
validateProtoCompiler(toolchain, protoc);
110150
}
111151

112152
@Override
@@ -135,10 +175,13 @@ public void protoToolchainBlacklistProtoLibraries() throws Exception {
135175
")");
136176

137177
update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
138-
139-
validateStarlarkProtoLangToolchain(
178+
StarlarkInfo toolchain =
140179
(StarlarkInfo)
141-
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey()));
180+
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey());
181+
Label protoc = Label.parseAbsoluteUnchecked(ProtoConstants.DEFAULT_PROTOC_LABEL);
182+
183+
validateStarlarkProtoLangToolchain(toolchain);
184+
validateProtoCompiler(toolchain, protoc);
142185
}
143186

144187
@Override
@@ -167,10 +210,13 @@ public void protoToolchainBlacklistTransitiveProtos() throws Exception {
167210
")");
168211

169212
update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
170-
171-
validateStarlarkProtoLangToolchain(
213+
StarlarkInfo toolchain =
172214
(StarlarkInfo)
173-
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey()));
215+
getConfiguredTarget("//foo:toolchain").get(getStarlarkProtoLangToolchainInfoKey());
216+
Label protoc = Label.parseAbsoluteUnchecked(ProtoConstants.DEFAULT_PROTOC_LABEL);
217+
218+
validateStarlarkProtoLangToolchain(toolchain);
219+
validateProtoCompiler(toolchain, protoc);
174220
}
175221

176222
@Override

0 commit comments

Comments
 (0)