xref: /aosp_15_r20/build/bazel/rules/apex/apex_aab.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Workerload("@bazel_skylib//lib:paths.bzl", "paths")
16*7594170eSAndroid Build Coastguard Workerload("//build/bazel/rules:toolchain_utils.bzl", "verify_toolchain_exists")
17*7594170eSAndroid Build Coastguard Workerload(":apex_info.bzl", "ApexInfo")
18*7594170eSAndroid Build Coastguard Workerload(":bundle.bzl", "build_bundle_config")
19*7594170eSAndroid Build Coastguard Worker
20*7594170eSAndroid Build Coastguard Workerdef _arch_transition_impl(settings, _attr):
21*7594170eSAndroid Build Coastguard Worker    """Implementation of arch_transition.
22*7594170eSAndroid Build Coastguard Worker
23*7594170eSAndroid Build Coastguard Worker    Six arch products are included for mainline modules: x86, x86_64, x86_64only, arm, arm64, arm64only.
24*7594170eSAndroid Build Coastguard Worker    """
25*7594170eSAndroid Build Coastguard Worker    old_platform = str(settings["//command_line_option:platforms"][0])
26*7594170eSAndroid Build Coastguard Worker
27*7594170eSAndroid Build Coastguard Worker    # We can't use platforms alone to differentiate between x86_64 and x86_64
28*7594170eSAndroid Build Coastguard Worker    # with a secondary arch, which is significant for apex packaging that can
29*7594170eSAndroid Build Coastguard Worker    # optionally include the secondary arch's libs. That is currently determined
30*7594170eSAndroid Build Coastguard Worker    # by DeviceSecondaryArch in apex's lib inclusion logic, so we explicitly set
31*7594170eSAndroid Build Coastguard Worker    # DeviceSecondaryArch to "" for the 64bit only cases.
32*7594170eSAndroid Build Coastguard Worker
33*7594170eSAndroid Build Coastguard Worker    # TODO(b/249685973) Instead of using these __internal_x86 platforms, use
34*7594170eSAndroid Build Coastguard Worker    # the mainline_modules_<arch> android products
35*7594170eSAndroid Build Coastguard Worker    return {
36*7594170eSAndroid Build Coastguard Worker        # these key names must correspond to mainline_modules_<arch> product name suffixes.
37*7594170eSAndroid Build Coastguard Worker        "arm": {
38*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_arm",
39*7594170eSAndroid Build Coastguard Worker        },
40*7594170eSAndroid Build Coastguard Worker        "arm64": {
41*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_arm64",
42*7594170eSAndroid Build Coastguard Worker        },
43*7594170eSAndroid Build Coastguard Worker        "arm64only": {
44*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_arm64only",
45*7594170eSAndroid Build Coastguard Worker        },
46*7594170eSAndroid Build Coastguard Worker        "x86": {
47*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_x86",
48*7594170eSAndroid Build Coastguard Worker        },
49*7594170eSAndroid Build Coastguard Worker        "x86_64": {
50*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_x86_64",
51*7594170eSAndroid Build Coastguard Worker        },
52*7594170eSAndroid Build Coastguard Worker        "x86_64only": {
53*7594170eSAndroid Build Coastguard Worker            "//command_line_option:platforms": old_platform + "__internal_x86_64only",
54*7594170eSAndroid Build Coastguard Worker        },
55*7594170eSAndroid Build Coastguard Worker    }
56*7594170eSAndroid Build Coastguard Worker
57*7594170eSAndroid Build Coastguard Worker# Multi-arch transition.
58*7594170eSAndroid Build Coastguard Workerarch_transition = transition(
59*7594170eSAndroid Build Coastguard Worker    implementation = _arch_transition_impl,
60*7594170eSAndroid Build Coastguard Worker    inputs = [
61*7594170eSAndroid Build Coastguard Worker        "//command_line_option:platforms",
62*7594170eSAndroid Build Coastguard Worker    ],
63*7594170eSAndroid Build Coastguard Worker    outputs = [
64*7594170eSAndroid Build Coastguard Worker        "//command_line_option:platforms",
65*7594170eSAndroid Build Coastguard Worker    ],
66*7594170eSAndroid Build Coastguard Worker)
67*7594170eSAndroid Build Coastguard Worker
68*7594170eSAndroid Build Coastguard Workerdef _merge_base_files(ctx, module_name, base_files):
69*7594170eSAndroid Build Coastguard Worker    """Run merge_zips to merge all files created for each arch by _apex_base_file."""
70*7594170eSAndroid Build Coastguard Worker    merged_base_file = ctx.actions.declare_file(module_name + "/" + module_name + ".zip")
71*7594170eSAndroid Build Coastguard Worker
72*7594170eSAndroid Build Coastguard Worker    # Arguments
73*7594170eSAndroid Build Coastguard Worker    args = ctx.actions.args()
74*7594170eSAndroid Build Coastguard Worker    args.add("--ignore-duplicates")
75*7594170eSAndroid Build Coastguard Worker    args.add(merged_base_file)
76*7594170eSAndroid Build Coastguard Worker    args.add_all(base_files)
77*7594170eSAndroid Build Coastguard Worker
78*7594170eSAndroid Build Coastguard Worker    ctx.actions.run(
79*7594170eSAndroid Build Coastguard Worker        inputs = base_files,
80*7594170eSAndroid Build Coastguard Worker        outputs = [merged_base_file],
81*7594170eSAndroid Build Coastguard Worker        executable = ctx.executable._merge_zips,
82*7594170eSAndroid Build Coastguard Worker        arguments = [args],
83*7594170eSAndroid Build Coastguard Worker        mnemonic = "ApexMergeBaseFiles",
84*7594170eSAndroid Build Coastguard Worker    )
85*7594170eSAndroid Build Coastguard Worker    return merged_base_file
86*7594170eSAndroid Build Coastguard Worker
87*7594170eSAndroid Build Coastguard Workerdef _apex_bundle(ctx, module_name, merged_base_file, bundle_config_file):
88*7594170eSAndroid Build Coastguard Worker    """Run bundletool to create the aab file."""
89*7594170eSAndroid Build Coastguard Worker
90*7594170eSAndroid Build Coastguard Worker    # Outputs
91*7594170eSAndroid Build Coastguard Worker    bundle_file = ctx.actions.declare_file(module_name + "/" + module_name + ".aab")
92*7594170eSAndroid Build Coastguard Worker
93*7594170eSAndroid Build Coastguard Worker    # Arguments
94*7594170eSAndroid Build Coastguard Worker    args = ctx.actions.args()
95*7594170eSAndroid Build Coastguard Worker    args.add("build-bundle")
96*7594170eSAndroid Build Coastguard Worker    args.add_all(["--config", bundle_config_file])
97*7594170eSAndroid Build Coastguard Worker    args.add_all(["--modules", merged_base_file])
98*7594170eSAndroid Build Coastguard Worker    args.add_all(["--output", bundle_file])
99*7594170eSAndroid Build Coastguard Worker
100*7594170eSAndroid Build Coastguard Worker    ctx.actions.run(
101*7594170eSAndroid Build Coastguard Worker        inputs = [
102*7594170eSAndroid Build Coastguard Worker            bundle_config_file,
103*7594170eSAndroid Build Coastguard Worker            merged_base_file,
104*7594170eSAndroid Build Coastguard Worker        ],
105*7594170eSAndroid Build Coastguard Worker        outputs = [bundle_file],
106*7594170eSAndroid Build Coastguard Worker        executable = ctx.executable._bundletool,
107*7594170eSAndroid Build Coastguard Worker        arguments = [args],
108*7594170eSAndroid Build Coastguard Worker        mnemonic = "ApexBundleFile",
109*7594170eSAndroid Build Coastguard Worker    )
110*7594170eSAndroid Build Coastguard Worker    return bundle_file
111*7594170eSAndroid Build Coastguard Worker
112*7594170eSAndroid Build Coastguard Workerdef _sign_bundle(ctx, aapt2, avbtool, module_name, bundle_file, apex_info):
113*7594170eSAndroid Build Coastguard Worker    """ Run dev_sign_bundle to sign the bundle_file."""
114*7594170eSAndroid Build Coastguard Worker
115*7594170eSAndroid Build Coastguard Worker    # Python3 interpreter for dev_sign_bundle to run other python scripts.
116*7594170eSAndroid Build Coastguard Worker    python_interpreter = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime.interpreter
117*7594170eSAndroid Build Coastguard Worker    if python_interpreter.basename != "python3":
118*7594170eSAndroid Build Coastguard Worker        python3 = ctx.actions.declare_file("python3")
119*7594170eSAndroid Build Coastguard Worker        ctx.actions.symlink(
120*7594170eSAndroid Build Coastguard Worker            output = python3,
121*7594170eSAndroid Build Coastguard Worker            target_file = python_interpreter,
122*7594170eSAndroid Build Coastguard Worker            is_executable = True,
123*7594170eSAndroid Build Coastguard Worker        )
124*7594170eSAndroid Build Coastguard Worker        python_interpreter = python3
125*7594170eSAndroid Build Coastguard Worker
126*7594170eSAndroid Build Coastguard Worker    # Input directory for dev_sign_bundle.
127*7594170eSAndroid Build Coastguard Worker    input_bundle_file = ctx.actions.declare_file(module_name + "/sign_bundle/input_dir/" + bundle_file.basename)
128*7594170eSAndroid Build Coastguard Worker    ctx.actions.symlink(
129*7594170eSAndroid Build Coastguard Worker        output = input_bundle_file,
130*7594170eSAndroid Build Coastguard Worker        target_file = bundle_file,
131*7594170eSAndroid Build Coastguard Worker    )
132*7594170eSAndroid Build Coastguard Worker
133*7594170eSAndroid Build Coastguard Worker    # Output directory  for dev_sign_bundle
134*7594170eSAndroid Build Coastguard Worker    output_dir = ctx.actions.declare_directory(module_name + "/sign_bundle/output_dir")
135*7594170eSAndroid Build Coastguard Worker
136*7594170eSAndroid Build Coastguard Worker    # Temporary directory for dev_sign_bundle
137*7594170eSAndroid Build Coastguard Worker    tmp_dir = ctx.actions.declare_directory(module_name + "/sign_bundle/tmp_dir")
138*7594170eSAndroid Build Coastguard Worker
139*7594170eSAndroid Build Coastguard Worker    # Jar file of prebuilts/bundletool
140*7594170eSAndroid Build Coastguard Worker    bundletool_jarfile = ctx.attr._bundletool_lib.files.to_list()[0]
141*7594170eSAndroid Build Coastguard Worker
142*7594170eSAndroid Build Coastguard Worker    # Keystore file
143*7594170eSAndroid Build Coastguard Worker    keystore_file = ctx.attr.dev_keystore.files.to_list()[0]
144*7594170eSAndroid Build Coastguard Worker
145*7594170eSAndroid Build Coastguard Worker    # ANDROID_HOST_OUT environment
146*7594170eSAndroid Build Coastguard Worker    debugfs_static = ctx.actions.declare_file(module_name + "/sign_bundle/android_host_out/bin/debugfs_static")
147*7594170eSAndroid Build Coastguard Worker    ctx.actions.symlink(
148*7594170eSAndroid Build Coastguard Worker        output = debugfs_static,
149*7594170eSAndroid Build Coastguard Worker        target_file = ctx.executable._debugfs,
150*7594170eSAndroid Build Coastguard Worker        is_executable = True,
151*7594170eSAndroid Build Coastguard Worker    )
152*7594170eSAndroid Build Coastguard Worker    fsck_erofs = ctx.actions.declare_file(module_name + "/sign_bundle/android_host_out/bin/fsck.erofs")
153*7594170eSAndroid Build Coastguard Worker    ctx.actions.symlink(
154*7594170eSAndroid Build Coastguard Worker        output = fsck_erofs,
155*7594170eSAndroid Build Coastguard Worker        target_file = ctx.executable._fsck_erofs,
156*7594170eSAndroid Build Coastguard Worker        is_executable = True,
157*7594170eSAndroid Build Coastguard Worker    )
158*7594170eSAndroid Build Coastguard Worker    signapk_jar = ctx.actions.declare_file(module_name + "/sign_bundle/android_host_out/framework/signapk.jar")
159*7594170eSAndroid Build Coastguard Worker    ctx.actions.symlink(
160*7594170eSAndroid Build Coastguard Worker        output = signapk_jar,
161*7594170eSAndroid Build Coastguard Worker        target_file = ctx.attr._signapk_jar.files.to_list()[0],
162*7594170eSAndroid Build Coastguard Worker        is_executable = False,
163*7594170eSAndroid Build Coastguard Worker    )
164*7594170eSAndroid Build Coastguard Worker    libconscrypt_openjdk_jni_so = ctx.actions.declare_file(module_name + "/sign_bundle/android_host_out/lib64/libconscrypt_openjdk_jni.so")
165*7594170eSAndroid Build Coastguard Worker    ctx.actions.symlink(
166*7594170eSAndroid Build Coastguard Worker        output = libconscrypt_openjdk_jni_so,
167*7594170eSAndroid Build Coastguard Worker        target_file = ctx.attr._libconscrypt_openjdk_jni.files.to_list()[1],
168*7594170eSAndroid Build Coastguard Worker        is_executable = False,
169*7594170eSAndroid Build Coastguard Worker    )
170*7594170eSAndroid Build Coastguard Worker
171*7594170eSAndroid Build Coastguard Worker    java_runtime = ctx.attr._java_runtime[java_common.JavaRuntimeInfo]
172*7594170eSAndroid Build Coastguard Worker
173*7594170eSAndroid Build Coastguard Worker    # Tools
174*7594170eSAndroid Build Coastguard Worker    tools = [
175*7594170eSAndroid Build Coastguard Worker        ctx.executable.dev_sign_bundle,
176*7594170eSAndroid Build Coastguard Worker        ctx.executable._deapexer,
177*7594170eSAndroid Build Coastguard Worker        ctx.executable._sign_apex,
178*7594170eSAndroid Build Coastguard Worker        ctx.executable._openssl,
179*7594170eSAndroid Build Coastguard Worker        ctx.executable._zip2zip,
180*7594170eSAndroid Build Coastguard Worker        aapt2,
181*7594170eSAndroid Build Coastguard Worker        avbtool.files_to_run.executable,
182*7594170eSAndroid Build Coastguard Worker        python_interpreter,
183*7594170eSAndroid Build Coastguard Worker        debugfs_static,
184*7594170eSAndroid Build Coastguard Worker        fsck_erofs,
185*7594170eSAndroid Build Coastguard Worker        bundletool_jarfile,
186*7594170eSAndroid Build Coastguard Worker        signapk_jar,
187*7594170eSAndroid Build Coastguard Worker        libconscrypt_openjdk_jni_so,
188*7594170eSAndroid Build Coastguard Worker        java_runtime.files,
189*7594170eSAndroid Build Coastguard Worker    ]
190*7594170eSAndroid Build Coastguard Worker
191*7594170eSAndroid Build Coastguard Worker    # Inputs
192*7594170eSAndroid Build Coastguard Worker    inputs = [
193*7594170eSAndroid Build Coastguard Worker        input_bundle_file,
194*7594170eSAndroid Build Coastguard Worker        keystore_file,
195*7594170eSAndroid Build Coastguard Worker        apex_info.bundle_key_info.private_key,
196*7594170eSAndroid Build Coastguard Worker        apex_info.container_key_info.pem,
197*7594170eSAndroid Build Coastguard Worker        apex_info.container_key_info.pk8,
198*7594170eSAndroid Build Coastguard Worker    ]
199*7594170eSAndroid Build Coastguard Worker
200*7594170eSAndroid Build Coastguard Worker    # Outputs
201*7594170eSAndroid Build Coastguard Worker    outputs = [output_dir, tmp_dir]
202*7594170eSAndroid Build Coastguard Worker
203*7594170eSAndroid Build Coastguard Worker    # Arguments
204*7594170eSAndroid Build Coastguard Worker    java_bin = paths.join(java_runtime.java_home, "bin")
205*7594170eSAndroid Build Coastguard Worker    args = ctx.actions.args()
206*7594170eSAndroid Build Coastguard Worker    args.add_all(["--input_dir", input_bundle_file.dirname])
207*7594170eSAndroid Build Coastguard Worker    args.add_all(["--output_dir", output_dir.path])
208*7594170eSAndroid Build Coastguard Worker    args.add_all(["--temp_dir", tmp_dir.path])
209*7594170eSAndroid Build Coastguard Worker    args.add_all(["--aapt2_path", aapt2.path])
210*7594170eSAndroid Build Coastguard Worker    args.add_all(["--bundletool_path", bundletool_jarfile.path])
211*7594170eSAndroid Build Coastguard Worker    args.add_all(["--deapexer_path", ctx.executable._deapexer.path])
212*7594170eSAndroid Build Coastguard Worker    args.add_all(["--debugfs_path", ctx.executable._debugfs.path])
213*7594170eSAndroid Build Coastguard Worker    args.add_all(["--java_binary_path", paths.join(java_bin, "java")])
214*7594170eSAndroid Build Coastguard Worker    args.add_all(["--apex_signer_path", ctx.executable._sign_apex])
215*7594170eSAndroid Build Coastguard Worker
216*7594170eSAndroid Build Coastguard Worker    ctx.actions.run(
217*7594170eSAndroid Build Coastguard Worker        inputs = inputs,
218*7594170eSAndroid Build Coastguard Worker        outputs = outputs,
219*7594170eSAndroid Build Coastguard Worker        executable = ctx.executable.dev_sign_bundle,
220*7594170eSAndroid Build Coastguard Worker        arguments = [args],
221*7594170eSAndroid Build Coastguard Worker        tools = tools,
222*7594170eSAndroid Build Coastguard Worker        env = {
223*7594170eSAndroid Build Coastguard Worker            # necessary for dev_sign_bundle.
224*7594170eSAndroid Build Coastguard Worker            "BAZEL_ANDROID_HOST_OUT": paths.dirname(debugfs_static.dirname),
225*7594170eSAndroid Build Coastguard Worker            "PATH": ":".join(
226*7594170eSAndroid Build Coastguard Worker                [
227*7594170eSAndroid Build Coastguard Worker                    python_interpreter.dirname,
228*7594170eSAndroid Build Coastguard Worker                    ctx.executable._deapexer.dirname,
229*7594170eSAndroid Build Coastguard Worker                    avbtool.files_to_run.executable.dirname,
230*7594170eSAndroid Build Coastguard Worker                    ctx.executable._openssl.dirname,
231*7594170eSAndroid Build Coastguard Worker                    ctx.executable._zip2zip.dirname,
232*7594170eSAndroid Build Coastguard Worker                    java_bin,
233*7594170eSAndroid Build Coastguard Worker                ],
234*7594170eSAndroid Build Coastguard Worker            ),
235*7594170eSAndroid Build Coastguard Worker        },
236*7594170eSAndroid Build Coastguard Worker        mnemonic = "ApexSignBundleFile",
237*7594170eSAndroid Build Coastguard Worker    )
238*7594170eSAndroid Build Coastguard Worker
239*7594170eSAndroid Build Coastguard Worker    apks_file = ctx.actions.declare_file(module_name + "/" + module_name + ".apks")
240*7594170eSAndroid Build Coastguard Worker    cert_info_file = ctx.actions.declare_file(module_name + "/" + module_name + ".cert_info.txt")
241*7594170eSAndroid Build Coastguard Worker    ctx.actions.run_shell(
242*7594170eSAndroid Build Coastguard Worker        inputs = [output_dir],
243*7594170eSAndroid Build Coastguard Worker        outputs = [apks_file, cert_info_file],
244*7594170eSAndroid Build Coastguard Worker        command = " ".join(["cp", output_dir.path + "/" + module_name + "/*", apks_file.dirname]),
245*7594170eSAndroid Build Coastguard Worker    )
246*7594170eSAndroid Build Coastguard Worker
247*7594170eSAndroid Build Coastguard Worker    return [apks_file, cert_info_file]
248*7594170eSAndroid Build Coastguard Worker
249*7594170eSAndroid Build Coastguard Workerdef _apex_aab_impl(ctx):
250*7594170eSAndroid Build Coastguard Worker    """Implementation of apex_aab rule.
251*7594170eSAndroid Build Coastguard Worker
252*7594170eSAndroid Build Coastguard Worker    This drives the process of creating aab file from apex files created for each arch."""
253*7594170eSAndroid Build Coastguard Worker    verify_toolchain_exists(ctx, "//build/bazel/rules/apex:apex_toolchain_type")
254*7594170eSAndroid Build Coastguard Worker    apex_toolchain = ctx.toolchains["//build/bazel/rules/apex:apex_toolchain_type"].toolchain_info
255*7594170eSAndroid Build Coastguard Worker
256*7594170eSAndroid Build Coastguard Worker    prefixed_apex_files = []
257*7594170eSAndroid Build Coastguard Worker    apex_base_files = []
258*7594170eSAndroid Build Coastguard Worker    bundle_config_file = None
259*7594170eSAndroid Build Coastguard Worker    module_name = ctx.attr.mainline_module[0].label.name
260*7594170eSAndroid Build Coastguard Worker    for arch in ctx.split_attr.mainline_module:
261*7594170eSAndroid Build Coastguard Worker        apex_info = ctx.split_attr.mainline_module[arch][ApexInfo]
262*7594170eSAndroid Build Coastguard Worker        apex_base_files.append(apex_info.base_file)
263*7594170eSAndroid Build Coastguard Worker
264*7594170eSAndroid Build Coastguard Worker        arch_subdir = "mainline_modules_%s" % arch
265*7594170eSAndroid Build Coastguard Worker
266*7594170eSAndroid Build Coastguard Worker        # A mapping of files to a prefix directory they should be copied to.
267*7594170eSAndroid Build Coastguard Worker        # These files will be accessible with the apex_files output_group.
268*7594170eSAndroid Build Coastguard Worker        mapping = {
269*7594170eSAndroid Build Coastguard Worker            apex_info.base_file: arch_subdir,
270*7594170eSAndroid Build Coastguard Worker            apex_info.signed_output: arch_subdir,
271*7594170eSAndroid Build Coastguard Worker            apex_info.symbols_used_by_apex: arch_subdir + "/ndk_apis_usedby_apex",
272*7594170eSAndroid Build Coastguard Worker            apex_info.backing_libs: arch_subdir + "/ndk_apis_backedby_apex",
273*7594170eSAndroid Build Coastguard Worker            apex_info.java_symbols_used_by_apex: arch_subdir + "/java_apis_used_by_apex",
274*7594170eSAndroid Build Coastguard Worker            # TODO(b/262267680): create licensetexts
275*7594170eSAndroid Build Coastguard Worker            # TODO(b/262267551): create shareprojects
276*7594170eSAndroid Build Coastguard Worker        }
277*7594170eSAndroid Build Coastguard Worker
278*7594170eSAndroid Build Coastguard Worker        # Forward the individual files for all variants in an additional output group,
279*7594170eSAndroid Build Coastguard Worker        # so dependents can easily access the multi-arch base APEX files by building
280*7594170eSAndroid Build Coastguard Worker        # this target with --output_groups=apex_files.
281*7594170eSAndroid Build Coastguard Worker        #
282*7594170eSAndroid Build Coastguard Worker        # Copy them into an arch-specific directory, since they have the same basename.
283*7594170eSAndroid Build Coastguard Worker        for _file, _dir in mapping.items():
284*7594170eSAndroid Build Coastguard Worker            _out = ctx.actions.declare_file(_dir + "/" + _file.basename)
285*7594170eSAndroid Build Coastguard Worker            ctx.actions.run_shell(
286*7594170eSAndroid Build Coastguard Worker                inputs = [_file],
287*7594170eSAndroid Build Coastguard Worker                outputs = [_out],
288*7594170eSAndroid Build Coastguard Worker                command = " ".join(["cp", _file.path, _out.path]),
289*7594170eSAndroid Build Coastguard Worker            )
290*7594170eSAndroid Build Coastguard Worker            prefixed_apex_files.append(_out)
291*7594170eSAndroid Build Coastguard Worker
292*7594170eSAndroid Build Coastguard Worker    # Create .aab file
293*7594170eSAndroid Build Coastguard Worker    bundle_config_file = build_bundle_config(ctx.actions, ctx.label.name)
294*7594170eSAndroid Build Coastguard Worker    merged_base_file = _merge_base_files(ctx, module_name, apex_base_files)
295*7594170eSAndroid Build Coastguard Worker    bundle_file = _apex_bundle(ctx, module_name, merged_base_file, bundle_config_file)
296*7594170eSAndroid Build Coastguard Worker
297*7594170eSAndroid Build Coastguard Worker    # Create .apks file
298*7594170eSAndroid Build Coastguard Worker    apex_info = ctx.attr.mainline_module[0][ApexInfo]
299*7594170eSAndroid Build Coastguard Worker    package_name = apex_info.package_name
300*7594170eSAndroid Build Coastguard Worker
301*7594170eSAndroid Build Coastguard Worker    if ctx.attr.dev_sign_bundle and ctx.attr.dev_keystore and (package_name.startswith("com.google.android") or package_name.startswith("com.google.mainline")):
302*7594170eSAndroid Build Coastguard Worker        signed_files = _sign_bundle(ctx, apex_toolchain.aapt2, apex_toolchain.avbtool, module_name, bundle_file, apex_info)
303*7594170eSAndroid Build Coastguard Worker        return [
304*7594170eSAndroid Build Coastguard Worker            DefaultInfo(files = depset([bundle_file] + signed_files)),
305*7594170eSAndroid Build Coastguard Worker            OutputGroupInfo(apex_files = depset(prefixed_apex_files), signed_files = signed_files),
306*7594170eSAndroid Build Coastguard Worker        ]
307*7594170eSAndroid Build Coastguard Worker
308*7594170eSAndroid Build Coastguard Worker    return [
309*7594170eSAndroid Build Coastguard Worker        DefaultInfo(files = depset([bundle_file])),
310*7594170eSAndroid Build Coastguard Worker        OutputGroupInfo(apex_files = depset(prefixed_apex_files)),
311*7594170eSAndroid Build Coastguard Worker    ]
312*7594170eSAndroid Build Coastguard Worker
313*7594170eSAndroid Build Coastguard Worker# apex_aab rule creates multi-arch outputs of a Mainline module, such as the
314*7594170eSAndroid Build Coastguard Worker# Android Apk Bundle (.aab) file of the APEX specified in mainline_module.
315*7594170eSAndroid Build Coastguard Worker# There is no equivalent Soong module, and it is currently done in shell script
316*7594170eSAndroid Build Coastguard Worker# by invoking Soong multiple times.
317*7594170eSAndroid Build Coastguard Worker_apex_aab = rule(
318*7594170eSAndroid Build Coastguard Worker    implementation = _apex_aab_impl,
319*7594170eSAndroid Build Coastguard Worker    toolchains = [
320*7594170eSAndroid Build Coastguard Worker        # The apex toolchain is not mandatory so that we don't get toolchain resolution errors
321*7594170eSAndroid Build Coastguard Worker        # even when the aab is not compatible with the current target (via target_compatible_with).
322*7594170eSAndroid Build Coastguard Worker        config_common.toolchain_type("//build/bazel/rules/apex:apex_toolchain_type", mandatory = False),
323*7594170eSAndroid Build Coastguard Worker        "@bazel_tools//tools/python:toolchain_type",
324*7594170eSAndroid Build Coastguard Worker    ],
325*7594170eSAndroid Build Coastguard Worker    attrs = {
326*7594170eSAndroid Build Coastguard Worker        "dev_keystore": attr.label(
327*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
328*7594170eSAndroid Build Coastguard Worker            executable = False,
329*7594170eSAndroid Build Coastguard Worker        ),
330*7594170eSAndroid Build Coastguard Worker        "dev_sign_bundle": attr.label(
331*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
332*7594170eSAndroid Build Coastguard Worker            executable = True,
333*7594170eSAndroid Build Coastguard Worker        ),
334*7594170eSAndroid Build Coastguard Worker        "mainline_module": attr.label(
335*7594170eSAndroid Build Coastguard Worker            mandatory = True,
336*7594170eSAndroid Build Coastguard Worker            cfg = arch_transition,
337*7594170eSAndroid Build Coastguard Worker            providers = [ApexInfo],
338*7594170eSAndroid Build Coastguard Worker            doc = "The label of a mainline module target",
339*7594170eSAndroid Build Coastguard Worker        ),
340*7594170eSAndroid Build Coastguard Worker        "_allowlist_function_transition": attr.label(
341*7594170eSAndroid Build Coastguard Worker            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
342*7594170eSAndroid Build Coastguard Worker            doc = "Allow transition.",
343*7594170eSAndroid Build Coastguard Worker        ),
344*7594170eSAndroid Build Coastguard Worker        "_bundletool": attr.label(
345*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
346*7594170eSAndroid Build Coastguard Worker            executable = True,
347*7594170eSAndroid Build Coastguard Worker            default = "//prebuilts/bundletool",
348*7594170eSAndroid Build Coastguard Worker        ),
349*7594170eSAndroid Build Coastguard Worker        "_bundletool_lib": attr.label(
350*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
351*7594170eSAndroid Build Coastguard Worker            executable = False,
352*7594170eSAndroid Build Coastguard Worker            default = "//prebuilts/bundletool:bundletool-lib",
353*7594170eSAndroid Build Coastguard Worker        ),
354*7594170eSAndroid Build Coastguard Worker        "_deapexer": attr.label(
355*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
356*7594170eSAndroid Build Coastguard Worker            executable = True,
357*7594170eSAndroid Build Coastguard Worker            default = "//system/apex/tools:deapexer",
358*7594170eSAndroid Build Coastguard Worker        ),
359*7594170eSAndroid Build Coastguard Worker        "_debugfs": attr.label(
360*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
361*7594170eSAndroid Build Coastguard Worker            executable = True,
362*7594170eSAndroid Build Coastguard Worker            default = "//external/e2fsprogs/debugfs:debugfs_static",
363*7594170eSAndroid Build Coastguard Worker        ),
364*7594170eSAndroid Build Coastguard Worker        "_fsck_erofs": attr.label(
365*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
366*7594170eSAndroid Build Coastguard Worker            executable = True,
367*7594170eSAndroid Build Coastguard Worker            default = "//external/erofs-utils:fsck.erofs",
368*7594170eSAndroid Build Coastguard Worker        ),
369*7594170eSAndroid Build Coastguard Worker        "_java_runtime": attr.label(
370*7594170eSAndroid Build Coastguard Worker            default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
371*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
372*7594170eSAndroid Build Coastguard Worker            providers = [java_common.JavaRuntimeInfo],
373*7594170eSAndroid Build Coastguard Worker        ),
374*7594170eSAndroid Build Coastguard Worker        "_libconscrypt_openjdk_jni": attr.label(
375*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
376*7594170eSAndroid Build Coastguard Worker            executable = False,
377*7594170eSAndroid Build Coastguard Worker            default = "//external/conscrypt:libconscrypt_openjdk_jni",
378*7594170eSAndroid Build Coastguard Worker        ),
379*7594170eSAndroid Build Coastguard Worker        "_merge_zips": attr.label(
380*7594170eSAndroid Build Coastguard Worker            allow_single_file = True,
381*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
382*7594170eSAndroid Build Coastguard Worker            executable = True,
383*7594170eSAndroid Build Coastguard Worker            default = "//build/soong/cmd/merge_zips",
384*7594170eSAndroid Build Coastguard Worker        ),
385*7594170eSAndroid Build Coastguard Worker        "_openssl": attr.label(
386*7594170eSAndroid Build Coastguard Worker            allow_single_file = True,
387*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
388*7594170eSAndroid Build Coastguard Worker            executable = True,
389*7594170eSAndroid Build Coastguard Worker            default = "//prebuilts/build-tools:linux-x86/bin/openssl",
390*7594170eSAndroid Build Coastguard Worker        ),
391*7594170eSAndroid Build Coastguard Worker        "_sign_apex": attr.label(
392*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
393*7594170eSAndroid Build Coastguard Worker            executable = True,
394*7594170eSAndroid Build Coastguard Worker            default = "//build/make/tools/releasetools:sign_apex",
395*7594170eSAndroid Build Coastguard Worker        ),
396*7594170eSAndroid Build Coastguard Worker        "_signapk_jar": attr.label(
397*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
398*7594170eSAndroid Build Coastguard Worker            executable = False,
399*7594170eSAndroid Build Coastguard Worker            default = "//build/bazel/rules/apex:signapk_deploy_jar",
400*7594170eSAndroid Build Coastguard Worker        ),
401*7594170eSAndroid Build Coastguard Worker        "_zip2zip": attr.label(
402*7594170eSAndroid Build Coastguard Worker            allow_single_file = True,
403*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
404*7594170eSAndroid Build Coastguard Worker            executable = True,
405*7594170eSAndroid Build Coastguard Worker            default = "//build/soong/cmd/zip2zip:zip2zip",
406*7594170eSAndroid Build Coastguard Worker        ),
407*7594170eSAndroid Build Coastguard Worker        "_zipper": attr.label(
408*7594170eSAndroid Build Coastguard Worker            cfg = "exec",
409*7594170eSAndroid Build Coastguard Worker            executable = True,
410*7594170eSAndroid Build Coastguard Worker            default = "@bazel_tools//tools/zip:zipper",
411*7594170eSAndroid Build Coastguard Worker        ),
412*7594170eSAndroid Build Coastguard Worker    },
413*7594170eSAndroid Build Coastguard Worker)
414*7594170eSAndroid Build Coastguard Worker
415*7594170eSAndroid Build Coastguard Workerdef apex_aab(name, mainline_module, dev_sign_bundle = None, dev_keystore = None, target_compatible_with = [], **kwargs):
416*7594170eSAndroid Build Coastguard Worker    target_compatible_with = select({
417*7594170eSAndroid Build Coastguard Worker        "//build/bazel_common_rules/platforms/os:android": [],
418*7594170eSAndroid Build Coastguard Worker        "//conditions:default": ["@platforms//:incompatible"],
419*7594170eSAndroid Build Coastguard Worker    }) + target_compatible_with
420*7594170eSAndroid Build Coastguard Worker
421*7594170eSAndroid Build Coastguard Worker    _apex_aab(
422*7594170eSAndroid Build Coastguard Worker        name = name,
423*7594170eSAndroid Build Coastguard Worker        mainline_module = mainline_module,
424*7594170eSAndroid Build Coastguard Worker        dev_sign_bundle = dev_sign_bundle,
425*7594170eSAndroid Build Coastguard Worker        dev_keystore = dev_keystore,
426*7594170eSAndroid Build Coastguard Worker        target_compatible_with = target_compatible_with,
427*7594170eSAndroid Build Coastguard Worker        **kwargs
428*7594170eSAndroid Build Coastguard Worker    )
429