aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/AndroidIdeInfoProvider.java
blob: 59dc6414dbd9c5b48f284997b1e19f29974042e6 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2015 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.android;

import static com.google.devtools.build.lib.rules.android.AndroidSkylarkData.fromNoneable;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.skylarkbuildapi.android.AndroidIdeInfoProviderApi;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

/** An Android target provider to provide Android-specific info to IDEs. */
@Immutable
public final class AndroidIdeInfoProvider extends NativeInfo
    implements AndroidIdeInfoProviderApi<Artifact, OutputJar> {

  public static final String PROVIDER_NAME = "AndroidIdeInfo";
  public static final Provider PROVIDER = new Provider();

  /** Builder for {@link AndroidIdeInfoProvider} */
  public static class Builder {
    private Artifact manifest = null;
    private Artifact generatedManifest = null;
    private Artifact apk = null;
    private Artifact resourceApk = null;
    private Artifact idlClassJar = null;
    private Artifact idlSourceJar = null;
    private OutputJar resourceJar = null;
    private String javaPackage = null;
    private String idlImportRoot = null;
    private final Set<Artifact> idlSrcs = new LinkedHashSet<>();
    private final Set<Artifact> idlGeneratedJavaFiles = new LinkedHashSet<>();
    private final Set<Artifact> apksUnderTest = new LinkedHashSet<>();
    private boolean definesAndroidResources;
    private Artifact aar = null;
    private Map<String, NestedSet<Artifact>> nativeLibs = null;

    public AndroidIdeInfoProvider build() {
      return new AndroidIdeInfoProvider(
          javaPackage,
          idlImportRoot,
          manifest,
          generatedManifest,
          apk,
          idlClassJar,
          idlSourceJar,
          resourceJar,
          definesAndroidResources,
          aar,
          ImmutableList.copyOf(idlSrcs),
          ImmutableList.copyOf(idlGeneratedJavaFiles),
          ImmutableList.copyOf(apksUnderTest),
          nativeLibs != null
              ? ImmutableMap.copyOf(nativeLibs)
              : ImmutableMap.<String, NestedSet<Artifact>>of(),
          resourceApk);
    }

    public Builder setJavaPackage(String javaPackage) {
      this.javaPackage = javaPackage;
      return this;
    }

    public Builder setDefinesAndroidResources(boolean definesAndroidResources) {
      this.definesAndroidResources = definesAndroidResources;
      return this;
    }

    public Builder setApk(Artifact apk) {
      Preconditions.checkState(this.apk == null);
      this.apk = apk;
      return this;
    }

    public Builder setManifest(Artifact manifest) {
      Preconditions.checkState(this.manifest == null);
      this.manifest = manifest;
      return this;
    }

    public Builder setGeneratedManifest(Artifact manifest) {
      Preconditions.checkState(this.generatedManifest == null);
      this.generatedManifest = manifest;
      return this;
    }

    public Builder setIdlClassJar(@Nullable Artifact idlClassJar) {
      Preconditions.checkState(this.idlClassJar == null);
      this.idlClassJar = idlClassJar;
      return this;
    }

    public Builder setIdlSourceJar(@Nullable Artifact idlSourceJar) {
      Preconditions.checkState(this.idlSourceJar == null);
      this.idlSourceJar = idlSourceJar;
      return this;
    }

    public Builder setResourceJar(OutputJar resourceJar) {
      this.resourceJar = resourceJar;
      return this;
    }

    public Builder setResourceApk(Artifact resourceApk) {
      this.resourceApk = resourceApk;
      return this;
    }

    public Builder setAar(Artifact aar) {
      this.aar = aar;
      return this;
    }

    public Builder setNativeLibs(Map<String, NestedSet<Artifact>> nativeLibs) {
      this.nativeLibs = nativeLibs;
      return this;
    }

    public Builder addIdlImportRoot(String idlImportRoot) {
      this.idlImportRoot = idlImportRoot;
      return this;
    }

    /** Add "idl_srcs" contents. */
    public Builder addIdlSrcs(Collection<Artifact> idlSrcs) {
      this.idlSrcs.addAll(idlSrcs);
      return this;
    }

    /** Add the java files generated from "idl_srcs". */
    public Builder addIdlGeneratedJavaFiles(Collection<Artifact> idlGeneratedJavaFiles) {
      this.idlGeneratedJavaFiles.addAll(idlGeneratedJavaFiles);
      return this;
    }

    public Builder addAllApksUnderTest(Iterable<Artifact> apks) {
      Iterables.addAll(apksUnderTest, apks);
      return this;
    }
  }

  private final String javaPackage;
  private final String idlImportRoot;
  private final Artifact manifest;
  private final Artifact generatedManifest;
  private final Artifact signedApk;
  @Nullable private final Artifact idlClassJar;
  @Nullable private final Artifact idlSourceJar;
  @Nullable private final OutputJar resourceJar;
  @Nullable private final Artifact resourceApk;
  private final boolean definesAndroidResources;
  private final Artifact aar;
  private final ImmutableCollection<Artifact> idlSrcs;
  private final ImmutableCollection<Artifact> idlGeneratedJavaFiles;
  private final ImmutableCollection<Artifact> apksUnderTest;
  private final ImmutableMap<String, NestedSet<Artifact>> nativeLibs;

  public AndroidIdeInfoProvider(
      String javaPackage,
      String idlImportRoot,
      @Nullable Artifact manifest,
      @Nullable Artifact generatedManifest,
      @Nullable Artifact signedApk,
      @Nullable Artifact idlClassJar,
      @Nullable Artifact idlSourceJar,
      @Nullable OutputJar resourceJar,
      boolean definesAndroidResources,
      @Nullable Artifact aar,
      ImmutableCollection<Artifact> idlSrcs,
      ImmutableCollection<Artifact> idlGeneratedJavaFiles,
      ImmutableCollection<Artifact> apksUnderTest,
      ImmutableMap<String, NestedSet<Artifact>> nativeLibs,
      Artifact resourceApk) {
    super(PROVIDER);
    this.javaPackage = javaPackage;
    this.idlImportRoot = idlImportRoot;
    this.manifest = manifest;
    this.generatedManifest = generatedManifest;
    this.signedApk = signedApk;
    this.idlClassJar = idlClassJar;
    this.idlSourceJar = idlSourceJar;
    this.resourceJar = resourceJar;
    this.definesAndroidResources = definesAndroidResources;
    this.aar = aar;
    this.idlSrcs = idlSrcs;
    this.idlGeneratedJavaFiles = idlGeneratedJavaFiles;
    this.apksUnderTest = apksUnderTest;
    this.nativeLibs = nativeLibs;
    this.resourceApk = resourceApk;
  }

  @Override
  public String getJavaPackage() {
    return javaPackage;
  }

  @Override
  @Nullable
  public Artifact getManifest() {
    return manifest;
  }

  @Override
  @Nullable
  public Artifact getGeneratedManifest() {
    return generatedManifest;
  }

  @Override
  public boolean definesAndroidResources() {
    return this.definesAndroidResources;
  }

  @Override
  @Nullable
  public String getIdlImportRoot() {
    return idlImportRoot;
  }

  @Override
  @Nullable
  public Artifact getSignedApk() {
    return signedApk;
  }

  @Override
  @Nullable
  public Artifact getIdlClassJar() {
    return idlClassJar;
  }

  @Override
  @Nullable
  public Artifact getIdlSourceJar() {
    return idlSourceJar;
  }

  @Override
  @Nullable
  public OutputJar getResourceJar() {
    return resourceJar;
  }

  @Override
  @Nullable
  public Artifact getAar() {
    return aar;
  }

  @Override
  @Nullable
  public Artifact getResourceApk() {
    return resourceApk;
  }

  @Override
  public ImmutableCollection<Artifact> getIdlSrcs() {
    return idlSrcs;
  }

  @Override
  public ImmutableCollection<Artifact> getIdlGeneratedJavaFiles() {
    return idlGeneratedJavaFiles;
  }

  @Override
  public ImmutableCollection<Artifact> getApksUnderTest() {
    return apksUnderTest;
  }

  public ImmutableMap<String, NestedSet<Artifact>> getNativeLibs() {
    return nativeLibs;
  }

  @Override
  public ImmutableMap<String, SkylarkNestedSet> getNativeLibsSkylark() {
    ImmutableMap.Builder<String, SkylarkNestedSet> builder = ImmutableMap.builder();
    for (Map.Entry<String, NestedSet<Artifact>> entry : getNativeLibs().entrySet()) {
      builder.put(entry.getKey(), SkylarkNestedSet.of(Artifact.class, entry.getValue()));
    }
    return builder.build();
  }

  /** Provider class for {@link AndroidIdeInfoProvider} objects. */
  public static class Provider extends BuiltinProvider<AndroidIdeInfoProvider>
      implements AndroidIdeInfoProviderApi.Provider<Artifact, OutputJar> {
    private Provider() {
      super(PROVIDER_NAME, AndroidIdeInfoProvider.class);
    }

    @Override
    public AndroidIdeInfoProvider createInfo(
        String javaPackage,
        Object manifest,
        Object generatedManifest,
        String idlImportRoot,
        SkylarkList<Artifact> idlSrcs,
        SkylarkList<Artifact> idlGeneratedJavaFiles,
        Object idlSourceJar,
        Object idlClassJar,
        boolean definesAndroidResources,
        Object resourceJar,
        Artifact resourceApk,
        Object signedApk,
        Object aar,
        SkylarkList<Artifact> apksUnderTest,
        SkylarkDict<String, SkylarkNestedSet> nativeLibs)
        throws EvalException {
      Map<String, SkylarkNestedSet> nativeLibsMap =
          nativeLibs.getContents(String.class, SkylarkNestedSet.class, "native_libs");

      ImmutableMap.Builder<String, NestedSet<Artifact>> builder = ImmutableMap.builder();
      for (Map.Entry<String, SkylarkNestedSet> entry : nativeLibsMap.entrySet()) {
        builder.put(entry.getKey(), entry.getValue().getSet(Artifact.class));
      }
      return new AndroidIdeInfoProvider(
          javaPackage,
          idlImportRoot,
          fromNoneable(manifest, Artifact.class),
          fromNoneable(generatedManifest, Artifact.class),
          fromNoneable(signedApk, Artifact.class),
          fromNoneable(idlClassJar, Artifact.class),
          fromNoneable(idlSourceJar, Artifact.class),
          fromNoneable(resourceJar, OutputJar.class),
          definesAndroidResources,
          fromNoneable(aar, Artifact.class),
          idlSrcs.getImmutableList(),
          idlGeneratedJavaFiles.getImmutableList(),
          apksUnderTest.getImmutableList(),
          builder.build(),
          resourceApk);
    }
  }
}