Search in sources :

Example 1 with JavaCompilationArgs

use of com.google.devtools.build.lib.rules.java.JavaCompilationArgs 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 2 with JavaCompilationArgs

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

the class SkylarkJavaLiteProtoLibraryTest method testJavacOpts.

/** Protobufs should always be compiled with the default and proto javacopts. */
@Test
public void testJavacOpts() throws Exception {
    ConfiguredTarget rule = scratchConfiguredTarget("x", "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 = [':proto_lib'])", "proto_library(name = 'proto_lib',", "              srcs = ['input1.proto', 'input2.proto'])");
    JavaCompilationArgs compilationArgs = getProvider(JavaCompilationArgsProvider.class, rule).getJavaCompilationArgs();
    assertThat(compilationArgs.getInstrumentationMetadata()).isEmpty();
    JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class, rule);
    assertThat(sourceJarsProvider).isNotNull();
    assertThat(prettyJarNames(sourceJarsProvider.getSourceJars())).containsExactly("x/proto_lib-lite-src.jar");
    ImmutableListMultimap<String, Artifact> runtimeJars = Multimaps.index(compilationArgs.getRuntimeJars(), ROOT_RELATIVE_PATH_STRING);
    Artifact jar = Iterables.getOnlyElement(runtimeJars.get("x/libproto_lib-lite.jar"));
    JavaCompileAction action = (JavaCompileAction) getGeneratingAction(jar);
    List<String> commandLine = ImmutableList.copyOf(action.buildCommandLine());
    assertThat(commandLine).contains("-protoMarkerForTest");
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) JavaCompilationArgs(com.google.devtools.build.lib.rules.java.JavaCompilationArgs) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Artifact(com.google.devtools.build.lib.actions.Artifact) JavaCompileAction(com.google.devtools.build.lib.rules.java.JavaCompileAction) Test(org.junit.Test)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)2 JavaCompilationArgs (com.google.devtools.build.lib.rules.java.JavaCompilationArgs)2 JavaCompilationArgsProvider (com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 JavaCompileAction (com.google.devtools.build.lib.rules.java.JavaCompileAction)1 JavaSkylarkApiProvider (com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider)1 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)1 ProtoJavaApiInfoProvider (com.google.devtools.build.lib.rules.java.ProtoJavaApiInfoProvider)1 Test (org.junit.Test)1