xref: /aosp_15_r20/external/bazelbuild-rules_android/mobile_install/mi.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
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"""Aspect for mobile-install."""
15
16load(":adapters.bzl", "adapters")
17load(":debug.bzl", "debug")
18load(":tools.bzl", "TOOL_ATTRS")
19load("//rules/flags:flags.bzl", "flags")
20
21def aspect_impl(target, ctx):
22    """Calls the adapter for a given rule and returns its providers.
23
24    Args:
25      target: Target of the MI command
26      ctx: Current context
27
28    Returns:
29      A list of providers
30    """
31    adapter = adapters.get(ctx.rule.kind)
32
33    if not adapter:
34        return []
35
36    # Debug.
37    infos = adapter.adapt(target, ctx)
38    if flags.get(ctx).debug:
39        infos.append(OutputGroupInfo(**debug.make_output_groups(infos)))
40    return infos
41
42def make_aspect(
43        dex_shards = 16,
44        is_cmd = True,
45        is_test = False,
46        res_shards = 1,
47        tools = TOOL_ATTRS):
48    """Make aspect for incremental android apps.
49
50    Args:
51      dex_shards: Number of dex shards to split the project across.
52      is_cmd: A Boolean, when True the aspect is running in the context of the
53        mobile-install command. If False it is as a rule (e.g. mi_test).
54      res_shards: Number of Android resource shards during processing.
55    Returns:
56      A configured aspect.
57    """
58    attrs = dict(
59        _mi_dex_shards = attr.int(default = dex_shards),
60        _mi_is_cmd = attr.bool(default = is_cmd),
61        _mi_res_shards = attr.int(default = res_shards),
62        _mi_is_test = attr.bool(default = is_test),
63    )
64    attrs.update(tools)
65    return aspect(
66        attr_aspects = adapters.get_all_aspect_attrs(),
67        attrs = attrs,
68        required_aspect_providers = [
69            [JavaInfo],  # JavaLiteProtoLibrary aspect.
70        ],
71        fragments = ["cpp", "java"],
72        host_fragments = ["jvm"],
73        implementation = aspect_impl,
74    )
75
76# MIASPECT allows you to run the aspect directly on a Blaze/Bazel command.
77#
78# Example:
79#   bazel build \
80#     --aspects=@rules_android//mobile_install:mi.bzl%MIASPECT
81#     --output_groups=mobile_install_INTERNAL_,mobile_install_launcher_INTERNAL_,-_,-defaults \
82#     java/com/example/exampleapp:exampleapp
83MIASPECT = make_aspect()
84MIRESASPECT = MIASPECT  # Deprecated, needs to get removed from MobileInstallCommand.java first.
85