• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Common methods for use by the IntelliJ Aspect."""
16
17load(":java.bzl", _java = "java")
18load(":utils.bzl", _utils = "utils")
19
20def _extract_idl_jars(
21        ctx,
22        idl_java_srcs = [],
23        jar = None,
24        manifest_proto = None,
25        out_srcjar = None,
26        out_jar = None,
27        idlclass = None,
28        host_javabase = None):
29    """Extracts the idl class and src jars."""
30    args = ctx.actions.args()
31    args.add("--class_jar", jar)
32    args.add("--manifest_proto", manifest_proto)
33    args.add("--output_class_jar", out_jar)
34    args.add("--output_source_jar", out_srcjar)
35    args.add("--temp_dir", out_jar.dirname)
36    args.add_all(idl_java_srcs)
37
38    _java.run(
39        ctx = ctx,
40        host_javabase = host_javabase,
41        executable = idlclass,
42        arguments = [args],
43        inputs = idl_java_srcs + [jar, manifest_proto],
44        outputs = [out_srcjar, out_jar],
45        mnemonic = "AndroidIdlJars",
46        progress_message = "Building idl jars %s" % out_jar.path,
47    )
48
49def _make_android_ide_info(
50        ctx,
51        idl_ctx = None,
52        resources_ctx = None,
53        defines_resources = False,
54        java_package = None,
55        manifest = None,
56        merged_manifest = None,
57        resources_apk = None,
58        idl_import_root = None,
59        idl_srcs = [],
60        idl_java_srcs = [],
61        java_info = None,
62        r_jar = None,
63        signed_apk = None,
64        aar = None,
65        apks_under_test = [],
66        native_libs = dict(),
67        idlclass = None,
68        host_javabase = None):
69    # TODO(b/154513292): Clean up bad usages of context objects.
70    if idl_ctx:
71        idl_import_root = idl_ctx.idl_import_root
72        idl_srcs = idl_ctx.idl_srcs
73        idl_java_srcs = idl_ctx.idl_java_srcs
74    if resources_ctx:
75        defines_resources = resources_ctx.defines_resources
76        merged_manifest = resources_ctx.merged_manifest
77        resources_apk = resources_ctx.resources_apk
78
79    if not defines_resources:
80        java_package = None
81        merged_manifest = None
82
83    # Extracts idl related classes from the jar and creates a src jar
84    # for the idl generated java.
85    idl_jar = None
86    idl_srcjar = None
87
88    # TODO(djwhang): JavaInfo.outputs.jar.manifest_proto is not created by
89    # Kotlin compile. Determine if this is the same manifest_proto produced
90    # by turbine, this could be pulled during annotation processing.
91    jar = _utils.only(java_info.outputs.jars)
92    if idl_java_srcs and jar.manifest_proto:
93        idl_jar = ctx.actions.declare_file("lib%s-idl.jar" % ctx.label.name)
94        idl_srcjar = \
95            ctx.actions.declare_file("lib%s-idl.srcjar" % ctx.label.name)
96
97        jar = _utils.only(java_info.outputs.jars)
98        _extract_idl_jars(
99            ctx,
100            idl_java_srcs = idl_java_srcs,
101            jar = jar.class_jar,
102            manifest_proto = jar.manifest_proto,
103            out_jar = idl_jar,
104            out_srcjar = idl_srcjar,
105            idlclass = idlclass,
106            host_javabase = host_javabase,
107        )
108
109    return AndroidIdeInfo(
110        java_package,
111        manifest,
112        merged_manifest,
113        idl_import_root,
114        idl_srcs,
115        idl_java_srcs,
116        idl_srcjar,
117        idl_jar,
118        defines_resources,
119        r_jar,
120        resources_apk,
121        signed_apk,
122        aar,
123        apks_under_test,
124        native_libs,
125    )
126
127def _make_legacy_android_provider(android_ide_info):
128    # Create the ClassJar "object" for the target.android.idl.output field.
129    if android_ide_info.idl_class_jar:
130        idl_class_jar = struct(
131            class_jar = android_ide_info.idl_class_jar,
132            ijar = None,
133            source_jar = android_ide_info.idl_source_jar,
134        )
135    else:
136        idl_class_jar = None
137
138    return struct(
139        aar = android_ide_info.aar,
140        apk = android_ide_info.signed_apk,
141        apks_under_test = android_ide_info.apks_under_test,
142        defines_resources = android_ide_info.defines_android_resources,
143        idl = struct(
144            import_root = android_ide_info.idl_import_root,
145            sources = android_ide_info.idl_srcs,
146            generated_java_files = android_ide_info.idl_generated_java_files,
147            output = idl_class_jar,
148        ),
149        java_package = android_ide_info.java_package,
150        manifest = android_ide_info.manifest,
151        merged_manifest = android_ide_info.generated_manifest,
152        native_libs = android_ide_info.native_libs,
153        resource_apk = android_ide_info.resource_apk,
154        resource_jar = android_ide_info.resource_jar,
155    )
156
157intellij = struct(
158    make_android_ide_info = _make_android_ide_info,
159    make_legacy_android_provider = _make_legacy_android_provider,
160)
161
162# Only visible for testing.
163testing = struct(
164    extract_idl_jars = _extract_idl_jars,
165)
166