Skip to content

Commit

Permalink
Add plugin_format_flag attribute to ProtoLangToolchainRule
Browse files Browse the repository at this point in the history
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
  • Loading branch information
comius authored and copybara-github committed Nov 25, 2021
1 parent d69a55c commit 45ce3dd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ private static ToolchainInvocation createDescriptorSetToolchain(
// A rule that concatenates the artifacts from ctx.deps.proto.transitive_descriptor_sets
// provides similar results.
"--descriptor_set_out=%s",
/* pluginFormatFlag = */ null,
/* pluginExecutable= */ null,
/* runtime= */ null,
/* providedProtoSources= */ ImmutableList.of()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addProvider(
ProtoLangToolchainProvider.create(
flag,
ruleContext.attributes().get("plugin_format_flag", Type.STRING),
ruleContext.getPrerequisite("plugin", FilesToRunProvider.class),
ruleContext.getPrerequisite("runtime"),
// We intentionally flatten the NestedSet here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
public abstract class ProtoLangToolchainProvider implements TransitiveInfoProvider {
public abstract String outReplacementFormatFlag();

@Nullable
public abstract String pluginFormatFlag();

@Nullable
public abstract FilesToRunProvider pluginExecutable();

Expand Down Expand Up @@ -64,12 +67,14 @@ public NestedSet<Artifact> blacklistedProtos() {
@AutoCodec.Instantiator
public static ProtoLangToolchainProvider createForDeserialization(
String outReplacementFormatFlag,
String pluginFormatFlag,
FilesToRunProvider pluginExecutable,
TransitiveInfoCollection runtime,
ImmutableList<ProtoSource> providedProtoSources,
NestedSet<Artifact> blacklistedProtos) {
return new AutoValue_ProtoLangToolchainProvider(
outReplacementFormatFlag,
pluginFormatFlag,
pluginExecutable,
runtime,
providedProtoSources,
Expand All @@ -78,6 +83,7 @@ public static ProtoLangToolchainProvider createForDeserialization(

public static ProtoLangToolchainProvider create(
String outReplacementFormatFlag,
String pluginFormatFlag,
FilesToRunProvider pluginExecutable,
TransitiveInfoCollection runtime,
ImmutableList<ProtoSource> providedProtoSources) {
Expand All @@ -87,6 +93,7 @@ public static ProtoLangToolchainProvider create(
}
return new AutoValue_ProtoLangToolchainProvider(
outReplacementFormatFlag,
pluginFormatFlag,
pluginExecutable,
runtime,
providedProtoSources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("command_line", Type.STRING).mandatory())

/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(plugin_format_flag) -->
If provided, this value will be passed to proto-compiler to use the plugin. The value must
contain a single %s which is replaced with plugin executable.
<code>--plugin=protoc-gen-PLUGIN=<executable>.</code>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("plugin_format_flag", Type.STRING))

/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(plugin) -->
If provided, will be made available to the action that calls the proto-compiler, and will be
passed to the proto-compiler:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ public void commandLine_basic() throws Exception {
ProtoLangToolchainProvider toolchainNoPlugin =
ProtoLangToolchainProvider.create(
"--java_out=param1,param2:%s",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());

ProtoLangToolchainProvider toolchainWithPlugin =
ProtoLangToolchainProvider.create(
"--PLUGIN_pluginName_out=param3,param4:%s",
/* pluginFormatFlag= */ "--plugin=protoc-gen-PLUGIN_pluginName=%s",
plugin,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());
Expand Down Expand Up @@ -175,6 +177,7 @@ public void commandLine_strictDeps() throws Exception {
ProtoLangToolchainProvider toolchain =
ProtoLangToolchainProvider.create(
"--java_out=param1,param2:%s",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());
Expand Down Expand Up @@ -213,6 +216,7 @@ public void commandLine_exports() throws Exception {
ProtoLangToolchainProvider toolchain =
ProtoLangToolchainProvider.create(
"--java_out=param1,param2:%s",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());
Expand Down Expand Up @@ -283,6 +287,7 @@ public String toString() {
ProtoLangToolchainProvider toolchain =
ProtoLangToolchainProvider.create(
"--java_out=param1,param2:%s",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());
Expand Down Expand Up @@ -317,13 +322,15 @@ public void exceptionIfSameName() throws Exception {
ProtoLangToolchainProvider toolchain1 =
ProtoLangToolchainProvider.create(
"dontcare",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());

ProtoLangToolchainProvider toolchain2 =
ProtoLangToolchainProvider.create(
"dontcare",
/* pluginFormatFlag= */ null,
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void setUp() throws Exception {

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

Expand Down Expand Up @@ -73,6 +74,7 @@ public void protoToolchain() throws Exception {
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:denied']",
Expand Down Expand Up @@ -101,6 +103,7 @@ public void protoToolchainBlacklistProtoLibraries() throws Exception {
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:descriptors', '//third_party/x:any']",
Expand Down Expand Up @@ -129,6 +132,7 @@ public void protoToolchainBlacklistTransitiveProtos() throws Exception {
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:any']",
Expand Down

0 comments on commit 45ce3dd

Please sign in to comment.