Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 15 | VERSIONS = [ |
| 16 | 'default', |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 17 | '9.0.0', |
| 18 | '8.1.0', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 19 | '7.0.0', |
| 20 | '6.0.1', |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 21 | '5.1.1', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 22 | ] |
| 23 | |
| 24 | DIRS = { |
| 25 | 'default': 'art', |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 26 | '9.0.0': 'art-9.0.0', |
| 27 | '8.1.0': 'art-8.1.0', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 28 | '7.0.0': 'art-7.0.0', |
| 29 | '6.0.1': 'art-6.0.1', |
| 30 | '5.1.1': 'art-5.1.1', |
| 31 | } |
| 32 | |
| 33 | PRODUCTS = { |
| 34 | 'default': 'angler', |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 35 | '9.0.0': 'marlin', |
| 36 | '8.1.0': 'marlin', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 37 | '7.0.0': 'angler', |
| 38 | '6.0.1': 'angler', |
| 39 | '5.1.1': 'mako', |
| 40 | } |
| 41 | |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 42 | ARCHS = { |
| 43 | 'default': 'arm64', |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 44 | '9.0.0': 'arm64', |
| 45 | '8.1.0': 'arm64', |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 46 | '7.0.0': 'arm64', |
| 47 | '6.0.1': 'arm64', |
| 48 | '5.1.1': 'arm', |
| 49 | } |
| 50 | |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 51 | VERBOSE_OPTIONS = [ |
| 52 | 'verifier', |
| 53 | 'compiler', |
| 54 | 'gc', |
| 55 | 'jit', |
| 56 | 'jni', |
| 57 | 'class', |
| 58 | 'all', |
| 59 | ] |
| 60 | |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 61 | def ParseOptions(): |
| 62 | parser = optparse.OptionParser() |
| 63 | parser.add_option('--version', |
| 64 | help='Version of dex2oat. (defaults to latest, eg, tools/linux/art).', |
| 65 | choices=VERSIONS, |
| 66 | default='default') |
| 67 | parser.add_option('--all', |
| 68 | help='Run dex2oat on all possible versions', |
| 69 | default=False, |
| 70 | action='store_true') |
| 71 | parser.add_option('--output', |
| 72 | help='Where to place the output oat (defaults to no output / temp file).', |
| 73 | default=None) |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 74 | parser.add_option('--verbose', |
| 75 | help='Enable verbose dex2oat logging.', |
| 76 | choices=VERBOSE_OPTIONS, |
| 77 | default=None) |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 78 | return parser.parse_args() |
| 79 | |
| 80 | def Main(): |
| 81 | (options, args) = ParseOptions() |
| 82 | if len(args) != 1: |
| 83 | print "Can only take a single dex/zip/jar/apk file as input." |
| 84 | return 1 |
| 85 | if options.all and options.output: |
| 86 | print "Can't write output when running all versions." |
| 87 | return 1 |
| 88 | dexfile = args[0] |
| 89 | oatfile = options.output |
| 90 | versions = VERSIONS if options.all else [options.version] |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 91 | verbose = [options.verbose] if options.verbose else [] |
| 92 | if 'all' in verbose: |
| 93 | verbose = [x for x in VERBOSE_OPTIONS if x is not 'all'] |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 94 | for version in versions: |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 95 | run(dexfile, oatfile, version, verbose) |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 96 | print |
| 97 | return 0 |
| 98 | |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 99 | def run(dexfile, oatfile=None, version='default', verbose=[]): |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 100 | # dex2oat accepts non-existent dex files, check here instead |
| 101 | if not os.path.exists(dexfile): |
| 102 | raise Exception('DEX file not found: "{}"'.format(dexfile)) |
| 103 | with utils.TempDir() as temp: |
| 104 | if not oatfile: |
| 105 | oatfile = os.path.join(temp, "out.oat") |
| 106 | base = os.path.join(LINUX_DIR, DIRS[version]) |
| 107 | product = PRODUCTS[version] |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 108 | arch = ARCHS[version] |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 109 | cmd = [ |
| 110 | os.path.join(base, 'bin', 'dex2oat'), |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 111 | '--android-root=' + os.path.join(base, 'product', product, 'system'), |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 112 | '--runtime-arg', |
| 113 | '-Xnorelocate', |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 114 | '--dex-file=' + dexfile, |
| 115 | '--oat-file=' + oatfile, |
Ian Zerny | 08cd386 | 2018-11-27 13:00:02 +0100 | [diff] [blame] | 116 | '--instruction-set=' + arch, |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 117 | ] |
Ian Zerny | 4269d59 | 2019-01-16 10:40:38 +0100 | [diff] [blame] | 118 | for flag in verbose: |
| 119 | cmd += ['--runtime-arg', '-verbose:' + flag] |
Ian Zerny | 4a2d599 | 2018-11-08 11:42:07 +0100 | [diff] [blame] | 120 | env = {"LD_LIBRARY_PATH": os.path.join(base, 'lib')} |
| 121 | utils.PrintCmd(cmd) |
| 122 | subprocess.check_call(cmd, env = env) |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | sys.exit(Main()) |