Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: Source/bindings/scripts/generate_global_constructors.py

Issue 174953005: Clean up compute_interfaces_info.py and generate_global_constructors.py (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 22 matching lines...) Expand all
113 lines = (['partial interface %s {\n' % interface_name] + 97 lines = (['partial interface %s {\n' % interface_name] +
114 [' %s;\n' % constructor_attribute 98 [' %s;\n' % constructor_attribute
115 for constructor_attribute in sorted(constructor_attributes_list)] + 99 for constructor_attribute in sorted(constructor_attributes_list)] +
116 ['};\n']) 100 ['};\n'])
117 write_file(lines, destination_filename, only_if_changed) 101 write_file(lines, destination_filename, only_if_changed)
118 102
119 103
120 ################################################################################ 104 ################################################################################
121 105
122 def main(): 106 def main():
123 options = parse_options() 107 options, args = parse_options()
124 108
125 # Input IDL files are passed in a file, due to OS command line length 109 # Input IDL files are passed in a file, due to OS command line length
126 # limits. This is generated at GYP time, which is ok b/c files are static. 110 # limits. This is generated at GYP time, which is ok b/c files are static.
127 with open(options.idl_files_list) as idl_files_list: 111 with open(options.idl_files_list) as idl_files_list:
128 idl_files = [line.rstrip('\n') for line in idl_files_list] 112 idl_files = [line.rstrip('\n') for line in idl_files_list]
129 113
130 # Output IDL files (to generate) are passed at the command line, since 114 # Output IDL files (to generate) are passed at the command line, since
131 # these are in the build directory, which is determined at build time, not 115 # these are in the build directory, which is determined at build time, not
132 # GYP time. 116 # GYP time.
133 global_constructors_filenames = { 117 # These are passed as pairs of GlobalObjectName, GlobalObject.idl
134 'Window': options.window_constructors_file, 118 interface_name_filename = [(args[i], args[i + 1])
135 'WorkerGlobalScope': options.workerglobalscope_constructors_file, 119 for i in range(0, len(args), 2)]
136 'SharedWorkerGlobalScope': options.sharedworkerglobalscope_constructors_ file, 120 global_objects.update(
137 'DedicatedWorkerGlobalScope': options.dedicatedworkerglobalscope_constru ctors_file, 121 (interface_name, {
138 'ServiceWorkerGlobalScope': options.serviceworkerglobalscope_constructor s_file, 122 'filename': filename,
139 } 123 'constructors': [],
140 global_constructors.update(dict([ 124 })
141 (global_object, []) 125 for interface_name, filename in interface_name_filename)
142 for global_object in global_constructors_filenames]))
143 126
144 for idl_filename in idl_files: 127 for idl_filename in idl_files:
145 record_global_constructors(idl_filename) 128 record_global_constructors(idl_filename)
146 129
147 for interface_name, filename in global_constructors_filenames.iteritems(): 130 for interface_name, global_object in global_objects.iteritems():
148 write_global_constructors_partial_interface(interface_name, filename, gl obal_constructors[interface_name], options.write_file_only_if_changed) 131 write_global_constructors_partial_interface(interface_name, global_objec t['filename'], global_object['constructors'], options.write_file_only_if_changed )
149 132
150 133
151 if __name__ == '__main__': 134 if __name__ == '__main__':
152 sys.exit(main()) 135 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698