| 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 | |
| 16 | 15 |
| 17 | 16 |
| 18 def DoMultiDex(options, paths): | 17 def _CreateCombinedMainDexList(main_dex_list_paths): |
| 19 main_dex_list = [] | 18 main_dex_list = [] |
| 20 main_dex_list_files = build_utils.ParseGypList(options.main_dex_list_paths) | 19 for m in main_dex_list_paths: |
| 21 for m in main_dex_list_files: | |
| 22 with open(m) as main_dex_list_file: | 20 with open(m) as main_dex_list_file: |
| 23 main_dex_list.extend(l for l in main_dex_list_file if l) | 21 main_dex_list.extend(l for l in main_dex_list_file if l) |
| 24 | 22 return '\n'.join(main_dex_list) |
| 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) | |
| 46 | 23 |
| 47 | 24 |
| 48 def DoDex(options, paths, dex_args=None): | 25 def _RemoveUnwantedFilesFromZip(dex_path): |
| 49 dx_binary = os.path.join(options.android_sdk_tools, 'dx') | 26 iz = zipfile.ZipFile(dex_path, 'r') |
| 50 # See http://crbug.com/272064 for context on --force-jumbo. | 27 tmp_dex_path = '%s.tmp.zip' % dex_path |
| 51 # See https://github.com/android/platform_dalvik/commit/dd140a22d for | 28 oz = zipfile.ZipFile(tmp_dex_path, 'w', zipfile.ZIP_DEFLATED) |
| 52 # --num-threads. | 29 for i in iz.namelist(): |
| 53 dex_cmd = [dx_binary, '--num-threads=8', '--dex', '--force-jumbo', | 30 if i.endswith('.dex'): |
| 54 '--output', options.dex_path] | 31 oz.writestr(i, iz.read(i)) |
| 55 if options.no_locals != '0': | 32 os.remove(dex_path) |
| 56 dex_cmd.append('--no-locals') | 33 os.rename(tmp_dex_path, dex_path) |
| 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') | |
| 73 | 34 |
| 74 | 35 |
| 75 def main(): | 36 def _ParseArgs(args): |
| 76 args = build_utils.ExpandFileArgs(sys.argv[1:]) | 37 args = build_utils.ExpandFileArgs(args) |
| 77 | 38 |
| 78 parser = optparse.OptionParser() | 39 parser = optparse.OptionParser() |
| 79 build_utils.AddDepfileOption(parser) | 40 build_utils.AddDepfileOption(parser) |
| 80 | 41 |
| 81 parser.add_option('--android-sdk-tools', | 42 parser.add_option('--android-sdk-tools', |
| 82 help='Android sdk build tools directory.') | 43 help='Android sdk build tools directory.') |
| 83 parser.add_option('--output-directory', | 44 parser.add_option('--output-directory', |
| 84 default=os.getcwd(), | 45 default=os.getcwd(), |
| 85 help='Path to the output build directory.') | 46 help='Path to the output build directory.') |
| 86 parser.add_option('--dex-path', help='Dex output path.') | 47 parser.add_option('--dex-path', help='Dex output path.') |
| (...skipping 13 matching lines...) Expand all Loading... |
| 100 help='A list of paths to exclude from the dex file.') | 61 help='A list of paths to exclude from the dex file.') |
| 101 parser.add_option('--main-dex-list-paths', | 62 parser.add_option('--main-dex-list-paths', |
| 102 help='A list of paths containing a list of the classes to ' | 63 help='A list of paths containing a list of the classes to ' |
| 103 'include in the main dex.') | 64 'include in the main dex.') |
| 104 | 65 |
| 105 options, paths = parser.parse_args(args) | 66 options, paths = parser.parse_args(args) |
| 106 | 67 |
| 107 required_options = ('android_sdk_tools',) | 68 required_options = ('android_sdk_tools',) |
| 108 build_utils.CheckOptions(options, parser, required=required_options) | 69 build_utils.CheckOptions(options, parser, required=required_options) |
| 109 | 70 |
| 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 += build_utils.ParseGypList(options.inputs) | 115 paths += 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 = build_utils.ParseGypList(options.excluded_paths) | 119 exclude_paths = 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 if options.multi_dex and options.main_dex_list_paths: | 123 input_paths = 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) | |
| 131 | 124 |
| 132 if options.depfile: | 125 dx_binary = os.path.join(options.android_sdk_tools, 'dx') |
| 133 build_utils.WriteDepfile( | 126 # See http://crbug.com/272064 for context on --force-jumbo. |
| 134 options.depfile, | 127 # See https://github.com/android/platform_dalvik/commit/dd140a22d for |
| 135 paths + build_utils.GetPythonDependencies()) | 128 # --num-threads. |
| 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') |
| 136 | 133 |
| 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) |
| 137 | 152 |
| 138 | 153 |
| 139 if __name__ == '__main__': | 154 if __name__ == '__main__': |
| 140 sys.exit(main()) | 155 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |