aboutsummaryrefslogtreecommitdiff
path: root/rules/idl.bzl
blob: 8dc52d335e5a4a2bacb58f8f1cae5f7b79b19d67 (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
# Copyright 2018 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.

"""Bazel Android IDL library for the Android rules."""

load(":java.bzl", _java = "java")
load(":path.bzl", _path = "path")
load(":utils.bzl", _log = "log")

_AIDL_TOOLCHAIN_MISSING_ERROR = (
    "IDL sources provided without the Android IDL toolchain."
)

_AIDL_JAVA_ROOT_UNDETERMINABLE_ERROR = (
    "Cannot determine java/javatests root for import %s."
)

IDLContextInfo = provider(
    doc = "Contains data from processing Android IDL.",
    fields = dict(
        idl_srcs = "List of IDL sources",
        idl_import_root = "IDL import root",
        idl_java_srcs = "List of IDL Java sources",
        idl_deps =
            "List of IDL targets required for Java compilation, Proguard, etc.",
        providers = "The list of all providers to propagate.",
    ),
)

def _gen_java_from_idl(
        ctx,
        out_idl_java_src = None,
        idl_src = None,
        transitive_idl_import_roots = [],
        transitive_idl_imports = [],
        transitive_idl_preprocessed = [],
        aidl = None,
        aidl_lib = None,
        aidl_framework = None):
    args = ctx.actions.args()
    args.add("-b")
    args.add_all(transitive_idl_import_roots, format_each = "-I%s")
    args.add(aidl_framework, format = "-p%s")
    args.add_all(transitive_idl_preprocessed, format_each = "-p%s")
    args.add(idl_src)
    args.add(out_idl_java_src)

    ctx.actions.run(
        executable = aidl,
        arguments = [args],
        inputs = depset(
            [aidl_framework],
            transitive = [
                aidl_lib.files,
                transitive_idl_imports,
                transitive_idl_preprocessed,
            ],
        ),
        outputs = [out_idl_java_src],
        mnemonic = "AndroidIDLGenerate",
        progress_message = "Android IDL generation %s" % idl_src.path,
    )

def _get_idl_import_root_path(
        package,
        idl_import_root,
        idl_file_root_path):
    package_path = _path.relative(
        idl_file_root_path,
        package,
    )
    return _path.relative(
        package_path,
        idl_import_root,
    )

def _collect_unique_idl_import_root_paths(
        package,
        idl_import_root,
        idl_imports):
    idl_import_roots = dict()
    for idl_import in idl_imports:
        idl_import_roots[_get_idl_import_root_path(
            package,
            idl_import_root,
            idl_import.root.path,
        )] = True
    return sorted(idl_import_roots.keys())

def _collect_unique_java_roots(idl_imports):
    idl_import_roots = dict()
    for idl_import in idl_imports:
        java_root = _java.root(idl_import.path)
        if not java_root:
            _log.error(_AIDL_JAVA_ROOT_UNDETERMINABLE_ERROR % idl_import.path)
        idl_import_roots[java_root] = True
    return sorted(idl_import_roots.keys())

def _determine_idl_import_roots(
        package,
        idl_import_root = None,
        idl_imports = []):
    if idl_import_root == None:
        return _collect_unique_java_roots(idl_imports)
    return _collect_unique_idl_import_root_paths(
        package,
        idl_import_root,
        idl_imports,
    )

def _process(
        ctx,
        idl_srcs = [],
        idl_parcelables = [],
        idl_import_root = None,
        idl_preprocessed = [],
        deps = [],
        exports = [],
        aidl = None,
        aidl_lib = None,
        aidl_framework = None):
    """Processes Android IDL.

    Args:
      ctx: The context.
      idl_srcs: sequence of Files. A list of the aidl source files to be
        processed into Java source files and then compiled. Optional.
      idl_parcelables: sequence of Files. A list of Android IDL definitions to
        supply as imports. These files will be made available as imports for any
        android_library target that depends on this library, directly or via its
        transitive closure, but will not be translated to Java or compiled.

        Only .aidl files that correspond directly to .java sources in this library
        should be included (e.g. custom implementations of Parcelable), otherwise
        idl_srcs should be used.

        These files must be placed appropriately for the aidl compiler to find
        them. See the description of idl_import_root for information about what
        this means. Optional.
      idl_import_root: string. Package-relative path to the root of the java
        package tree containing idl sources included in this library. This path
        will be used as the import root when processing idl sources that depend on
        this library.

        When idl_import_root is specified, both idl_parcelables and idl_srcs must
        be at the path specified by the java package of the object they represent
        under idl_import_root. When idl_import_root is not specified, both
        idl_parcelables and idl_srcs must be at the path specified by their
        package under a Java root. Optional.
      idl_preprocessed: sequence of Files. A list of preprocessed Android IDL
        definitions to supply as imports. These files will be made available as
        imports for any android_library target that depends on this library,
        directly or via its transitive closure, but will not be translated to
        Java or compiled.

        Only preprocessed .aidl files that correspond directly to .java sources
        in this library should be included (e.g. custom implementations of
        Parcelable), otherwise use idl_srcs for Android IDL definitions that
        need to be translated to Java interfaces and use idl_parcelable for
        non-preprcessed AIDL files. Optional.
      deps: sequence of Targets. A list of dependencies. Optional.
      exports: sequence of Targets. A list of exports. Optional.
      aidl: Target. A target pointing to the aidl executable to be used for
        Java code generation from *.idl source files. Optional, unless idl_srcs
        are supplied.
      aidl_lib: Target. A target pointing to the aidl_lib library required
        during Java compilation when Java code is generated from idl sources.
        Optional, unless idl_srcs are supplied.
      aidl_framework: Target. A target pointing to the aidl framework. Optional,
        unless idl_srcs are supplied.

    Returns:
      A IDLContextInfo provider.
    """
    if idl_srcs and not (aidl and aidl_lib and aidl_framework):
        _log.error(_AIDL_TOOLCHAIN_MISSING_ERROR)

    transitive_idl_import_roots = []
    transitive_idl_imports = []
    transitive_idl_preprocessed = []
    for dep in deps + exports:
        transitive_idl_import_roots.append(dep.transitive_idl_import_roots)
        transitive_idl_imports.append(dep.transitive_idl_imports)
        transitive_idl_preprocessed.append(dep.transitive_idl_preprocessed)

    idl_java_srcs = []
    for idl_src in idl_srcs:
        idl_java_src = ctx.actions.declare_file(
            ctx.label.name + "_aidl/" + idl_src.path.replace(".aidl", ".java"),
        )
        idl_java_srcs.append(idl_java_src)
        _gen_java_from_idl(
            ctx,
            out_idl_java_src = idl_java_src,
            idl_src = idl_src,
            transitive_idl_import_roots = depset(
                _determine_idl_import_roots(
                    ctx.label.package,
                    idl_import_root,
                    idl_parcelables + idl_srcs,
                ),
                transitive = transitive_idl_import_roots,
                order = "preorder",
            ),
            transitive_idl_imports = depset(
                idl_parcelables + idl_srcs,
                transitive = transitive_idl_imports,
                order = "preorder",
            ),
            transitive_idl_preprocessed = depset(
                transitive = transitive_idl_preprocessed,
            ),
            aidl = aidl,
            aidl_lib = aidl_lib,
            aidl_framework = aidl_framework,
        )

    return IDLContextInfo(
        idl_srcs = idl_srcs,
        idl_import_root = idl_import_root,
        idl_java_srcs = idl_java_srcs,
        idl_deps = [aidl_lib] if idl_java_srcs else [],
        providers = [
            # TODO(b/146216105): Make this a Starlark provider.
            AndroidIdlInfo(
                depset(
                    _determine_idl_import_roots(
                        ctx.label.package,
                        idl_import_root,
                        idl_parcelables + idl_srcs + idl_preprocessed,
                    ),
                    transitive = transitive_idl_import_roots,
                    order = "preorder",
                ),
                depset(
                    idl_parcelables + idl_srcs + idl_preprocessed,
                    transitive = transitive_idl_imports,
                    order = "preorder",
                ),
                depset(),  # TODO(b/146216105): Delete this field once in Starlark.
                depset(idl_preprocessed, transitive = transitive_idl_preprocessed),
            ),
        ],
    )

idl = struct(
    process = _process,
)

# Visible for testing.
testing = struct(
    get_idl_import_root_path = _get_idl_import_root_path,
)