diff options
author | elenairina <elenairina@google.com> | 2018-02-12 06:15:34 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-12 06:17:26 -0800 |
commit | b65de60ab464fd3c9d46ca5760e309db2fcc7fa7 (patch) | |
tree | bb5ecd13c6773af3d9f3488c0f14a1c93a308f10 /src/main/java/com/google/devtools | |
parent | c7e343ad314aeab6283b5939bc8126a112140a11 (diff) |
Move common proto aspect logic to a new class.
PiperOrigin-RevId: 185369902
Diffstat (limited to 'src/main/java/com/google/devtools')
7 files changed, 248 insertions, 157 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD index 270c8a8340..196e2a3d96 100644 --- a/src/main/java/com/google/devtools/build/lib/BUILD +++ b/src/main/java/com/google/devtools/build/lib/BUILD @@ -811,6 +811,7 @@ java_library( "rules/java/proto/JavaLiteProtoAspect.java", "rules/java/proto/JavaLiteProtoLibrary.java", "rules/java/proto/JavaProtoAspect.java", + "rules/java/proto/JavaProtoAspectCommon.java", "rules/java/proto/JavaProtoLibrary.java", "rules/java/proto/JavaProtoLibraryAspectProvider.java", "rules/java/proto/JavaProtoSkylarkCommon.java", diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java index 6b3c8b9863..f8e5725c59 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java @@ -19,7 +19,6 @@ import static com.google.devtools.build.lib.packages.Aspect.INJECTING_RULE_KIND_ import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; -import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.PROTO_TOOLCHAIN_ATTR; import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.getProtoToolchainLabel; import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; @@ -36,6 +35,7 @@ import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.rules.java.JavaConfiguration; import com.google.devtools.build.lib.rules.java.JavaInfo; import com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoLibrary; +import com.google.devtools.build.lib.rules.java.proto.JavaProtoAspectCommon; import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; /** Declaration of the {@code java_lite_proto_library} rule. */ @@ -68,7 +68,7 @@ public class BazelJavaLiteProtoLibraryRule implements RuleDefinition { .aspect(javaProtoAspect, aspectParameters)) .add(attr("strict_deps", BOOLEAN).value(true).undocumented("for migration")) .add( - attr(PROTO_TOOLCHAIN_ATTR, LABEL) + attr(JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, LABEL) .mandatoryNativeProviders( ImmutableList.<Class<? extends TransitiveInfoProvider>>of( ProtoLangToolchainProvider.class)) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/DexArchiveAspect.java b/src/main/java/com/google/devtools/build/lib/rules/android/DexArchiveAspect.java index 6677f09a67..92eaa877f5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/DexArchiveAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/DexArchiveAspect.java @@ -60,7 +60,7 @@ import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider; import com.google.devtools.build.lib.rules.java.JavaCompilationInfoProvider; import com.google.devtools.build.lib.rules.java.JavaInfo; import com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider; -import com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect; +import com.google.devtools.build.lib.rules.java.proto.JavaProtoAspectCommon; import com.google.devtools.build.lib.rules.java.proto.JavaProtoLibraryAspectProvider; import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; import com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider; @@ -115,7 +115,7 @@ public final class DexArchiveAspect extends NativeAspectClass implements Configu ":android_sdk", "aidl_lib", // for the aidl runtime in the android_sdk rule // To get from proto_library through proto_lang_toolchain rule to proto runtime library. - JavaLiteProtoAspect.PROTO_TOOLCHAIN_ATTR, "runtime"); + JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, "runtime"); private static final FlagMatcher DEXOPTS_SUPPORTED_IN_DEXBUILDER = new FlagMatcher( diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java index a59282a6bc..d486b0e756 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java @@ -16,7 +16,6 @@ package com.google.devtools.build.lib.rules.java.proto; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.getOnlyElement; -import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET; import static com.google.devtools.build.lib.cmdline.Label.parseAbsoluteUnchecked; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; @@ -28,12 +27,10 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ConfiguredAspect; import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory; import com.google.devtools.build.lib.analysis.RuleContext; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMap; import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMapBuilder; import com.google.devtools.build.lib.analysis.WrappingProvider; -import com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; @@ -46,11 +43,7 @@ import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault; import com.google.devtools.build.lib.packages.NativeAspectClass; 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.JavaCompilationHelper; import com.google.devtools.build.lib.rules.java.JavaConfiguration; -import com.google.devtools.build.lib.rules.java.JavaInfo; -import com.google.devtools.build.lib.rules.java.JavaLibraryHelper; import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo; import com.google.devtools.build.lib.rules.java.JavaSemantics; @@ -68,8 +61,6 @@ import javax.annotation.Nullable; /** An Aspect which JavaLiteProtoLibrary injects to build Java Lite protos. */ public class JavaLiteProtoAspect extends NativeAspectClass implements ConfiguredAspectFactory { - public static final String PROTO_TOOLCHAIN_ATTR = ":aspect_proto_toolchain_for_javalite"; - public static LabelLateBoundDefault<?> getProtoToolchainLabel(String defaultValue) { return LabelLateBoundDefault.fromTargetConfiguration( ProtoConfiguration.class, @@ -105,7 +96,9 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured checkNotNull(ctatBase.getConfiguredTarget().getProvider(ProtoSupportDataProvider.class)) .getSupportData(); - Impl impl = new Impl(ruleContext, supportData, javaSemantics); + JavaProtoAspectCommon aspectCommon = + JavaProtoAspectCommon.getLiteInstance(ruleContext, javaSemantics); + Impl impl = new Impl(ruleContext, supportData, aspectCommon); impl.addProviders(aspect); return aspect.build(); @@ -121,7 +114,7 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured .advertiseProvider(JavaProtoLibraryAspectProvider.class) .advertiseProvider(ImmutableList.of(JavaSkylarkApiProvider.PROTO_NAME)) .add( - attr(PROTO_TOOLCHAIN_ATTR, LABEL) + attr(JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, LABEL) .mandatoryNativeProviders( ImmutableList.<Class<? extends TransitiveInfoProvider>>of( ProtoLangToolchainProvider.class)) @@ -156,14 +149,16 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured */ private final JavaCompilationArgsProvider dependencyCompilationArgs; - private final JavaSemantics javaSemantics; + private final JavaProtoAspectCommon aspectCommon; private final Iterable<JavaProtoLibraryAspectProvider> javaProtoLibraryAspectProviders; Impl( - final RuleContext ruleContext, final SupportData supportData, JavaSemantics javaSemantics) { + RuleContext ruleContext, + SupportData supportData, + JavaProtoAspectCommon aspectCommon) { this.ruleContext = ruleContext; this.supportData = supportData; - this.javaSemantics = javaSemantics; + this.aspectCommon = aspectCommon; this.javaProtoLibraryAspectProviders = ruleContext.getPrerequisites( "deps", RuleConfiguredTarget.Mode.TARGET, JavaProtoLibraryAspectProvider.class); @@ -190,11 +185,12 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured new TransitiveInfoProviderMapBuilder(); if (supportData.hasProtoSources()) { - Artifact sourceJar = getSourceJarArtifact(); + Artifact sourceJar = aspectCommon.getSourceJarArtifact(); createProtoCompileAction(sourceJar); - Artifact outputJar = getOutputJarArtifact(); + Artifact outputJar = aspectCommon.getOutputJarArtifact(); - generatedCompilationArgsProvider = createJavaCompileAction(sourceJar, outputJar); + generatedCompilationArgsProvider = + aspectCommon.createJavaCompileAction(sourceJar, outputJar, dependencyCompilationArgs); NestedSet<Artifact> javaSourceJars = NestedSetBuilder.<Artifact>stableOrder().add(sourceJar).build(); @@ -221,7 +217,8 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured } javaProvidersBuilder.add(generatedCompilationArgsProvider); - javaProvidersBuilder.add(createCcLinkParamsStore(ruleContext, getProtoRuntimeDeps())); + javaProvidersBuilder.add(createCcLinkParamsStore( + ruleContext, aspectCommon.getProtoRuntimeDeps())); TransitiveInfoProviderMap javaProviders = javaProvidersBuilder.build(); aspect @@ -235,7 +232,7 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured createNonStrictCompilationArgsProvider( javaProtoLibraryAspectProviders, generatedCompilationArgsProvider.getJavaCompilationArgs(), - getProtoRuntimeDeps()))); + aspectCommon.getProtoRuntimeDeps()))); } private void createProtoCompileAction(Artifact sourceJar) { @@ -243,7 +240,8 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured ruleContext, ImmutableList.of( new ProtoCompileActionBuilder.ToolchainInvocation( - "javalite", getProtoToolchainProvider(), sourceJar.getExecPathString())), + "javalite", aspectCommon.getProtoToolchainProvider(), + sourceJar.getExecPathString())), supportData.getDirectProtoSources(), supportData.getTransitiveImports(), supportData.getProtosInDirectDeps(), @@ -252,53 +250,5 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured "JavaLite", true /* allowServices */); } - - private JavaCompilationArgsProvider createJavaCompileAction( - Artifact sourceJar, Artifact outputJar) { - JavaLibraryHelper helper = - new JavaLibraryHelper(ruleContext) - .setOutput(outputJar) - .addSourceJars(sourceJar) - .setJavacOpts(ProtoJavacOpts.constructJavacOpts(ruleContext)); - helper.addDep(dependencyCompilationArgs).setCompilationStrictDepsMode(StrictDepsMode.OFF); - for (TransitiveInfoCollection t : getProtoRuntimeDeps()) { - JavaCompilationArgsProvider provider = - JavaInfo.getProvider(JavaCompilationArgsProvider.class, t); - if (provider != null) { - helper.addDep(provider); - } - } - - JavaCompilationArtifacts artifacts = - helper.build( - javaSemantics, - JavaCompilationHelper.getJavaToolchainProvider(ruleContext), - JavaRuntimeInfo.forHost(ruleContext), - JavaCompilationHelper.getInstrumentationJars(ruleContext), - JavaRuleOutputJarsProvider.builder(), - /*createOutputSourceJar*/ false, - /*outputSourceJar=*/ null); - return helper.buildCompilationArgsProvider( - artifacts, /*isReportedAsStrict=*/ true, /*isNeverlink=*/ false); - } - - private ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() { - TransitiveInfoCollection runtime = getProtoToolchainProvider().runtime(); - return runtime != null ? ImmutableList.of(runtime) : ImmutableList.of(); - } - - private ProtoLangToolchainProvider getProtoToolchainProvider() { - return checkNotNull( - ruleContext.getPrerequisite( - PROTO_TOOLCHAIN_ATTR, TARGET, ProtoLangToolchainProvider.class)); - } - - private Artifact getSourceJarArtifact() { - return ruleContext.getGenfilesArtifact(ruleContext.getLabel().getName() + "-lite-src.jar"); - } - - private Artifact getOutputJarArtifact() { - return ruleContext.getBinArtifact("lib" + ruleContext.getLabel().getName() + "-lite.jar"); - } } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java index 4b27b73dfa..e92896e168 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java @@ -14,10 +14,8 @@ package com.google.devtools.build.lib.rules.java.proto; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET; import static com.google.devtools.build.lib.collect.nestedset.Order.STABLE_ORDER; -import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.PROTO_TOOLCHAIN_ATTR; import static com.google.devtools.build.lib.rules.java.proto.JplCcLinkParams.createCcLinkParamsStore; import static com.google.devtools.build.lib.rules.java.proto.StrictDepsUtils.constructJcapFromAspectDeps; @@ -45,7 +43,6 @@ import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider; import com.google.devtools.build.lib.rules.java.ProguardLibrary; import com.google.devtools.build.lib.rules.java.ProguardSpecProvider; import com.google.devtools.build.lib.rules.java.ProtoJavaApiInfoAspectProvider; -import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; /** Implementation of the java_lite_proto_library rule. */ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory { @@ -114,7 +111,8 @@ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory { NestedSet<Artifact> specs = new ProguardLibrary(ruleContext).collectProguardSpecs(ImmutableMultimap.<Mode, String>of()); - TransitiveInfoCollection runtime = getProtoToolchainProvider(ruleContext).runtime(); + TransitiveInfoCollection runtime = + JavaProtoAspectCommon.getLiteProtoToolchainProvider(ruleContext).runtime(); if (runtime == null) { return new ProguardSpecProvider(specs); } @@ -129,10 +127,4 @@ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory { .addTransitive(specProvider.getTransitiveProguardSpecs()) .build()); } - - private ProtoLangToolchainProvider getProtoToolchainProvider(RuleContext ruleContext) { - return checkNotNull( - ruleContext.getPrerequisite( - PROTO_TOOLCHAIN_ATTR, TARGET, ProtoLangToolchainProvider.class)); - } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java index ed35597aa4..93f8482048 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java @@ -16,7 +16,6 @@ package com.google.devtools.build.lib.rules.java.proto; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.getOnlyElement; -import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET; import static com.google.devtools.build.lib.cmdline.Label.parseAbsoluteUnchecked; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; @@ -28,11 +27,9 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ConfiguredAspect; import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory; import com.google.devtools.build.lib.analysis.RuleContext; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMap; import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMapBuilder; import com.google.devtools.build.lib.analysis.WrappingProvider; -import com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; @@ -45,19 +42,14 @@ import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault; import com.google.devtools.build.lib.packages.NativeAspectClass; import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider; -import com.google.devtools.build.lib.rules.java.JavaCompilationHelper; import com.google.devtools.build.lib.rules.java.JavaConfiguration; -import com.google.devtools.build.lib.rules.java.JavaInfo; -import com.google.devtools.build.lib.rules.java.JavaLibraryHelper; import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; -import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo; import com.google.devtools.build.lib.rules.java.JavaSemantics; import com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider; import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider; import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder; import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.ToolchainInvocation; import com.google.devtools.build.lib.rules.proto.ProtoConfiguration; -import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; import com.google.devtools.build.lib.rules.proto.ProtoSourceFileBlacklist; import com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider; import com.google.devtools.build.lib.rules.proto.ProtoSupportDataProvider; @@ -68,7 +60,6 @@ import javax.annotation.Nullable; /** An Aspect which JavaProtoLibrary injects to build Java SPEED protos. */ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspectFactory { - private static final String SPEED_PROTO_TOOLCHAIN_ATTR = ":aspect_java_proto_toolchain"; private final LabelLateBoundDefault<?> hostJdkAttribute; private static LabelLateBoundDefault<?> getSpeedProtoToolchainLabel(String defaultValue) { @@ -112,7 +103,9 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe checkNotNull(ctatBase.getConfiguredTarget().getProvider(ProtoSupportDataProvider.class)) .getSupportData(); - Impl impl = new Impl(ruleContext, supportData, javaSemantics, rpcSupport); + JavaProtoAspectCommon aspectCommon = + JavaProtoAspectCommon.getSpeedInstance(ruleContext, javaSemantics, rpcSupport); + Impl impl = new Impl(ruleContext, supportData, aspectCommon, rpcSupport); if (impl.shouldGenerateCode() && ActionReuser.reuseExistingActions(ctatBase.getConfiguredTarget(), ruleContext, aspect)) { return aspect.build(); @@ -131,7 +124,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe .advertiseProvider(JavaProtoLibraryAspectProvider.class) .advertiseProvider(ImmutableList.of(JavaSkylarkApiProvider.PROTO_NAME)) .add( - attr(SPEED_PROTO_TOOLCHAIN_ATTR, LABEL) + attr(JavaProtoAspectCommon.SPEED_PROTO_TOOLCHAIN_ATTR, LABEL) // TODO(carmi): reinstate mandatoryNativeProviders(ProtoLangToolchainProvider) // once it's in a Bazel release. .legacyAllowAnyFileType() @@ -160,7 +153,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe private final SupportData supportData; private final RpcSupport rpcSupport; - private final JavaSemantics javaSemantics; + private final JavaProtoAspectCommon aspectCommon; /** * Compilation-args from all dependencies, merged together. This is typically the input to a @@ -171,14 +164,14 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe private final Iterable<JavaProtoLibraryAspectProvider> javaProtoLibraryAspectProviders; Impl( - final RuleContext ruleContext, - final SupportData supportData, - JavaSemantics javaSemantics, + RuleContext ruleContext, + SupportData supportData, + JavaProtoAspectCommon aspectCommon, RpcSupport rpcSupport) { this.ruleContext = ruleContext; this.supportData = supportData; - this.javaSemantics = javaSemantics; this.rpcSupport = rpcSupport; + this.aspectCommon = aspectCommon; this.javaProtoLibraryAspectProviders = ruleContext.getPrerequisites( "deps", RuleConfiguredTarget.Mode.TARGET, JavaProtoLibraryAspectProvider.class); @@ -205,11 +198,12 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe new TransitiveInfoProviderMapBuilder(); if (shouldGenerateCode()) { - Artifact sourceJar = getSourceJarArtifact(); + Artifact sourceJar = aspectCommon.getSourceJarArtifact(); createProtoCompileAction(sourceJar); - Artifact outputJar = getOutputJarArtifact(); + Artifact outputJar = aspectCommon.getOutputJarArtifact(); - generatedCompilationArgsProvider = createJavaCompileAction(sourceJar, outputJar); + generatedCompilationArgsProvider = + aspectCommon.createJavaCompileAction(sourceJar, outputJar, dependencyCompilationArgs); NestedSet<Artifact> javaSourceJars = NestedSetBuilder.<Artifact>stableOrder().add(sourceJar).build(); @@ -236,7 +230,8 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe } javaProvidersBuilder.add(generatedCompilationArgsProvider); - javaProvidersBuilder.add(createCcLinkParamsStore(ruleContext, getProtoRuntimeDeps())); + javaProvidersBuilder.add(createCcLinkParamsStore( + ruleContext, aspectCommon.getProtoRuntimeDeps())); TransitiveInfoProviderMap javaProviders = javaProvidersBuilder.build(); aspect .addSkylarkTransitiveInfo( @@ -249,7 +244,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe createNonStrictCompilationArgsProvider( javaProtoLibraryAspectProviders, generatedCompilationArgsProvider.getJavaCompilationArgs(), - getProtoRuntimeDeps()))); + aspectCommon.getProtoRuntimeDeps()))); } /** @@ -263,7 +258,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe final ProtoSourceFileBlacklist protoBlackList; NestedSetBuilder<Artifact> blacklistedProtos = NestedSetBuilder.stableOrder(); - blacklistedProtos.addTransitive(getProtoToolchainProvider().blacklistedProtos()); + blacklistedProtos.addTransitive(aspectCommon.getProtoToolchainProvider().blacklistedProtos()); blacklistedProtos.addTransitive(rpcSupport.getBlacklist(ruleContext)); protoBlackList = new ProtoSourceFileBlacklist(ruleContext, blacklistedProtos.build()); @@ -275,7 +270,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe ImmutableList.Builder<ToolchainInvocation> invocations = ImmutableList.builder(); invocations.add( new ToolchainInvocation( - "java", checkNotNull(getProtoToolchainProvider()), sourceJar.getExecPathString())); + "java", aspectCommon.getProtoToolchainProvider(), sourceJar.getExecPathString())); invocations.addAll(rpcSupport.getToolchainInvocation(ruleContext, sourceJar)); ProtoCompileActionBuilder.registerActions( ruleContext, @@ -288,57 +283,5 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe "Java (Immutable)", rpcSupport.allowServices(ruleContext)); } - - private JavaCompilationArgsProvider createJavaCompileAction( - Artifact sourceJar, Artifact outputJar) { - JavaLibraryHelper helper = - new JavaLibraryHelper(ruleContext) - .setOutput(outputJar) - .addSourceJars(sourceJar) - .setJavacOpts(ProtoJavacOpts.constructJavacOpts(ruleContext)); - helper.addDep(dependencyCompilationArgs).setCompilationStrictDepsMode(StrictDepsMode.OFF); - for (TransitiveInfoCollection t : getProtoRuntimeDeps()) { - JavaCompilationArgsProvider provider = - JavaInfo.getProvider(JavaCompilationArgsProvider.class, t); - if (provider != null) { - helper.addDep(provider); - } - } - - return helper.buildCompilationArgsProvider( - helper.build( - javaSemantics, - JavaCompilationHelper.getJavaToolchainProvider(ruleContext), - JavaRuntimeInfo.forHost(ruleContext), - JavaCompilationHelper.getInstrumentationJars(ruleContext), - JavaRuleOutputJarsProvider.builder(), - /*createOutputSourceJar*/ false, - /*outputSourceJar=*/ null), - /*isReportedAsStrict=*/ true, - /*isNeverlink=*/ false); - } - - private ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() { - ImmutableList.Builder<TransitiveInfoCollection> result = ImmutableList.builder(); - TransitiveInfoCollection runtime = getProtoToolchainProvider().runtime(); - if (runtime != null) { - result.add(runtime); - } - result.addAll(rpcSupport.getRuntimes(ruleContext)); - return result.build(); - } - - private ProtoLangToolchainProvider getProtoToolchainProvider() { - return ruleContext.getPrerequisite( - SPEED_PROTO_TOOLCHAIN_ATTR, TARGET, ProtoLangToolchainProvider.class); - } - - private Artifact getSourceJarArtifact() { - return ruleContext.getGenfilesArtifact(ruleContext.getLabel().getName() + "-speed-src.jar"); - } - - private Artifact getOutputJarArtifact() { - return ruleContext.getBinArtifact("lib" + ruleContext.getLabel().getName() + "-speed.jar"); - } } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java new file mode 100644 index 0000000000..101101bb25 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspectCommon.java @@ -0,0 +1,205 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.rules.java.proto; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.RuleContext; +import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; +import com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode; +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.JavaCompilationHelper; +import com.google.devtools.build.lib.rules.java.JavaInfo; +import com.google.devtools.build.lib.rules.java.JavaLibraryHelper; +import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; +import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo; +import com.google.devtools.build.lib.rules.java.JavaSemantics; +import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; + +/** + * Common logic used by java*_proto aspects (e.g. {@link JavaLiteProtoAspect}). + */ +public class JavaProtoAspectCommon { + + // The name of an attribute of {@link JavaProtoAspect} used for storing the {@link Label} of + // the proto toolchain for Java. + public static final String SPEED_PROTO_TOOLCHAIN_ATTR = ":aspect_java_proto_toolchain"; + // The name of an attribute of {@link JavaLiteProtoLibrary} and {@link JavaLiteProtoAspect} used + // for storing the {@link Label} of the proto toolchain for Java-lite. + public static final String LITE_PROTO_TOOLCHAIN_ATTR = ":aspect_proto_toolchain_for_javalite"; + + private static final String SPEED_JAR_SUFFIX = "-speed"; + private static final String LITE_JAR_SUFFIX = "-lite"; + private static final String MUTABLE_JAR_SUFFIX = "-new-mutable"; + + private final RuleContext ruleContext; + private final JavaSemantics javaSemantics; + private final String protoToolchainAttr; + private final String jarSuffix; + private final RpcSupport rpcSupport; + + /** + * Returns a {@link JavaProtoAspectCommon} instance that handles logic for + * {@code java_proto_library}. + */ + static JavaProtoAspectCommon getSpeedInstance( + RuleContext ruleContext, JavaSemantics javaSemantics, RpcSupport rpcSupport) { + return new JavaProtoAspectCommon( + ruleContext, + javaSemantics, + rpcSupport, + SPEED_PROTO_TOOLCHAIN_ATTR, + SPEED_JAR_SUFFIX); + } + + /** + * Returns a {@link JavaProtoAspectCommon} instance that handles logic for + * {@code java_lite_proto_library}. + */ + static JavaProtoAspectCommon getLiteInstance( + RuleContext ruleContext, JavaSemantics javaSemantics) { + return new JavaProtoAspectCommon( + ruleContext, + javaSemantics, + null, + LITE_PROTO_TOOLCHAIN_ATTR, + LITE_JAR_SUFFIX); + } + + /** + * Returns a {@link JavaProtoAspectCommon} instance that handles logic for + * {@code java_mutable_proto_library}. + */ + public static JavaProtoAspectCommon getMutableInstance( + RuleContext ruleContext, + JavaSemantics javaSemantics, + RpcSupport rpcSupport, + String protoToolchainAttr) { + return new JavaProtoAspectCommon( + ruleContext, + javaSemantics, + rpcSupport, + protoToolchainAttr, + MUTABLE_JAR_SUFFIX); + } + + private JavaProtoAspectCommon( + RuleContext ruleContext, + JavaSemantics javaSemantics, + RpcSupport rpcSupport, + String protoToolchainAttr, + String jarSuffix) { + this.ruleContext = ruleContext; + this.javaSemantics = javaSemantics; + this.protoToolchainAttr = protoToolchainAttr; + this.jarSuffix = jarSuffix; + this.rpcSupport = rpcSupport; + } + + /** + * Registers an action that compiles the given {@code sourceJar} and archives the compiled classes + * into {@code outputJar}, using {@code dep} as information about the dependencies compilation. + * + * @return a {@JavaCompilationArgsProvider} wrapping information about the compilation action + * that was registered. + */ + public JavaCompilationArgsProvider createJavaCompileAction( + Artifact sourceJar, Artifact outputJar, JavaCompilationArgsProvider dep) { + JavaLibraryHelper helper = + new JavaLibraryHelper(ruleContext) + .setOutput(outputJar) + .addSourceJars(sourceJar) + .setJavacOpts(ProtoJavacOpts.constructJavacOpts(ruleContext)); + helper.addDep(dep).setCompilationStrictDepsMode(StrictDepsMode.OFF); + for (TransitiveInfoCollection t : getProtoRuntimeDeps()) { + JavaCompilationArgsProvider provider = + JavaInfo.getProvider(JavaCompilationArgsProvider.class, t); + if (provider != null) { + helper.addDep(provider); + } + } + + JavaCompilationArtifacts artifacts = + helper.build( + javaSemantics, + JavaCompilationHelper.getJavaToolchainProvider(ruleContext), + JavaRuntimeInfo.forHost(ruleContext), + JavaCompilationHelper.getInstrumentationJars(ruleContext), + JavaRuleOutputJarsProvider.builder(), + /*createOutputSourceJar*/ false, + /*outputSourceJar=*/ null); + return helper.buildCompilationArgsProvider( + artifacts, /*isReportedAsStrict=*/ true, /*isNeverlink=*/ false); + } + + /** + * Returns a list of all the target needed by proto libraries (e.g. {@code java_proto_library}) at + * runtime. + */ + public ImmutableList<TransitiveInfoCollection> getProtoRuntimeDeps() { + ImmutableList.Builder<TransitiveInfoCollection> result = ImmutableList.builder(); + TransitiveInfoCollection runtime = getProtoToolchainProvider().runtime(); + if (runtime != null) { + result.add(runtime); + } + if (rpcSupport != null) { + result.addAll(rpcSupport.getRuntimes(ruleContext)); + } + return result.build(); + } + + /** + * Returns the toolchain that specifies how to generate code from {@code .proto} files. + */ + public ProtoLangToolchainProvider getProtoToolchainProvider() { + return checkNotNull(ruleContext.getPrerequisite( + protoToolchainAttr, TARGET, ProtoLangToolchainProvider.class)); + } + + /** + * Returns the toolchain that specifies how to generate Java-lite code from {@code .proto} files. + */ + static ProtoLangToolchainProvider getLiteProtoToolchainProvider(RuleContext ruleContext) { + return ruleContext.getPrerequisite( + LITE_PROTO_TOOLCHAIN_ATTR, TARGET, ProtoLangToolchainProvider.class); + } + + /** + * Returns an {@link Artifact} corresponding to a source jar. Its name is computed from the label + * name and the library type of the current instance. For example, if the instance is created with + * {@link getLiteInstance} the name of the jar will be "<label>-lite-src.jar". + * + * The {@link Artifact} will be created in the bazel-genfiles directory. + */ + public Artifact getSourceJarArtifact() { + return ruleContext.getGenfilesArtifact( + ruleContext.getLabel().getName() + jarSuffix + "-src.jar"); + } + + /** + * Returns an {@link Artifact} corresponding to an output jar. Its name is computed from the label + * name and the library type of the current instance. For example, if the instance is created with + * {@link getLiteInstance} the name of the jar will be "lib<label>-lite.jar". + * + * The {@link Artifact} will be created in the bazel-bin directory. + */ + public Artifact getOutputJarArtifact() { + return ruleContext.getBinArtifact( + "lib" + ruleContext.getLabel().getName() + jarSuffix + ".jar"); + } +} |