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 17 matching lines...) Expand all Loading... |
28 | 28 |
29 """Resolve interface dependencies, producing a merged IdlDefinitions object. | 29 """Resolve interface dependencies, producing a merged IdlDefinitions object. |
30 | 30 |
31 This library computes interface dependencies (partial interfaces and | 31 This library computes interface dependencies (partial interfaces and |
32 implements), reads the dependency files, and merges them to the IdlDefinitions | 32 implements), reads the dependency files, and merges them to the IdlDefinitions |
33 for the main IDL file, producing an IdlDefinitions object representing the | 33 for the main IDL file, producing an IdlDefinitions object representing the |
34 entire interface. | 34 entire interface. |
35 | 35 |
36 It also checks whether a file should have bindings generated, or whether | 36 It also checks whether a file should have bindings generated, or whether |
37 instead it is just a dependency. | 37 instead it is just a dependency. |
38 | |
39 FIXME: Currently a stub, as part of landing the parser incrementally. | |
40 Just computes dependencies, and should always return None, indicating | |
41 bindings should not be generated. | |
42 Does not read IDL files or merge IdlDefinitions yet. | |
43 """ | 38 """ |
44 | 39 |
45 import os.path | 40 import os.path |
46 | 41 |
| 42 import idl_reader |
47 | 43 |
48 def merge_interface_dependencies(idl_definitions, idl_filename, interface_depend
encies_filename, additional_idl_filenames): | 44 |
| 45 class InterfaceNotFoundError(Exception): |
| 46 """Raised if (partial) interface not found in target.""" |
| 47 pass |
| 48 |
| 49 |
| 50 class InvalidPartialInterfaceError(Exception): |
| 51 """Raised if a file listed as a partial interface is not in fact so.""" |
| 52 pass |
| 53 |
| 54 |
| 55 def merge_dependencies(definitions, idl_filename, interface_dependencies_filenam
e, additional_idl_filenames, verbose=False): |
49 """Merges dependencies into an existing IDL document. | 56 """Merges dependencies into an existing IDL document. |
50 | 57 |
51 Modifies idl_definitions in place by adding parsed dependencies, and checks | 58 Modifies definitions in place by adding parsed dependencies, and checks |
52 whether bindings should be generated, returning bool. | 59 whether bindings should be generated, returning bool. |
53 | 60 |
54 FIXME: stub: parser not implemented yet | |
55 | |
56 Arguments: | 61 Arguments: |
57 idl_definitions: IdlDefinitions object, modified in place | 62 definitions: IdlDefinitions object, modified in place |
58 idl_filename: filename of main IDL file for the interface | 63 idl_filename: filename of main IDL file for the interface |
59 interface_dependencies_file: filename of dependencies file (produced by
compute_dependencies.py) | 64 interface_dependencies_file: filename of dependencies file (produced by
compute_dependencies.py) |
60 additional_idl_files: list of additional files, not listed in interface_
dependencies_file, for which bindings should nonetheless be generated | 65 additional_idl_files: list of additional files, not listed in interface_
dependencies_file, for which bindings should nonetheless be generated |
61 Returns: | 66 Returns: |
62 bool, whether bindings should be generated or not. | 67 bool, whether bindings should be generated or not. |
63 """ | 68 """ |
64 basename = os.path.basename(idl_filename) | 69 basename = os.path.basename(idl_filename) |
65 interface_name, _ = os.path.splitext(basename) | 70 interface_name, _ = os.path.splitext(basename) |
66 | 71 |
67 dependency_idl_filenames = compute_dependency_idl_files(basename, interface_
dependencies_filename, additional_idl_filenames) | 72 dependency_idl_filenames = compute_dependency_idl_files(basename, interface_
dependencies_filename, additional_idl_filenames) |
68 if dependency_idl_filenames is None: | 73 if dependency_idl_filenames is None: |
69 return False | 74 return False |
70 # FIXME: currently dependency_idl_files *must* be None (indicating that | 75 # The Blink IDL filenaming convention is that the file <interface_name>.idl |
71 # dummy .cpp and .h files should be generated), as actual parser not | 76 # MUST contain the interface "interface_name" or exception "interface_name". |
72 # present yet. | 77 if interface_name in definitions.exceptions: |
73 raise RuntimeError('Stub: parser not implemented yet') | 78 # Exceptions do not have dependencies, so no merging necessary |
| 79 return definitions |
| 80 try: |
| 81 interface = definitions.interfaces[interface_name] |
| 82 except KeyError: |
| 83 raise InterfaceNotFoundError('Could not find interface or exception "{in
terface_name}" in {basename}'.format(**locals())) |
| 84 merge_interface_dependencies(interface, idl_filename, dependency_idl_filenam
es, verbose=verbose) |
| 85 |
| 86 return definitions |
74 | 87 |
75 | 88 |
76 def compute_dependency_idl_files(target_idl_basename, interface_dependencies_fil
ename, additional_idl_filenames): | 89 def compute_dependency_idl_files(target_idl_basename, interface_dependencies_fil
ename, additional_idl_filenames): |
77 """Returns list of IDL file dependencies for a given main IDL file. | 90 """Returns list of IDL file dependencies for a given main IDL file. |
78 | 91 |
79 - Returns a list of IDL files on which a given IDL file depends, | 92 - Returns a list of IDL files on which a given IDL file depends, |
80 possibly empty. | 93 possibly empty. |
81 Dependencies consist of partial interface files and files for other | 94 Dependencies consist of partial interface files and files for other |
82 interfaces that the given interface implements. | 95 interfaces that the given interface implements. |
83 - Returns an empty list also if the given IDL file is an additional IDL | 96 - Returns an empty list also if the given IDL file is an additional IDL |
(...skipping 30 matching lines...) Expand all Loading... |
114 return dependency_filenames.split() | 127 return dependency_filenames.split() |
115 | 128 |
116 # additional_idl_files is a list of IDL files which should not be included | 129 # additional_idl_files is a list of IDL files which should not be included |
117 # in DerivedSources*.cpp, and hence are not listed in the interface | 130 # in DerivedSources*.cpp, and hence are not listed in the interface |
118 # dependencies file, but for which we should generate .cpp and .h files. | 131 # dependencies file, but for which we should generate .cpp and .h files. |
119 additional_idl_basenames = set(map(os.path.basename, additional_idl_filename
s)) | 132 additional_idl_basenames = set(map(os.path.basename, additional_idl_filename
s)) |
120 if target_idl_basename in additional_idl_basenames: | 133 if target_idl_basename in additional_idl_basenames: |
121 return [] | 134 return [] |
122 | 135 |
123 return None | 136 return None |
| 137 |
| 138 |
| 139 def merge_interface_dependencies(interface, idl_filename, dependency_idl_filenam
es, verbose=False): |
| 140 """Merge partial interfaces in supplementary_idl_files into target_document. |
| 141 |
| 142 No return: modifies target_document in place. |
| 143 """ |
| 144 # Sort so order consistent, so can compare output from run to run. |
| 145 for dependency_idl_filename in sorted(dependency_idl_filenames): |
| 146 dependency_interface_name, _ = os.path.splitext(os.path.basename(depende
ncy_idl_filename)) |
| 147 definitions = idl_reader.read_idl_file(dependency_idl_filename, verbose=
verbose) |
| 148 |
| 149 for dependency_interface in definitions.interfaces.itervalues(): |
| 150 # Supplemental files contain either partial interfaces for |
| 151 # the (single) target interface, in which case the interface names |
| 152 # must agree, or interfaces that are implemented by the target |
| 153 # interface, in which case the interface names differ. |
| 154 if dependency_interface.is_partial and dependency_interface.name !=
interface.name: |
| 155 raise InvalidPartialInterfaceError('%s is not a partial interfac
e of %s. There maybe a bug in the the dependency generator (compute_depedencies.
py).' % (dependency_idl_filename, idl_filename)) |
| 156 if 'ImplementedAs' in dependency_interface.extended_attributes: |
| 157 del dependency_interface.extended_attributes['ImplementedAs'] |
| 158 merge_dependency_interface(interface, dependency_interface, dependen
cy_interface_name) |
| 159 |
| 160 |
| 161 def merge_dependency_interface(interface, dependency_interface, dependency_inter
face_name): |
| 162 """Merge dependency_interface into interface. |
| 163 |
| 164 No return: modifies interface in place. |
| 165 """ |
| 166 def merge_lists(source_list, target_list): |
| 167 for element in source_list: |
| 168 if dependency_interface.is_partial: |
| 169 element.extended_attributes['ImplementedBy'] = dependency_interf
ace_name |
| 170 element.extended_attributes.update(dependency_interface.extended_att
ributes) |
| 171 target_list.append(element) |
| 172 |
| 173 merge_lists(dependency_interface.attributes, interface.attributes) |
| 174 merge_lists(dependency_interface.constants, interface.constants) |
| 175 merge_lists(dependency_interface.functions, interface.functions) |
OLD | NEW |