OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
| 7 import logging |
7 import optparse | 8 import optparse |
8 import os | 9 import os |
9 import sys | 10 import sys |
| 11 import tempfile |
| 12 import zipfile |
10 | 13 |
11 from util import build_utils | 14 from util import build_utils |
12 from util import md5_check | 15 from util import md5_check |
13 | 16 |
| 17 sys.path.append(os.path.abspath(os.path.join( |
| 18 os.path.dirname(__file__), os.pardir))) |
| 19 from pylib import constants |
14 | 20 |
15 def DoDex(options, paths): | 21 |
16 dx_binary = os.path.join(options.android_sdk_tools, 'dx') | 22 def DoMultiDex(options, paths): |
| 23 main_dex_list = [] |
| 24 main_dex_list_files = build_utils.ParseGypList(options.main_dex_list_paths) |
| 25 for m in main_dex_list_files: |
| 26 with open(m) as main_dex_list_file: |
| 27 main_dex_list.extend(l for l in main_dex_list_file if l) |
| 28 |
| 29 with tempfile.NamedTemporaryFile(suffix='.txt') as combined_main_dex_list: |
| 30 combined_main_dex_list.write('\n'.join(main_dex_list)) |
| 31 combined_main_dex_list.flush() |
| 32 |
| 33 dex_args = [ |
| 34 '--multi-dex', |
| 35 '--minimal-main-dex', |
| 36 '--main-dex-list=%s' % combined_main_dex_list.name |
| 37 ] |
| 38 |
| 39 DoDex(options, paths, dex_args=dex_args) |
| 40 |
| 41 if options.dex_path.endswith('.zip'): |
| 42 iz = zipfile.ZipFile(options.dex_path, 'r') |
| 43 tmp_dex_path = '%s.tmp.zip' % options.dex_path |
| 44 oz = zipfile.ZipFile(tmp_dex_path, 'w', zipfile.ZIP_DEFLATED) |
| 45 for i in iz.namelist(): |
| 46 if i.endswith('.dex'): |
| 47 oz.writestr(i, iz.read(i)) |
| 48 os.remove(options.dex_path) |
| 49 os.rename(tmp_dex_path, options.dex_path) |
| 50 |
| 51 |
| 52 def DoDex(options, paths, dex_args=None): |
| 53 dx_binary = os.path.join(constants.ANDROID_SDK_TOOLS, 'dx') |
17 # See http://crbug.com/272064 for context on --force-jumbo. | 54 # See http://crbug.com/272064 for context on --force-jumbo. |
18 dex_cmd = [dx_binary, '--dex', '--force-jumbo', '--output', options.dex_path] | 55 dex_cmd = [dx_binary, '--dex', '--force-jumbo', '--output', options.dex_path] |
19 if options.no_locals != '0': | 56 if options.no_locals != '0': |
20 dex_cmd.append('--no-locals') | 57 dex_cmd.append('--no-locals') |
21 | 58 |
| 59 if dex_args: |
| 60 dex_cmd += dex_args |
| 61 |
22 dex_cmd += paths | 62 dex_cmd += paths |
23 | 63 |
24 record_path = '%s.md5.stamp' % options.dex_path | 64 record_path = '%s.md5.stamp' % options.dex_path |
25 md5_check.CallAndRecordIfStale( | 65 md5_check.CallAndRecordIfStale( |
26 lambda: build_utils.CheckOutput(dex_cmd, print_stderr=False), | 66 lambda: build_utils.CheckOutput(dex_cmd, print_stderr=False), |
27 record_path=record_path, | 67 record_path=record_path, |
28 input_paths=paths, | 68 input_paths=paths, |
29 input_strings=dex_cmd, | 69 input_strings=dex_cmd, |
30 force=not os.path.exists(options.dex_path)) | 70 force=not os.path.exists(options.dex_path)) |
31 build_utils.WriteJson( | 71 build_utils.WriteJson( |
32 [os.path.relpath(p, options.output_directory) for p in paths], | 72 [os.path.relpath(p, options.output_directory) for p in paths], |
33 options.dex_path + '.inputs') | 73 options.dex_path + '.inputs') |
34 | 74 |
35 | 75 |
36 def main(): | 76 def main(): |
37 args = build_utils.ExpandFileArgs(sys.argv[1:]) | 77 args = build_utils.ExpandFileArgs(sys.argv[1:]) |
38 | 78 |
39 parser = optparse.OptionParser() | 79 parser = optparse.OptionParser() |
40 build_utils.AddDepfileOption(parser) | 80 build_utils.AddDepfileOption(parser) |
41 | 81 |
42 parser.add_option('--android-sdk-tools', | |
43 help='Android sdk build tools directory.') | |
44 parser.add_option('--output-directory', | 82 parser.add_option('--output-directory', |
45 default=os.getcwd(), | 83 default=os.getcwd(), |
46 help='Path to the output build directory.') | 84 help='Path to the output build directory.') |
47 parser.add_option('--dex-path', help='Dex output path.') | 85 parser.add_option('--dex-path', help='Dex output path.') |
48 parser.add_option('--configuration-name', | 86 parser.add_option('--configuration-name', |
49 help='The build CONFIGURATION_NAME.') | 87 help='The build CONFIGURATION_NAME.') |
50 parser.add_option('--proguard-enabled', | 88 parser.add_option('--proguard-enabled', |
51 help='"true" if proguard is enabled.') | 89 help='"true" if proguard is enabled.') |
52 parser.add_option('--proguard-enabled-input-path', | 90 parser.add_option('--proguard-enabled-input-path', |
53 help=('Path to dex in Release mode when proguard ' | 91 help=('Path to dex in Release mode when proguard ' |
54 'is enabled.')) | 92 'is enabled.')) |
55 parser.add_option('--no-locals', | 93 parser.add_option('--no-locals', |
56 help='Exclude locals list from the dex file.') | 94 help='Exclude locals list from the dex file.') |
| 95 parser.add_option('--multi-dex', default=False, action='store_true', |
| 96 help='Create multiple dex files.') |
57 parser.add_option('--inputs', help='A list of additional input paths.') | 97 parser.add_option('--inputs', help='A list of additional input paths.') |
58 parser.add_option('--excluded-paths', | 98 parser.add_option('--excluded-paths', |
59 help='A list of paths to exclude from the dex file.') | 99 help='A list of paths to exclude from the dex file.') |
| 100 parser.add_option('--main-dex-list-paths', |
| 101 help='A list of paths containing a list of the classes to ' |
| 102 'include in the main dex.') |
60 | 103 |
61 options, paths = parser.parse_args(args) | 104 options, paths = parser.parse_args(args) |
62 | 105 |
63 required_options = ('android_sdk_tools',) | 106 build_utils.CheckOptions(options, parser) |
64 build_utils.CheckOptions(options, parser, required=required_options) | |
65 | 107 |
66 if (options.proguard_enabled == 'true' | 108 if (options.proguard_enabled == 'true' |
67 and options.configuration_name == 'Release'): | 109 and options.configuration_name == 'Release'): |
68 paths = [options.proguard_enabled_input_path] | 110 paths = [options.proguard_enabled_input_path] |
69 | 111 |
70 if options.inputs: | 112 if options.inputs: |
71 paths += build_utils.ParseGypList(options.inputs) | 113 paths += build_utils.ParseGypList(options.inputs) |
72 | 114 |
73 if options.excluded_paths: | 115 if options.excluded_paths: |
74 # Excluded paths are relative to the output directory. | 116 # Excluded paths are relative to the output directory. |
75 exclude_paths = build_utils.ParseGypList(options.excluded_paths) | 117 exclude_paths = build_utils.ParseGypList(options.excluded_paths) |
76 paths = [p for p in paths if not | 118 paths = [p for p in paths if not |
77 os.path.relpath(p, options.output_directory) in exclude_paths] | 119 os.path.relpath(p, options.output_directory) in exclude_paths] |
78 | 120 |
79 DoDex(options, paths) | 121 if options.multi_dex and options.main_dex_list_paths: |
| 122 DoMultiDex(options, paths) |
| 123 else: |
| 124 if options.multi_dex: |
| 125 logging.warning('--multi-dex is unused without --main-dex-list-paths') |
| 126 elif options.main_dex_list_paths: |
| 127 logging.warning('--main-dex-list-paths is unused without --multi-dex') |
| 128 DoDex(options, paths) |
80 | 129 |
81 if options.depfile: | 130 if options.depfile: |
82 build_utils.WriteDepfile( | 131 build_utils.WriteDepfile( |
83 options.depfile, | 132 options.depfile, |
84 paths + build_utils.GetPythonDependencies()) | 133 paths + build_utils.GetPythonDependencies()) |
85 | 134 |
86 | 135 |
87 | 136 |
88 if __name__ == '__main__': | 137 if __name__ == '__main__': |
89 sys.exit(main()) | 138 sys.exit(main()) |
OLD | NEW |