aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java
blob: b9e9d70968294c20a6563a17a0c85a3973941f6c (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
// Copyright 2016 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 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.analysis.skylark.SkylarkApiProvider;
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.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import javax.annotation.Nullable;

/**
 * A class that exposes the Android providers to Skylark. It is intended to provide a simple and
 * stable interface for Skylark users.
 */
@SkylarkModule(
  name = "AndroidSkylarkApiProvider",
  title = "android",
  category = SkylarkModuleCategory.PROVIDER,
  doc =
      "Provides access to information about Android rules. Every Android-related target provides "
          + "this struct, accessible as a <code>android</code> field on a "
          + "<a href=\"Target.html\">target</a>."
)
@Immutable
public class AndroidSkylarkApiProvider extends SkylarkApiProvider {
  /** The name of the field in Skylark used to access this class. */
  public static final String NAME = "android";

  private final IdlInfo idlInfo = new IdlInfo();

  @SkylarkCallable(
    name = "apk",
    structField = true,
    allowReturnNones = true,
    doc = "Returns an APK produced by this target."
  )
  public Artifact getApk() {
    return getIdeInfoProvider().getSignedApk();
  }

  private AndroidIdeInfoProvider getIdeInfoProvider() {
    return getInfo().getProvider(AndroidIdeInfoProvider.class);
  }

  @SkylarkCallable(
    name = "java_package",
    structField = true,
    allowReturnNones = true,
    doc = "Returns a java package for this target."
  )
  public String getJavaPackage() {
    return getIdeInfoProvider().getJavaPackage();
  }

  @SkylarkCallable(
    name = "manifest",
    structField = true,
    allowReturnNones = true,
    doc = "Returns a manifest file for this target."
  )
  public Artifact getManifest() {
    return getIdeInfoProvider().getManifest();
  }

  @SkylarkCallable(
    name = "merged_manifest",
    structField = true,
    allowReturnNones = true,
    doc = "Returns a manifest file for this target after all processing, e.g.: merging, etc."
  )
  public Artifact getMergedManifest() {
    return getIdeInfoProvider().getGeneratedManifest();
  }

  @SkylarkCallable(
    name = "native_libs",
    structField = true,
    doc =
        "Returns the native libraries as a dictionary of the libraries' architecture as a string "
            + "to a set of the native library files, or the empty dictionary if there are no "
            + "native libraries."
  )
  public ImmutableMap<String, NestedSet<Artifact>> getNativeLibs() {
    return getIdeInfoProvider().getNativeLibs();
  }

  @SkylarkCallable(
    name = "resource_apk",
    structField = true,
    doc = "Returns the resources container for the target."
  )
  public Artifact getResourceApk() {
    return getIdeInfoProvider().getResourceApk();
  }

  @SkylarkCallable(
    name = "apks_under_test",
    structField = true,
    allowReturnNones = true,
    doc = "Returns a collection of APKs that this target tests."
  )
  public ImmutableCollection<Artifact> getApksUnderTest() {
    return getIdeInfoProvider().getApksUnderTest();
  }

  @SkylarkCallable(
    name = "defines_resources",
    structField = true,
    doc = "Returns <code>True</code> if the target defines any Android resources directly."
  )
  public boolean definesAndroidResources() {
    return getIdeInfoProvider().definesAndroidResources();
  }

  @SkylarkCallable(
    name = "idl",
    structField = true,
    doc = "Returns information about IDL files associated with this target."
  )
  public IdlInfo getIdlInfo() {
    return idlInfo;
  }

  @SkylarkCallable(
    name = "resources",
    structField = true,
    doc = "Returns resources defined by this target."
  )
  public NestedSet<Artifact> getResources() {
    return collectDirectArtifacts(ResourceType.RESOURCES);
  }

  @SkylarkCallable(
    name = "resource_jar",
    structField = true,
    allowReturnNones = true,
    doc = "Returns a jar file for classes generated from resources."
  )
  @Nullable
  public JavaRuleOutputJarsProvider.OutputJar getResourceJar() {
    return getIdeInfoProvider().getResourceJar();
  }

  @SkylarkCallable(
    name = "aar",
    structField = true,
    allowReturnNones = true,
    doc = "Returns the aar output of this target."
  )
  public Artifact getAar() {
    return getIdeInfoProvider().getAar();
  }

  private NestedSet<Artifact> collectDirectArtifacts(final ResourceType resources) {
    AndroidResourcesProvider provider = getInfo().getProvider(AndroidResourcesProvider.class);
    if (provider == null) {
      return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
    }
    // This will iterate over all (direct) resources. If this turns out to be a performance
    // problem, {@link ResourceContainer#getArtifacts} can be changed to return NestedSets.
    return NestedSetBuilder.wrap(
        Order.STABLE_ORDER,
        Iterables.concat(
            Iterables.transform(
                provider.getDirectAndroidResources(),
                (ResourceContainer resourceContainer) ->
                    resourceContainer.getArtifacts(resources))));
  }

  /** Helper class to provide information about IDLs related to this rule. */
  @SkylarkModule(
    name = "AndroidSkylarkIdlInfo",
    category = SkylarkModuleCategory.NONE,
    doc = "Provides access to information about Android rules."
  )
  @Immutable
  public class IdlInfo {
    @SkylarkCallable(
      name = "import_root",
      structField = true,
      allowReturnNones = true,
      doc = "Returns the root of IDL packages if not the java root."
    )
    public String getImportRoot() {
      return getIdeInfoProvider().getIdlImportRoot();
    }

    @SkylarkCallable(name = "sources", structField = true, doc = "Returns a list of IDL files.")
    public ImmutableCollection<Artifact> getSources() {
      return getIdeInfoProvider().getIdlSrcs();
    }

    @SkylarkCallable(
      name = "generated_java_files",
      structField = true,
      doc = "Returns a list Java files generated from IDL sources."
    )
    public ImmutableCollection<Artifact> getIdlGeneratedJavaFiles() {
      return getIdeInfoProvider().getIdlGeneratedJavaFiles();
    }

    @SkylarkCallable(
      name = "output",
      structField = true,
      allowReturnNones = true,
      doc = "Returns a jar file for classes generated from IDL sources."
    )
    @Nullable
    public JavaRuleOutputJarsProvider.OutputJar getIdlOutput() {
      if (getIdeInfoProvider().getIdlClassJar() == null) {
        return null;
      }

      Artifact idlSourceJar = getIdeInfoProvider().getIdlSourceJar();
      return new OutputJar(
          getIdeInfoProvider().getIdlClassJar(),
          null,
          idlSourceJar == null ? ImmutableList.<Artifact>of() : ImmutableList.of(idlSourceJar));
    }
  }
}