aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationArgs.java
blob: 145d6467dee9cf3ff033f8566e52cfe57bf18830 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2014 Google Inc. 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;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
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.util.FileType;

import java.util.Collection;

/**
 * A container of Java compilation artifacts.
 */
public final class JavaCompilationArgs {
  // TODO(bazel-team): It would be desirable to use LinkOrderNestedSet here so that
  // parents-before-deps is preserved for graphs that are not trees. However, the legacy
  // JavaLibraryCollector implemented naive link ordering and many targets in the
  // depot depend on the consistency of left-to-right ordering that is not provided by
  // LinkOrderNestedSet. They simply list their local dependencies before
  // other targets that may use conflicting dependencies, and the local deps
  // appear earlier on the classpath, as desired. Behavior of LinkOrderNestedSet
  // can be very unintuitive in case of conflicting orders, because the order is
  // decided by the rightmost branch in such cases. For example, if A depends on {junit4,
  // B}, B depends on {C, D}, C depends on {junit3}, and D depends on {junit4},
  // the classpath of A will have junit3 before junit4.
  private final NestedSet<Artifact> runtimeJars;
  private final NestedSet<Artifact> compileTimeJars;
  private final NestedSet<Artifact> instrumentationMetadata;

  public static final JavaCompilationArgs EMPTY_ARGS = new JavaCompilationArgs(
    NestedSetBuilder.<Artifact>create(Order.NAIVE_LINK_ORDER),
    NestedSetBuilder.<Artifact>create(Order.NAIVE_LINK_ORDER),
    NestedSetBuilder.<Artifact>create(Order.NAIVE_LINK_ORDER));

  private JavaCompilationArgs(NestedSet<Artifact> runtimeJars,
      NestedSet<Artifact> compileTimeJars,
      NestedSet<Artifact> instrumentationMetadata) {
    this.runtimeJars = runtimeJars;
    this.compileTimeJars = compileTimeJars;
    this.instrumentationMetadata = instrumentationMetadata;
  }

  /**
   * Returns transitive runtime jars.
   */
  public NestedSet<Artifact> getRuntimeJars() {
    return runtimeJars;
  }

  /**
   * Returns transitive compile-time jars.
   */
  public NestedSet<Artifact> getCompileTimeJars() {
    return compileTimeJars;
  }

  /**
   * Returns transitive instrumentation metadata jars.
   */
  public NestedSet<Artifact> getInstrumentationMetadata() {
    return instrumentationMetadata;
  }

  /**
   * Returns a new builder instance.
   */
  public static final Builder builder() {
    return new Builder();
  }

  /**
   * Builder for {@link JavaCompilationArgs}.
   *
 *
   */
  public static final class Builder {
    private final NestedSetBuilder<Artifact> runtimeJarsBuilder =
        NestedSetBuilder.naiveLinkOrder();
    private final NestedSetBuilder<Artifact> compileTimeJarsBuilder =
        NestedSetBuilder.naiveLinkOrder();
    private final NestedSetBuilder<Artifact> instrumentationMetadataBuilder =
        NestedSetBuilder.naiveLinkOrder();

    /**
     * Use {@code TransitiveJavaCompilationArgs#builder()} to instantiate the builder.
     */
    private Builder() {
    }

    /**
     * Legacy method for dealing with objects which construct
     * {@link JavaCompilationArtifacts} objects.
     */
    // TODO(bazel-team): Remove when we get rid of JavaCompilationArtifacts.
    public Builder merge(JavaCompilationArtifacts other, boolean isNeverLink) {
      if (!isNeverLink) {
        addRuntimeJars(other.getRuntimeJars());
      }
      addCompileTimeJars(other.getCompileTimeJars());
      addInstrumentationMetadata(other.getInstrumentationMetadata());
      return this;
    }

    /**
     * Legacy method for dealing with objects which construct
     * {@link JavaCompilationArtifacts} objects.
     */
    public Builder merge(JavaCompilationArtifacts other) {
      return merge(other, false);
    }

    public Builder addRuntimeJar(Artifact runtimeJar) {
      this.runtimeJarsBuilder.add(runtimeJar);
      return this;
    }

    public Builder addRuntimeJars(Iterable<Artifact> runtimeJars) {
      this.runtimeJarsBuilder.addAll(runtimeJars);
      return this;
    }

    public Builder addCompileTimeJar(Artifact compileTimeJar) {
      this.compileTimeJarsBuilder.add(compileTimeJar);
      return this;
    }

    public Builder addCompileTimeJars(Iterable<Artifact> compileTimeJars) {
      this.compileTimeJarsBuilder.addAll(compileTimeJars);
      return this;
    }

    public Builder addInstrumentationMetadata(Artifact instrumentationMetadata) {
      this.instrumentationMetadataBuilder.add(instrumentationMetadata);
      return this;
    }

    public Builder addInstrumentationMetadata(Collection<Artifact> instrumentationMetadata) {
      this.instrumentationMetadataBuilder.addAll(instrumentationMetadata);
      return this;
    }

    public Builder addTransitiveCompilationArgs(
        JavaCompilationArgsProvider dep, boolean recursive, ClasspathType type) {
      JavaCompilationArgs args = recursive
          ? dep.getRecursiveJavaCompilationArgs()
          : dep.getJavaCompilationArgs();
      addTransitiveArgs(args, type);
      return this;
    }

    public Builder addTransitiveCompilationArgs(
        SourcesJavaCompilationArgsProvider dep, boolean recursive, ClasspathType type) {
      JavaCompilationArgs args;
      if (recursive) {
        args = dep.getRecursiveJavaCompilationArgs();
      } else {
        args = dep.getJavaCompilationArgs();
      }
      addTransitiveArgs(args, type);
      return this;
    }

    public Builder addSourcesTransitiveCompilationArgs(
        Iterable<? extends SourcesJavaCompilationArgsProvider> deps,
        boolean recursive,
        ClasspathType type) {
      for (SourcesJavaCompilationArgsProvider dep : deps) {
        addTransitiveCompilationArgs(dep, recursive, type);
      }

      return this;
    }

    /**
     * Merges the artifacts of another target.
     */
    public Builder addTransitiveTarget(TransitiveInfoCollection dep, boolean recursive,
        ClasspathType type) {
      JavaCompilationArgsProvider provider = dep.getProvider(JavaCompilationArgsProvider.class);
      if (provider != null) {
        addTransitiveCompilationArgs(provider, recursive, type);
        return this;
      } else {
        NestedSet<Artifact> filesToBuild =
            dep.getProvider(FileProvider.class).getFilesToBuild();
        for (Artifact jar : FileType.filter(filesToBuild, JavaSemantics.JAR)) {
          addCompileTimeJar(jar);
          addRuntimeJar(jar);
        }
      }
      return this;
    }

    /**
     * Merges the artifacts of a collection of targets.
     */
    public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection> deps,
        boolean recursive, ClasspathType type) {
      for (TransitiveInfoCollection dep : deps) {
        addTransitiveTarget(dep, recursive, type);
      }
      return this;
    }

    /**
     * Merges the artifacts of a collection of targets.
     */
    public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection> deps,
        boolean recursive) {
      return addTransitiveTargets(deps, recursive, ClasspathType.BOTH);
    }

    /**
     * Merges the artifacts of a collection of targets.
     */
    public Builder addTransitiveDependencies(Iterable<JavaCompilationArgsProvider> deps,
        boolean recursive) {
      for (JavaCompilationArgsProvider dep : deps) {
        addTransitiveDependency(dep, recursive, ClasspathType.BOTH);
      }
      return this;
    }

    /**
     * Merges the artifacts of another target.
     */
    private Builder addTransitiveDependency(JavaCompilationArgsProvider dep, boolean recursive,
        ClasspathType type) {
      JavaCompilationArgs args = recursive
          ? dep.getRecursiveJavaCompilationArgs()
          : dep.getJavaCompilationArgs();
      addTransitiveArgs(args, type);
      return this;
    }

    /**
     * Merges the artifacts of a collection of targets.
     */
    public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection> deps) {
      return addTransitiveTargets(deps, /*recursive=*/true, ClasspathType.BOTH);
    }

    /**
     * Includes the contents of another instance of JavaCompilationArgs.
     *
     * @param args the JavaCompilationArgs instance
     * @param type the classpath(s) to consider
     */
    public Builder addTransitiveArgs(JavaCompilationArgs args, ClasspathType type) {
      if (!ClasspathType.RUNTIME_ONLY.equals(type)) {
        compileTimeJarsBuilder.addTransitive(args.getCompileTimeJars());
      }
      if (!ClasspathType.COMPILE_ONLY.equals(type)) {
        runtimeJarsBuilder.addTransitive(args.getRuntimeJars());
      }
      instrumentationMetadataBuilder.addTransitive(
          args.getInstrumentationMetadata());
      return this;
    }

    /**
     * Builds a {@link JavaCompilationArgs} object.
     */
    public JavaCompilationArgs build() {
      return new JavaCompilationArgs(
          runtimeJarsBuilder.build(),
          compileTimeJarsBuilder.build(),
          instrumentationMetadataBuilder.build());
    }
  }

  /**
   *  Enum to specify transitive compilation args traversal
   */
  public static enum ClasspathType {
      /* treat the same for compile time and runtime */
      BOTH,

      /* Only include on compile classpath */
      COMPILE_ONLY,

      /* Only include on runtime classpath */
      RUNTIME_ONLY;
  }
}