Skip to content

Commit

Permalink
Automated rollback of commit ae349e9.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

This CL breaks proto_common.bzl file which seems that can't load providers.bzl file.

*** Original change description ***

Export ProtoLangToolchainInfo provider and flip proto_lang_toolchain rule

I’ve exported ProtoLangToolchainInfo provider from it’s native class by adding two new functions: one that’s creating starlark provider (create function), and the other that’s wrapping the starlark provider as a native one (get function).

Also, in all locations where native ProtoLangToolchainInfo provider was used/created, I’ve modified it with previously mentioned functions.

I’ve also replaced native ProtoL...

***

PiperOrigin-RevId: 446171449
  • Loading branch information
kotlaja authored and copybara-github committed May 3, 2022
1 parent b2cbc9a commit 5a6b1a8
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import com.google.devtools.build.lib.rules.proto.BazelProtoLibraryRule;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
import com.google.devtools.build.lib.rules.proto.ProtoInfo;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainRule;
import com.google.devtools.build.lib.rules.python.PyInfo;
import com.google.devtools.build.lib.rules.python.PyRuleClasses.PySymlink;
Expand Down Expand Up @@ -291,6 +292,8 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
new StarlarkAspectStub(),
new ProviderStub());
builder.addStarlarkBootstrap(bootstrap);
builder.addStarlarkBuiltinsInternal(
"ProtoLangToolchainInfo", ProtoLangToolchainProvider.PROVIDER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
.aspect(javaProtoAspect))
.add(
attr(JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, LABEL)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER_ID)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER.id())
.value(getProtoToolchainLabel(DEFAULT_PROTO_TOOLCHAIN_LABEL)))
.advertiseStarlarkProvider(StarlarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.google.devtools.build.lib.bazel.rules.java.BazelJavaSemantics;
import com.google.devtools.build.lib.packages.AspectDefinition;
import com.google.devtools.build.lib.packages.AspectParameters;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.rules.java.proto.JavaProtoAspect;
import com.google.devtools.build.lib.rules.java.proto.RpcSupport;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider;

/** An Aspect which BazelJavaProtoLibrary injects to build Java SPEED protos. */
public class BazelJavaProtoAspect extends JavaProtoAspect {
Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean allowServices(RuleContext ruleContext) {
}

@Override
public Optional<StarlarkInfo> getToolchain(RuleContext ruleContext) {
public Optional<ProtoLangToolchainProvider> getToolchain(RuleContext ruleContext) {
return Optional.absent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public AspectDefinition getDefinition(AspectParameters params) {
ImmutableList.of(
// For proto_lang_toolchain rules, where we just want to get at their runtime
// deps.
ImmutableSet.of(ProtoLangToolchainProvider.PROVIDER_ID)))
ImmutableSet.of(ProtoLangToolchainProvider.PROVIDER.id())))
.addToolchainTypes(
ToolchainTypeRequirement.create(
Label.parseAbsoluteUnchecked(toolsRepository + sdkToolchainLabel)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault;
import com.google.devtools.build.lib.packages.NativeAspectClass;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.rules.cpp.AspectLegalCppSemantics;
import com.google.devtools.build.lib.rules.cpp.CcCommon;
Expand Down Expand Up @@ -130,7 +129,7 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
.useToolchainTransition(true)
.add(
attr(PROTO_TOOLCHAIN_ATTR, LABEL)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER_ID)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER.id())
.value(PROTO_TOOLCHAIN_LABEL))
.add(
attr(CcToolchain.CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME, LABEL)
Expand Down Expand Up @@ -289,7 +288,7 @@ private static void checkProtoLibrariesInDeps(

private boolean areSrcsExcluded() throws RuleErrorException, InterruptedException {
return !ProtoCommon.shouldGenerateCode(
ruleContext, protoTarget, getStarlarkProtoToolchainProvider(), "cc_proto_library");
ruleContext, protoTarget, getProtoToolchainProvider(), "cc_proto_library");
}

private FeatureConfiguration getFeatureConfiguration()
Expand Down Expand Up @@ -462,19 +461,15 @@ private void createProtoCompileAction(Collection<Artifact> outputs)
ProtoCommon.compile(
ruleContext,
protoTarget,
getStarlarkProtoToolchainProvider(),
getProtoToolchainProvider(),
outputs,
genfilesPath,
"Generating C++ proto_library %{label}");
}
}

private ProtoLangToolchainProvider getProtoToolchainProvider() {
return ProtoLangToolchainProvider.get(ruleContext, PROTO_TOOLCHAIN_ATTR);
}

private StarlarkInfo getStarlarkProtoToolchainProvider() {
return ProtoLangToolchainProvider.getStarlarkProvider(ruleContext, PROTO_TOOLCHAIN_ATTR);
return ruleContext.getPrerequisite(PROTO_TOOLCHAIN_ATTR, ProtoLangToolchainProvider.PROVIDER);
}

public void addProviders(ConfiguredAspect.Builder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
ImmutableList.of(StarlarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())))
.add(
attr(JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, LABEL)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER_ID)
.mandatoryProviders(ProtoLangToolchainProvider.PROVIDER.id())
.value(getProtoToolchainLabel(defaultProtoToolchainLabel)))
.add(
attr(JavaRuleClasses.JAVA_TOOLCHAIN_ATTRIBUTE_NAME, LABEL)
Expand Down Expand Up @@ -245,7 +245,7 @@ private void createProtoCompileAction(Artifact sourceJar)
ProtoCommon.compile(
ruleContext,
protoTarget,
aspectCommon.getStarlarkProtoToolchainProvider(),
aspectCommon.getProtoToolchainProvider(),
ImmutableList.of(sourceJar),
sourceJar.getExecPathString(),
"Generating JavaLite proto_library %{label}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void addProviders(ConfiguredAspect.Builder aspect)
if (ProtoCommon.shouldGenerateCode(
ruleContext,
protoTarget,
aspectCommon.getStarlarkProtoToolchainProvider(),
aspectCommon.getProtoToolchainProvider(),
"java_proto_library")) {
Artifact sourceJar = aspectCommon.getSourceJarArtifact();
createProtoCompileAction(sourceJar);
Expand Down Expand Up @@ -320,7 +320,7 @@ private void createProtoCompileAction(Artifact sourceJar)
ProtoCommon.compile(
ruleContext,
protoTarget,
aspectCommon.getStarlarkProtoToolchainProvider(),
aspectCommon.getProtoToolchainProvider(),
ImmutableList.of(sourceJar),
/* pluginOutput= */ null,
additionalArgs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
import com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts;
import com.google.devtools.build.lib.rules.java.JavaInfo;
Expand Down Expand Up @@ -160,22 +159,16 @@ public ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() {

/** Returns the toolchain that specifies how to generate code from {@code .proto} files. */
public ProtoLangToolchainProvider getProtoToolchainProvider() {
return checkNotNull(ProtoLangToolchainProvider.get(ruleContext, protoToolchainAttr));
}

/**
* Returns the Starlark toolchain that specifies how to generate code from {@code .proto} files.
*/
public StarlarkInfo getStarlarkProtoToolchainProvider() {
return checkNotNull(
ProtoLangToolchainProvider.getStarlarkProvider(ruleContext, protoToolchainAttr));
ruleContext.getPrerequisite(protoToolchainAttr, ProtoLangToolchainProvider.PROVIDER));
}

/**
* Returns the toolchain that specifies how to generate Java-lite code from {@code .proto} files.
*/
static ProtoLangToolchainProvider getLiteProtoToolchainProvider(RuleContext ruleContext) {
return ProtoLangToolchainProvider.get(ruleContext, LITE_PROTO_TOOLCHAIN_ATTR);
return ruleContext.getPrerequisite(
LITE_PROTO_TOOLCHAIN_ATTR, ProtoLangToolchainProvider.PROVIDER);
}

/**
Expand Down Expand Up @@ -213,9 +206,9 @@ boolean shouldGenerateCode(ConfiguredTarget protoTarget, String ruleName)

boolean shouldGenerate =
ProtoCommon.shouldGenerateCode(
ruleContext, protoTarget, getStarlarkProtoToolchainProvider(), ruleName);
ruleContext, protoTarget, getProtoToolchainProvider(), ruleName);
if (rpcSupport != null) {
Optional<StarlarkInfo> toolchain = rpcSupport.getToolchain(ruleContext);
Optional<ProtoLangToolchainProvider> toolchain = rpcSupport.getToolchain(ruleContext);
if (toolchain.isPresent()) {
if (!ProtoCommon.shouldGenerateCode(ruleContext, protoTarget, toolchain.get(), ruleName)) {
shouldGenerate = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
import com.google.devtools.build.lib.rules.java.JavaInfo;
import com.google.devtools.build.lib.rules.proto.ProtoCommon;
Expand All @@ -50,7 +49,7 @@ public void createProtoCompileAction(
ProtoCommon.compile(
starlarkRuleContext.getRuleContext(),
target,
getStarlarkProtoToolchainProvider(starlarkRuleContext, protoToolchainAttr),
getProtoToolchainProvider(starlarkRuleContext, protoToolchainAttr),
ImmutableList.of(sourceJar),
sourceJar.getExecPathString(),
"Generating JavaLite proto_library %{label}");
Expand Down Expand Up @@ -91,13 +90,6 @@ private static ProtoLangToolchainProvider getProtoToolchainProvider(
StarlarkRuleContext starlarkRuleContext, String protoToolchainAttr) throws EvalException {
ConfiguredTarget javaliteToolchain =
(ConfiguredTarget) checkNotNull(starlarkRuleContext.getAttr().getValue(protoToolchainAttr));
return checkNotNull(ProtoLangToolchainProvider.get(javaliteToolchain));
}

private static StarlarkInfo getStarlarkProtoToolchainProvider(
StarlarkRuleContext starlarkRuleContext, String protoToolchainAttr) throws EvalException {
ConfiguredTarget javaliteToolchain =
(ConfiguredTarget) checkNotNull(starlarkRuleContext.getAttr().getValue(protoToolchainAttr));
return checkNotNull(ProtoLangToolchainProvider.getStarlarkProvider(javaliteToolchain));
return checkNotNull(javaliteToolchain.get(ProtoLangToolchainProvider.PROVIDER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.google.devtools.build.lib.analysis.starlark.Args;
import com.google.devtools.build.lib.packages.AspectDefinition;
import com.google.devtools.build.lib.packages.AspectParameters;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider;
import net.starlark.java.eval.EvalException;

/** Used by java_proto_library to support Google-specific features. */
Expand All @@ -35,7 +35,7 @@ void populateAdditionalArgs(RuleContext ruleContext, Artifact sourceJar, Args ar

boolean allowServices(RuleContext ruleContext);

Optional<StarlarkInfo> getToolchain(RuleContext ruleContext);
Optional<ProtoLangToolchainProvider> getToolchain(RuleContext ruleContext);

ImmutableList<TransitiveInfoCollection> getRuntimes(RuleContext ruleContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.NativeAspectClass;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.AppleToolchain;
Expand Down Expand Up @@ -387,14 +386,13 @@ private ConfiguredAspect java(ConfiguredTarget base, RuleContext ruleContext)
private ConfiguredAspect proto(ConfiguredTarget base, RuleContext ruleContext)
throws InterruptedException, ActionConflictException {
ProtoLangToolchainProvider protoToolchain =
ProtoLangToolchainProvider.get(ruleContext, J2OBJC_PROTO_TOOLCHAIN_ATTR);
StarlarkInfo starlarkProtoToolchain =
ProtoLangToolchainProvider.getStarlarkProvider(ruleContext, J2OBJC_PROTO_TOOLCHAIN_ATTR);
ruleContext.getPrerequisite(
J2OBJC_PROTO_TOOLCHAIN_ATTR, ProtoLangToolchainProvider.PROVIDER);

try {
// Avoid pulling in any generated files from forbidden protos.
ImmutableList<Artifact> filteredProtoSources =
ImmutableList.copyOf(
ProtoCommon.filterSources(ruleContext, base, starlarkProtoToolchain));
ImmutableList.copyOf(ProtoCommon.filterSources(ruleContext, base, protoToolchain));

J2ObjcSource j2ObjcSource = protoJ2ObjcSource(ruleContext, base, filteredProtoSources);

Expand All @@ -405,7 +403,7 @@ private ConfiguredAspect proto(ConfiguredTarget base, RuleContext ruleContext)

directJ2ObjcMappingFileProvider =
createJ2ObjcProtoCompileActions(
base, starlarkProtoToolchain, ruleContext, filteredProtoSources, j2ObjcSource);
base, protoToolchain, ruleContext, filteredProtoSources, j2ObjcSource);
}

return buildAspect(
Expand Down Expand Up @@ -639,7 +637,7 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(

private J2ObjcMappingFileProvider createJ2ObjcProtoCompileActions(
ConfiguredTarget base,
StarlarkInfo protoToolchain,
ProtoLangToolchainProvider protoToolchain,
RuleContext ruleContext,
ImmutableList<Artifact> filteredProtoSources,
J2ObjcSource j2ObjcSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ java_library(
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//third_party:auto_value",
"//third_party:guava",
"//third_party:jsr305",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.devtools.build.lib.concurrent.BlazeInterners;
import com.google.devtools.build.lib.packages.BazelModuleContext;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -196,7 +195,7 @@ public static ImmutableList<Artifact> declareGeneratedFilesPython(
public static void compile(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
StarlarkInfo protoLangToolchainInfo,
ProtoLangToolchainProvider protoLangToolchainInfo,
Iterable<Artifact> generatedFiles,
@Nullable Object pluginOutput,
@Nullable Args additionalArgs,
Expand Down Expand Up @@ -230,7 +229,7 @@ public static void compile(
public static void compile(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
StarlarkInfo protoLangToolchainInfo,
ProtoLangToolchainProvider protoLangToolchainInfo,
Iterable<Artifact> generatedFiles,
@Nullable Object pluginOutput,
String progressMessage)
Expand All @@ -252,7 +251,7 @@ public static void compile(
public static boolean shouldGenerateCode(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
StarlarkInfo protoLangToolchainInfo,
ProtoLangToolchainProvider protoLangToolchainInfo,
String ruleName)
throws RuleErrorException, InterruptedException {
StarlarkFunction shouldGenerateCode =
Expand All @@ -270,7 +269,9 @@ public static boolean shouldGenerateCode(
}

public static Sequence<Artifact> filterSources(
RuleContext ruleContext, ConfiguredTarget protoTarget, StarlarkInfo protoLangToolchainInfo)
RuleContext ruleContext,
ConfiguredTarget protoTarget,
ProtoLangToolchainProvider protoLangToolchainInfo)
throws RuleErrorException, InterruptedException {
StarlarkFunction filterSources =
(StarlarkFunction)
Expand Down
Loading

0 comments on commit 5a6b1a8

Please sign in to comment.