Skip to content

Commit

Permalink
Extend proto_lang_toolchain rule with progress_message and mnemonic a…
Browse files Browse the repository at this point in the history
…ttributes.

This is needed for proto_common.generate_sources function.

Two public attributes are added to proto_lang_toolchain rule. Both have defaults, so no immediate migration of targets is needed.

PiperOrigin-RevId: 437714094
  • Loading branch information
comius authored and copybara-github committed Mar 28, 2022
1 parent 66e29cd commit b4587f8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public ConfiguredTarget create(RuleContext ruleContext)
// on memory consumption should be neglectable.
providedProtoSources.build().toList(),
ruleContext.getPrerequisite(":proto_compiler", FilesToRunProvider.class),
ruleContext.getConfiguration().getFragment(ProtoConfiguration.class).protocOpts()))
ruleContext.getConfiguration().getFragment(ProtoConfiguration.class).protocOpts(),
ruleContext.attributes().get("progress_message", Type.STRING),
ruleContext.attributes().get("mnemonic", Type.STRING)))
.setFilesToBuild(NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER))
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public StarlarkList<String> protocOptsForStarlark() {

public abstract ImmutableList<String> protocOpts();

@StarlarkMethod(
name = "progress_message",
doc = "Progress message to set on the proto compiler action.",
structField = true)
public abstract String progressMessage();

@StarlarkMethod(
name = "mnemonic",
doc = "Mnemonic to set on the proto compiler action.",
structField = true)
public abstract String mnemonic();

/**
* This makes the blacklisted_protos member available in the provider. It can be removed after
* users are migrated and a sufficient time for Bazel rules to migrate has elapsed.
Expand All @@ -98,7 +110,9 @@ public static ProtoLangToolchainProvider create(
TransitiveInfoCollection runtime,
ImmutableList<ProtoSource> providedProtoSources,
FilesToRunProvider protoc,
ImmutableList<String> protocOpts) {
ImmutableList<String> protocOpts,
String progressMessage,
String mnemonic) {
NestedSetBuilder<Artifact> blacklistedProtos = NestedSetBuilder.stableOrder();
for (ProtoSource protoSource : providedProtoSources) {
blacklistedProtos.add(protoSource.getOriginalSourceFile());
Expand All @@ -111,6 +125,8 @@ public static ProtoLangToolchainProvider create(
providedProtoSources,
protoc,
protocOpts,
progressMessage,
mnemonic,
blacklistedProtos.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public class ProtoLangToolchainRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
return builder
/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(progress_message) -->
This value will be set as the progress message on protoc action.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("progress_message", Type.STRING).value("Generating proto_library %{label}"))

/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(mnemonic) -->
This value will be set as the mnemonic on protoc action.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("mnemonic", Type.STRING).value("GenProto"))

/* <!-- #BLAZE_RULE(proto_lang_toolchain).ATTRIBUTE(command_line) -->
This value will be passed to proto-compiler to generate the code. Only include the parts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ public void commandLine_basic() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");
ProtoLangToolchainProvider toolchainWithPlugin =
ProtoLangToolchainProvider.create(
"--PLUGIN_pluginName_out=param3,param4:%s",
Expand All @@ -138,7 +140,9 @@ public void commandLine_basic() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");
useConfiguration("--strict_proto_deps=OFF");

RuleContext ruleContext =
Expand Down Expand Up @@ -206,7 +210,9 @@ public void commandLine_strictDeps() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");

RuleContext ruleContext =
getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
Expand Down Expand Up @@ -244,7 +250,9 @@ public void commandLine_exports() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");
useConfiguration("--strict_proto_deps=OFF");

RuleContext ruleContext =
Expand Down Expand Up @@ -317,7 +325,9 @@ public void outReplacementAreLazilyEvaluated() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");

RuleContext ruleContext =
getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
Expand Down Expand Up @@ -353,7 +363,9 @@ public void exceptionIfSameName() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");
ProtoLangToolchainProvider toolchain2 =
ProtoLangToolchainProvider.create(
"dontcare=%s",
Expand All @@ -362,7 +374,9 @@ public void exceptionIfSameName() throws Exception {
/* runtime= */ mock(TransitiveInfoCollection.class),
/* providedProtoSources= */ ImmutableList.of(),
/* protoc= */ FilesToRunProvider.EMPTY,
/* protocOpts= */ ImmutableList.of());
/* protocOpts= */ ImmutableList.of(),
/* progressMessage = */ "",
/* mnemonic= */ "");

RuleContext ruleContext =
getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ private void validateProtoLangToolchain(ProtoLangToolchainProvider toolchain) th
Label protoc = Label.parseAbsoluteUnchecked(ProtoConstants.DEFAULT_PROTOC_LABEL);
assertThat(toolchain.protoc().getExecutable().prettyPrint())
.isEqualTo(protoc.toPathFragment().getPathString());

assertThat(toolchain.progressMessage()).isEqualTo("Progress Message %{label}");
assertThat(toolchain.mnemonic()).isEqualTo("MyMnemonic");
}

@Test
Expand All @@ -83,7 +86,9 @@ public void protoToolchain() throws Exception {
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:denied']",
" blacklisted_protos = ['//third_party/x:denied'],",
" progress_message = 'Progress Message %{label}',",
" mnemonic = 'MyMnemonic',",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
Expand Down Expand Up @@ -112,7 +117,9 @@ public void protoToolchainBlacklistProtoLibraries() throws Exception {
" 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']",
" blacklisted_protos = ['//third_party/x:descriptors', '//third_party/x:any'],",
" progress_message = 'Progress Message %{label}',",
" mnemonic = 'MyMnemonic',",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
Expand Down Expand Up @@ -141,7 +148,9 @@ public void protoToolchainBlacklistTransitiveProtos() throws Exception {
" plugin_format_flag = '--plugin=%s',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:any']",
" blacklisted_protos = ['//third_party/x:any'],",
" progress_message = 'Progress Message %{label}',",
" mnemonic = 'MyMnemonic',",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
Expand Down Expand Up @@ -169,5 +178,6 @@ public void optionalFieldsAreEmpty() throws Exception {
assertThat(toolchain.runtime()).isNull();
assertThat(toolchain.blacklistedProtos().toList()).isEmpty();
assertThat(toolchain.forbiddenProtos().toList()).isEmpty();
assertThat(toolchain.mnemonic()).isEqualTo("GenProto");
}
}

0 comments on commit b4587f8

Please sign in to comment.