OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 CodeGeneratorDictionaryImpl. It assumes that all union types are already | 316 CodeGeneratorDictionaryImpl. It assumes that all union types are already |
317 collected. It doesn't process idl files directly. | 317 collected. It doesn't process idl files directly. |
318 """ | 318 """ |
319 def __init__(self, info_provider, cache_dir, output_dir, target_component): | 319 def __init__(self, info_provider, cache_dir, output_dir, target_component): |
320 self.info_provider = info_provider | 320 self.info_provider = info_provider |
321 self.jinja_env = initialize_jinja_env(cache_dir) | 321 self.jinja_env = initialize_jinja_env(cache_dir) |
322 self.output_dir = output_dir | 322 self.output_dir = output_dir |
323 self.target_component = target_component | 323 self.target_component = target_component |
324 set_global_type_info(info_provider) | 324 set_global_type_info(info_provider) |
325 | 325 |
326 def generate_code(self): | 326 def _collect_unique_unions(self, union_types): |
327 union_types = self.info_provider.union_types | 327 """This method iterates over |union_types| to remove duplicated entries |
328 if not union_types: | 328 in |union_types| and to collect additional information (e.g. whether we |
329 return () | 329 need to generate *OrNull class). Returns a dict which contains the |
330 header_template = self.jinja_env.get_template('union.h') | 330 information. |
331 cpp_template = self.jinja_env.get_template('union.cpp') | 331 """ |
332 template_context = v8_union.union_context( | 332 unique_unions = {} |
333 union_types, self.info_provider.interfaces_info) | 333 for union_type in union_types: |
| 334 cpp_type = union_type.cpp_type |
| 335 if cpp_type not in unique_unions.keys(): |
| 336 unique_unions[cpp_type] = { |
| 337 'union_type': union_type, |
| 338 } |
| 339 if union_type.includes_nullable_type: |
| 340 unique_unions[cpp_type]['needs_ornull_converter'] = True |
| 341 return unique_unions |
| 342 |
| 343 def _generate_container_code(self, name, union_type_dict): |
| 344 """Generates a union type container into separate .h/.cpp files. |
| 345 Returns the path and its contents as a tuple. |
| 346 """ |
| 347 header_template = self.jinja_env.get_template('union_container.h') |
| 348 cpp_template = self.jinja_env.get_template('union_container.cpp') |
| 349 template_context = v8_union.container_context( |
| 350 union_type_dict['union_type'], self.info_provider.interfaces_info) |
334 template_context['code_generator'] = module_pyname | 351 template_context['code_generator'] = module_pyname |
| 352 template_context['needs_ornull_converter'] = ( |
| 353 union_type_dict.get('needs_ornull_converter')) |
| 354 |
| 355 header_text = header_template.render(template_context) |
| 356 cpp_text = cpp_template.render(template_context) |
| 357 header_path = posixpath.join(self.output_dir, '%s.h' % name) |
| 358 cpp_path = posixpath.join(self.output_dir, '%s.cpp' % name) |
| 359 return ( |
| 360 (header_path, header_text), |
| 361 (cpp_path, cpp_text), |
| 362 ) |
| 363 |
| 364 def _generate_aggregate_header(self, names): |
| 365 """Generates a header file which contains all union type container |
| 366 headers. |
| 367 FIXME: We might want to remove the aggregated header once we directly |
| 368 include necessary header files in Blink side. |
| 369 """ |
335 capitalized_component = self.target_component.capitalize() | 370 capitalized_component = self.target_component.capitalize() |
336 template_context['header_filename'] = 'bindings/%s/v8/UnionTypes%s.h' %
( | 371 context = { |
337 self.target_component, capitalized_component) | 372 'code_generator': module_pyname, |
338 template_context['macro_guard'] = 'UnionType%s_h' % capitalized_componen
t | 373 'header_includes': sorted([ |
339 | 374 'bindings/%s/v8/%s.h' % (self.target_component, name) |
| 375 for name in names]), |
| 376 'macro_guard': 'UnionType%s_h' % capitalized_component, |
| 377 } |
340 # Add UnionTypesCore.h as a dependency when we generate modules union ty
pes | 378 # Add UnionTypesCore.h as a dependency when we generate modules union ty
pes |
341 # because we only generate union type containers which are used by both | 379 # because we only generate union type containers which are used by both |
342 # core and modules in UnionTypesCore.h. | 380 # core and modules in UnionTypesCore.h. |
343 # FIXME: This is an ad hoc workaround and we need a general way to | 381 # FIXME: This is an ad hoc workaround and we need a general way to |
344 # handle core <-> modules dependency. | 382 # handle core <-> modules dependency. |
345 if self.target_component == 'modules': | 383 if self.target_component == 'modules': |
346 template_context['header_includes'] = sorted( | 384 context['header_includes'] = sorted( |
347 template_context['header_includes'] + | 385 context['header_includes'] + |
348 ['bindings/core/v8/UnionTypesCore.h']) | 386 ['bindings/core/v8/UnionTypesCore.h']) |
349 | 387 template = self.jinja_env.get_template('union.h') |
350 header_text = header_template.render(template_context) | 388 header_text = template.render(context) |
351 cpp_text = cpp_template.render(template_context) | |
352 header_path = posixpath.join(self.output_dir, | 389 header_path = posixpath.join(self.output_dir, |
353 'UnionTypes%s.h' % capitalized_component) | 390 'UnionTypes%s.h' % capitalized_component) |
| 391 return (header_path, header_text) |
| 392 |
| 393 def _generate_aggregate_cpp(self, names): |
| 394 """Generates a cpp file which contains all union type container |
| 395 implementations. We need this aggregated cpp file as the output of |
| 396 a GYP/GN rule. |
| 397 """ |
| 398 capitalized_component = self.target_component.capitalize() |
| 399 context = { |
| 400 'code_generator': module_pyname, |
| 401 'cpp_files': sorted([ |
| 402 'bindings/%s/v8/%s.cpp' % (self.target_component, name) |
| 403 for name in names]), |
| 404 } |
| 405 template = self.jinja_env.get_template('union.cpp') |
| 406 cpp_text = template.render(context) |
354 cpp_path = posixpath.join(self.output_dir, | 407 cpp_path = posixpath.join(self.output_dir, |
355 'UnionTypes%s.cpp' % capitalized_component) | 408 'UnionTypes%s.cpp' % capitalized_component) |
356 return ( | 409 return (cpp_path, cpp_text) |
357 (header_path, header_text), | 410 |
358 (cpp_path, cpp_text), | 411 def generate_code(self): |
359 ) | 412 unique_unions = self._collect_unique_unions( |
| 413 self.info_provider.union_types) |
| 414 if not unique_unions: |
| 415 return () |
| 416 outputs = set() |
| 417 for name, union_type_dict in unique_unions.iteritems(): |
| 418 outputs.update(self._generate_container_code(name, union_type_dict)) |
| 419 |
| 420 outputs.add(self._generate_aggregate_header(unique_unions.keys())) |
| 421 outputs.add(self._generate_aggregate_cpp(unique_unions.keys())) |
| 422 return outputs |
360 | 423 |
361 | 424 |
362 def initialize_jinja_env(cache_dir): | 425 def initialize_jinja_env(cache_dir): |
363 jinja_env = jinja2.Environment( | 426 jinja_env = jinja2.Environment( |
364 loader=jinja2.FileSystemLoader(templates_dir), | 427 loader=jinja2.FileSystemLoader(templates_dir), |
365 # Bytecode cache is not concurrency-safe unless pre-cached: | 428 # Bytecode cache is not concurrency-safe unless pre-cached: |
366 # if pre-cached this is read-only, but writing creates a race condition. | 429 # if pre-cached this is read-only, but writing creates a race condition. |
367 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 430 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
368 keep_trailing_newline=True, # newline-terminate generated files | 431 keep_trailing_newline=True, # newline-terminate generated files |
369 lstrip_blocks=True, # so can indent control flow tags | 432 lstrip_blocks=True, # so can indent control flow tags |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 | 501 |
439 # Create a dummy file as output for the build system, | 502 # Create a dummy file as output for the build system, |
440 # since filenames of individual cache files are unpredictable and opaque | 503 # since filenames of individual cache files are unpredictable and opaque |
441 # (they are hashes of the template path, which varies based on environment) | 504 # (they are hashes of the template path, which varies based on environment) |
442 with open(dummy_filename, 'w') as dummy_file: | 505 with open(dummy_filename, 'w') as dummy_file: |
443 pass # |open| creates or touches the file | 506 pass # |open| creates or touches the file |
444 | 507 |
445 | 508 |
446 if __name__ == '__main__': | 509 if __name__ == '__main__': |
447 sys.exit(main(sys.argv)) | 510 sys.exit(main(sys.argv)) |
OLD | NEW |