Skip to content

Commit 45ce3dd

Browse files
comiuscopybara-github
authored andcommitted
Add plugin_format_flag attribute to ProtoLangToolchainRule
This makes it possible to pair it with command_line attribute which could contain dependent data, for example: ``` proto_lang_toolchain( name = "j2objc_proto_toolchain", blacklisted_protos = [], command_line = "--PLUGIN_j2objc_out=file_dir_mapping,generate_class_mappings:$(OUT)", + plugin_format_flag = "--plugin=protoc-gen-PLUGIN_j2objc=%s", plugin = "//third_party/java/j2objc:proto_plugin", runtime = "//third_party/java/j2objc:proto_runtime", visibility = ["//visibility:public"], ) ``` It also retains reference to the flag on proto_lang_toolchain rule and so doesn't cause a memory regression when proto_lang_libraries are Starlarkyfied. PiperOrigin-RevId: 412287195
1 parent d69a55c commit 45ce3dd

6 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ private static ToolchainInvocation createDescriptorSetToolchain(
321321
// A rule that concatenates the artifacts from ctx.deps.proto.transitive_descriptor_sets
322322
// provides similar results.
323323
"--descriptor_set_out=%s",
324+
/* pluginFormatFlag = */ null,
324325
/* pluginExecutable= */ null,
325326
/* runtime= */ null,
326327
/* providedProtoSources= */ ImmutableList.of()),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
5252
.addProvider(
5353
ProtoLangToolchainProvider.create(
5454
flag,
55+
ruleContext.attributes().get("plugin_format_flag", Type.STRING),
5556
ruleContext.getPrerequisite("plugin", FilesToRunProvider.class),
5657
ruleContext.getPrerequisite("runtime"),
5758
// We intentionally flatten the NestedSet here.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
public abstract class ProtoLangToolchainProvider implements TransitiveInfoProvider {
3737
public abstract String outReplacementFormatFlag();
3838

39+
@Nullable
40+
public abstract String pluginFormatFlag();
41+
3942
@Nullable
4043
public abstract FilesToRunProvider pluginExecutable();
4144

@@ -64,12 +67,14 @@ public NestedSet<Artifact> blacklistedProtos() {
6467
@AutoCodec.Instantiator
6568
public static ProtoLangToolchainProvider createForDeserialization(
6669
String outReplacementFormatFlag,
70+
String pluginFormatFlag,
6771
FilesToRunProvider pluginExecutable,
6872
TransitiveInfoCollection runtime,
6973
ImmutableList<ProtoSource> providedProtoSources,
7074
NestedSet<Artifact> blacklistedProtos) {
7175
return new AutoValue_ProtoLangToolchainProvider(
7276
outReplacementFormatFlag,
77+
pluginFormatFlag,
7378
pluginExecutable,
7479
runtime,
7580
providedProtoSources,
@@ -78,6 +83,7 @@ public static ProtoLangToolchainProvider createForDeserialization(
7883

7984
public static ProtoLangToolchainProvider create(
8085
String outReplacementFormatFlag,
86+
String pluginFormatFlag,
8187
FilesToRunProvider pluginExecutable,
8288
TransitiveInfoCollection runtime,
8389
ImmutableList<ProtoSource> providedProtoSources) {
@@ -87,6 +93,7 @@ public static ProtoLangToolchainProvider create(
8793
}
8894
return new AutoValue_ProtoLangToolchainProvider(
8995
outReplacementFormatFlag,
96+
pluginFormatFlag,
9097
pluginExecutable,
9198
runtime,
9299
providedProtoSources,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
4343
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
4444
.add(attr("command_line", Type.STRING).mandatory())
4545

46+
/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(plugin_format_flag) -->
47+
If provided, this value will be passed to proto-compiler to use the plugin. The value must
48+
contain a single %s which is replaced with plugin executable.
49+
<code>--plugin=protoc-gen-PLUGIN=<executable>.</code>
50+
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
51+
.add(attr("plugin_format_flag", Type.STRING))
52+
4653
/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(plugin) -->
4754
If provided, will be made available to the action that calls the proto-compiler, and will be
4855
passed to the proto-compiler:

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ public void commandLine_basic() throws Exception {
105105
ProtoLangToolchainProvider toolchainNoPlugin =
106106
ProtoLangToolchainProvider.create(
107107
"--java_out=param1,param2:%s",
108+
/* pluginFormatFlag= */ null,
108109
/* pluginExecutable= */ null,
109110
/* runtime= */ mock(TransitiveInfoCollection.class),
110111
/* providedProtoSources= */ ImmutableList.of());
111112

112113
ProtoLangToolchainProvider toolchainWithPlugin =
113114
ProtoLangToolchainProvider.create(
114115
"--PLUGIN_pluginName_out=param3,param4:%s",
116+
/* pluginFormatFlag= */ "--plugin=protoc-gen-PLUGIN_pluginName=%s",
115117
plugin,
116118
/* runtime= */ mock(TransitiveInfoCollection.class),
117119
/* providedProtoSources= */ ImmutableList.of());
@@ -175,6 +177,7 @@ public void commandLine_strictDeps() throws Exception {
175177
ProtoLangToolchainProvider toolchain =
176178
ProtoLangToolchainProvider.create(
177179
"--java_out=param1,param2:%s",
180+
/* pluginFormatFlag= */ null,
178181
/* pluginExecutable= */ null,
179182
/* runtime= */ mock(TransitiveInfoCollection.class),
180183
/* providedProtoSources= */ ImmutableList.of());
@@ -213,6 +216,7 @@ public void commandLine_exports() throws Exception {
213216
ProtoLangToolchainProvider toolchain =
214217
ProtoLangToolchainProvider.create(
215218
"--java_out=param1,param2:%s",
219+
/* pluginFormatFlag= */ null,
216220
/* pluginExecutable= */ null,
217221
/* runtime= */ mock(TransitiveInfoCollection.class),
218222
/* providedProtoSources= */ ImmutableList.of());
@@ -283,6 +287,7 @@ public String toString() {
283287
ProtoLangToolchainProvider toolchain =
284288
ProtoLangToolchainProvider.create(
285289
"--java_out=param1,param2:%s",
290+
/* pluginFormatFlag= */ null,
286291
/* pluginExecutable= */ null,
287292
/* runtime= */ mock(TransitiveInfoCollection.class),
288293
/* providedProtoSources= */ ImmutableList.of());
@@ -317,13 +322,15 @@ public void exceptionIfSameName() throws Exception {
317322
ProtoLangToolchainProvider toolchain1 =
318323
ProtoLangToolchainProvider.create(
319324
"dontcare",
325+
/* pluginFormatFlag= */ null,
320326
/* pluginExecutable= */ null,
321327
/* runtime= */ mock(TransitiveInfoCollection.class),
322328
/* providedProtoSources= */ ImmutableList.of());
323329

324330
ProtoLangToolchainProvider toolchain2 =
325331
ProtoLangToolchainProvider.create(
326332
"dontcare",
333+
/* pluginFormatFlag= */ null,
327334
/* pluginExecutable= */ null,
328335
/* runtime= */ mock(TransitiveInfoCollection.class),
329336
/* providedProtoSources= */ ImmutableList.of());

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void setUp() throws Exception {
4141

4242
private void validateProtoLangToolchain(ProtoLangToolchainProvider toolchain) throws Exception {
4343
assertThat(toolchain.outReplacementFormatFlag()).isEqualTo("cmd-line");
44+
assertThat(toolchain.pluginFormatFlag()).isEqualTo("--plugin=%s");
4445
assertThat(toolchain.pluginExecutable().getExecutable().getRootRelativePathString())
4546
.isEqualTo("third_party/x/plugin");
4647

@@ -73,6 +74,7 @@ public void protoToolchain() throws Exception {
7374
"proto_lang_toolchain(",
7475
" name = 'toolchain',",
7576
" command_line = 'cmd-line',",
77+
" plugin_format_flag = '--plugin=%s',",
7678
" plugin = '//third_party/x:plugin',",
7779
" runtime = '//third_party/x:runtime',",
7880
" blacklisted_protos = ['//third_party/x:denied']",
@@ -101,6 +103,7 @@ public void protoToolchainBlacklistProtoLibraries() throws Exception {
101103
"proto_lang_toolchain(",
102104
" name = 'toolchain',",
103105
" command_line = 'cmd-line',",
106+
" plugin_format_flag = '--plugin=%s',",
104107
" plugin = '//third_party/x:plugin',",
105108
" runtime = '//third_party/x:runtime',",
106109
" blacklisted_protos = ['//third_party/x:descriptors', '//third_party/x:any']",
@@ -129,6 +132,7 @@ public void protoToolchainBlacklistTransitiveProtos() throws Exception {
129132
"proto_lang_toolchain(",
130133
" name = 'toolchain',",
131134
" command_line = 'cmd-line',",
135+
" plugin_format_flag = '--plugin=%s',",
132136
" plugin = '//third_party/x:plugin',",
133137
" runtime = '//third_party/x:runtime',",
134138
" blacklisted_protos = ['//third_party/x:any']",

0 commit comments

Comments
 (0)