| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Generates interface properties on global objects. | 7 """Generates interface properties on global objects. |
| 8 | 8 |
| 9 Concretely these are implemented as "constructor attributes", meaning | 9 Concretely these are implemented as "constructor attributes", meaning |
| 10 "attributes whose name ends with Constructor" (special-cased by code generator), | 10 "attributes whose name ends with Constructor" (special-cased by code generator), |
| 11 hence "global constructors" for short. | 11 hence "global constructors" for short. |
| 12 | 12 |
| 13 For reference on global objects, see: | 13 For reference on global objects, see: |
| 14 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes#TOC-GlobalCon
text-i- | 14 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes#TOC-GlobalCon
text-i- |
| 15 | 15 |
| 16 Design document: | 16 Design document: |
| 17 http://www.chromium.org/developers/design-documents/idl-build | 17 http://www.chromium.org/developers/design-documents/idl-build |
| 18 """ | 18 """ |
| 19 | 19 |
| 20 import optparse | 20 import optparse |
| 21 import os | 21 import os |
| 22 import re | 22 import re |
| 23 import sys | 23 import sys |
| 24 | 24 |
| 25 from utilities import get_file_contents, write_file, get_interface_extended_attr
ibutes_from_idl, is_callback_interface_from_idl, get_partial_interface_name_from
_idl | 25 from utilities import get_file_contents, write_file, get_interface_extended_attr
ibutes_from_idl, is_callback_interface_from_idl |
| 26 | 26 |
| 27 global_constructors = {} | 27 global_objects = {} |
| 28 | 28 |
| 29 | 29 |
| 30 def parse_options(): | 30 def parse_options(): |
| 31 parser = optparse.OptionParser() | 31 parser = optparse.OptionParser() |
| 32 parser.add_option('--idl-files-list', help='file listing IDL files') | 32 parser.add_option('--idl-files-list', help='file listing IDL files') |
| 33 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') | 33 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') |
| 34 parser.add_option('--window-constructors-file', help='output file') | |
| 35 parser.add_option('--workerglobalscope-constructors-file', help='output file
') | |
| 36 parser.add_option('--sharedworkerglobalscope-constructors-file', help='outpu
t file') | |
| 37 parser.add_option('--dedicatedworkerglobalscope-constructors-file', help='ou
tput file') | |
| 38 parser.add_option('--serviceworkerglobalscope-constructors-file', help='outp
ut file') | |
| 39 | 34 |
| 40 options, _ = parser.parse_args() | 35 options, args = parser.parse_args() |
| 41 | 36 |
| 42 if options.idl_files_list is None: | 37 if options.idl_files_list is None: |
| 43 parser.error('Must specify a file listing IDL files using --idl-files-li
st.') | 38 parser.error('Must specify a file listing IDL files using --idl-files-li
st.') |
| 44 if options.write_file_only_if_changed is None: | 39 if options.write_file_only_if_changed is None: |
| 45 parser.error('Must specify whether output files are only written if chan
ged using --write-file-only-if-changed.') | 40 parser.error('Must specify whether output files are only written if chan
ged using --write-file-only-if-changed.') |
| 46 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | 41 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
| 47 if options.window_constructors_file is None: | |
| 48 parser.error('Must specify an output file using --window-constructors-fi
le.') | |
| 49 if options.workerglobalscope_constructors_file is None: | |
| 50 parser.error('Must specify an output file using --workerglobalscope-cons
tructors-file.') | |
| 51 if options.sharedworkerglobalscope_constructors_file is None: | |
| 52 parser.error('Must specify an output file using --sharedworkerglobalscop
e-constructors-file.') | |
| 53 if options.dedicatedworkerglobalscope_constructors_file is None: | |
| 54 parser.error('Must specify an output file using --dedicatedworkerglobals
cope-constructors-file.') | |
| 55 if options.serviceworkerglobalscope_constructors_file is None: | |
| 56 parser.error('Must specify an output file using --serviceworkerglobalsco
pe-constructors-file.') | |
| 57 | 42 |
| 58 return options | 43 return options, args |
| 59 | 44 |
| 60 | 45 |
| 61 def record_global_constructors(idl_filename): | 46 def record_global_constructors(idl_filename): |
| 62 interface_name, _ = os.path.splitext(os.path.basename(idl_filename)) | 47 interface_name, _ = os.path.splitext(os.path.basename(idl_filename)) |
| 63 full_path = os.path.realpath(idl_filename) | 48 full_path = os.path.realpath(idl_filename) |
| 64 idl_file_contents = get_file_contents(full_path) | 49 idl_file_contents = get_file_contents(full_path) |
| 65 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co
ntents) | 50 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co
ntents) |
| 66 | 51 |
| 67 # An interface property is produced for every non-callback interface | 52 # An interface property is produced for every non-callback interface |
| 68 # that does not have [NoInterfaceObject]. | 53 # that does not have [NoInterfaceObject]. |
| 69 # Callback interfaces with constants also have interface properties, | 54 # Callback interfaces with constants also have interface properties, |
| 70 # but there are none of these in Blink. | 55 # but there are none of these in Blink. |
| 71 # http://heycam.github.io/webidl/#es-interfaces | 56 # http://heycam.github.io/webidl/#es-interfaces |
| 72 if (is_callback_interface_from_idl(idl_file_contents) or | 57 if (is_callback_interface_from_idl(idl_file_contents) or |
| 73 get_partial_interface_name_from_idl(idl_file_contents) or | |
| 74 'NoInterfaceObject' in extended_attributes): | 58 'NoInterfaceObject' in extended_attributes): |
| 75 return | 59 return |
| 76 | 60 |
| 77 global_contexts = extended_attributes.get('GlobalContext', 'Window').split('
&') | 61 global_contexts = extended_attributes.get('GlobalContext', 'Window').split('
&') |
| 78 new_constructors_list = generate_global_constructors_list(interface_name, ex
tended_attributes) | 62 new_constructors_list = generate_global_constructors_list(interface_name, ex
tended_attributes) |
| 79 for interface_name in global_contexts: | 63 for interface_name in global_contexts: |
| 80 global_constructors[interface_name].extend(new_constructors_list) | 64 global_objects[interface_name]['constructors'].extend(new_constructors_l
ist) |
| 81 | 65 |
| 82 | 66 |
| 83 def generate_global_constructors_list(interface_name, extended_attributes): | 67 def generate_global_constructors_list(interface_name, extended_attributes): |
| 84 extended_attributes_list = [ | 68 extended_attributes_list = [ |
| 85 name + '=' + extended_attributes[name] | 69 name + '=' + extended_attributes[name] |
| 86 for name in 'Conditional', 'PerContextEnabled', 'RuntimeEnabled' | 70 for name in 'Conditional', 'PerContextEnabled', 'RuntimeEnabled' |
| 87 if name in extended_attributes] | 71 if name in extended_attributes] |
| 88 if extended_attributes_list: | 72 if extended_attributes_list: |
| 89 extended_string = '[%s] ' % ', '.join(extended_attributes_list) | 73 extended_string = '[%s] ' % ', '.join(extended_attributes_list) |
| 90 else: | 74 else: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 118 [' %s;\n' % constructor_attribute | 102 [' %s;\n' % constructor_attribute |
| 119 # FIXME: sort by interface name (not first by extended attributes) | 103 # FIXME: sort by interface name (not first by extended attributes) |
| 120 for constructor_attribute in sorted(constructor_attributes_list)]
+ | 104 for constructor_attribute in sorted(constructor_attributes_list)]
+ |
| 121 ['};\n']) | 105 ['};\n']) |
| 122 write_file(lines, destination_filename, only_if_changed) | 106 write_file(lines, destination_filename, only_if_changed) |
| 123 | 107 |
| 124 | 108 |
| 125 ################################################################################ | 109 ################################################################################ |
| 126 | 110 |
| 127 def main(): | 111 def main(): |
| 128 options = parse_options() | 112 options, args = parse_options() |
| 129 | 113 |
| 130 # Input IDL files are passed in a file, due to OS command line length | 114 # Input IDL files are passed in a file, due to OS command line length |
| 131 # limits. This is generated at GYP time, which is ok b/c files are static. | 115 # limits. This is generated at GYP time, which is ok b/c files are static. |
| 132 with open(options.idl_files_list) as idl_files_list: | 116 with open(options.idl_files_list) as idl_files_list: |
| 133 idl_files = [line.rstrip('\n') for line in idl_files_list] | 117 idl_files = [line.rstrip('\n') for line in idl_files_list] |
| 134 | 118 |
| 135 # Output IDL files (to generate) are passed at the command line, since | 119 # Output IDL files (to generate) are passed at the command line, since |
| 136 # these are in the build directory, which is determined at build time, not | 120 # these are in the build directory, which is determined at build time, not |
| 137 # GYP time. | 121 # GYP time. |
| 138 global_constructors_filenames = { | 122 # These are passed as pairs of GlobalObjectName, GlobalObject.idl |
| 139 'Window': options.window_constructors_file, | 123 interface_name_filename = [(args[i], args[i + 1]) |
| 140 'WorkerGlobalScope': options.workerglobalscope_constructors_file, | 124 for i in range(0, len(args), 2)] |
| 141 'SharedWorkerGlobalScope': options.sharedworkerglobalscope_constructors_
file, | 125 global_objects.update( |
| 142 'DedicatedWorkerGlobalScope': options.dedicatedworkerglobalscope_constru
ctors_file, | 126 (interface_name, { |
| 143 'ServiceWorkerGlobalScope': options.serviceworkerglobalscope_constructor
s_file, | 127 'filename': filename, |
| 144 } | 128 'constructors': [], |
| 145 global_constructors.update(dict([ | 129 }) |
| 146 (global_object, []) | 130 for interface_name, filename in interface_name_filename) |
| 147 for global_object in global_constructors_filenames])) | |
| 148 | 131 |
| 149 for idl_filename in idl_files: | 132 for idl_filename in idl_files: |
| 150 record_global_constructors(idl_filename) | 133 record_global_constructors(idl_filename) |
| 151 | 134 |
| 152 for interface_name, filename in global_constructors_filenames.iteritems(): | 135 for interface_name, global_object in global_objects.iteritems(): |
| 153 write_global_constructors_partial_interface(interface_name, filename, gl
obal_constructors[interface_name], options.write_file_only_if_changed) | 136 write_global_constructors_partial_interface(interface_name, global_objec
t['filename'], global_object['constructors'], options.write_file_only_if_changed
) |
| 154 | 137 |
| 155 | 138 |
| 156 if __name__ == '__main__': | 139 if __name__ == '__main__': |
| 157 sys.exit(main()) | 140 sys.exit(main()) |
| OLD | NEW |