Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 2 | # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | import optparse |
| 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | import utils |
| 12 | |
| 13 | LINUX_DIR = os.path.join(utils.TOOLS_DIR, 'linux') |
| 14 | |
Ian Zerny | e43058e | 2022-11-11 11:37:47 +0100 | [diff] [blame] | 15 | LATEST = '12.0.0' |
| 16 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 17 | VERSIONS = [ |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 18 | '12.0.0', |
| 19 | # TODO(b/258170524): Fix the broken dex2oat versions. |
| 20 | # 'default', |
| 21 | # '9.0.0', |
| 22 | # '8.1.0', |
| 23 | # '7.0.0', |
| 24 | '6.0.1', |
| 25 | # '5.1.1', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 26 | ] |
| 27 | |
| 28 | DIRS = { |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 29 | '12.0.0': 'host/art-12.0.0-beta4', |
| 30 | 'default': 'art', |
| 31 | '9.0.0': 'art-9.0.0', |
| 32 | '8.1.0': 'art-8.1.0', |
| 33 | '7.0.0': 'art-7.0.0', |
| 34 | '6.0.1': 'art-6.0.1', |
| 35 | '5.1.1': 'art-5.1.1', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | PRODUCTS = { |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 39 | '12.0.0': 'redfin', |
| 40 | 'default': 'angler', |
| 41 | '9.0.0': 'marlin', |
| 42 | '8.1.0': 'marlin', |
| 43 | '7.0.0': 'angler', |
| 44 | '6.0.1': 'angler', |
| 45 | '5.1.1': 'mako', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 48 | ARCHS = { |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 49 | '12.0.0': 'x86_64', |
| 50 | 'default': 'arm64', |
| 51 | '9.0.0': 'arm64', |
| 52 | '8.1.0': 'arm64', |
| 53 | '7.0.0': 'arm64', |
| 54 | '6.0.1': 'arm64', |
| 55 | '5.1.1': 'arm', |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 58 | VERBOSE_OPTIONS = [ |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 59 | 'verifier', |
| 60 | 'compiler', |
| 61 | 'gc', |
| 62 | 'jit', |
| 63 | 'jni', |
| 64 | 'class', |
| 65 | 'all', |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 66 | ] |
| 67 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 68 | BOOT_IMAGE = {'12.0.0': 'apex/art_boot_images/javalib/boot.art'} |
| 69 | |
Ian Zerny | e43058e | 2022-11-11 11:37:47 +0100 | [diff] [blame] | 70 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 71 | def ParseOptions(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 72 | parser = optparse.OptionParser() |
| 73 | parser.add_option('--version', |
| 74 | help='Version of dex2oat. (defaults to latest: ' + |
| 75 | LATEST + ').', |
| 76 | choices=VERSIONS, |
| 77 | default=LATEST) |
| 78 | parser.add_option( |
| 79 | '--device', |
| 80 | help='Run dex2oat on this device (this is passed as the -s SERIAL.') |
| 81 | parser.add_option('--all', |
| 82 | help='Run dex2oat on all possible versions', |
| 83 | default=False, |
| 84 | action='store_true') |
| 85 | parser.add_option( |
| 86 | '--output', |
| 87 | help= |
| 88 | 'Where to place the output oat (defaults to no output / temp file).', |
| 89 | default=None) |
| 90 | parser.add_option('--verbose', |
| 91 | help='Enable verbose dex2oat logging.', |
| 92 | choices=VERBOSE_OPTIONS, |
| 93 | default=None) |
| 94 | return parser.parse_args() |
| 95 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 96 | |
| 97 | def Main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 98 | (options, args) = ParseOptions() |
| 99 | if len(args) != 1: |
| 100 | print("Can only take a single dex/zip/jar/apk file as input.") |
| 101 | return 1 |
| 102 | if (options.device): |
| 103 | return run_device_dex2oat(options, args) |
| 104 | else: |
| 105 | return run_host_dex2oat(options, args) |
| 106 | |
Søren Gjesse | afbf8fc | 2023-08-17 13:52:16 +0200 | [diff] [blame] | 107 | |
| 108 | def run_host_dex2oat(options, args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 109 | if options.all and options.output: |
| 110 | print("Can't write output when running all versions.") |
| 111 | return 1 |
| 112 | dexfile = args[0] |
| 113 | oatfile = options.output |
| 114 | versions = VERSIONS if options.all else [options.version] |
| 115 | for version in versions: |
| 116 | run(options, dexfile, oatfile, version) |
| 117 | print("") |
| 118 | return 0 |
| 119 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 120 | |
Søren Gjesse | afbf8fc | 2023-08-17 13:52:16 +0200 | [diff] [blame] | 121 | def adb_cmd(serial, *args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 122 | cmd = ['adb', '-s', serial] |
| 123 | cmd.extend(args) |
| 124 | return cmd |
| 125 | |
Søren Gjesse | afbf8fc | 2023-08-17 13:52:16 +0200 | [diff] [blame] | 126 | |
| 127 | def append_dex2oat_verbose_flags(options, cmd): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 128 | verbose = [options.verbose] if options.verbose else [] |
| 129 | if 'all' in verbose: |
| 130 | verbose = [x for x in VERBOSE_OPTIONS if x != 'all'] |
| 131 | for flag in verbose: |
| 132 | cmd += ['--runtime-arg', '-verbose:' + flag] |
| 133 | return cmd |
| 134 | |
Søren Gjesse | afbf8fc | 2023-08-17 13:52:16 +0200 | [diff] [blame] | 135 | |
| 136 | def run_device_dex2oat(options, args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 137 | serial = options.device |
| 138 | dexfile = args[0] |
| 139 | device_dexfile = '/data/local/tmp/' + os.path.basename(dexfile) |
| 140 | device_oatfile = '/data/local/tmp/unused.oat' |
| 141 | cmd = adb_cmd(serial, 'shell', 'rm', '-f', device_dexfile, device_oatfile) |
| 142 | utils.PrintCmd(cmd) |
| 143 | subprocess.check_call(cmd) |
| 144 | cmd = adb_cmd(serial, 'push', dexfile, device_dexfile) |
| 145 | utils.PrintCmd(cmd) |
| 146 | subprocess.check_call(cmd) |
| 147 | cmd = adb_cmd(serial, 'logcat', '-c') |
| 148 | utils.PrintCmd(cmd) |
| 149 | subprocess.check_call(cmd) |
| 150 | cmd = adb_cmd(serial, 'shell', 'dex2oat', '--dex-file=' + device_dexfile, |
| 151 | '--oat-file=/data/local/tmp/unused.oat') |
| 152 | append_dex2oat_verbose_flags(options, cmd) |
| 153 | utils.PrintCmd(cmd) |
| 154 | subprocess.check_call(cmd) |
| 155 | cmd = adb_cmd(serial, 'logcat', '-d', '-s', 'dex2oat') |
| 156 | utils.PrintCmd(cmd) |
| 157 | subprocess.check_call(cmd) |
| 158 | return 0 |
| 159 | |
Søren Gjesse | afbf8fc | 2023-08-17 13:52:16 +0200 | [diff] [blame] | 160 | |
| 161 | def run(options, dexfile, oatfile=None, version=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 162 | if not version: |
| 163 | version = LATEST |
| 164 | # dex2oat accepts non-existent dex files, check here instead |
| 165 | if not os.path.exists(dexfile): |
| 166 | raise Exception('DEX file not found: "{}"'.format(dexfile)) |
| 167 | with utils.TempDir() as temp: |
| 168 | if not oatfile: |
| 169 | oatfile = os.path.join(temp, "out.oat") |
| 170 | base = os.path.join(LINUX_DIR, DIRS[version]) |
| 171 | product = PRODUCTS[version] |
| 172 | arch = ARCHS[version] |
| 173 | cmd = [ |
| 174 | os.path.join(base, 'bin', 'dex2oat'), |
| 175 | '--android-root=' + |
| 176 | os.path.join(base, 'product', product, 'system'), |
| 177 | '--runtime-arg', |
| 178 | '-Xnorelocate', |
| 179 | '--dex-file=' + dexfile, |
| 180 | '--oat-file=' + oatfile, |
| 181 | '--instruction-set=' + arch, |
| 182 | ] |
| 183 | append_dex2oat_verbose_flags(options, cmd) |
| 184 | if version in BOOT_IMAGE: |
| 185 | cmd += ['--boot-image=' + BOOT_IMAGE[version]] |
| 186 | env = {"LD_LIBRARY_PATH": os.path.join(base, 'lib')} |
| 187 | utils.PrintCmd(cmd) |
| 188 | with utils.ChangedWorkingDirectory(base): |
| 189 | subprocess.check_call(cmd, env=env) |
| 190 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 191 | |
| 192 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 193 | sys.exit(Main()) |