Chromium Code Reviews| 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): | |
| 49 """Merges dependencies into an existing IDL document. | |
| 50 | 44 |
| 51 Modifies idl_definitions in place by adding parsed dependencies, and checks | 45 class InterfaceNotFoundError(Exception): |
| 46 """Raised if (partial) interface not found in target.""" | |
|
haraken
2013/07/22 01:50:23
Slightly better: "Raised if (partial) interface no
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 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 resolve_dependencies(definitions, idl_filename, interface_dependencies_filen ame, additional_idl_filenames, verbose=False): | |
| 56 """Resolves dependencies, merging them into an existing IDL document. | |
|
haraken
2013/07/22 01:50:23
Slightly better: "Resolves interface dependencies
Nils Barth (inactive)
2013/07/22 06:32:01
I've changed comment, rewording a bit.
| |
| 57 | |
| 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] | |
|
haraken
2013/07/22 01:50:23
Nit: interface => target_interface (for clarity)
Nils Barth (inactive)
2013/07/22 06:32:01
Fixed throughout.
| |
| 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 |
| 84 file. | 97 file. |
| 85 - Otherwise, return None. This happens when the given IDL file is a | 98 - Otherwise, return None. This happens when the given IDL file is a |
| 86 dependency, for which we don't want to generate bindings. | 99 dependency, for which we don't want to generate bindings. |
| 87 """ | 100 """ |
| 88 if interface_dependencies_filename is None: | |
| 89 return [] | |
| 90 | |
| 91 # The format of the interface dependencies file is: | 101 # The format of the interface dependencies file is: |
| 92 # | 102 # |
| 93 # Document.idl P.idl | 103 # Document.idl P.idl |
| 94 # Event.idl | 104 # Event.idl |
| 95 # Window.idl Q.idl R.idl S.idl | 105 # Window.idl Q.idl R.idl S.idl |
| 96 # ... | 106 # ... |
| 97 # | 107 # |
| 98 # The above indicates that: | 108 # The above indicates that: |
| 99 # Document.idl depends on P.idl, | 109 # Document.idl depends on P.idl, |
| 100 # Event.idl depends on no other IDL files, and | 110 # Event.idl depends on no other IDL files, and |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 114 return dependency_filenames.split() | 124 return dependency_filenames.split() |
| 115 | 125 |
| 116 # additional_idl_files is a list of IDL files which should not be included | 126 # 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 | 127 # in DerivedSources*.cpp, and hence are not listed in the interface |
| 118 # dependencies file, but for which we should generate .cpp and .h files. | 128 # 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)) | 129 additional_idl_basenames = set(map(os.path.basename, additional_idl_filename s)) |
| 120 if target_idl_basename in additional_idl_basenames: | 130 if target_idl_basename in additional_idl_basenames: |
| 121 return [] | 131 return [] |
| 122 | 132 |
| 123 return None | 133 return None |
| 134 | |
| 135 | |
| 136 def merge_interface_dependencies(interface, idl_filename, dependency_idl_filenam es, verbose=False): | |
|
haraken
2013/07/22 01:50:23
Nit: interface => target_interface
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 137 """Merge partial interfaces in dependency_idl_files into target_document. | |
|
haraken
2013/07/22 01:50:23
dependency_idl_files => dependency_idl_filename
ta
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 138 | |
| 139 No return: modifies target_document in place. | |
|
haraken
2013/07/22 01:50:23
target_document => target_interface
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 140 """ | |
| 141 # Sort so order consistent, so can compare output from run to run. | |
| 142 for dependency_idl_filename in sorted(dependency_idl_filenames): | |
| 143 dependency_interface_name, _ = os.path.splitext(os.path.basename(depende ncy_idl_filename)) | |
| 144 definitions = idl_reader.read_idl_file(dependency_idl_filename, verbose= verbose) | |
| 145 | |
| 146 for dependency_interface in definitions.interfaces.itervalues(): | |
| 147 # Dependency files contain either partial interfaces for | |
| 148 # the (single) target interface, in which case the interface names | |
| 149 # must agree, or interfaces that are implemented by the target | |
| 150 # interface, in which case the interface names differ. | |
| 151 if dependency_interface.is_partial and dependency_interface.name != interface.name: | |
| 152 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)) | |
| 153 if 'ImplementedAs' in dependency_interface.extended_attributes: | |
| 154 del dependency_interface.extended_attributes['ImplementedAs'] | |
| 155 merge_dependency_interface(interface, dependency_interface, dependen cy_interface_name) | |
| 156 | |
| 157 | |
| 158 def merge_dependency_interface(interface, dependency_interface, dependency_inter face_name): | |
|
haraken
2013/07/22 01:50:23
Nit: interface => target_interface
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 159 """Merge dependency_interface into interface. | |
|
haraken
2013/07/22 01:50:23
Ditto.
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 160 | |
| 161 No return: modifies interface in place. | |
|
haraken
2013/07/22 01:50:23
Ditto.
Nils Barth (inactive)
2013/07/22 06:32:01
Done.
| |
| 162 """ | |
| 163 def merge_lists(source_list, target_list): | |
| 164 for element in source_list: | |
| 165 if dependency_interface.is_partial: | |
| 166 element.extended_attributes['ImplementedBy'] = dependency_interf ace_name | |
| 167 element.extended_attributes.update(dependency_interface.extended_att ributes) | |
| 168 target_list.append(element) | |
| 169 | |
| 170 merge_lists(dependency_interface.attributes, interface.attributes) | |
| 171 merge_lists(dependency_interface.constants, interface.constants) | |
| 172 merge_lists(dependency_interface.functions, interface.functions) | |
| OLD | NEW |