Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: build/android/gyp/process_resources.py

Issue 1129103002: Revert of Actually use --extra-r-text-files in process_resources.py (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « AUTHORS ('k') | build/android/gyp/write_build_config.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 resources to generate R.java, and prepare for packaging. 7 """Process Android resources to generate R.java, and prepare for packaging.
8 8
9 This will crunch images and generate v14 compatible resources 9 This will crunch images and generate v14 compatible resources
10 (see generate_v14_compatible_resources.py). 10 (see generate_v14_compatible_resources.py).
11 """ 11 """
12 12
13 import codecs
13 import optparse 14 import optparse
14 import os 15 import os
15 import re 16 import re
16 import shutil 17 import shutil
17 import sys 18 import sys
18 import zipfile 19 import zipfile
19 20
20 import generate_v14_compatible_resources 21 import generate_v14_compatible_resources
21 22
22 from util import build_utils 23 from util import build_utils
23 24
24 # Import jinja2 from third_party/jinja2
25 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../third_party'))
26 from jinja2 import Template # pylint: disable=F0401
27
28 25
29 def ParseArgs(args): 26 def ParseArgs(args):
30 """Parses command line options. 27 """Parses command line options.
31 28
32 Returns: 29 Returns:
33 An options object as from optparse.OptionsParser.parse_args() 30 An options object as from optparse.OptionsParser.parse_args()
34 """ 31 """
35 parser = optparse.OptionParser() 32 parser = optparse.OptionParser()
36 build_utils.AddDepfileOption(parser) 33 build_utils.AddDepfileOption(parser)
37 34
(...skipping 15 matching lines...) Expand all
53 parser.add_option('--dependencies-res-zips', 50 parser.add_option('--dependencies-res-zips',
54 help='Resources from dependents.') 51 help='Resources from dependents.')
55 52
56 parser.add_option('--resource-zip-out', 53 parser.add_option('--resource-zip-out',
57 help='Path for output zipped resources.') 54 help='Path for output zipped resources.')
58 55
59 parser.add_option('--R-dir', 56 parser.add_option('--R-dir',
60 help='directory to hold generated R.java.') 57 help='directory to hold generated R.java.')
61 parser.add_option('--srcjar-out', 58 parser.add_option('--srcjar-out',
62 help='Path to srcjar to contain generated R.java.') 59 help='Path to srcjar to contain generated R.java.')
63 parser.add_option('--r-text-out',
64 help='Path to store the R.txt file generated by appt.')
65 60
66 parser.add_option('--proguard-file', 61 parser.add_option('--proguard-file',
67 help='Path to proguard.txt generated file') 62 help='Path to proguard.txt generated file')
68 63
69 parser.add_option( 64 parser.add_option(
70 '--v14-verify-only', 65 '--v14-verify-only',
71 action='store_true', 66 action='store_true',
72 help='Do not generate v14 resources. Instead, just verify that the ' 67 help='Do not generate v14 resources. Instead, just verify that the '
73 'resources are already compatible with v14, i.e. they don\'t use ' 68 'resources are already compatible with v14, i.e. they don\'t use '
74 'attributes that cause crashes on certain devices.') 69 'attributes that cause crashes on certain devices.')
75 70
76 parser.add_option( 71 parser.add_option(
77 '--extra-res-packages', 72 '--extra-res-packages',
78 help='Additional package names to generate R.java files for') 73 help='Additional package names to generate R.java files for')
74 # TODO(cjhopman): Actually use --extra-r-text-files. We currently include all
75 # the resources in all R.java files for a particular apk.
79 parser.add_option( 76 parser.add_option(
80 '--extra-r-text-files', 77 '--extra-r-text-files',
81 help='For each additional package, the R.txt file should contain a ' 78 help='For each additional package, the R.txt file should contain a '
82 'list of resources to be included in the R.java file in the format ' 79 'list of resources to be included in the R.java file in the format '
83 'generated by aapt') 80 'generated by aapt')
84 81
85 parser.add_option( 82 parser.add_option(
86 '--all-resources-zip-out', 83 '--all-resources-zip-out',
87 help='Path for output of all resources. This includes resources in ' 84 help='Path for output of all resources. This includes resources in '
88 'dependencies.') 85 'dependencies.')
(...skipping 15 matching lines...) Expand all
104 'resource_zip_out', 101 'resource_zip_out',
105 ) 102 )
106 build_utils.CheckOptions(options, parser, required=required_options) 103 build_utils.CheckOptions(options, parser, required=required_options)
107 104
108 if (options.R_dir is None) == (options.srcjar_out is None): 105 if (options.R_dir is None) == (options.srcjar_out is None):
109 raise Exception('Exactly one of --R-dir or --srcjar-out must be specified.') 106 raise Exception('Exactly one of --R-dir or --srcjar-out must be specified.')
110 107
111 return options 108 return options
112 109
113 110
114 def CreateExtraRJavaFiles( 111 def CreateExtraRJavaFiles(r_dir, extra_packages):
115 r_dir, extra_packages, extra_r_text_files, shared_resources): 112 java_files = build_utils.FindInDirectory(r_dir, "R.java")
116 if len(extra_packages) != len(extra_r_text_files): 113 if len(java_files) != 1:
117 raise Exception('Need one R.txt file per extra package') 114 return
115 r_java_file = java_files[0]
116 r_java_contents = codecs.open(r_java_file, encoding='utf-8').read()
118 117
119 all_resources = {} 118 for package in extra_packages:
120 r_txt_file = os.path.join(r_dir, 'R.txt') 119 package_r_java_dir = os.path.join(r_dir, *package.split('.'))
121 if not os.path.exists(r_txt_file): 120 build_utils.MakeDirectory(package_r_java_dir)
122 return 121 package_r_java_path = os.path.join(package_r_java_dir, 'R.java')
123 with open(r_txt_file) as f: 122 new_r_java = re.sub(r'package [.\w]*;', u'package %s;' % package,
124 for line in f: 123 r_java_contents)
125 m = re.match(r'(int(?:\[\])?) (\w+) (\w+) (.+)$', line) 124 codecs.open(package_r_java_path, 'w', encoding='utf-8').write(new_r_java)
126 if not m: 125 # TODO(cjhopman): These extra package's R.java files should be filtered to
127 raise Exception('Unexpected line in R.txt: %s' % line) 126 # only contain the resources listed in their R.txt files. At this point, we
128 java_type, resource_type, name, value = m.groups() 127 # have already compiled those other libraries, so doing this would only
129 all_resources[(resource_type, name)] = (java_type, value) 128 # affect how the code in this .apk target could refer to the resources.
130
131 for package, r_text_file in zip(extra_packages, extra_r_text_files):
132 if os.path.exists(r_text_file):
133 package_r_java_dir = os.path.join(r_dir, *package.split('.'))
134 build_utils.MakeDirectory(package_r_java_dir)
135 package_r_java_path = os.path.join(package_r_java_dir, 'R.java')
136 CreateExtraRJavaFile(
137 package, package_r_java_path, r_text_file, all_resources,
138 shared_resources)
139
140
141 def CreateExtraRJavaFile(
142 package, r_java_path, r_text_file, all_resources, shared_resources):
143 resources = {}
144 with open(r_text_file) as f:
145 for line in f:
146 m = re.match(r'int(?:\[\])? (\w+) (\w+) ', line)
147 if not m:
148 raise Exception('Unexpected line in R.txt: %s' % line)
149 resource_type, name = m.groups()
150 java_type, value = all_resources[(resource_type, name)]
151 if resource_type not in resources:
152 resources[resource_type] = []
153 resources[resource_type].append((name, java_type, value))
154
155 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */
156
157 package {{ package }};
158
159 public final class R {
160 {% for resource_type in resources %}
161 public static final class {{ resource_type }} {
162 {% for name, java_type, value in resources[resource_type] %}
163 {% if shared_resources %}
164 public static {{ java_type }} {{ name }} = {{ value }};
165 {% else %}
166 public static final {{ java_type }} {{ name }} = {{ value }};
167 {% endif %}
168 {% endfor %}
169 }
170 {% endfor %}
171 {% if shared_resources %}
172 public static void onResourcesLoaded(int packageId) {
173 {% for resource_type in resources %}
174 {% for name, java_type, value in resources[resource_type] %}
175 {% if java_type == 'int[]' %}
176 for(int i = 0; i < {{ resource_type }}.{{ name }}.length; ++i) {
177 {{ resource_type }}.{{ name }}[i] =
178 ({{ resource_type }}.{{ name }}[i] & 0x00ffffff)
179 | (packageId << 24);
180 }
181 {% else %}
182 {{ resource_type }}.{{ name }} =
183 ({{ resource_type }}.{{ name }} & 0x00ffffff)
184 | (packageId << 24);
185 {% endif %}
186 {% endfor %}
187 {% endfor %}
188 }
189 {% endif %}
190 }
191 """, trim_blocks=True, lstrip_blocks=True)
192
193 output = template.render(package=package, resources=resources,
194 shared_resources=shared_resources)
195 with open(r_java_path, 'w') as f:
196 f.write(output)
197 129
198 130
199 def CrunchDirectory(aapt, input_dir, output_dir): 131 def CrunchDirectory(aapt, input_dir, output_dir):
200 """Crunches the images in input_dir and its subdirectories into output_dir. 132 """Crunches the images in input_dir and its subdirectories into output_dir.
201 133
202 If an image is already optimized, crunching often increases image size. In 134 If an image is already optimized, crunching often increases image size. In
203 this case, the crunched image is overwritten with the original image. 135 this case, the crunched image is overwritten with the original image.
204 """ 136 """
205 aapt_cmd = [aapt, 137 aapt_cmd = [aapt,
206 'crunch', 138 'crunch',
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 package_command += ['--custom-package', options.custom_package] 273 package_command += ['--custom-package', options.custom_package]
342 if options.proguard_file: 274 if options.proguard_file:
343 package_command += ['-G', options.proguard_file] 275 package_command += ['-G', options.proguard_file]
344 if options.shared_resources: 276 if options.shared_resources:
345 package_command.append('--shared-lib') 277 package_command.append('--shared-lib')
346 build_utils.CheckOutput(package_command, print_stderr=False) 278 build_utils.CheckOutput(package_command, print_stderr=False)
347 279
348 if options.extra_res_packages: 280 if options.extra_res_packages:
349 CreateExtraRJavaFiles( 281 CreateExtraRJavaFiles(
350 gen_dir, 282 gen_dir,
351 build_utils.ParseGypList(options.extra_res_packages), 283 build_utils.ParseGypList(options.extra_res_packages))
352 build_utils.ParseGypList(options.extra_r_text_files),
353 options.shared_resources)
354 284
355 # This is the list of directories with resources to put in the final .zip 285 # This is the list of directories with resources to put in the final .zip
356 # file. The order of these is important so that crunched/v14 resources 286 # file. The order of these is important so that crunched/v14 resources
357 # override the normal ones. 287 # override the normal ones.
358 zip_resource_dirs = input_resource_dirs + [v14_dir] 288 zip_resource_dirs = input_resource_dirs + [v14_dir]
359 289
360 base_crunch_dir = os.path.join(temp_dir, 'crunch') 290 base_crunch_dir = os.path.join(temp_dir, 'crunch')
361 291
362 # Crunch image resources. This shrinks png files and is necessary for 292 # Crunch image resources. This shrinks png files and is necessary for
363 # 9-patch images to display correctly. 'aapt crunch' accepts only a single 293 # 9-patch images to display correctly. 'aapt crunch' accepts only a single
364 # directory at a time and deletes everything in the output directory. 294 # directory at a time and deletes everything in the output directory.
365 for idx, input_dir in enumerate(input_resource_dirs): 295 for idx, input_dir in enumerate(input_resource_dirs):
366 crunch_dir = os.path.join(base_crunch_dir, str(idx)) 296 crunch_dir = os.path.join(base_crunch_dir, str(idx))
367 build_utils.MakeDirectory(crunch_dir) 297 build_utils.MakeDirectory(crunch_dir)
368 zip_resource_dirs.append(crunch_dir) 298 zip_resource_dirs.append(crunch_dir)
369 CrunchDirectory(aapt, input_dir, crunch_dir) 299 CrunchDirectory(aapt, input_dir, crunch_dir)
370 300
371 ZipResources(zip_resource_dirs, options.resource_zip_out) 301 ZipResources(zip_resource_dirs, options.resource_zip_out)
372 302
373 if options.all_resources_zip_out: 303 if options.all_resources_zip_out:
374 CombineZips([options.resource_zip_out] + dep_zips, 304 CombineZips([options.resource_zip_out] + dep_zips,
375 options.all_resources_zip_out) 305 options.all_resources_zip_out)
376 306
377 if options.R_dir: 307 if options.R_dir:
378 build_utils.DeleteDirectory(options.R_dir) 308 build_utils.DeleteDirectory(options.R_dir)
379 shutil.copytree(gen_dir, options.R_dir) 309 shutil.copytree(gen_dir, options.R_dir)
380 else: 310 else:
381 build_utils.ZipDir(options.srcjar_out, gen_dir) 311 build_utils.ZipDir(options.srcjar_out, gen_dir)
382 312
383 if options.r_text_out:
384 r_text_path = os.path.join(gen_dir, 'R.txt')
385 shutil.copyfile(r_text_path, options.r_text_out)
386
387 if options.depfile: 313 if options.depfile:
388 input_files += build_utils.GetPythonDependencies() 314 input_files += build_utils.GetPythonDependencies()
389 build_utils.WriteDepfile(options.depfile, input_files) 315 build_utils.WriteDepfile(options.depfile, input_files)
390 316
391 if options.stamp: 317 if options.stamp:
392 build_utils.Touch(options.stamp) 318 build_utils.Touch(options.stamp)
393 319
394 320
395 if __name__ == '__main__': 321 if __name__ == '__main__':
396 main() 322 main()
OLDNEW
« no previous file with comments | « AUTHORS ('k') | build/android/gyp/write_build_config.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698