OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (C) 2013 Google Inc. All rights reserved. | 3 # Copyright (C) 2013 Google Inc. All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 Design doc: http://www.chromium.org/developers/design-documents/idl-build | 41 Design doc: http://www.chromium.org/developers/design-documents/idl-build |
42 """ | 42 """ |
43 | 43 |
44 from collections import defaultdict | 44 from collections import defaultdict |
45 import optparse | 45 import optparse |
46 import os | 46 import os |
47 import posixpath | 47 import posixpath |
48 import sys | 48 import sys |
49 | 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 | 50 from utilities import get_file_contents, read_file_to_list, idl_filename_to_inte
rface_name, idl_filename_to_component, write_pickle_file, get_interface_extended
_attributes_from_idl, is_callback_interface_from_idl, is_dictionary_from_idl, ge
t_partial_interface_name_from_idl, get_implements_from_idl, get_parent_interface
, get_put_forward_interfaces_from_idl |
51 | 51 |
52 module_path = os.path.dirname(__file__) | 52 module_path = os.path.dirname(__file__) |
53 source_path = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir)) | 53 source_path = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir)) |
54 | 54 |
55 # Global variables (filled in and exported) | 55 # Global variables (filled in and exported) |
56 interfaces_info = {} | 56 interfaces_info = {} |
57 partial_interface_files = defaultdict(lambda: { | 57 partial_interface_files = defaultdict(lambda: { |
58 'full_paths': [], | 58 'full_paths': [], |
59 'include_paths': [], | 59 'include_paths': [], |
60 }) | 60 }) |
61 | 61 |
62 | 62 |
63 def parse_options(): | 63 def parse_options(): |
64 usage = 'Usage: %prog [options] [generated1.idl]...' | 64 usage = 'Usage: %prog [options] [generated1.idl]...' |
65 parser = optparse.OptionParser(usage=usage) | 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') | 66 parser.add_option('--idl-files-list', help='file listing IDL files') |
68 parser.add_option('--interfaces-info-file', help='output pickle file') | 67 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') | 68 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 | 69 |
71 options, args = parser.parse_args() | 70 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: | 71 if options.interfaces_info_file is None: |
75 parser.error('Must specify an output file using --interfaces-info-file.'
) | 72 parser.error('Must specify an output file using --interfaces-info-file.'
) |
76 if options.idl_files_list is None: | 73 if options.idl_files_list is None: |
77 parser.error('Must specify a file listing IDL files using --idl-files-li
st.') | 74 parser.error('Must specify a file listing IDL files using --idl-files-li
st.') |
78 if options.write_file_only_if_changed is None: | 75 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.') | 76 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
) | 77 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
81 return options, args | 78 return options, args |
82 | 79 |
83 | 80 |
84 ################################################################################ | 81 ################################################################################ |
85 # Computations | 82 # Computations |
86 ################################################################################ | 83 ################################################################################ |
87 | 84 |
| 85 def relative_dir_posix(idl_filename): |
| 86 """Returns relative path to the directory of idl_file in POSIX format.""" |
| 87 relative_path_local = os.path.relpath(idl_filename, source_path) |
| 88 relative_dir_local = os.path.dirname(relative_path_local) |
| 89 return relative_dir_local.replace(os.path.sep, posixpath.sep) |
| 90 |
| 91 |
88 def include_path(idl_filename, implemented_as=None): | 92 def include_path(idl_filename, implemented_as=None): |
89 """Returns relative path to header file in POSIX format; used in includes. | 93 """Returns relative path to header file in POSIX format; used in includes. |
90 | 94 |
91 POSIX format is used for consistency of output, so reference tests are | 95 POSIX format is used for consistency of output, so reference tests are |
92 platform-independent. | 96 platform-independent. |
93 """ | 97 """ |
94 relative_path_local = os.path.relpath(idl_filename, source_path) | 98 relative_dir = relative_dir_posix(idl_filename) |
95 relative_dir_local = os.path.dirname(relative_path_local) | |
96 relative_dir_posix = relative_dir_local.replace(os.path.sep, posixpath.sep) | |
97 | 99 |
98 # IDL file basename is used even if only a partial interface file | 100 # IDL file basename is used even if only a partial interface file |
99 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename)) | 101 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename)) |
100 cpp_class_name = implemented_as or idl_file_basename | 102 cpp_class_name = implemented_as or idl_file_basename |
101 | 103 |
102 return posixpath.join(relative_dir_posix, cpp_class_name + '.h') | 104 return posixpath.join(relative_dir, cpp_class_name + '.h') |
103 | 105 |
104 | 106 |
105 def add_paths_to_partials_dict(partial_interface_name, full_path, this_include_p
ath=None): | 107 def add_paths_to_partials_dict(partial_interface_name, full_path, this_include_p
ath=None): |
106 paths_dict = partial_interface_files[partial_interface_name] | 108 paths_dict = partial_interface_files[partial_interface_name] |
107 paths_dict['full_paths'].append(full_path) | 109 paths_dict['full_paths'].append(full_path) |
108 if this_include_path: | 110 if this_include_path: |
109 paths_dict['include_paths'].append(this_include_path) | 111 paths_dict['include_paths'].append(this_include_path) |
110 | 112 |
111 | 113 |
112 def compute_info_individual(idl_filename, component_dir): | 114 def compute_info_individual(idl_filename): |
113 full_path = os.path.realpath(idl_filename) | 115 full_path = os.path.realpath(idl_filename) |
114 idl_file_contents = get_file_contents(full_path) | 116 idl_file_contents = get_file_contents(full_path) |
115 | 117 |
116 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co
ntents) | 118 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co
ntents) |
117 implemented_as = extended_attributes.get('ImplementedAs') | 119 implemented_as = extended_attributes.get('ImplementedAs') |
118 this_include_path = include_path(idl_filename, implemented_as) | 120 relative_dir = relative_dir_posix(idl_filename) |
| 121 this_include_path = None if 'NoImplHeader' in extended_attributes else inclu
de_path(idl_filename, implemented_as) |
119 | 122 |
120 # Handle partial interfaces | 123 # Handle partial interfaces |
121 partial_interface_name = get_partial_interface_name_from_idl(idl_file_conten
ts) | 124 partial_interface_name = get_partial_interface_name_from_idl(idl_file_conten
ts) |
122 if partial_interface_name: | 125 if partial_interface_name: |
123 add_paths_to_partials_dict(partial_interface_name, full_path, this_inclu
de_path) | 126 add_paths_to_partials_dict(partial_interface_name, full_path, this_inclu
de_path) |
124 return | 127 return |
125 | 128 |
126 # If not a partial interface, the basename is the interface name | 129 # If not a partial interface, the basename is the interface name |
127 interface_name = idl_filename_to_interface_name(idl_filename) | 130 interface_name = idl_filename_to_interface_name(idl_filename) |
128 | 131 |
129 # 'implements' statements can be included in either the file for the | 132 # 'implements' statements can be included in either the file for the |
130 # implement*ing* interface (lhs of 'implements') or implement*ed* interface | 133 # implement*ing* interface (lhs of 'implements') or implement*ed* interface |
131 # (rhs of 'implements'). Store both for now, then merge to implement*ing* | 134 # (rhs of 'implements'). Store both for now, then merge to implement*ing* |
132 # interface later. | 135 # interface later. |
133 left_interfaces, right_interfaces = get_implements_from_idl(idl_file_content
s, interface_name) | 136 left_interfaces, right_interfaces = get_implements_from_idl(idl_file_content
s, interface_name) |
134 | 137 |
135 interfaces_info[interface_name] = { | 138 interfaces_info[interface_name] = { |
136 'component_dir': component_dir, | |
137 'extended_attributes': extended_attributes, | 139 'extended_attributes': extended_attributes, |
138 'full_path': full_path, | 140 'full_path': full_path, |
139 'implemented_as': implemented_as, | 141 'implemented_as': implemented_as, |
140 'implemented_by_interfaces': left_interfaces, # private, merged to next | 142 'implemented_by_interfaces': left_interfaces, # private, merged to next |
141 'implements_interfaces': right_interfaces, | 143 'implements_interfaces': right_interfaces, |
142 'include_path': this_include_path, | 144 'include_path': this_include_path, |
143 'is_callback_interface': is_callback_interface_from_idl(idl_file_content
s), | 145 'is_callback_interface': is_callback_interface_from_idl(idl_file_content
s), |
144 'is_dictionary': is_dictionary_from_idl(idl_file_contents), | 146 'is_dictionary': is_dictionary_from_idl(idl_file_contents), |
145 # FIXME: temporary private field, while removing old treatement of | 147 # FIXME: temporary private field, while removing old treatement of |
146 # 'implements': http://crbug.com/360435 | 148 # 'implements': http://crbug.com/360435 |
147 'is_legacy_treat_as_partial_interface': 'LegacyTreatAsPartialInterface'
in extended_attributes, | 149 'is_legacy_treat_as_partial_interface': 'LegacyTreatAsPartialInterface'
in extended_attributes, |
148 'parent': get_parent_interface(idl_file_contents), | 150 'parent': get_parent_interface(idl_file_contents), |
149 # Interfaces that are referenced (used as types) and that we introspect | 151 # Interfaces that are referenced (used as types) and that we introspect |
150 # during code generation (beyond interface-level data ([ImplementedAs], | 152 # during code generation (beyond interface-level data ([ImplementedAs], |
151 # is_callback_interface, ancestors, and inherited extended attributes): | 153 # is_callback_interface, ancestors, and inherited extended attributes): |
152 # deep dependencies. | 154 # deep dependencies. |
153 # These cause rebuilds of referrers, due to the dependency, so these | 155 # These cause rebuilds of referrers, due to the dependency, so these |
154 # should be minimized; currently only targets of [PutForwards]. | 156 # should be minimized; currently only targets of [PutForwards]. |
155 'referenced_interfaces': get_put_forward_interfaces_from_idl(idl_file_co
ntents), | 157 'referenced_interfaces': get_put_forward_interfaces_from_idl(idl_file_co
ntents), |
| 158 'relative_dir': relative_dir, |
156 } | 159 } |
157 | 160 |
158 | 161 |
159 def info_individual(): | 162 def info_individual(): |
160 """Returns info packaged as a dict.""" | 163 """Returns info packaged as a dict.""" |
161 return { | 164 return { |
162 'interfaces_info': interfaces_info, | 165 'interfaces_info': interfaces_info, |
163 # Can't pickle defaultdict, convert to dict | 166 # Can't pickle defaultdict, convert to dict |
164 'partial_interface_files': dict(partial_interface_files), | 167 'partial_interface_files': dict(partial_interface_files), |
165 } | 168 } |
166 | 169 |
167 | 170 |
168 ################################################################################ | 171 ################################################################################ |
169 | 172 |
170 def main(): | 173 def main(): |
171 options, args = parse_options() | 174 options, args = parse_options() |
172 | 175 |
173 # Static IDL files are passed in a file (generated at GYP time), due to OS | 176 # Static IDL files are passed in a file (generated at GYP time), due to OS |
174 # command line length limits | 177 # command line length limits |
175 idl_files = read_file_to_list(options.idl_files_list) | 178 idl_files = read_file_to_list(options.idl_files_list) |
176 # Generated IDL files are passed at the command line, since these are in the | 179 # Generated IDL files are passed at the command line, since these are in the |
177 # build directory, which is determined at build time, not GYP time, so these | 180 # build directory, which is determined at build time, not GYP time, so these |
178 # cannot be included in the file listing static files | 181 # cannot be included in the file listing static files |
179 idl_files.extend(args) | 182 idl_files.extend(args) |
180 | 183 |
181 # Compute information for individual files | 184 # Compute information for individual files |
182 # Information is stored in global variables interfaces_info and | 185 # Information is stored in global variables interfaces_info and |
183 # partial_interface_files. | 186 # partial_interface_files. |
184 for idl_filename in idl_files: | 187 for idl_filename in idl_files: |
185 compute_info_individual(idl_filename, options.component_dir) | 188 compute_info_individual(idl_filename) |
186 | 189 |
187 write_pickle_file(options.interfaces_info_file, | 190 write_pickle_file(options.interfaces_info_file, |
188 info_individual(), | 191 info_individual(), |
189 options.write_file_only_if_changed) | 192 options.write_file_only_if_changed) |
190 | 193 |
191 | 194 |
192 if __name__ == '__main__': | 195 if __name__ == '__main__': |
193 sys.exit(main()) | 196 sys.exit(main()) |
OLD | NEW |