Home | History | Annotate | Download | only in gyp
      1 #!/usr/bin/env python
      2 #
      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
      5 # found in the LICENSE file.
      6 
      7 """Process Android library resources to generate R.java and crunched images."""
      8 
      9 import optparse
     10 import os
     11 import shlex
     12 import shutil
     13 
     14 from util import build_utils
     15 
     16 def ParseArgs():
     17   """Parses command line options.
     18 
     19   Returns:
     20     An options object as from optparse.OptionsParser.parse_args()
     21   """
     22   parser = optparse.OptionParser()
     23   parser.add_option('--android-sdk', help='path to the Android SDK folder')
     24   parser.add_option('--android-sdk-tools',
     25                     help='path to the Android SDK build tools folder')
     26   parser.add_option('--R-dir', help='directory to hold generated R.java')
     27   parser.add_option('--res-dirs',
     28                     help='directories containing resources to be packaged')
     29   parser.add_option('--crunch-input-dir',
     30                     help='directory containing images to be crunched')
     31   parser.add_option('--crunch-output-dir',
     32                     help='directory to hold crunched resources')
     33   parser.add_option('--non-constant-id', action='store_true')
     34   parser.add_option('--custom-package', help='Java package for R.java')
     35   parser.add_option('--android-manifest', help='AndroidManifest.xml path')
     36   parser.add_option('--stamp', help='File to touch on success')
     37 
     38   # This is part of a temporary fix for crbug.com/177552.
     39   # TODO(newt): remove this once crbug.com/177552 is fixed in ninja.
     40   parser.add_option('--ignore', help='this argument is ignored')
     41   (options, args) = parser.parse_args()
     42 
     43   if args:
     44     parser.error('No positional arguments should be given.')
     45 
     46   # Check that required options have been provided.
     47   required_options = ('android_sdk', 'android_sdk_tools', 'R_dir',
     48                       'res_dirs', 'crunch_input_dir', 'crunch_output_dir')
     49   build_utils.CheckOptions(options, parser, required=required_options)
     50 
     51   return options
     52 
     53 
     54 def MoveImagesToNonMdpiFolders(res_root):
     55   """Move images from drawable-*-mdpi-* folders to drawable-* folders.
     56 
     57   Why? http://crbug.com/289843
     58   """
     59   for src_dir_name in os.listdir(res_root):
     60     src_components = src_dir_name.split('-')
     61     if src_components[0] != 'drawable' or 'mdpi' not in src_components:
     62       continue
     63     src_dir = os.path.join(res_root, src_dir_name)
     64     if not os.path.isdir(src_dir):
     65       continue
     66     dst_components = [c for c in src_components if c != 'mdpi']
     67     assert dst_components != src_components
     68     dst_dir_name = '-'.join(dst_components)
     69     dst_dir = os.path.join(res_root, dst_dir_name)
     70     build_utils.MakeDirectory(dst_dir)
     71     for src_file_name in os.listdir(src_dir):
     72       if not src_file_name.endswith('.png'):
     73         continue
     74       src_file = os.path.join(src_dir, src_file_name)
     75       dst_file = os.path.join(dst_dir, src_file_name)
     76       assert not os.path.lexists(dst_file)
     77       shutil.move(src_file, dst_file)
     78 
     79 
     80 def main():
     81   options = ParseArgs()
     82   android_jar = os.path.join(options.android_sdk, 'android.jar')
     83   aapt = os.path.join(options.android_sdk_tools, 'aapt')
     84 
     85   build_utils.MakeDirectory(options.R_dir)
     86 
     87   # Generate R.java. This R.java contains non-final constants and is used only
     88   # while compiling the library jar (e.g. chromium_content.jar). When building
     89   # an apk, a new R.java file with the correct resource -> ID mappings will be
     90   # generated by merging the resources from all libraries and the main apk
     91   # project.
     92   package_command = [aapt,
     93                      'package',
     94                      '-m',
     95                      '-M', options.android_manifest,
     96                      '--auto-add-overlay',
     97                      '-I', android_jar,
     98                      '--output-text-symbols', options.R_dir,
     99                      '-J', options.R_dir]
    100   res_dirs = shlex.split(options.res_dirs)
    101   for res_dir in res_dirs:
    102     package_command += ['-S', res_dir]
    103   if options.non_constant_id:
    104     package_command.append('--non-constant-id')
    105   if options.custom_package:
    106     package_command += ['--custom-package', options.custom_package]
    107   build_utils.CheckOutput(package_command)
    108 
    109   # Crunch image resources. This shrinks png files and is necessary for 9-patch
    110   # images to display correctly.
    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)
    119 
    120   if options.stamp:
    121     build_utils.Touch(options.stamp)
    122 
    123 
    124 if __name__ == '__main__':
    125   main()
    126