aboutsummaryrefslogtreecommitdiff
path: root/rules/attrs.bzl
blob: a72fd4f710e7b8f04a4c3f44b0f29418a948d8c4 (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
# 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.

"""Common attributes for Android rules."""

load(":utils.bzl", "log")
load(":native_toolchain_attrs.bzl", "ANDROID_SDK_TOOLCHAIN_TYPE_DEFAULT")

def _add(attrs, *others):
    new = {}
    new.update(attrs)
    for o in others:
        for name in o.keys():
            if name in new:
                log.error("Attr '%s' is defined twice." % name)
            new[name] = o[name]
    return new

def _replace(attrs, **kwargs):
    # Verify that new values are replacing existing ones, not adding.
    for name in kwargs.keys():
        if name not in attrs:
            log.error("Attr '%s' is not defined, replacement failed." % name)
    new = dict()
    new.update(attrs)
    new.update(kwargs)
    return new

def _make_tristate_attr(default, doc = "", mandatory = False):
    return attr.int(
        default = default,
        doc = doc,
        mandatory = mandatory,
        values = [-1, 0, 1],
    )

def _normalize_tristate(attr_value):
    """Normalizes the tristate value going into a rule.

    This is required because "tristate" is not officially supported as an
    attribute type. An equivalent attribute type is an in with a constrained
    set of values, namely [-1, 0, 1]. Unfortunately, tristate accepts
    multiple types, integers, booleans and strings ("auto"). As a result, this
    method normalizes the inputs to an integer.

    This method needs to be applied to attributes that were formally tristate
    to normalize the inputs.
    """
    if type(attr_value) == "int":
        return attr_value

    if type(attr_value) == "string":
        if attr_value.lower() == "auto":
            return -1

    if type(attr_value) == "bool":
        return int(attr_value)

    return attr_value  # Return unknown type, let the rule fail.

_tristate = struct(
    create = _make_tristate_attr,
    normalize = _normalize_tristate,
    yes = 1,
    no = 0,
    auto = -1,
)

_JAVA_RUNTIME = dict(
    _host_javabase = attr.label(
        cfg = "exec",
        default = Label("//tools/jdk:current_java_runtime"),
    ),
)


# Android SDK attribute.
_ANDROID_SDK = dict(
    _android_sdk = attr.label(
        allow_rules = ["android_sdk"],
        default = configuration_field(
            fragment = "android",
            name = "android_sdk_label",
        ),
        providers = [AndroidSdkInfo],
    ),
)

# Compilation attributes for Android rules.
_COMPILATION = _add(
    dict(
        assets = attr.label_list(
            allow_files = True,
            cfg = "target",
            doc = ("The list of assets to be packaged. This is typically a glob of " +
                   "all files under the assets directory. You can also reference " +
                   "other rules (any rule that produces files) or exported files in " +
                   "the other packages, as long as all those files are under the " +
                   "assets_dir directory in the corresponding package."),
        ),
        assets_dir = attr.string(
            doc = ("The string giving the path to the files in assets. " +
                   "The pair assets and assets_dir describe packaged assets and either both " +
                   "attributes should be provided or none of them."),
        ),
        custom_package = attr.string(
            doc = ("Java package for which java sources will be generated. " +
                   "By default the package is inferred from the directory where the BUILD file " +
                   "containing the rule is. You can specify a different package but this is " +
                   "highly discouraged since it can introduce classpath conflicts with other " +
                   "libraries that will only be detected at runtime."),
        ),
        manifest = attr.label(
            allow_single_file = [".xml"],
            doc = ("The name of the Android manifest file, normally " +
                   "AndroidManifest.xml. Must be defined if resource_files or assets are defined."),
        ),
        resource_files = attr.label_list(
            allow_files = True,
            doc = ("The list of resources to be packaged. This " +
                   "is typically a glob of all files under the res directory. Generated files " +
                   "(from genrules) can be referenced by Label here as well. The only " +
                   "restriction is that the generated outputs must be under the same \"res\" " +
                   "directory as any other resource files that are included."),
        ),
        data = attr.label_list(
            allow_files = True,
            doc = (
                "Files needed by this rule at runtime. May list file or rule targets. Generally allows any target.\n\n" +
                "The default outputs and runfiles of targets in the `data` attribute should appear in the `*.runfiles` area of" +
                "any executable which is output by or has a runtime dependency on this target. " +
                "This may include data files or binaries used when this target's " +
                "[srcs](https://docs.bazel.build/versions/main/be/common-definitions.html#typical.srcs) are executed. " +
                "See the [data dependencies](https://docs.bazel.build/versions/main/build-ref.html#data) section " +
                "for more information about how to depend on and use data files.\n\n" +
                "New rules should define a `data` attribute if they process inputs which might use other inputs at runtime. " +
                "Rules' implementation functions must also " +
                "[populate the target's runfiles](https://docs.bazel.build/versions/main/skylark/rules.html#runfiles) " +
                "from the outputs and runfiles of any `data` attribute, as well as runfiles from any dependency attribute " +
                "which provides either source code or runtime dependencies."
            ),
        ),
        plugins = attr.label_list(
            providers = [JavaPluginInfo],
            cfg = "exec",
            doc = (
                "Java compiler plugins to run at compile-time. " +
                "Every `java_plugin` specified in the plugins attribute will be run whenever this rule is built. " +
                "A library may also inherit plugins from dependencies that use [exported_plugins](https://docs.bazel.build/versions/main/be/java.html#java_library.exported_plugins). " +
                "Resources generated by the plugin will be included in the resulting jar of this rule."
            ),
        ),
        javacopts = attr.string_list(
            doc = (
                "Extra compiler options for this library. " +
                "Subject to \"[Make variable](https://docs.bazel.build/versions/main/be/make-variables.html)\" substitution and " +
                "[Bourne shell tokenization](https://docs.bazel.build/versions/main/be/common-definitions.html#sh-tokenization).\n" +
                "These compiler options are passed to javac after the global compiler options."
            ),
        ),
        # TODO: Expose getPlugins() in JavaConfiguration.java
        #       com/google/devtools/build/lib/rules/java/JavaConfiguration.java
        #       com/google/devtools/build/lib/rules/java/JavaOptions.java
        #
        # _java_plugins = attr.label(
        #     allow_rules = ["java_plugin"],
        #     default = configuration_field(
        #         fragment = "java",
        #         name = "plugin",
        #     ),
        # ),
    ),
    _JAVA_RUNTIME,
)

# Attributes for rules that use the AndroidDataContext android_data.make_context
_DATA_CONTEXT = _add(
    dict(
        # Additional attrs needed for AndroidDataContext
        _add_g3itr_xslt = attr.label(
            cfg = "exec",
            default = Label("//tools/android/xslt:add_g3itr.xslt"),
            allow_single_file = True,
        ),
        _android_manifest_merge_tool = attr.label(
            cfg = "exec",
            default = Label("//tools/android:merge_manifests"),
            executable = True,
        ),
        # TODO(b/145617058) Switching back to head RPBB until the Android rules release process is improved
        _android_resources_busybox = attr.label(
            cfg = "exec",
            default = Label("//rules:ResourceProcessorBusyBox"),
            executable = True,
        ),
        _xsltproc_tool = attr.label(
            cfg = "exec",
            default = Label("//tools/android/xslt:xslt"),
            allow_files = True,
        ),
    ),
    _ANDROID_SDK,
)







ANDROID_SDK_ATTRS = dict(
    aapt = attr.label(
        allow_single_file = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    aapt2 = attr.label(
        allow_single_file = True,
        cfg = "exec",
        executable = True,
    ),
    aidl = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    aidl_lib = attr.label(
        allow_files = [".jar"],
    ),
    android_jar = attr.label(
        allow_single_file = [".jar"],
        cfg = "exec",
        mandatory = True,
    ),
    annotations_jar = attr.label(
        allow_single_file = [".jar"],
        cfg = "exec",
    ),
    apkbuilder = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
    ),
    apksigner = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    adb = attr.label(
        allow_single_file = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    build_tools_version = attr.string(),
    dexdump = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = False,
    ),
    dx = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    framework_aidl = attr.label(
        allow_single_file = True,
        cfg = "exec",
        mandatory = True,
    ),
    legacy_main_dex_list_generator = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
    ),
    main_dex_classes = attr.label(
        allow_single_file = True,
        cfg = "exec",
        mandatory = True,
    ),
    main_dex_list_creator = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    proguard = attr.label(
        allow_files = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    shrinked_android_jar = attr.label(
        allow_single_file = True,
        cfg = "exec",
    ),
    source_properties = attr.label(
        allow_single_file = True,
        cfg = "exec",
    ),
    zipalign = attr.label(
        allow_single_file = True,
        cfg = "exec",
        executable = True,
        mandatory = True,
    ),
    _proguard = attr.label(
        cfg = "exec",
        default = configuration_field(
            fragment = "java",
            name = "proguard_top",
        ),
    ),
    _system = attr.label(
        default = Label("//tools/android:bootclasspath_android_only"),
    ),
)

# Attributes for resolving platform-based toolchains. Only needed by the native
# DexArchiveAspect.
_ANDROID_TOOLCHAIN_ATTRS = dict(
    _android_sdk_toolchain_type = attr.label(
        allow_rules = ["toolchain_type"],
        default = ANDROID_SDK_TOOLCHAIN_TYPE_DEFAULT,
    ),
)

ANDROID_TOOLS_DEFAULTS_JAR_ATTRS = _add(_ANDROID_SDK)

_AUTOMATIC_EXEC_GROUPS_ENABLED = dict(
    _use_auto_exec_groups = attr.bool(default = True),
)

attrs = struct(
    ANDROID_SDK = _ANDROID_SDK,
    COMPILATION = _COMPILATION,
    DATA_CONTEXT = _DATA_CONTEXT,
    JAVA_RUNTIME = _JAVA_RUNTIME,
    ANDROID_TOOLCHAIN_ATTRS = _ANDROID_TOOLCHAIN_ATTRS,
    tristate = _tristate,
    add = _add,
    replace = _replace,
    AUTOMATIC_EXEC_GROUPS_ENABLED = _AUTOMATIC_EXEC_GROUPS_ENABLED,
)