Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / gyp / dex.py
blob898ee8f66f959e62135db123224fdb83db2ddaae
1 #!/usr/bin/env python
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
5 # found in the LICENSE file.
7 import logging
8 import optparse
9 import os
10 import sys
11 import tempfile
12 import zipfile
14 from util import build_utils
15 from util import md5_check
18 def DoMultiDex(options, paths):
19 main_dex_list = []
20 main_dex_list_files = build_utils.ParseGypList(options.main_dex_list_paths)
21 for m in main_dex_list_files:
22 with open(m) as main_dex_list_file:
23 main_dex_list.extend(l for l in main_dex_list_file if l)
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()
29 dex_args = [
30 '--multi-dex',
31 '--minimal-main-dex',
32 '--main-dex-list=%s' % combined_main_dex_list.name
35 DoDex(options, paths, dex_args=dex_args)
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)
48 def DoDex(options, paths, dex_args=None):
49 dx_binary = os.path.join(options.android_sdk_tools, 'dx')
50 # See http://crbug.com/272064 for context on --force-jumbo.
51 # See https://github.com/android/platform_dalvik/commit/dd140a22d for
52 # --num-threads.
53 dex_cmd = [dx_binary, '--num-threads=8', '--dex', '--force-jumbo',
54 '--output', options.dex_path]
55 if options.no_locals != '0':
56 dex_cmd.append('--no-locals')
58 if dex_args:
59 dex_cmd += dex_args
61 dex_cmd += paths
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')
75 def main():
76 args = build_utils.ExpandFileArgs(sys.argv[1:])
78 parser = optparse.OptionParser()
79 build_utils.AddDepfileOption(parser)
81 parser.add_option('--android-sdk-tools',
82 help='Android sdk build tools directory.')
83 parser.add_option('--output-directory',
84 default=os.getcwd(),
85 help='Path to the output build directory.')
86 parser.add_option('--dex-path', help='Dex output path.')
87 parser.add_option('--configuration-name',
88 help='The build CONFIGURATION_NAME.')
89 parser.add_option('--proguard-enabled',
90 help='"true" if proguard is enabled.')
91 parser.add_option('--proguard-enabled-input-path',
92 help=('Path to dex in Release mode when proguard '
93 'is enabled.'))
94 parser.add_option('--no-locals',
95 help='Exclude locals list from the dex file.')
96 parser.add_option('--multi-dex', default=False, action='store_true',
97 help='Create multiple dex files.')
98 parser.add_option('--inputs', help='A list of additional input paths.')
99 parser.add_option('--excluded-paths',
100 help='A list of paths to exclude from the dex file.')
101 parser.add_option('--main-dex-list-paths',
102 help='A list of paths containing a list of the classes to '
103 'include in the main dex.')
105 options, paths = parser.parse_args(args)
107 required_options = ('android_sdk_tools',)
108 build_utils.CheckOptions(options, parser, required=required_options)
110 if (options.proguard_enabled == 'true'
111 and options.configuration_name == 'Release'):
112 paths = [options.proguard_enabled_input_path]
114 if options.inputs:
115 paths += build_utils.ParseGypList(options.inputs)
117 if options.excluded_paths:
118 # Excluded paths are relative to the output directory.
119 exclude_paths = build_utils.ParseGypList(options.excluded_paths)
120 paths = [p for p in paths if not
121 os.path.relpath(p, options.output_directory) in exclude_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)
132 if options.depfile:
133 build_utils.WriteDepfile(
134 options.depfile,
135 paths + build_utils.GetPythonDependencies())
139 if __name__ == '__main__':
140 sys.exit(main())