| OLD | NEW | 
|    1 #!/usr/bin/env python |    1 #!/usr/bin/env python | 
|    2 # |    2 # | 
|    3 # Copyright (c) 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 re |  | 
|   12 import shlex |   11 import shlex | 
|   13 import shutil |   12 import shutil | 
|   14  |   13  | 
|   15 from util import build_utils |   14 from util import build_utils | 
|   16  |   15  | 
|   17 def ParseArgs(): |   16 def ParseArgs(): | 
|   18   """Parses command line options. |   17   """Parses command line options. | 
|   19  |   18  | 
|   20   Returns: |   19   Returns: | 
|   21     An options object as from optparse.OptionsParser.parse_args() |   20     An options object as from optparse.OptionsParser.parse_args() | 
|   22   """ |   21   """ | 
|   23   parser = optparse.OptionParser() |   22   parser = optparse.OptionParser() | 
|   24   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') | 
|   25   parser.add_option('--android-sdk-tools', |   24   parser.add_option('--android-sdk-tools', | 
|   26                     help='path to the Android SDK build tools folder') |   25                     help='path to the Android SDK build tools folder') | 
|   27   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') | 
|   28   parser.add_option('--dependencies-res-dirs', |   27   parser.add_option('--res-dirs', | 
|   29                     help='directories containing resources to be packaged') |   28                     help='directories containing resources to be packaged') | 
|   30   parser.add_option('--resource-dir', |   29   parser.add_option('--crunch-input-dir', | 
|   31                     help='directory containing this target\'s resources.') |   30                     help='directory containing images to be crunched') | 
|   32   parser.add_option('--crunch-output-dir', |   31   parser.add_option('--crunch-output-dir', | 
|   33                     help='directory to hold crunched resources') |   32                     help='directory to hold crunched resources') | 
|   34   parser.add_option('--non-constant-id', action='store_true') |   33   parser.add_option('--non-constant-id', action='store_true') | 
|   35   parser.add_option('--custom-package', help='Java package for R.java') |   34   parser.add_option('--custom-package', help='Java package for R.java') | 
|   36   parser.add_option('--android-manifest', help='AndroidManifest.xml path') |   35   parser.add_option('--android-manifest', help='AndroidManifest.xml path') | 
|   37   parser.add_option('--stamp', help='File to touch on success') |   36   parser.add_option('--stamp', help='File to touch on success') | 
|   38  |   37  | 
|   39   parser.add_option( |  | 
|   40       '--extra-res-packages', |  | 
|   41       help='Additional package names to generate R.java files for') |  | 
|   42   parser.add_option( |  | 
|   43       '--extra-r-text-files', |  | 
|   44       help='For each additional package, the R.txt file should contain a ' |  | 
|   45       'list of resources to be included in the R.java file in the format ' |  | 
|   46       'generated by aapt') |  | 
|   47  |  | 
|   48   (options, args) = parser.parse_args() |   38   (options, args) = parser.parse_args() | 
|   49  |   39  | 
|   50   if args: |   40   if args: | 
|   51     parser.error('No positional arguments should be given.') |   41     parser.error('No positional arguments should be given.') | 
|   52  |   42  | 
|   53   # Check that required options have been provided. |   43   # Check that required options have been provided. | 
|   54   required_options = ( |   44   required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', | 
|   55       'android_sdk', |   45                       'res_dirs', 'crunch_input_dir', 'crunch_output_dir') | 
|   56       'android_sdk_tools', |  | 
|   57       'android_manifest', |  | 
|   58       'dependencies_res_dirs', |  | 
|   59       'resource_dir', |  | 
|   60       'crunch_output_dir', |  | 
|   61       'R_dir', |  | 
|   62       ) |  | 
|   63   build_utils.CheckOptions(options, parser, required=required_options) |   46   build_utils.CheckOptions(options, parser, required=required_options) | 
|   64  |   47  | 
|   65   return options |   48   return options | 
|   66  |   49  | 
|   67  |   50  | 
|   68 def CreateExtraRJavaFiles( |  | 
|   69     r_dir, extra_packages, extra_r_text_files): |  | 
|   70   if len(extra_packages) != len(extra_r_text_files): |  | 
|   71     raise Exception('--extra-res-packages and --extra-r-text-files' |  | 
|   72                     'should have the same length') |  | 
|   73  |  | 
|   74   java_files = build_utils.FindInDirectory(r_dir, "R.java") |  | 
|   75   if len(java_files) != 1: |  | 
|   76     return |  | 
|   77   r_java_file = java_files[0] |  | 
|   78   r_java_contents = open(r_java_file).read() |  | 
|   79  |  | 
|   80   for package in extra_packages: |  | 
|   81     package_r_java_dir = os.path.join(r_dir, *package.split('.')) |  | 
|   82     build_utils.MakeDirectory(package_r_java_dir) |  | 
|   83     package_r_java_path = os.path.join(package_r_java_dir, 'R.java') |  | 
|   84     open(package_r_java_path, 'w').write( |  | 
|   85         re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) |  | 
|   86     # TODO(cjhopman): These extra package's R.java files should be filtered to |  | 
|   87     # only contain the resources listed in their R.txt files. At this point, we |  | 
|   88     # have already compiled those other libraries, so doing this would only |  | 
|   89     # affect how the code in this .apk target could refer to the resources. |  | 
|   90  |  | 
|   91  |  | 
|   92  |  | 
|   93 def MoveImagesToNonMdpiFolders(res_root): |   51 def MoveImagesToNonMdpiFolders(res_root): | 
|   94   """Move images from drawable-*-mdpi-* folders to drawable-* folders. |   52   """Move images from drawable-*-mdpi-* folders to drawable-* folders. | 
|   95  |   53  | 
|   96   Why? http://crbug.com/289843 |   54   Why? http://crbug.com/289843 | 
|   97   """ |   55   """ | 
|   98   for src_dir_name in os.listdir(res_root): |   56   for src_dir_name in os.listdir(res_root): | 
|   99     src_components = src_dir_name.split('-') |   57     src_components = src_dir_name.split('-') | 
|  100     if src_components[0] != 'drawable' or 'mdpi' not in src_components: |   58     if src_components[0] != 'drawable' or 'mdpi' not in src_components: | 
|  101       continue |   59       continue | 
|  102     src_dir = os.path.join(res_root, src_dir_name) |   60     src_dir = os.path.join(res_root, src_dir_name) | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|  130     if line and not 'libpng warning' in line: |   88     if line and not 'libpng warning' in line: | 
|  131       return True |   89       return True | 
|  132   return False |   90   return False | 
|  133  |   91  | 
|  134  |   92  | 
|  135 def main(): |   93 def main(): | 
|  136   options = ParseArgs() |   94   options = ParseArgs() | 
|  137   android_jar = os.path.join(options.android_sdk, 'android.jar') |   95   android_jar = os.path.join(options.android_sdk, 'android.jar') | 
|  138   aapt = os.path.join(options.android_sdk_tools, 'aapt') |   96   aapt = os.path.join(options.android_sdk_tools, 'aapt') | 
|  139  |   97  | 
|  140   build_utils.DeleteDirectory(options.R_dir) |  | 
|  141   build_utils.MakeDirectory(options.R_dir) |   98   build_utils.MakeDirectory(options.R_dir) | 
|  142  |   99  | 
|  143   # Generate R.java. This R.java contains non-final constants and is used only |  100   # Generate R.java. This R.java contains non-final constants and is used only | 
|  144   # while compiling the library jar (e.g. chromium_content.jar). When building |  101   # while compiling the library jar (e.g. chromium_content.jar). When building | 
|  145   # an apk, a new R.java file with the correct resource -> ID mappings will be |  102   # an apk, a new R.java file with the correct resource -> ID mappings will be | 
|  146   # generated by merging the resources from all libraries and the main apk |  103   # generated by merging the resources from all libraries and the main apk | 
|  147   # project. |  104   # project. | 
|  148   package_command = [aapt, |  105   package_command = [aapt, | 
|  149                      'package', |  106                      'package', | 
|  150                      '-m', |  107                      '-m', | 
|  151                      '-M', options.android_manifest, |  108                      '-M', options.android_manifest, | 
|  152                      '--auto-add-overlay', |  109                      '--auto-add-overlay', | 
|  153                      '-I', android_jar, |  110                      '-I', android_jar, | 
|  154                      '--output-text-symbols', options.R_dir, |  111                      '--output-text-symbols', options.R_dir, | 
|  155                      '-J', options.R_dir] |  112                      '-J', options.R_dir] | 
|  156   all_res_dirs = ([options.resource_dir] |  113   res_dirs = shlex.split(options.res_dirs) | 
|  157                   + shlex.split(options.dependencies_res_dirs)) |  114   for res_dir in res_dirs: | 
|  158   for res_dir in all_res_dirs: |  | 
|  159     package_command += ['-S', res_dir] |  115     package_command += ['-S', res_dir] | 
|  160   if options.non_constant_id: |  116   if options.non_constant_id: | 
|  161     package_command.append('--non-constant-id') |  117     package_command.append('--non-constant-id') | 
|  162   if options.custom_package: |  118   if options.custom_package: | 
|  163     package_command += ['--custom-package', options.custom_package] |  119     package_command += ['--custom-package', options.custom_package] | 
|  164  |  | 
|  165   build_utils.CheckOutput(package_command) |  120   build_utils.CheckOutput(package_command) | 
|  166  |  121  | 
|  167   if options.extra_res_packages: |  | 
|  168     CreateExtraRJavaFiles( |  | 
|  169         options.R_dir, |  | 
|  170         build_utils.ParseGypList(options.extra_res_packages), |  | 
|  171         build_utils.ParseGypList(options.extra_r_text_files)) |  | 
|  172  |  | 
|  173   # Crunch image resources. This shrinks png files and is necessary for 9-patch |  122   # Crunch image resources. This shrinks png files and is necessary for 9-patch | 
|  174   # images to display correctly. |  123   # images to display correctly. | 
|  175   build_utils.DeleteDirectory(options.crunch_output_dir) |  | 
|  176   build_utils.MakeDirectory(options.crunch_output_dir) |  124   build_utils.MakeDirectory(options.crunch_output_dir) | 
|  177   aapt_cmd = [aapt, |  125   aapt_cmd = [aapt, | 
|  178               'crunch', |  126               'crunch', | 
|  179               '-S', options.resource_dir, |  127               '-S', options.crunch_input_dir, | 
|  180               '-C', options.crunch_output_dir] |  128               '-C', options.crunch_output_dir] | 
|  181   build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) |  129   build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) | 
|  182  |  130  | 
|  183   MoveImagesToNonMdpiFolders(options.crunch_output_dir) |  131   MoveImagesToNonMdpiFolders(options.crunch_output_dir) | 
|  184  |  132  | 
|  185   if options.stamp: |  133   if options.stamp: | 
|  186     build_utils.Touch(options.stamp) |  134     build_utils.Touch(options.stamp) | 
|  187  |  135  | 
|  188  |  136  | 
|  189 if __name__ == '__main__': |  137 if __name__ == '__main__': | 
|  190   main() |  138   main() | 
| OLD | NEW |