blob: 8df8f865eb15c26ee53ecc38581a0c50c9c34f29 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Ian Zerny4a2d5992018-11-08 11:42:07 +01002# 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
6import optparse
7import os
8import subprocess
9import sys
10
11import utils
12
13LINUX_DIR = os.path.join(utils.TOOLS_DIR, 'linux')
14
Ian Zernye43058e2022-11-11 11:37:47 +010015LATEST = '12.0.0'
16
Ian Zerny4a2d5992018-11-08 11:42:07 +010017VERSIONS = [
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020018 '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 Zerny4a2d5992018-11-08 11:42:07 +010026]
27
28DIRS = {
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020029 '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 Zerny4a2d5992018-11-08 11:42:07 +010036}
37
38PRODUCTS = {
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020039 '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 Zerny4a2d5992018-11-08 11:42:07 +010046}
47
Ian Zerny08cd3862018-11-27 13:00:02 +010048ARCHS = {
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020049 '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 Zerny08cd3862018-11-27 13:00:02 +010056}
57
Ian Zerny4269d592019-01-16 10:40:38 +010058VERBOSE_OPTIONS = [
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020059 'verifier',
60 'compiler',
61 'gc',
62 'jit',
63 'jni',
64 'class',
65 'all',
Ian Zerny4269d592019-01-16 10:40:38 +010066]
67
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020068BOOT_IMAGE = {'12.0.0': 'apex/art_boot_images/javalib/boot.art'}
69
Ian Zernye43058e2022-11-11 11:37:47 +010070
Ian Zerny4a2d5992018-11-08 11:42:07 +010071def ParseOptions():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020072 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 Zerny4a2d5992018-11-08 11:42:07 +010096
97def Main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020098 (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 Gjesseafbf8fc2023-08-17 13:52:16 +0200107
108def run_host_dex2oat(options, args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200109 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 Zerny4a2d5992018-11-08 11:42:07 +0100120
Søren Gjesseafbf8fc2023-08-17 13:52:16 +0200121def adb_cmd(serial, *args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200122 cmd = ['adb', '-s', serial]
123 cmd.extend(args)
124 return cmd
125
Søren Gjesseafbf8fc2023-08-17 13:52:16 +0200126
127def append_dex2oat_verbose_flags(options, cmd):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200128 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 Gjesseafbf8fc2023-08-17 13:52:16 +0200135
136def run_device_dex2oat(options, args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200137 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 Gjesseafbf8fc2023-08-17 13:52:16 +0200160
161def run(options, dexfile, oatfile=None, version=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200162 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 Zerny4a2d5992018-11-08 11:42:07 +0100191
192if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200193 sys.exit(Main())