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

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

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

Powered by Google App Engine
This is Rietveld 408576698