Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / build / android / gyp / process_resources.py
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   (options, args) = parser.parse_args()
39
40   if args:
41     parser.error('No positional arguments should be given.')
42
43   # Check that required options have been provided.
44   required_options = ('android_sdk', 'android_sdk_tools', 'R_dir',
45                       'res_dirs', 'crunch_input_dir', 'crunch_output_dir')
46   build_utils.CheckOptions(options, parser, required=required_options)
47
48   return options
49
50
51 def MoveImagesToNonMdpiFolders(res_root):
52   """Move images from drawable-*-mdpi-* folders to drawable-* folders.
53
54   Why? http://crbug.com/289843
55   """
56   for src_dir_name in os.listdir(res_root):
57     src_components = src_dir_name.split('-')
58     if src_components[0] != 'drawable' or 'mdpi' not in src_components:
59       continue
60     src_dir = os.path.join(res_root, src_dir_name)
61     if not os.path.isdir(src_dir):
62       continue
63     dst_components = [c for c in src_components if c != 'mdpi']
64     assert dst_components != src_components
65     dst_dir_name = '-'.join(dst_components)
66     dst_dir = os.path.join(res_root, dst_dir_name)
67     build_utils.MakeDirectory(dst_dir)
68     for src_file_name in os.listdir(src_dir):
69       if not src_file_name.endswith('.png'):
70         continue
71       src_file = os.path.join(src_dir, src_file_name)
72       dst_file = os.path.join(dst_dir, src_file_name)
73       assert not os.path.lexists(dst_file)
74       shutil.move(src_file, dst_file)
75
76
77 def DidCrunchFail(returncode, stderr):
78   """Determines whether aapt crunch failed from its return code and output.
79
80   Because aapt's return code cannot be trusted, any output to stderr is
81   an indication that aapt has failed (http://crbug.com/314885), except
82   lines that contain "libpng warning", which is a known non-error condition
83   (http://crbug.com/364355).
84   """
85   if returncode != 0:
86     return True
87   for line in stderr.splitlines():
88     if line and not 'libpng warning' in line:
89       return True
90   return False
91
92
93 def main():
94   options = ParseArgs()
95   android_jar = os.path.join(options.android_sdk, 'android.jar')
96   aapt = os.path.join(options.android_sdk_tools, 'aapt')
97
98   build_utils.MakeDirectory(options.R_dir)
99
100   # Generate R.java. This R.java contains non-final constants and is used only
101   # while compiling the library jar (e.g. chromium_content.jar). When building
102   # an apk, a new R.java file with the correct resource -> ID mappings will be
103   # generated by merging the resources from all libraries and the main apk
104   # project.
105   package_command = [aapt,
106                      'package',
107                      '-m',
108                      '-M', options.android_manifest,
109                      '--auto-add-overlay',
110                      '-I', android_jar,
111                      '--output-text-symbols', options.R_dir,
112                      '-J', options.R_dir]
113   res_dirs = shlex.split(options.res_dirs)
114   for res_dir in res_dirs:
115     package_command += ['-S', res_dir]
116   if options.non_constant_id:
117     package_command.append('--non-constant-id')
118   if options.custom_package:
119     package_command += ['--custom-package', options.custom_package]
120   build_utils.CheckOutput(package_command)
121
122   # Crunch image resources. This shrinks png files and is necessary for 9-patch
123   # images to display correctly.
124   build_utils.MakeDirectory(options.crunch_output_dir)
125   aapt_cmd = [aapt,
126               'crunch',
127               '-S', options.crunch_input_dir,
128               '-C', options.crunch_output_dir]
129   build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail)
130
131   MoveImagesToNonMdpiFolders(options.crunch_output_dir)
132
133   if options.stamp:
134     build_utils.Touch(options.stamp)
135
136
137 if __name__ == '__main__':
138   main()