aboutsummaryrefslogtreecommitdiff
path: root/rules/android_sdk_repository/helper.bzl
blob: 43f715ff51035f7d40b9a58caac7cfcde271b44c (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# 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.

"""Helpers for the build file used in android_sdk_repository."""

load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
load("@rules_java//java:defs.bzl", "java_binary", "java_import")

def _bool_flag_impl(_unused_ctx):
    pass

_bool_flag = rule(
    implementation = _bool_flag_impl,
    build_setting = config.bool(flag = True),
)

def _int_flag_impl(ctx):
    allowed_values = ctx.attr.values
    value = ctx.build_setting_value
    if len(allowed_values) != 0 and value not in ctx.attr.values:
        fail(
            "Error setting %s: invalid value '%d'. Allowed values are %s" % (
                ctx.label,
                value,
                ",".join([str(val) for val in allowed_values]),
            ),
        )

int_flag = rule(
    implementation = _int_flag_impl,
    build_setting = config.int(flag = True),
    attrs = {
        "values": attr.int_list(
            doc = "The list of allowed values for this setting. An error is raised if any other value is given.",
        ),
    },
    doc = "An int-typed build setting that can be set on the command line",
)

def _create_config_setting_rule():
    """Create config_setting rule for windows.

    These represent the matching --host_cpu values.
    """
    name = "windows"
    if not native.existing_rule(name):
        native.config_setting(
            name = name,
            values = {"host_cpu": "x64_" + name},
        )

    native.config_setting(
        name = "d8_standalone_dexer",
        values = {"define": "android_standalone_dexing_tool=d8_compat_dx"},
    )

    native.config_setting(
        name = "dx_standalone_dexer",
        values = {"define": "android_standalone_dexing_tool=dx_compat_dx"},
    )

    _bool_flag(
        name = "allow_proguard",
        build_setting_default = True,
    )

    native.config_setting(
        name = "disallow_proguard",
        flag_values = {":allow_proguard": "false"},
    )

def create_android_sdk_rules(
        name,
        build_tools_version,
        build_tools_directory,
        api_levels,
        default_api_level):
    """Generate android_sdk rules for the API levels in the Android SDK.

    Args:
      name: string, the name of the repository being generated.
      build_tools_version: string, the version of Android's build tools to use.
      build_tools_directory: string, the directory name of the build tools in
          sdk's build-tools directory.
      api_levels: list of ints, the API levels from which to get android.jar
          et al. and create android_sdk rules.
      default_api_level: int, the API level to alias the default sdk to if
          --android_sdk is not specified on the command line.
    """

    _create_config_setting_rule()

    int_flag(
        name = "api_level",
        build_setting_default = default_api_level,
        values = api_levels,
        visibility = ["//visibility:public"],
    )

    windows_only_files = [
        "build-tools/%s/aapt.exe" % build_tools_directory,
        "build-tools/%s/aidl.exe" % build_tools_directory,
        "build-tools/%s/zipalign.exe" % build_tools_directory,
        "platform-tools/adb.exe",
    ] + native.glob(
        ["build-tools/%s/aapt2.exe" % build_tools_directory],
        allow_empty = True,
    )

    linux_only_files = [
        "build-tools/%s/aapt" % build_tools_directory,
        "build-tools/%s/aidl" % build_tools_directory,
        "build-tools/%s/zipalign" % build_tools_directory,
        "platform-tools/adb",
    ] + native.glob(
        ["extras", "build-tools/%s/aapt2" % build_tools_directory],
        allow_empty = True,
        exclude_directories = 0,
    )

    # This filegroup is used to pass the minimal contents of the SDK to the
    # Android integration tests. Note that in order to work on Windows, we cannot
    # include directories and must keep the size small.
    native.filegroup(
        name = "files",
        srcs = [
            "build-tools/%s/lib/apksigner.jar" % build_tools_directory,
            "build-tools/%s/lib/d8.jar" % build_tools_directory,
            "build-tools/%s/lib/dx.jar" % build_tools_directory,
            "build-tools/%s/mainDexClasses.rules" % build_tools_directory,
            ":build_tools_libs",
        ] + [
            "platforms/android-%d/%s" % (api_level, filename)
            for api_level in api_levels
            for filename in ["android.jar", "framework.aidl"]
        ] + select({
            ":windows": windows_only_files,
            "//conditions:default": linux_only_files,
        }),
    )

    for api_level in api_levels:
        if api_level >= 23:
            # Android 23 removed most of org.apache.http from android.jar and moved it
            # to a separate jar.
            java_import(
                name = "org_apache_http_legacy-%d" % api_level,
                jars = ["platforms/android-%d/optional/org.apache.http.legacy.jar" % api_level],
            )

        if api_level >= 28:
            # Android 28 removed most of android.test from android.jar and moved it
            # to separate jars.
            java_import(
                name = "legacy_test-%d" % api_level,
                jars = [
                    "platforms/android-%d/optional/android.test.base.jar" % api_level,
                    "platforms/android-%d/optional/android.test.mock.jar" % api_level,
                    "platforms/android-%d/optional/android.test.runner.jar" % api_level,
                ],
                neverlink = 1,
            )

        native.config_setting(
            name = "api_%d_enabled" % api_level,
            flag_values = {
                ":api_level": str(api_level),
            },
        )

        # TODO(katre): Use the Starlark android_sdk
        native.android_sdk(
            name = "sdk-%d" % api_level,
            aapt = select({
                ":windows": "build-tools/%s/aapt.exe" % build_tools_directory,
                "//conditions:default": ":aapt_binary",
            }),
            aapt2 = select({
                ":windows": "build-tools/%s/aapt2.exe" % build_tools_directory,
                "//conditions:default": ":aapt2_binary",
            }),
            adb = select({
                ":windows": "platform-tools/adb.exe",
                "//conditions:default": "platform-tools/adb",
            }),
            aidl = select({
                ":windows": "build-tools/%s/aidl.exe" % build_tools_directory,
                "//conditions:default": ":aidl_binary",
            }),
            android_jar = "platforms/android-%d/android.jar" % api_level,
            apksigner = ":apksigner",
            build_tools_version = build_tools_version,
            dx = select({
                "d8_standalone_dexer": ":d8_compat_dx",
                "dx_standalone_dexer": ":dx_binary",
                "//conditions:default": ":d8_compat_dx",
            }),
            framework_aidl = "platforms/android-%d/framework.aidl" % api_level,
            legacy_main_dex_list_generator = ":generate_main_dex_list",
            main_dex_classes = "build-tools/%s/mainDexClasses.rules" % build_tools_directory,
            proguard = select({
                ":disallow_proguard": ":fail",
                "//conditions:default": "@bazel_tools//tools/jdk:proguard",
            }),
            shrinked_android_jar = "platforms/android-%d/android.jar" % api_level,
            # See https://github.com/bazelbuild/bazel/issues/8757
            tags = ["__ANDROID_RULES_MIGRATION__"],
            zipalign = select({
                ":windows": "build-tools/%s/zipalign.exe" % build_tools_directory,
                "//conditions:default": ":zipalign_binary",
            }),
        )

        native.toolchain(
            name = "sdk-%d-toolchain" % api_level,
            exec_compatible_with = HOST_CONSTRAINTS,
            target_compatible_with = [
                "@platforms//os:android",
            ],
            toolchain = ":sdk-%d" % api_level,
            toolchain_type = "@bazel_tools//tools/android:sdk_toolchain_type",
            target_settings = [
                ":api_%d_enabled" % api_level,
            ],
        )

    native.alias(
        name = "org_apache_http_legacy",
        actual = ":org_apache_http_legacy-%d" % default_api_level,
    )

    sdk_alias_dict = {
        "//conditions:default": "sdk-%d" % default_api_level,
    }

    for api_level in api_levels:
        sdk_alias_dict[":api_%d_enabled" % api_level] = "sdk-%d" % api_level

    native.alias(
        name = "sdk",
        actual = select(
            sdk_alias_dict,
            no_match_error = "Unknown Android SDK level, valid levels are %s" % ",".join([str(level) for level in api_levels]),
        ),
    )

    java_binary(
        name = "apksigner",
        main_class = "com.android.apksigner.ApkSignerTool",
        runtime_deps = ["build-tools/%s/lib/apksigner.jar" % build_tools_directory],
    )

    native.filegroup(
        name = "build_tools_libs",
        srcs = native.glob([
            "build-tools/%s/lib/**" % build_tools_directory,
            # Build tools version 24.0.0 added a lib64 folder.
            "build-tools/%s/lib64/**" % build_tools_directory,
        ], allow_empty = True),
    )

    for tool in ["aapt", "aapt2", "aidl", "zipalign"]:
        native.genrule(
            name = tool + "_runner",
            srcs = [],
            outs = [tool + "_runner.sh"],
            cmd = "\n".join([
                "cat > $@ << 'EOF'",
                "#!/bin/bash",
                "set -eu",
                # The tools under build-tools/VERSION require the libraries under
                # build-tools/VERSION/lib, so we can't simply depend on them as a
                # file like we do with aapt.
                # On Windows however we can use these binaries directly because
                # there's no runfiles support so Bazel just creates a junction to
                # {SDK}/build-tools.
                "SDK=$${0}.runfiles/%s" % name,
                # If $${SDK} is not a directory, it means that this tool is running
                # from a runfiles directory, in the case of
                # android_instrumentation_test. Hence, use the androidsdk
                # that's already present in the runfiles of the current context.
                "if [[ ! -d $${SDK} ]] ; then",
                "  SDK=$$(pwd)/../%s" % name,
                "fi",
                "tool=$${SDK}/build-tools/%s/%s" % (build_tools_directory, tool),
                "exec env LD_LIBRARY_PATH=$${SDK}/build-tools/%s/lib64 $$tool $$*" % build_tools_directory,
                "EOF\n",
            ]),
        )

        native.sh_binary(
            name = tool + "_binary",
            srcs = [tool + "_runner.sh"],
            data = [
                ":build_tools_libs",
                "build-tools/%s/%s" % (build_tools_directory, tool),
            ],
        )

    native.sh_binary(
        name = "fail",
        srcs = select({
            ":windows": [":generate_fail_cmd"],
            "//conditions:default": [":generate_fail_sh"],
        }),
    )

    native.genrule(
        name = "generate_fail_sh",
        outs = ["fail.sh"],
        cmd = "echo -e '#!/bin/bash\\nexit 1' >> $@; chmod +x $@",
        executable = 1,
    )

    native.genrule(
        name = "generate_fail_cmd",
        outs = ["fail.cmd"],
        cmd = "echo @exit /b 1 > $@",
        executable = 1,
    )

    native.genrule(
        name = "main_dex_list_creator_source",
        srcs = [],
        outs = ["main_dex_list_creator.sh"],
        cmd = "\n".join([
            "cat > $@ <<'EOF'",
            "#!/bin/bash",
            "",
            "MAIN_DEX_LIST=$$1",
            "STRIPPED_JAR=$$2",
            "JAR=$$3",
            "" +
            "JAVA_BINARY=$$0.runfiles/%s/main_dex_list_creator_java" % name,
            "$$JAVA_BINARY $$STRIPPED_JAR $$JAR > $$MAIN_DEX_LIST",
            "exit $$?",
            "",
            "EOF\n",
        ]),
    )

    native.sh_binary(
        name = "main_dex_list_creator",
        srcs = ["main_dex_list_creator.sh"],
        data = [":main_dex_list_creator_java"],
    )
    java_binary(
        name = "main_dex_list_creator_java",
        main_class = "com.android.multidex.ClassReferenceListBuilder",
        runtime_deps = [":dx_jar_import"],
    )
    java_binary(
        name = "dx_binary",
        main_class = "com.android.dx.command.Main",
        runtime_deps = [":dx_jar_import"],
    )
    java_import(
        name = "dx_jar_import",
        jars = ["build-tools/%s/lib/dx.jar" % build_tools_directory],
    )
    java_binary(
        name = "generate_main_dex_list",
        jvm_flags = [
            "-XX:+TieredCompilation",
            "-XX:TieredStopAtLevel=1",
            # Consistent with what we use for desugar.
            "-Xms8g",
            "-Xmx8g",
        ],
        main_class = "com.android.tools.r8.GenerateMainDexList",
        runtime_deps = ["@bazel_tools//src/tools/android/java/com/google/devtools/build/android/r8"],
    )
    java_binary(
        name = "d8_compat_dx",
        main_class = "com.google.devtools.build.android.r8.CompatDx",
        runtime_deps = [
            "@bazel_tools//src/tools/android/java/com/google/devtools/build/android/r8",
        ],
    )
    native.alias(
        name = "d8_jar_import",
        actual = "@android_gmaven_r8//jar",
    )

TAGDIR_TO_TAG_MAP = {
    "google_apis_playstore": "playstore",
    "google_apis": "google",
    "default": "android",
    "android-tv": "tv",
    "android-wear": "wear",
}

ARCHDIR_TO_ARCH_MAP = {
    "x86": "x86",
    "armeabi-v7a": "arm",
}

# buildifier: disable=unnamed-macro
def create_system_images_filegroups(system_image_dirs):
    """Generate filegroups for the system images in the Android SDK.

    Args:
      system_image_dirs: list of strings, the directories containing system image
          files to be used to create android_device rules.
    """

    # These images will need to be updated as Android releases new system images.
    # We are intentionally not adding future releases because there is no
    # guarantee that they will work out of the box. Supported system images should
    # be added here once they have been confirmed to work with the Bazel Android
    # testing infrastructure.
    system_images = [
        (tag, str(api), arch)
        for tag in ["android", "google"]
        for api in [10] + list(range(15, 20)) + list(range(21, 30))
        for arch in ("x86", "arm")
    ] + [
        ("playstore", str(api), "x86")
        for api in list(range(24, 30))
    ]
    tv_images = [
        ("tv", str(api), "x86")
        for api in range(21, 30)
    ] + [
        ("tv", "21", "arm"),
        ("tv", "23", "arm"),
    ]
    wear_images = [
        ("wear", str(api), "x86")
        for api in [23, 25, 26, 28]
    ] + [
        ("wear", str(api), "arm")
        for api in [23, 25]
    ]
    supported_system_images = system_images + tv_images + wear_images

    installed_system_images_dirs = {}
    for system_image_dir in system_image_dirs:
        apidir, tagdir, archdir = system_image_dir.split("/")[1:]
        if "-" not in apidir:
            continue
        api = apidir.split("-")[1]  # "android-24" --> "24", "android-O" --> "O"
        if tagdir not in TAGDIR_TO_TAG_MAP:
            continue
        tag = TAGDIR_TO_TAG_MAP[tagdir]
        if archdir not in ARCHDIR_TO_ARCH_MAP:
            continue
        arch = ARCHDIR_TO_ARCH_MAP[archdir]
        if (tag, api, arch) in supported_system_images:
            name = "emulator_images_%s_%s_%s" % (tag, api, arch)
            installed_system_images_dirs[name] = system_image_dir
        else:
            # TODO(bazel-team): If the user has an unsupported system image installed,
            # should we print a warning? This includes all 64-bit system-images.
            pass

    for (tag, api, arch) in supported_system_images:
        name = "emulator_images_%s_%s_%s" % (tag, api, arch)
        if name in installed_system_images_dirs:
            system_image_dir = installed_system_images_dirs[name]

            # For supported system images that exist in /sdk/system-images/, we
            # create a filegroup with their contents.
            native.filegroup(
                name = name,
                srcs = native.glob([
                    "%s/**" % system_image_dir,
                ]),
            )
            native.filegroup(
                name = "%s_qemu2_extra" % name,
                srcs = native.glob(["%s/kernel-ranchu" % system_image_dir], allow_empty = True),
            )
        else:
            # For supported system images that are not installed in the SDK, we
            # create a "poison pill" genrule to display a helpful error message to
            # a user who attempts to run a test against an android_device that
            # they don't have the system image for installed.
            native.genrule(
                name = name,
                outs = [
                    # Necessary so that the build doesn't fail in analysis because
                    # android_device expects a file named source.properties.
                    "poison_pill_for_%s/source.properties" % name,
                ],
                cmd = """echo \
          This rule requires that the Android SDK used by Bazel has the \
          following system image installed: %s. Please install this system \
          image through the Android SDK Manager and try again. ; \
          exit 1
          """ % name,
            )
            native.filegroup(
                name = "%s_qemu2_extra" % name,
                srcs = [],
            )