aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/proto/ActionReuser.java
blob: 3dec6f02864df3ed80e6321b740d11375acd8a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2017 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.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Iterables.isEmpty;
import static com.google.common.collect.Iterables.transform;
import static com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode.TARGET;
import static com.google.devtools.build.lib.rules.java.JavaCompilationArgs.ClasspathType.BOTH;
import static com.google.devtools.build.lib.rules.java.proto.JavaProtoLibraryTransitiveFilesToBuildProvider.GET_JARS;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgs;
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.JavaRuleOutputJarsProvider;
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.java.ProtoJavaApiInfoProvider;

public class ActionReuser {

  /**
   * 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) {
    ProtoJavaApiInfoProvider javaApi = base.getProvider(ProtoJavaApiInfoProvider.class);
    if (javaApi == null) {
      return false;
    }

    JavaCompilationArtifacts directJars = javaApi.getJavaCompilationArtifactsImmutable();
    if (isEmpty(directJars.getCompileTimeJars()) || javaApi.sourceJarImmutable() == null) {
      return false;
    }

    JavaCompilationArgs transitiveJars =
        JavaCompilationArgs.builder()
            .addTransitiveArgs(javaApi.getTransitiveJavaCompilationArgsImmutable(), BOTH)
            .addTransitiveDependencies(javaApi.getProtoRuntimeImmutable(), true /* recursive */)
            .merge(directJars)
            .build();

    Artifact outputJar = getOnlyElement(directJars.getRuntimeJars());
    Artifact compileTimeJar = getOnlyElement(directJars.getCompileTimeJars());
    Artifact sourceJar = checkNotNull(javaApi.sourceJarImmutable());

    JavaCompilationArgsProvider compilationArgsProvider =
        JavaCompilationArgsProvider.create(
            JavaCompilationArgs.builder().merge(directJars).build(),
            transitiveJars,
            NestedSetBuilder.create(
                Order.STABLE_ORDER, directJars.getCompileTimeDependencyArtifact()),
            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;
  }

  private static JavaRuleOutputJarsProvider createOutputJarProvider(
      Artifact outputJar, Artifact compileTimeJar, Artifact sourceJar) {
    return JavaRuleOutputJarsProvider.builder()
        .addOutputJar(outputJar, compileTimeJar, ImmutableList.of(sourceJar))
        .build();
  }

  private static JavaSourceJarsProvider createSrcJarProvider(Artifact sourceJar) {
    return JavaSourceJarsProvider.create(
        NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER),
        NestedSetBuilder.<Artifact>stableOrder().add(sourceJar).build());
  }
}