Index: bindings/scripts/compute_interfaces_info_individual.py |
diff --git a/bindings/scripts/compute_interfaces_info_individual.py b/bindings/scripts/compute_interfaces_info_individual.py |
index 58f6790e67601de6af82421e7b83d576c1fc582e..b052e14c162dae0010d6865a4e4441e7d8e3d2fa 100755 |
--- a/bindings/scripts/compute_interfaces_info_individual.py |
+++ b/bindings/scripts/compute_interfaces_info_individual.py |
@@ -47,24 +47,26 @@ import os |
import posixpath |
import sys |
-from utilities import get_file_contents, read_file_to_list, idl_filename_to_interface_name, idl_filename_to_component, write_pickle_file, get_interface_extended_attributes_from_idl, is_callback_interface_from_idl, is_dictionary_from_idl, get_partial_interface_name_from_idl, get_implements_from_idl, get_parent_interface, get_put_forward_interfaces_from_idl |
+from idl_definitions import Visitor |
+from idl_reader import IdlReader |
+from utilities import get_file_contents, read_file_to_list, idl_filename_to_interface_name, idl_filename_to_component, write_pickle_file, get_interface_extended_attributes_from_idl, is_callback_interface_from_idl |
module_path = os.path.dirname(__file__) |
source_path = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir)) |
-# Global variables (filled in and exported) |
-interfaces_info = {} |
-partial_interface_files = defaultdict(lambda: { |
- 'full_paths': [], |
- 'include_paths': [], |
-}) |
+ |
+class IdlBadFilenameError(Exception): |
+ """Raised if an IDL filename disagrees with the interface name in the file.""" |
+ pass |
def parse_options(): |
usage = 'Usage: %prog [options] [generated1.idl]...' |
parser = optparse.OptionParser(usage=usage) |
+ parser.add_option('--cache-directory', help='cache directory') |
parser.add_option('--idl-files-list', help='file listing IDL files') |
- parser.add_option('--interfaces-info-file', help='output pickle file') |
+ parser.add_option('--interfaces-info-file', help='interface info pickle file') |
+ parser.add_option('--component-info-file', help='component wide info pickle file') |
parser.add_option('--write-file-only-if-changed', type='int', help='if true, do not write an output file if it would be identical to the existing one, which avoids unnecessary rebuilds in ninja') |
options, args = parser.parse_args() |
@@ -104,68 +106,165 @@ def include_path(idl_filename, implemented_as=None): |
return posixpath.join(relative_dir, cpp_class_name + '.h') |
-def add_paths_to_partials_dict(partial_interface_name, full_path, this_include_path=None): |
- paths_dict = partial_interface_files[partial_interface_name] |
- paths_dict['full_paths'].append(full_path) |
- if this_include_path: |
- paths_dict['include_paths'].append(this_include_path) |
- |
- |
-def compute_info_individual(idl_filename): |
- full_path = os.path.realpath(idl_filename) |
- idl_file_contents = get_file_contents(full_path) |
- |
- extended_attributes = get_interface_extended_attributes_from_idl(idl_file_contents) |
- implemented_as = extended_attributes.get('ImplementedAs') |
- relative_dir = relative_dir_posix(idl_filename) |
- this_include_path = None if 'NoImplHeader' in extended_attributes else include_path(idl_filename, implemented_as) |
- |
- # Handle partial interfaces |
- partial_interface_name = get_partial_interface_name_from_idl(idl_file_contents) |
- if partial_interface_name: |
- add_paths_to_partials_dict(partial_interface_name, full_path, this_include_path) |
- return |
- |
- # If not a partial interface, the basename is the interface name |
- interface_name = idl_filename_to_interface_name(idl_filename) |
- |
- # 'implements' statements can be included in either the file for the |
- # implement*ing* interface (lhs of 'implements') or implement*ed* interface |
- # (rhs of 'implements'). Store both for now, then merge to implement*ing* |
- # interface later. |
- left_interfaces, right_interfaces = get_implements_from_idl(idl_file_contents, interface_name) |
- |
- interfaces_info[interface_name] = { |
- 'extended_attributes': extended_attributes, |
- 'full_path': full_path, |
- 'implemented_as': implemented_as, |
- 'implemented_by_interfaces': left_interfaces, # private, merged to next |
- 'implements_interfaces': right_interfaces, |
- 'include_path': this_include_path, |
- 'is_callback_interface': is_callback_interface_from_idl(idl_file_contents), |
- 'is_dictionary': is_dictionary_from_idl(idl_file_contents), |
- # FIXME: temporary private field, while removing old treatement of |
- # 'implements': http://crbug.com/360435 |
- 'is_legacy_treat_as_partial_interface': 'LegacyTreatAsPartialInterface' in extended_attributes, |
- 'parent': get_parent_interface(idl_file_contents), |
- # Interfaces that are referenced (used as types) and that we introspect |
- # during code generation (beyond interface-level data ([ImplementedAs], |
- # is_callback_interface, ancestors, and inherited extended attributes): |
- # deep dependencies. |
- # These cause rebuilds of referrers, due to the dependency, so these |
- # should be minimized; currently only targets of [PutForwards]. |
- 'referenced_interfaces': get_put_forward_interfaces_from_idl(idl_file_contents), |
- 'relative_dir': relative_dir, |
- } |
- |
- |
-def info_individual(): |
- """Returns info packaged as a dict.""" |
- return { |
- 'interfaces_info': interfaces_info, |
- # Can't pickle defaultdict, convert to dict |
- 'partial_interface_files': dict(partial_interface_files), |
- } |
+def get_implements_from_definitions(definitions, definition_name): |
+ left_interfaces = [] |
+ right_interfaces = [] |
+ for implement in definitions.implements: |
+ if definition_name == implement.left_interface: |
+ right_interfaces.append(implement.right_interface) |
+ elif definition_name == implement.right_interface: |
+ left_interfaces.append(implement.left_interface) |
+ else: |
+ raise IdlBadFilenameError( |
+ 'implements statement found in unrelated IDL file.\n' |
+ 'Statement is:\n' |
+ ' %s implements %s;\n' |
+ 'but filename is unrelated "%s.idl"' % |
+ (implement.left_interface, implement.right_interface, definition_name)) |
+ return left_interfaces, right_interfaces |
+ |
+ |
+def get_put_forward_interfaces_from_definition(definition): |
+ return sorted(set(attribute.idl_type.base_type |
+ for attribute in definition.attributes |
+ if 'PutForwards' in attribute.extended_attributes)) |
+ |
+ |
+def collect_union_types_from_definitions(definitions): |
+ """Traverse definitions and collect all union types.""" |
+ class UnionTypeCollector(Visitor): |
+ def collect(self, definitions): |
+ self._union_types = set() |
+ definitions.accept(self) |
+ return self._union_types |
+ |
+ def visit_typed_object(self, typed_object): |
+ for attribute_name in typed_object.idl_type_attributes: |
+ attribute = getattr(typed_object, attribute_name, None) |
+ if not attribute: |
+ continue |
+ for idl_type in attribute.idl_types(): |
+ if idl_type.is_union_type: |
+ self._union_types.add(idl_type) |
+ |
+ return UnionTypeCollector().collect(definitions) |
+ |
+ |
+class InterfaceInfoCollector(object): |
+ """A class that collects interface information from idl files.""" |
+ def __init__(self, cache_directory=None): |
+ self.reader = IdlReader(interfaces_info=None, outputdir=cache_directory) |
+ self.interfaces_info = {} |
+ self.partial_interface_files = defaultdict(lambda: { |
+ 'full_paths': [], |
+ 'include_paths': [], |
+ }) |
+ self.enumerations = set() |
+ self.union_types = set() |
+ self.typedefs = {} |
+ |
+ def add_paths_to_partials_dict(self, partial_interface_name, full_path, |
+ include_paths): |
+ paths_dict = self.partial_interface_files[partial_interface_name] |
+ paths_dict['full_paths'].append(full_path) |
+ paths_dict['include_paths'].extend(include_paths) |
+ |
+ def collect_info(self, idl_filename): |
+ """Reads an idl file and collects information which is required by the |
+ binding code generation.""" |
+ definitions = self.reader.read_idl_file(idl_filename) |
+ |
+ this_union_types = collect_union_types_from_definitions(definitions) |
+ self.union_types.update(this_union_types) |
+ self.typedefs.update(definitions.typedefs) |
+ # Check enum duplication. |
+ for enum_name in definitions.enumerations.keys(): |
+ for defined_enum in self.enumerations: |
+ if defined_enum.name == enum_name: |
+ raise Exception('Enumeration %s has multiple definitions' % enum_name) |
+ self.enumerations.update(definitions.enumerations.values()) |
+ |
+ if definitions.interfaces: |
+ definition = next(definitions.interfaces.itervalues()) |
+ interface_info = { |
+ 'is_callback_interface': definition.is_callback, |
+ 'is_dictionary': False, |
+ # Interfaces that are referenced (used as types) and that we |
+ # introspect during code generation (beyond interface-level |
+ # data ([ImplementedAs], is_callback_interface, ancestors, and |
+ # inherited extended attributes): deep dependencies. |
+ # These cause rebuilds of referrers, due to the dependency, |
+ # so these should be minimized; currently only targets of |
+ # [PutForwards]. |
+ 'referenced_interfaces': get_put_forward_interfaces_from_definition(definition), |
+ } |
+ elif definitions.dictionaries: |
+ definition = next(definitions.dictionaries.itervalues()) |
+ interface_info = { |
+ 'is_callback_interface': False, |
+ 'is_dictionary': True, |
+ 'referenced_interfaces': None, |
+ } |
+ else: |
+ return |
+ |
+ extended_attributes = definition.extended_attributes |
+ implemented_as = extended_attributes.get('ImplementedAs') |
+ full_path = os.path.realpath(idl_filename) |
+ this_include_path = None if 'NoImplHeader' in extended_attributes else include_path(idl_filename, implemented_as) |
+ if definition.is_partial: |
+ # We don't create interface_info for partial interfaces, but |
+ # adds paths to another dict. |
+ partial_include_paths = [] |
+ if this_include_path: |
+ partial_include_paths.append(this_include_path) |
+ if this_union_types: |
+ component = idl_filename_to_component(idl_filename) |
+ partial_include_paths.append( |
+ 'bindings/%s/v8/UnionTypes%s.h' % (component, component.capitalize())) |
+ self.add_paths_to_partials_dict(definition.name, full_path, partial_include_paths) |
+ return |
+ |
+ # 'implements' statements can be included in either the file for the |
+ # implement*ing* interface (lhs of 'implements') or implement*ed* interface |
+ # (rhs of 'implements'). Store both for now, then merge to implement*ing* |
+ # interface later. |
+ left_interfaces, right_interfaces = get_implements_from_definitions( |
+ definitions, definition.name) |
+ |
+ interface_info.update({ |
+ 'extended_attributes': extended_attributes, |
+ 'full_path': full_path, |
+ 'has_union_types': bool(this_union_types), |
+ 'implemented_as': implemented_as, |
+ 'implemented_by_interfaces': left_interfaces, |
+ 'implements_interfaces': right_interfaces, |
+ 'include_path': this_include_path, |
+ # FIXME: temporary private field, while removing old treatement of |
+ # 'implements': http://crbug.com/360435 |
+ 'is_legacy_treat_as_partial_interface': 'LegacyTreatAsPartialInterface' in extended_attributes, |
+ 'parent': definition.parent, |
+ 'relative_dir': relative_dir_posix(idl_filename), |
+ }) |
+ self.interfaces_info[definition.name] = interface_info |
+ |
+ def get_info_as_dict(self): |
+ """Returns info packaged as a dict.""" |
+ return { |
+ 'interfaces_info': self.interfaces_info, |
+ # Can't pickle defaultdict, convert to dict |
+ # FIXME: this should be included in get_component_info. |
+ 'partial_interface_files': dict(self.partial_interface_files), |
+ } |
+ |
+ def get_component_info_as_dict(self): |
+ """Returns component wide information as a dict.""" |
+ return { |
+ 'enumerations': dict((enum.name, enum.values) |
+ for enum in self.enumerations), |
+ 'typedefs': self.typedefs, |
+ 'union_types': self.union_types, |
+ } |
################################################################################ |
@@ -184,13 +283,16 @@ def main(): |
# Compute information for individual files |
# Information is stored in global variables interfaces_info and |
# partial_interface_files. |
+ info_collector = InterfaceInfoCollector(options.cache_directory) |
for idl_filename in idl_files: |
- compute_info_individual(idl_filename) |
+ info_collector.collect_info(idl_filename) |
write_pickle_file(options.interfaces_info_file, |
- info_individual(), |
+ info_collector.get_info_as_dict(), |
+ options.write_file_only_if_changed) |
+ write_pickle_file(options.component_info_file, |
+ info_collector.get_component_info_as_dict(), |
options.write_file_only_if_changed) |
- |
if __name__ == '__main__': |
sys.exit(main()) |