xref: /aosp_15_r20/external/angle/build/android/gyp/system_image_apks.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/env python3
2
3# Copyright 2022 The Chromium Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Generates APKs for use on system images."""
7
8import argparse
9import os
10import pathlib
11import tempfile
12import shutil
13import sys
14import zipfile
15
16_DIR_SOURCE_ROOT = str(pathlib.Path(__file__).parents[2])
17sys.path.append(os.path.join(_DIR_SOURCE_ROOT, 'build', 'android', 'gyp'))
18from util import build_utils
19
20
21def main():
22  parser = argparse.ArgumentParser()
23  parser.add_argument('--input', required=True, help='Input path')
24  parser.add_argument('--output', required=True, help='Output path')
25  parser.add_argument('--bundle-wrapper', help='APK operations script path')
26  parser.add_argument('--fuse-apk',
27                      help='Create single .apk rather than using apk splits',
28                      action='store_true')
29  args = parser.parse_args()
30
31  if not args.bundle_wrapper:
32    shutil.copyfile(args.input, args.output)
33    return
34
35  with tempfile.NamedTemporaryFile(suffix='.apks') as tmp_file:
36    cmd = [
37        args.bundle_wrapper, 'build-bundle-apks', '--output-apks', tmp_file.name
38    ]
39    cmd += ['--build-mode', 'system' if args.fuse_apk else 'system_apks']
40
41    # Creates a .apks zip file that contains the system image APK(s).
42    build_utils.CheckOutput(cmd)
43
44    if args.fuse_apk:
45      with zipfile.ZipFile(tmp_file.name) as z:
46        pathlib.Path(args.output).write_bytes(z.read('system/system.apk'))
47      return
48
49    # A single .apk file means a bundle where we take only the base split.
50    if args.output.endswith('.apk'):
51      with zipfile.ZipFile(tmp_file.name) as z:
52        pathlib.Path(args.output).write_bytes(z.read('splits/base-master.apk'))
53      return
54
55    # Use the full .apks.
56    shutil.copyfile(tmp_file.name, args.output)
57
58
59if __name__ == '__main__':
60  sys.exit(main())
61