| 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 logging |
| 8 import optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 import tempfile | 11 import tempfile |
| 12 import zipfile | 12 import zipfile |
| 13 | 13 |
| 14 from util import build_utils | 14 from util import build_utils |
| 15 from util import md5_check |
| 15 | 16 |
| 16 | 17 |
| 17 def _CreateCombinedMainDexList(main_dex_list_paths): | 18 def DoMultiDex(options, paths): |
| 18 main_dex_list = [] | 19 main_dex_list = [] |
| 19 for m in main_dex_list_paths: | 20 main_dex_list_files = build_utils.ParseGypList(options.main_dex_list_paths) |
| 21 for m in main_dex_list_files: |
| 20 with open(m) as main_dex_list_file: | 22 with open(m) as main_dex_list_file: |
| 21 main_dex_list.extend(l for l in main_dex_list_file if l) | 23 main_dex_list.extend(l for l in main_dex_list_file if l) |
| 22 return '\n'.join(main_dex_list) | 24 |
| 25 with tempfile.NamedTemporaryFile(suffix='.txt') as combined_main_dex_list: |
| 26 combined_main_dex_list.write('\n'.join(main_dex_list)) |
| 27 combined_main_dex_list.flush() |
| 28 |
| 29 dex_args = [ |
| 30 '--multi-dex', |
| 31 '--minimal-main-dex', |
| 32 '--main-dex-list=%s' % combined_main_dex_list.name |
| 33 ] |
| 34 |
| 35 DoDex(options, paths, dex_args=dex_args) |
| 36 |
| 37 if options.dex_path.endswith('.zip'): |
| 38 iz = zipfile.ZipFile(options.dex_path, 'r') |
| 39 tmp_dex_path = '%s.tmp.zip' % options.dex_path |
| 40 oz = zipfile.ZipFile(tmp_dex_path, 'w', zipfile.ZIP_DEFLATED) |
| 41 for i in iz.namelist(): |
| 42 if i.endswith('.dex'): |
| 43 oz.writestr(i, iz.read(i)) |
| 44 os.remove(options.dex_path) |
| 45 os.rename(tmp_dex_path, options.dex_path) |
| 23 | 46 |
| 24 | 47 |
| 25 def _RemoveUnwantedFilesFromZip(dex_path): | 48 def DoDex(options, paths, dex_args=None): |
| 26 iz = zipfile.ZipFile(dex_path, 'r') | 49 dx_binary = os.path.join(options.android_sdk_tools, 'dx') |
| 27 tmp_dex_path = '%s.tmp.zip' % dex_path | 50 # See http://crbug.com/272064 for context on --force-jumbo. |
| 28 oz = zipfile.ZipFile(tmp_dex_path, 'w', zipfile.ZIP_DEFLATED) | 51 # See https://github.com/android/platform_dalvik/commit/dd140a22d for |
| 29 for i in iz.namelist(): | 52 # --num-threads. |
| 30 if i.endswith('.dex'): | 53 dex_cmd = [dx_binary, '--num-threads=8', '--dex', '--force-jumbo', |
| 31 oz.writestr(i, iz.read(i)) | 54 '--output', options.dex_path] |
| 32 os.remove(dex_path) | 55 if options.no_locals != '0': |
| 33 os.rename(tmp_dex_path, dex_path) | 56 dex_cmd.append('--no-locals') |
| 57 |
| 58 if dex_args: |
| 59 dex_cmd += dex_args |
| 60 |
| 61 dex_cmd += paths |
| 62 |
| 63 record_path = '%s.md5.stamp' % options.dex_path |
| 64 md5_check.CallAndRecordIfStale( |
| 65 lambda: build_utils.CheckOutput(dex_cmd, print_stderr=False), |
| 66 record_path=record_path, |
| 67 input_paths=paths, |
| 68 input_strings=dex_cmd, |
| 69 force=not os.path.exists(options.dex_path)) |
| 70 build_utils.WriteJson( |
| 71 [os.path.relpath(p, options.output_directory) for p in paths], |
| 72 options.dex_path + '.inputs') |
| 34 | 73 |
| 35 | 74 |
| 36 def _ParseArgs(args): | 75 def main(): |
| 37 args = build_utils.ExpandFileArgs(args) | 76 args = build_utils.ExpandFileArgs(sys.argv[1:]) |
| 38 | 77 |
| 39 parser = optparse.OptionParser() | 78 parser = optparse.OptionParser() |
| 40 build_utils.AddDepfileOption(parser) | 79 build_utils.AddDepfileOption(parser) |
| 41 | 80 |
| 42 parser.add_option('--android-sdk-tools', | 81 parser.add_option('--android-sdk-tools', |
| 43 help='Android sdk build tools directory.') | 82 help='Android sdk build tools directory.') |
| 44 parser.add_option('--output-directory', | 83 parser.add_option('--output-directory', |
| 45 default=os.getcwd(), | 84 default=os.getcwd(), |
| 46 help='Path to the output build directory.') | 85 help='Path to the output build directory.') |
| 47 parser.add_option('--dex-path', help='Dex output path.') | 86 parser.add_option('--dex-path', help='Dex output path.') |
| (...skipping 13 matching lines...) Expand all Loading... |
| 61 help='A list of paths to exclude from the dex file.') | 100 help='A list of paths to exclude from the dex file.') |
| 62 parser.add_option('--main-dex-list-paths', | 101 parser.add_option('--main-dex-list-paths', |
| 63 help='A list of paths containing a list of the classes to ' | 102 help='A list of paths containing a list of the classes to ' |
| 64 'include in the main dex.') | 103 'include in the main dex.') |
| 65 | 104 |
| 66 options, paths = parser.parse_args(args) | 105 options, paths = parser.parse_args(args) |
| 67 | 106 |
| 68 required_options = ('android_sdk_tools',) | 107 required_options = ('android_sdk_tools',) |
| 69 build_utils.CheckOptions(options, parser, required=required_options) | 108 build_utils.CheckOptions(options, parser, required=required_options) |
| 70 | 109 |
| 71 if options.multi_dex and not options.main_dex_list_paths: | |
| 72 logging.warning('--multi-dex is unused without --main-dex-list-paths') | |
| 73 options.multi_dex = False | |
| 74 elif options.main_dex_list_paths and not options.multi_dex: | |
| 75 logging.warning('--main-dex-list-paths is unused without --multi-dex') | |
| 76 | |
| 77 if options.main_dex_list_paths: | |
| 78 options.main_dex_list_paths = build_utils.ParseGypList( | |
| 79 options.main_dex_list_paths) | |
| 80 if options.inputs: | |
| 81 options.inputs = build_utils.ParseGypList(options.inputs) | |
| 82 if options.excluded_paths: | |
| 83 options.excluded_paths = build_utils.ParseGypList(options.excluded_paths) | |
| 84 | |
| 85 return options, paths | |
| 86 | |
| 87 | |
| 88 def _OnStaleMd5(options, dex_cmd, paths): | |
| 89 if options.multi_dex: | |
| 90 combined_main_dex_list = tempfile.NamedTemporaryFile(suffix='.txt') | |
| 91 combined_main_dex_list.write( | |
| 92 _CreateCombinedMainDexList(options.main_dex_list_paths)) | |
| 93 combined_main_dex_list.flush() | |
| 94 dex_cmd.append('--main-dex-list=%s' % combined_main_dex_list.name) | |
| 95 | |
| 96 dex_cmd += paths | |
| 97 | |
| 98 build_utils.CheckOutput(dex_cmd, print_stderr=False) | |
| 99 | |
| 100 if options.dex_path.endswith('.zip'): | |
| 101 _RemoveUnwantedFilesFromZip(options.dex_path) | |
| 102 | |
| 103 build_utils.WriteJson( | |
| 104 [os.path.relpath(p, options.output_directory) for p in paths], | |
| 105 options.dex_path + '.inputs') | |
| 106 | |
| 107 | |
| 108 def main(args): | |
| 109 options, paths = _ParseArgs(args) | |
| 110 if (options.proguard_enabled == 'true' | 110 if (options.proguard_enabled == 'true' |
| 111 and options.configuration_name == 'Release'): | 111 and options.configuration_name == 'Release'): |
| 112 paths = [options.proguard_enabled_input_path] | 112 paths = [options.proguard_enabled_input_path] |
| 113 | 113 |
| 114 if options.inputs: | 114 if options.inputs: |
| 115 paths += options.inputs | 115 paths += build_utils.ParseGypList(options.inputs) |
| 116 | 116 |
| 117 if options.excluded_paths: | 117 if options.excluded_paths: |
| 118 # Excluded paths are relative to the output directory. | 118 # Excluded paths are relative to the output directory. |
| 119 exclude_paths = options.excluded_paths | 119 exclude_paths = build_utils.ParseGypList(options.excluded_paths) |
| 120 paths = [p for p in paths if not | 120 paths = [p for p in paths if not |
| 121 os.path.relpath(p, options.output_directory) in exclude_paths] | 121 os.path.relpath(p, options.output_directory) in exclude_paths] |
| 122 | 122 |
| 123 input_paths = list(paths) | 123 if options.multi_dex and options.main_dex_list_paths: |
| 124 DoMultiDex(options, paths) |
| 125 else: |
| 126 if options.multi_dex: |
| 127 logging.warning('--multi-dex is unused without --main-dex-list-paths') |
| 128 elif options.main_dex_list_paths: |
| 129 logging.warning('--main-dex-list-paths is unused without --multi-dex') |
| 130 DoDex(options, paths) |
| 124 | 131 |
| 125 dx_binary = os.path.join(options.android_sdk_tools, 'dx') | 132 if options.depfile: |
| 126 # See http://crbug.com/272064 for context on --force-jumbo. | 133 build_utils.WriteDepfile( |
| 127 # See https://github.com/android/platform_dalvik/commit/dd140a22d for | 134 options.depfile, |
| 128 # --num-threads. | 135 paths + build_utils.GetPythonDependencies()) |
| 129 dex_cmd = [dx_binary, '--num-threads=8', '--dex', '--force-jumbo', | |
| 130 '--output', options.dex_path] | |
| 131 if options.no_locals != '0': | |
| 132 dex_cmd.append('--no-locals') | |
| 133 | 136 |
| 134 if options.multi_dex: | |
| 135 input_paths.extend(options.main_dex_list_paths) | |
| 136 dex_cmd += [ | |
| 137 '--multi-dex', | |
| 138 '--minimal-main-dex', | |
| 139 ] | |
| 140 | |
| 141 output_paths = [ | |
| 142 options.dex_path, | |
| 143 options.dex_path + '.inputs', | |
| 144 ] | |
| 145 | |
| 146 build_utils.CallAndWriteDepfileIfStale( | |
| 147 lambda: _OnStaleMd5(options, dex_cmd, paths), | |
| 148 options, | |
| 149 input_paths=input_paths, | |
| 150 input_strings=dex_cmd, | |
| 151 output_paths=output_paths) | |
| 152 | 137 |
| 153 | 138 |
| 154 if __name__ == '__main__': | 139 if __name__ == '__main__': |
| 155 sys.exit(main(sys.argv[1:])) | 140 sys.exit(main()) |
| OLD | NEW |