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

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

Issue 2573613002: Shrink onResourcesLoaded() code size in webview/monochrome R.java (Closed)
Patch Set: Format Created 4 years 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 | « no previous file | no next file » | 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).
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 raise Exception('Unexpected line in R.txt: %s' % line) 192 raise Exception('Unexpected line in R.txt: %s' % line)
193 java_type, resource_type, name, value = m.groups() 193 java_type, resource_type, name, value = m.groups()
194 ret.append(TextSymbolsEntry(java_type, resource_type, name, value)) 194 ret.append(TextSymbolsEntry(java_type, resource_type, name, value))
195 return ret 195 return ret
196 196
197 197
198 def _CreateRJavaFile(package, resources_by_type, shared_resources): 198 def _CreateRJavaFile(package, resources_by_type, shared_resources):
199 """Generates the contents of a R.java file.""" 199 """Generates the contents of a R.java file."""
200 # Keep these assignments all on one line to make diffing against regular 200 # Keep these assignments all on one line to make diffing against regular
201 # aapt-generated files easier. 201 # aapt-generated files easier.
202 create_id = ('{{ e.resource_type }}.{{ e.name }} = ' 202 create_id = ('{{ e.resource_type }}.{{ e.name }} = '
agrieve 2016/12/12 20:40:51 nit: could now shorten these with ^=
F 2016/12/12 21:13:12 Done.
203 '({{ e.resource_type }}.{{ e.name }} & 0x00ffffff) |' 203 '{{ e.resource_type }}.{{ e.name }} ^ packageIdTransform;')
204 ' (packageId << 24);')
205 create_id_arr = ('{{ e.resource_type }}.{{ e.name }}[i] = ' 204 create_id_arr = ('{{ e.resource_type }}.{{ e.name }}[i] = '
206 '({{ e.resource_type }}.{{ e.name }}[i] & 0x00ffffff) |' 205 '{{ e.resource_type }}.{{ e.name }}[i] ^'
207 ' (packageId << 24);') 206 ' packageIdTransform;')
208 # Here we diverge from what aapt does. Because we have so many 207 # Here we diverge from what aapt does. Because we have so many
209 # resources, the onResourcesLoaded method was exceeding the 64KB limit that 208 # resources, the onResourcesLoaded method was exceeding the 64KB limit that
210 # Java imposes. For this reason we split onResourcesLoaded into different 209 # Java imposes. For this reason we split onResourcesLoaded into different
211 # methods for each resource type. 210 # methods for each resource type.
212 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */ 211 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */
213 212
214 package {{ package }}; 213 package {{ package }};
215 214
216 public final class R { 215 public final class R {
217 {% for resource_type in resource_types %} 216 {% for resource_type in resource_types %}
218 public static final class {{ resource_type }} { 217 public static final class {{ resource_type }} {
219 {% for e in resources[resource_type] %} 218 {% for e in resources[resource_type] %}
220 {% if shared_resources %} 219 {% if shared_resources %}
221 public static {{ e.java_type }} {{ e.name }} = {{ e.value }}; 220 public static {{ e.java_type }} {{ e.name }} = {{ e.value }};
222 {% else %} 221 {% else %}
223 public static final {{ e.java_type }} {{ e.name }} = {{ e.value }}; 222 public static final {{ e.java_type }} {{ e.name }} = {{ e.value }};
224 {% endif %} 223 {% endif %}
225 {% endfor %} 224 {% endfor %}
226 } 225 }
227 {% endfor %} 226 {% endfor %}
228 {% if shared_resources %} 227 {% if shared_resources %}
229 public static void onResourcesLoaded(int packageId) { 228 public static void onResourcesLoaded(int packageId) {
229 assert !sResourcesDidLoad;
230 sResourcesDidLoad = true;
231 int packageIdTransform = (packageId ^ 0x7f) << 24;
230 {% for resource_type in resource_types %} 232 {% for resource_type in resource_types %}
231 onResourcesLoaded{{ resource_type|title }}(packageId); 233 onResourcesLoaded{{ resource_type|title }}(packageIdTransform);
232 {% for e in resources[resource_type] %} 234 {% for e in resources[resource_type] %}
233 {% if e.java_type == 'int[]' %} 235 {% if e.java_type == 'int[]' %}
234 for(int i = 0; i < {{ e.resource_type }}.{{ e.name }}.length; ++i) { 236 for(int i = 0; i < {{ e.resource_type }}.{{ e.name }}.length; ++i) {
235 """ + create_id_arr + """ 237 """ + create_id_arr + """
236 } 238 }
237 {% endif %} 239 {% endif %}
238 {% endfor %} 240 {% endfor %}
239 {% endfor %} 241 {% endfor %}
240 } 242 }
243 private static boolean sResourcesDidLoad;
agrieve 2016/12/12 20:40:51 nit: can you move this to come at the top of the c
F 2016/12/12 21:13:13 Done.
241 {% for res_type in resource_types %} 244 {% for res_type in resource_types %}
242 private static void onResourcesLoaded{{ res_type|title }}(int packageId) { 245 private static void onResourcesLoaded{{ res_type|title }} (
246 int packageIdTransform) {
agrieve 2016/12/12 20:40:51 nit: indent 4 more spaces
F 2016/12/12 21:13:13 Done.
243 {% for e in resources[res_type] %} 247 {% for e in resources[res_type] %}
244 {% if res_type != 'styleable' and e.java_type != 'int[]' %} 248 {% if res_type != 'styleable' and e.java_type != 'int[]' %}
245 """ + create_id + """ 249 """ + create_id + """
246 {% endif %} 250 {% endif %}
247 {% endfor %} 251 {% endfor %}
248 } 252 }
249 {% endfor %} 253 {% endfor %}
250 {% endif %} 254 {% endif %}
251 } 255 }
252 """, trim_blocks=True, lstrip_blocks=True) 256 """, trim_blocks=True, lstrip_blocks=True)
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 options, 536 options,
533 input_paths=input_paths, 537 input_paths=input_paths,
534 input_strings=input_strings, 538 input_strings=input_strings,
535 output_paths=output_paths, 539 output_paths=output_paths,
536 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP). 540 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP).
537 force=options.R_dir) 541 force=options.R_dir)
538 542
539 543
540 if __name__ == '__main__': 544 if __name__ == '__main__':
541 main(sys.argv[1:]) 545 main(sys.argv[1:])
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698