| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 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 """Process Android library resources to generate R.java and crunched images.""" | 7 """Process Android library resources to generate R.java and crunched images.""" |
| 8 | 8 |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import shlex | 11 import shlex |
| 12 import shutil | 12 import shutil |
| 13 import tempfile | |
| 14 | 13 |
| 15 from util import build_utils | 14 from util import build_utils |
| 16 import mirror_images | |
| 17 | 15 |
| 18 def ParseArgs(): | 16 def ParseArgs(): |
| 19 """Parses command line options. | 17 """Parses command line options. |
| 20 | 18 |
| 21 Returns: | 19 Returns: |
| 22 An options object as from optparse.OptionsParser.parse_args() | 20 An options object as from optparse.OptionsParser.parse_args() |
| 23 """ | 21 """ |
| 24 parser = optparse.OptionParser() | 22 parser = optparse.OptionParser() |
| 25 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 23 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
| 26 parser.add_option('--android-sdk-tools', | 24 parser.add_option('--android-sdk-tools', |
| 27 help='path to the Android SDK build tools folder') | 25 help='path to the Android SDK build tools folder') |
| 28 parser.add_option('--R-dir', help='directory to hold generated R.java') | 26 parser.add_option('--R-dir', help='directory to hold generated R.java') |
| 29 parser.add_option('--res-dirs', | 27 parser.add_option('--res-dirs', |
| 30 help='directories containing resources to be packaged') | 28 help='directories containing resources to be packaged') |
| 31 parser.add_option('--image-input-dir', help='directory containing images to ' | 29 parser.add_option('--crunch-input-dir', |
| 32 'be crunched and possibly mirrored.') | 30 help='directory containing images to be crunched') |
| 33 parser.add_option('--crunch-output-dir', | 31 parser.add_option('--crunch-output-dir', |
| 34 help='directory to hold crunched resources') | 32 help='directory to hold crunched resources') |
| 35 parser.add_option('--mirror-config', help='config file specifying which ' | |
| 36 'images should be mirrored. Images will be mirrored only ' | |
| 37 'if this is provided.') | |
| 38 parser.add_option('--mirror-output-dir', | |
| 39 help='directory where mirrored images should be saved') | |
| 40 parser.add_option('--non-constant-id', action='store_true') | 33 parser.add_option('--non-constant-id', action='store_true') |
| 41 parser.add_option('--custom-package', help='Java package for R.java') | 34 parser.add_option('--custom-package', help='Java package for R.java') |
| 42 parser.add_option('--android-manifest', help='AndroidManifest.xml path') | 35 parser.add_option('--android-manifest', help='AndroidManifest.xml path') |
| 43 parser.add_option('--stamp', help='File to touch on success') | 36 parser.add_option('--stamp', help='File to touch on success') |
| 44 | 37 |
| 45 # This is part of a temporary fix for crbug.com/177552. | 38 # This is part of a temporary fix for crbug.com/177552. |
| 46 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. | 39 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. |
| 47 parser.add_option('--ignore', help='this argument is ignored') | 40 parser.add_option('--ignore', help='this argument is ignored') |
| 48 (options, args) = parser.parse_args() | 41 (options, args) = parser.parse_args() |
| 49 | 42 |
| 50 if args: | 43 if args: |
| 51 parser.error('No positional arguments should be given.') | 44 parser.error('No positional arguments should be given.') |
| 52 | 45 |
| 53 if (options.mirror_config is None) != (options.mirror_output_dir is None): | |
| 54 parser.error('--mirror-config and --mirror-output-dir must both be present ' | |
| 55 'or neither present.') | |
| 56 | |
| 57 # Check that required options have been provided. | 46 # Check that required options have been provided. |
| 58 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', | 47 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', |
| 59 'res_dirs', 'image_input_dir', 'crunch_output_dir') | 48 'res_dirs', 'crunch_input_dir', 'crunch_output_dir') |
| 60 build_utils.CheckOptions(options, parser, required=required_options) | 49 build_utils.CheckOptions(options, parser, required=required_options) |
| 61 | 50 |
| 62 return options | 51 return options |
| 63 | 52 |
| 64 | 53 |
| 65 def MoveImagesToNonMdpiFolders(res_root): | 54 def MoveImagesToNonMdpiFolders(res_root): |
| 66 """Move images from drawable-*-mdpi-* folders to drawable-* folders. | 55 """Move images from drawable-*-mdpi-* folders to drawable-* folders. |
| 67 | 56 |
| 68 Why? http://crbug.com/289843 | 57 Why? http://crbug.com/289843 |
| 69 """ | 58 """ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 81 build_utils.MakeDirectory(dst_dir) | 70 build_utils.MakeDirectory(dst_dir) |
| 82 for src_file_name in os.listdir(src_dir): | 71 for src_file_name in os.listdir(src_dir): |
| 83 if not src_file_name.endswith('.png'): | 72 if not src_file_name.endswith('.png'): |
| 84 continue | 73 continue |
| 85 src_file = os.path.join(src_dir, src_file_name) | 74 src_file = os.path.join(src_dir, src_file_name) |
| 86 dst_file = os.path.join(dst_dir, src_file_name) | 75 dst_file = os.path.join(dst_dir, src_file_name) |
| 87 assert not os.path.lexists(dst_file) | 76 assert not os.path.lexists(dst_file) |
| 88 shutil.move(src_file, dst_file) | 77 shutil.move(src_file, dst_file) |
| 89 | 78 |
| 90 | 79 |
| 91 def CrunchImages(aapt, input_res_dir, output_res_dir): | |
| 92 build_utils.MakeDirectory(output_res_dir) | |
| 93 aapt_cmd = [aapt, | |
| 94 'crunch', | |
| 95 '-S', input_res_dir, | |
| 96 '-C', output_res_dir] | |
| 97 build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True) | |
| 98 | |
| 99 | |
| 100 def main(): | 80 def main(): |
| 101 options = ParseArgs() | 81 options = ParseArgs() |
| 102 android_jar = os.path.join(options.android_sdk, 'android.jar') | 82 android_jar = os.path.join(options.android_sdk, 'android.jar') |
| 103 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 83 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
| 104 | 84 |
| 105 build_utils.MakeDirectory(options.R_dir) | 85 build_utils.MakeDirectory(options.R_dir) |
| 106 | 86 |
| 107 # Generate R.java. This R.java contains non-final constants and is used only | 87 # Generate R.java. This R.java contains non-final constants and is used only |
| 108 # while compiling the library jar (e.g. chromium_content.jar). When building | 88 # while compiling the library jar (e.g. chromium_content.jar). When building |
| 109 # an apk, a new R.java file with the correct resource -> ID mappings will be | 89 # an apk, a new R.java file with the correct resource -> ID mappings will be |
| 110 # generated by merging the resources from all libraries and the main apk | 90 # generated by merging the resources from all libraries and the main apk |
| 111 # project. | 91 # project. |
| 112 package_command = [aapt, | 92 package_command = [aapt, |
| 113 'package', | 93 'package', |
| 114 '-m', | 94 '-m', |
| 115 '-M', options.android_manifest, | 95 '-M', options.android_manifest, |
| 116 '--auto-add-overlay', | 96 '--auto-add-overlay', |
| 117 '-I', android_jar, | 97 '-I', android_jar, |
| 118 '--output-text-symbols', options.R_dir, | 98 '--output-text-symbols', options.R_dir, |
| 119 '-J', options.R_dir] | 99 '-J', options.R_dir] |
| 120 res_dirs = shlex.split(options.res_dirs) | 100 res_dirs = shlex.split(options.res_dirs) |
| 121 for res_dir in res_dirs: | 101 for res_dir in res_dirs: |
| 122 package_command += ['-S', res_dir] | 102 package_command += ['-S', res_dir] |
| 123 if options.non_constant_id: | 103 if options.non_constant_id: |
| 124 package_command.append('--non-constant-id') | 104 package_command.append('--non-constant-id') |
| 125 if options.custom_package: | 105 if options.custom_package: |
| 126 package_command += ['--custom-package', options.custom_package] | 106 package_command += ['--custom-package', options.custom_package] |
| 127 build_utils.CheckOutput(package_command) | 107 build_utils.CheckOutput(package_command) |
| 128 | 108 |
| 129 # Mirror images if requested. | |
| 130 if options.mirror_config: | |
| 131 # Mirrored images are generated into a temp dir. Once these images are | |
| 132 # crunched, they'll go in the desired mirror output dir. | |
| 133 mirrored_uncrunched_dir = tempfile.mkdtemp() | |
| 134 try: | |
| 135 mirror_images.main(['--config', options.mirror_config, | |
| 136 '--input-res-dir', options.image_input_dir, | |
| 137 '--output-res-dir', mirrored_uncrunched_dir]) | |
| 138 CrunchImages(aapt, mirrored_uncrunched_dir, options.mirror_output_dir) | |
| 139 finally: | |
| 140 shutil.rmtree(mirrored_uncrunched_dir) | |
| 141 | |
| 142 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 109 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
| 143 # images to display correctly. | 110 # images to display correctly. |
| 144 CrunchImages(aapt, options.image_input_dir, options.crunch_output_dir) | 111 build_utils.MakeDirectory(options.crunch_output_dir) |
| 112 aapt_cmd = [aapt, |
| 113 'crunch', |
| 114 '-S', options.crunch_input_dir, |
| 115 '-C', options.crunch_output_dir] |
| 116 build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True) |
| 117 |
| 118 MoveImagesToNonMdpiFolders(options.crunch_output_dir) |
| 145 | 119 |
| 146 if options.stamp: | 120 if options.stamp: |
| 147 build_utils.Touch(options.stamp) | 121 build_utils.Touch(options.stamp) |
| 148 | 122 |
| 149 | 123 |
| 150 if __name__ == '__main__': | 124 if __name__ == '__main__': |
| 151 main() | 125 main() |
| OLD | NEW |