Search in sources :

Example 1 with JavaCompilationArgsProvider

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.

the class JavaProtoLibrary method create.

@Override
public ConfiguredTarget create(final RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    JavaCompilationArgsProvider dependencyArgsProviders = JavaCompilationArgsProvider.merge(Iterables.<JavaCompilationArgsAspectProvider, JavaCompilationArgsProvider>transform(this.<JavaCompilationArgsAspectProvider>getDeps(ruleContext, JavaCompilationArgsAspectProvider.class), JavaCompilationArgsAspectProvider.GET_PROVIDER));
    if (!StrictDepsUtils.isStrictDepsJavaProtoLibrary(ruleContext)) {
        dependencyArgsProviders = StrictDepsUtils.makeNonStrict(dependencyArgsProviders);
    }
    Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()).addArtifacts(dependencyArgsProviders.getRecursiveJavaCompilationArgs().getRuntimeJars()).build();
    JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.merge(Iterables.<JavaSourceJarsAspectProvider, JavaSourceJarsProvider>transform(this.<JavaSourceJarsAspectProvider>getDeps(ruleContext, JavaSourceJarsAspectProvider.class), JavaSourceJarsAspectProvider.GET_PROVIDER));
    NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
    filesToBuild.addAll(sourceJarsProvider.getSourceJars());
    for (JavaProtoLibraryTransitiveFilesToBuildProvider provider : ruleContext.getPrerequisites("deps", Mode.TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class)) {
        filesToBuild.addTransitive(provider.getJars());
    }
    JavaRuleOutputJarsProvider ruleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().build();
    JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(dependencyArgsProviders);
    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild.build()).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)).addOutputGroup(OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)).add(JavaCompilationArgsProvider.class, dependencyArgsProviders).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).build();
}
Also used : JavaRuleOutputJarsProvider(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider) JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) JavaSkylarkApiProvider(com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) Artifact(com.google.devtools.build.lib.actions.Artifact) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaRunfilesProvider(com.google.devtools.build.lib.rules.java.JavaRunfilesProvider)

Example 2 with JavaCompilationArgsProvider

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.

the class ActionReuser method reuseExistingActions.

/**
   * If the underlying proto_library rule already registers the compile actions we need, just reuse
   * them. This will preserve memory.
   *
   * <p>TODO(b/36191931): Delete when it's time.
   */
public static boolean reuseExistingActions(ConfiguredTarget base, RuleContext ruleContext, ConfiguredAspect.Builder aspect) {
    if (!ruleContext.getConfiguration().getFragment(ProtoConfiguration.class).reuseJavaCompileActionsFromProtoLibrary()) {
        return false;
    }
    ProtoJavaApiInfoProvider javaApi = base.getProvider(ProtoJavaApiInfoProvider.class);
    if (javaApi == null) {
        return false;
    }
    JavaCompilationArgs directJars = javaApi.getJavaCompilationArgsImmutable();
    if (isEmpty(directJars.getCompileTimeJars()) || javaApi.sourceJarImmutable() == null) {
        return false;
    }
    JavaCompilationArgs transitiveJars = JavaCompilationArgs.builder().addTransitiveArgs(javaApi.getTransitiveJavaCompilationArgsImmutable(), BOTH).addTransitiveDependencies(javaApi.getProtoRuntimeImmutable(), true).addTransitiveArgs(directJars, BOTH).build();
    Artifact outputJar = getOnlyElement(directJars.getRuntimeJars());
    Artifact compileTimeJar = getOnlyElement(directJars.getCompileTimeJars());
    Artifact sourceJar = checkNotNull(javaApi.sourceJarImmutable());
    JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(directJars, transitiveJars, NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER), NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER));
    JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(createOutputJarProvider(outputJar, compileTimeJar, sourceJar)).setSourceJarsProvider(createSrcJarProvider(sourceJar)).setCompilationArgsProvider(compilationArgsProvider);
    NestedSet<Artifact> transitiveOutputJars = NestedSetBuilder.fromNestedSets(transform(ruleContext.getPrerequisites("deps", TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class), GET_JARS)).add(outputJar).build();
    aspect.addSkylarkTransitiveInfo(JavaSkylarkApiProvider.PROTO_NAME.getLegacyId(), skylarkApiProvider.build()).addProviders(new JavaProtoLibraryTransitiveFilesToBuildProvider(transitiveOutputJars), new JavaCompilationArgsAspectProvider(compilationArgsProvider));
    return true;
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) JavaSkylarkApiProvider(com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider) ProtoJavaApiInfoProvider(com.google.devtools.build.lib.rules.java.ProtoJavaApiInfoProvider) JavaCompilationArgs(com.google.devtools.build.lib.rules.java.JavaCompilationArgs) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 3 with JavaCompilationArgsProvider

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.

the class JavaLiteProtoLibrary method create.

@Override
public ConfiguredTarget create(final RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    JavaCompilationArgsProvider dependencyArgsProviders = JavaCompilationArgsProvider.merge(Iterables.<JavaCompilationArgsAspectProvider, JavaCompilationArgsProvider>transform(getDeps(ruleContext, JavaCompilationArgsAspectProvider.class), JavaCompilationArgsAspectProvider.GET_PROVIDER));
    if (!StrictDepsUtils.isStrictDepsJavaProtoLibrary(ruleContext)) {
        dependencyArgsProviders = StrictDepsUtils.makeNonStrict(dependencyArgsProviders);
    }
    Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName()).addArtifacts(dependencyArgsProviders.getRecursiveJavaCompilationArgs().getRuntimeJars()).build();
    JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.merge(Iterables.<JavaSourceJarsAspectProvider, JavaSourceJarsProvider>transform(getDeps(ruleContext, JavaSourceJarsAspectProvider.class), JavaSourceJarsAspectProvider.GET_PROVIDER));
    NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.stableOrder();
    filesToBuild.addAll(sourceJarsProvider.getSourceJars());
    for (JavaProtoLibraryTransitiveFilesToBuildProvider provider : ruleContext.getPrerequisites("deps", Mode.TARGET, JavaProtoLibraryTransitiveFilesToBuildProvider.class)) {
        filesToBuild.addTransitive(provider.getJars());
    }
    JavaRuleOutputJarsProvider javaRuleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().build();
    JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(javaRuleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(dependencyArgsProviders);
    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild.build()).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)).addOutputGroup(OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)).add(JavaCompilationArgsProvider.class, dependencyArgsProviders).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)).add(ProguardSpecProvider.class, getJavaLiteRuntimeSpec(ruleContext)).add(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider).build();
}
Also used : JavaRuleOutputJarsProvider(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider) JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) JavaSkylarkApiProvider(com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) JavaRunfilesProvider(com.google.devtools.build.lib.rules.java.JavaRunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaRunfilesProvider(com.google.devtools.build.lib.rules.java.JavaRunfilesProvider)

Example 4 with JavaCompilationArgsProvider

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.

the class SkylarkJavaLiteProtoLibraryTest method testEmptySrcsForJavaApi.

@Test
public void testEmptySrcsForJavaApi() throws Exception {
    ConfiguredTarget target = scratchConfiguredTarget("notbad", "lite_pb2", "load('//tools/build_rules/java_lite_proto_library:java_lite_proto_library.bzl',", "      'java_lite_proto_library')", "java_lite_proto_library(name = 'lite_pb2', deps = [':null_lib'])", "proto_library(name = 'null_lib')");
    JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class, target);
    assertThat(compilationArgsProvider).isNotNull();
    assertThat(compilationArgsProvider.getJavaCompilationArgs()).isNotNull();
    JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class, target);
    assertThat(sourceJarsProvider).isNotNull();
    assertThat(sourceJarsProvider.getSourceJars()).isNotNull();
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Test(org.junit.Test)

Example 5 with JavaCompilationArgsProvider

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider in project bazel by bazelbuild.

the class SkylarkJavaLiteProtoLibraryTest method jplCorrectlyDefinesDirectJars_strictDepsEnabled_aliasProto.

/**
   * Tests that a java_proto_library only provides direct jars corresponding on the proto_library
   * rules it directly depends on, excluding anything that the proto_library rules depends on
   * themselves. This does not concern strict-deps in the compilation of the generated Java code
   * itself, only compilation of regular code in java_library/java_binary and similar rules.
   *
   * <p>Here, a java_lite_proto_library dependes on an alias proto. We make sure that the system
   * behaves as if we depend directly on the aliased proto_library.
   */
@Test
public void jplCorrectlyDefinesDirectJars_strictDepsEnabled_aliasProto() throws Exception {
    scratch.file("x/BUILD", "load('//tools/build_rules/java_lite_proto_library:java_lite_proto_library.bzl',", "          'java_lite_proto_library')", "java_lite_proto_library(name = 'foo_java_proto_lite', deps = [':foo_proto'], ", "    strict_deps = 1)", "proto_library(", "    name = 'foo_proto',", "    deps = [ ':bar_proto' ],", ")", "proto_library(", "    name = 'bar_proto',", "    srcs = [ 'bar.proto' ],", ")");
    JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class, getConfiguredTarget("//x:foo_java_proto_lite"));
    Iterable<String> directJars = prettyJarNames(compilationArgsProvider.getJavaCompilationArgs().getCompileTimeJars());
    assertThat(directJars).containsExactly("x/libbar_proto-lite-hjar.jar");
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) Test(org.junit.Test)

Aggregations

JavaCompilationArgsProvider (com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider)9 Artifact (com.google.devtools.build.lib.actions.Artifact)5 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)4 JavaSkylarkApiProvider (com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider)4 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)4 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)3 JavaRuleOutputJarsProvider (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider)3 Test (org.junit.Test)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)2 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)2 JavaRunfilesProvider (com.google.devtools.build.lib.rules.java.JavaRunfilesProvider)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 Builder (com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder)1 DeployArchiveBuilder (com.google.devtools.build.lib.rules.java.DeployArchiveBuilder)1 JavaCompilationArgs (com.google.devtools.build.lib.rules.java.JavaCompilationArgs)1 JavaGenJarsProvider (com.google.devtools.build.lib.rules.java.JavaGenJarsProvider)1