OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2015 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 """Adds the code parts to a resource APK.""" | 7 """Adds the code parts to a resource APK.""" |
8 | 8 |
9 import argparse | 9 import argparse |
10 import itertools | 10 import itertools |
(...skipping 30 matching lines...) Expand all Loading... | |
41 help='Path to the output file', | 41 help='Path to the output file', |
42 required=True) | 42 required=True) |
43 parser.add_argument('--dex-file', | 43 parser.add_argument('--dex-file', |
44 help='Path to the classes.dex to use') | 44 help='Path to the classes.dex to use') |
45 # TODO(agrieve): Switch this to be a list of files rather than a directory. | 45 # TODO(agrieve): Switch this to be a list of files rather than a directory. |
46 parser.add_argument('--native-libs-dir', | 46 parser.add_argument('--native-libs-dir', |
47 help='Directory containing native libraries to include', | 47 help='Directory containing native libraries to include', |
48 default=[]) | 48 default=[]) |
49 parser.add_argument('--android-abi', | 49 parser.add_argument('--android-abi', |
50 help='Android architecture to use for native libraries') | 50 help='Android architecture to use for native libraries') |
51 parser.add_argument('--create-placeholder-lib', | 51 parser.add_argument('--native-lib-placeholders', |
52 action='store_true', | 52 help='GYP-list of native library placeholders to add.', |
53 help='Whether to add a dummy library file') | 53 default='[]') |
54 options = parser.parse_args(args) | 54 options = parser.parse_args(args) |
55 if not options.android_abi and (options.native_libs_dir or | 55 if not options.android_abi and (options.native_libs_dir or |
56 options.create_placeholder_lib): | 56 options.native_lib_placeholders): |
57 raise Exception('Must specify --android-abi with --native-libs-dir') | 57 raise Exception('Must specify --android-abi with --native-libs-dir') |
58 options.assets = build_utils.ParseGypList(options.assets) | 58 options.assets = build_utils.ParseGypList(options.assets) |
59 options.uncompressed_assets = build_utils.ParseGypList( | 59 options.uncompressed_assets = build_utils.ParseGypList( |
60 options.uncompressed_assets) | 60 options.uncompressed_assets) |
61 options.native_lib_placeholders = build_utils.ParseGypList( | |
62 options.native_lib_placeholders) | |
61 return options | 63 return options |
62 | 64 |
63 | 65 |
64 def _ListSubPaths(path): | 66 def _ListSubPaths(path): |
65 """Returns a list of full paths to all files in the given path.""" | 67 """Returns a list of full paths to all files in the given path.""" |
66 return [os.path.join(path, name) for name in os.listdir(path)] | 68 return [os.path.join(path, name) for name in os.listdir(path)] |
67 | 69 |
68 | 70 |
69 def _SplitAssetPath(path): | 71 def _SplitAssetPath(path): |
70 """Returns (src, dest) given an asset path in the form src[:dest].""" | 72 """Returns (src, dest) given an asset path in the form src[:dest].""" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 options = _ParseArgs(args) | 114 options = _ParseArgs(args) |
113 | 115 |
114 native_libs = [] | 116 native_libs = [] |
115 if options.native_libs_dir: | 117 if options.native_libs_dir: |
116 native_libs = _ListSubPaths(options.native_libs_dir) | 118 native_libs = _ListSubPaths(options.native_libs_dir) |
117 | 119 |
118 input_paths = [options.resource_apk, __file__] + native_libs | 120 input_paths = [options.resource_apk, __file__] + native_libs |
119 if options.dex_file: | 121 if options.dex_file: |
120 input_paths.append(options.dex_file) | 122 input_paths.append(options.dex_file) |
121 | 123 |
122 input_strings = [options.create_placeholder_lib, options.android_abi] | 124 input_strings = [options.android_abi, options.native_lib_placeholders] |
123 | 125 |
124 for path in itertools.chain(options.assets, options.uncompressed_assets): | 126 for path in itertools.chain(options.assets, options.uncompressed_assets): |
125 src_path, dest_path = _SplitAssetPath(path) | 127 src_path, dest_path = _SplitAssetPath(path) |
126 input_paths.append(src_path) | 128 input_paths.append(src_path) |
127 input_strings.append(dest_path) | 129 input_strings.append(dest_path) |
128 | 130 |
129 def on_stale_md5(): | 131 def on_stale_md5(): |
130 tmp_apk = options.output_apk + '.tmp' | 132 tmp_apk = options.output_apk + '.tmp' |
131 try: | 133 try: |
132 # Use a temp file to avoid creating an output if anything goes wrong. | 134 # Use a temp file to avoid creating an output if anything goes wrong. |
133 shutil.copyfile(options.resource_apk, tmp_apk) | 135 shutil.copyfile(options.resource_apk, tmp_apk) |
134 | 136 |
135 # TODO(agrieve): It would be more efficient to combine this step | 137 # TODO(agrieve): It would be more efficient to combine this step |
136 # with finalize_apk(), which sometimes aligns and uncompresses the | 138 # with finalize_apk(), which sometimes aligns and uncompresses the |
137 # native libraries. | 139 # native libraries. |
138 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: | 140 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: |
139 _AddAssets(apk, options.assets, disable_compression=False) | 141 _AddAssets(apk, options.assets, disable_compression=False) |
140 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) | 142 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) |
141 for path in native_libs: | 143 for path in native_libs: |
142 basename = os.path.basename(path) | 144 basename = os.path.basename(path) |
143 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) | 145 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) |
144 if options.create_placeholder_lib: | 146 for name in options.native_lib_placeholders: |
145 # Make it non-empty so that its checksum is non-zero and is not | 147 # Make it non-empty so that its checksum is non-zero and is not |
146 # ignored by md5_check. | 148 # ignored by md5_check. |
147 apk.writestr('lib/%s/libplaceholder.so' % options.android_abi, ':-)') | 149 apk.writestr('lib/%s/%s' % (options.android_abi, name), ':)', |
pkotwicz
2015/11/16 19:28:22
:-)
| |
150 zipfile.ZIP_STORED) | |
148 if options.dex_file: | 151 if options.dex_file: |
149 apk.write(options.dex_file, 'classes.dex') | 152 apk.write(options.dex_file, 'classes.dex') |
150 | 153 |
151 shutil.move(tmp_apk, options.output_apk) | 154 shutil.move(tmp_apk, options.output_apk) |
152 finally: | 155 finally: |
153 if os.path.exists(tmp_apk): | 156 if os.path.exists(tmp_apk): |
154 os.unlink(tmp_apk) | 157 os.unlink(tmp_apk) |
155 | 158 |
156 build_utils.CallAndWriteDepfileIfStale( | 159 build_utils.CallAndWriteDepfileIfStale( |
157 on_stale_md5, | 160 on_stale_md5, |
158 options, | 161 options, |
159 input_paths=input_paths, | 162 input_paths=input_paths, |
160 input_strings=input_strings, | 163 input_strings=input_strings, |
161 output_paths=[options.output_apk]) | 164 output_paths=[options.output_apk]) |
162 | 165 |
163 | 166 |
164 if __name__ == '__main__': | 167 if __name__ == '__main__': |
165 main(sys.argv[1:]) | 168 main(sys.argv[1:]) |
OLD | NEW |