| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # | |
| 3 # Copyright (C) 2013 Google Inc. All rights reserved. | |
| 4 # | |
| 5 # Redistribution and use in source and binary forms, with or without | |
| 6 # modification, are permitted provided that the following conditions are | |
| 7 # met: | |
| 8 # | |
| 9 # * Redistributions of source code must retain the above copyright | |
| 10 # notice, this list of conditions and the following disclaimer. | |
| 11 # * Redistributions in binary form must reproduce the above | |
| 12 # copyright notice, this list of conditions and the following disclaimer | |
| 13 # in the documentation and/or other materials provided with the | |
| 14 # distribution. | |
| 15 # * Neither the name of Google Inc. nor the names of its | |
| 16 # contributors may be used to endorse or promote products derived from | |
| 17 # this software without specific prior written permission. | |
| 18 # | |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | |
| 31 """Compute global interface information for individual IDL files. | |
| 32 | |
| 33 Auxiliary module for compute_interfaces_info_overall, which consolidates | |
| 34 this individual information, computing info that spans multiple files | |
| 35 (dependencies and ancestry). | |
| 36 | |
| 37 This distinction is so that individual interface info can be computed | |
| 38 separately for each component (avoiding duplicated reading of individual | |
| 39 files), then consolidated using *only* the info visible to a given component. | |
| 40 | |
| 41 Design doc: http://www.chromium.org/developers/design-documents/idl-build | |
| 42 """ | |
| 43 | |
| 44 from collections import defaultdict | |
| 45 import optparse | |
| 46 import os | |
| 47 import posixpath | |
| 48 import sys | |
| 49 | |
| 50 from utilities import get_file_contents, read_file_to_list, idl_filename_to_inte
rface_name, write_pickle_file, get_interface_extended_attributes_from_idl, is_ca
llback_interface_from_idl, is_dictionary_from_idl, get_partial_interface_name_fr
om_idl, get_implements_from_idl, get_parent_interface, get_put_forward_interface
s_from_idl | |
| 51 | |
| 52 module_path = os.path.dirname(__file__) | |
| 53 source_path = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir)) | |
| 54 | |
| 55 # Global variables (filled in and exported) | |
| 56 interfaces_info = {} | |
| 57 partial_interface_files = defaultdict(lambda: { | |
| 58 'full_paths': [], | |
| 59 'include_paths': [], | |
| 60 }) | |
| 61 | |
| 62 | |
| 63 def parse_options(): | |
| 64 usage = 'Usage: %prog [options] [generated1.idl]...' | |
| 65 parser = optparse.OptionParser(usage=usage) | |
| 66 parser.add_option('--component-dir', help='component directory') | |
| 67 parser.add_option('--idl-files-list', help='file listing IDL files') | |
| 68 parser.add_option('--interfaces-info-file', help='output pickle file') | |
| 69 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') | |
| 70 | |
| 71 options, args = parser.parse_args() | |
| 72 if options.component_dir is None: | |
| 73 parser.error('Must specify a component directory using --component-dir.'
) | |
| 74 if options.interfaces_info_file is None: | |
| 75 parser.error('Must specify an output file using --interfaces-info-file.'
) | |
| 76 if options.idl_files_list is None: | |
| 77 parser.error('Must specify a file listing IDL files using --idl-files-li
st.') | |
| 78 if options.write_file_only_if_changed is None: | |
| 79 parser.error('Must specify whether file is only written if changed using
--write-file-only-if-changed.') | |
| 80 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | |
| 81 return options, args | |
| 82 | |
| 83 | |
| 84 ################################################################################ | |
| 85 # Computations | |
| 86 ################################################################################ | |
| 87 | |
| 88 def relative_dir_posix(idl_filename): | |
| 89 """Returns relative path to the directory of idl_file in POSIX format.""" | |
| 90 relative_path_local = os.path.relpath(idl_filename, source_path) | |
| 91 relative_dir_local = os.path.dirname(relative_path_local) | |
| 92 return relative_dir_local.replace(os.path.sep, posixpath.sep) | |
| 93 | |
| 94 | |
| 95 def include_path(idl_filename, implemented_as=None): | |
| 96 """Returns relative path to header file in POSIX format; used in includes. | |
| 97 | |
| 98 POSIX format is used for consistency of output, so reference tests are | |
| 99 platform-independent. | |
| 100 """ | |
| 101 relative_dir = relative_dir_posix(idl_filename) | |
| 102 | |
| 103 # IDL file basename is used even if only a partial interface file | |
| 104 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename)) | |
| 105 cpp_class_name = implemented_as or idl_file_basename | |
| 106 | |
| 107 return posixpath.join(relative_dir, cpp_class_name + '.h') | |
| 108 | |
| 109 | |
| 110 def add_paths_to_partials_dict(partial_interface_name, full_path, this_include_p
ath=None): | |
| 111 paths_dict = partial_interface_files[partial_interface_name] | |
| 112 paths_dict['full_paths'].append(full_path) | |
| 113 if this_include_path: | |
| 114 paths_dict['include_paths'].append(this_include_path) | |
| 115 | |
| 116 | |
| 117 def compute_info_individual(idl_filename, component_dir): | |
| 118 full_path = os.path.realpath(idl_filename) | |
| 119 idl_file_contents = get_file_contents(full_path) | |
| 120 | |
| 121 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co
ntents) | |
| 122 implemented_as = extended_attributes.get('ImplementedAs') | |
| 123 relative_dir = relative_dir_posix(idl_filename) | |
| 124 this_include_path = None if 'NoImplHeader' in extended_attributes else inclu
de_path(idl_filename, implemented_as) | |
| 125 | |
| 126 # Handle partial interfaces | |
| 127 partial_interface_name = get_partial_interface_name_from_idl(idl_file_conten
ts) | |
| 128 if partial_interface_name: | |
| 129 add_paths_to_partials_dict(partial_interface_name, full_path, this_inclu
de_path) | |
| 130 return | |
| 131 | |
| 132 # If not a partial interface, the basename is the interface name | |
| 133 interface_name = idl_filename_to_interface_name(idl_filename) | |
| 134 | |
| 135 # 'implements' statements can be included in either the file for the | |
| 136 # implement*ing* interface (lhs of 'implements') or implement*ed* interface | |
| 137 # (rhs of 'implements'). Store both for now, then merge to implement*ing* | |
| 138 # interface later. | |
| 139 left_interfaces, right_interfaces = get_implements_from_idl(idl_file_content
s, interface_name) | |
| 140 | |
| 141 interfaces_info[interface_name] = { | |
| 142 'component_dir': component_dir, | |
| 143 'extended_attributes': extended_attributes, | |
| 144 'full_path': full_path, | |
| 145 'implemented_as': implemented_as, | |
| 146 'implemented_by_interfaces': left_interfaces, # private, merged to next | |
| 147 'implements_interfaces': right_interfaces, | |
| 148 'include_path': this_include_path, | |
| 149 'is_callback_interface': is_callback_interface_from_idl(idl_file_content
s), | |
| 150 'is_dictionary': is_dictionary_from_idl(idl_file_contents), | |
| 151 # FIXME: temporary private field, while removing old treatement of | |
| 152 # 'implements': http://crbug.com/360435 | |
| 153 'is_legacy_treat_as_partial_interface': 'LegacyTreatAsPartialInterface'
in extended_attributes, | |
| 154 'parent': get_parent_interface(idl_file_contents), | |
| 155 # Interfaces that are referenced (used as types) and that we introspect | |
| 156 # during code generation (beyond interface-level data ([ImplementedAs], | |
| 157 # is_callback_interface, ancestors, and inherited extended attributes): | |
| 158 # deep dependencies. | |
| 159 # These cause rebuilds of referrers, due to the dependency, so these | |
| 160 # should be minimized; currently only targets of [PutForwards]. | |
| 161 'referenced_interfaces': get_put_forward_interfaces_from_idl(idl_file_co
ntents), | |
| 162 'relative_dir': relative_dir, | |
| 163 } | |
| 164 | |
| 165 | |
| 166 def info_individual(): | |
| 167 """Returns info packaged as a dict.""" | |
| 168 return { | |
| 169 'interfaces_info': interfaces_info, | |
| 170 # Can't pickle defaultdict, convert to dict | |
| 171 'partial_interface_files': dict(partial_interface_files), | |
| 172 } | |
| 173 | |
| 174 | |
| 175 ################################################################################ | |
| 176 | |
| 177 def main(): | |
| 178 options, args = parse_options() | |
| 179 | |
| 180 # Static IDL files are passed in a file (generated at GYP time), due to OS | |
| 181 # command line length limits | |
| 182 idl_files = read_file_to_list(options.idl_files_list) | |
| 183 # Generated IDL files are passed at the command line, since these are in the | |
| 184 # build directory, which is determined at build time, not GYP time, so these | |
| 185 # cannot be included in the file listing static files | |
| 186 idl_files.extend(args) | |
| 187 | |
| 188 # Compute information for individual files | |
| 189 # Information is stored in global variables interfaces_info and | |
| 190 # partial_interface_files. | |
| 191 for idl_filename in idl_files: | |
| 192 compute_info_individual(idl_filename, options.component_dir) | |
| 193 | |
| 194 write_pickle_file(options.interfaces_info_file, | |
| 195 info_individual(), | |
| 196 options.write_file_only_if_changed) | |
| 197 | |
| 198 | |
| 199 if __name__ == '__main__': | |
| 200 sys.exit(main()) | |
| OLD | NEW |