Flutter Engine
The Flutter Engine
dartdomgenerator.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5"""This is the entry point to create Dart APIs from the IDL database."""
6
7import css_code_generator
8import os
9import sys
10
11# Setup all paths to find our PYTHON code
12
13# dart_dir is the location of dart's enlistment dartium (dartium-git/src/dart)
14# and Dart (dart-git/dart).
15dart_dir = os.path.abspath(
16 os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
17sys.path.insert(1, os.path.join(dart_dir, 'tools/dom/new_scripts'))
18sys.path.insert(1, os.path.join(dart_dir,
19 'third_party/WebCore/bindings/scripts'))
20
21# Dartium's third_party directory location is dartium-git/src/third_party
22# and Dart's third_party directory location is dart-git/dart/third_party.
23third_party_dir = os.path.join(dart_dir, 'third_party')
24
25ply_dir = os.path.join(third_party_dir, 'ply')
26# If ply directory found then we're a Dart enlistment; third_party location
27# is dart-git/dart/third_party
28if not os.path.exists(ply_dir):
29 # For Dartium (ply directory is dartium-git/src/third_party/ply) third_party
30 # location is dartium-git/src/third_party
31 third_party_dir = os.path.join(dart_dir, '..', 'third_party')
32 assert (os.path.exists(third_party_dir))
33else:
34 # It's Dart we need to make sure that WebCore is injected in our search path
35 # because this is where idl_parser is located for a Dart enlistment. Dartium
36 # can figure out the tools directory because of the location of where the
37 # scripts blink scripts are located.
38 sys.path.insert(1, os.path.join(dart_dir, 'third_party/WebCore'))
39
40sys.path.insert(1, third_party_dir)
41
42sys.path.insert(1, os.path.join(dart_dir, 'tools/dom/scripts'))
43
44import dartgenerator
45import database
46import fremontcutbuilder
47import logging
48import monitored
49import multiemitter
50import optparse
51import shutil
52import subprocess
53import time
54from dartmetadata import DartMetadata
55from generator import TypeRegistry
56from generate_blink_file import Generate_Blink
57from htmleventgenerator import HtmlEventGenerator
58from htmlrenamer import HtmlRenamer
59from prototype_htmleventgenerator import Prototype_HtmlEventGenerator
60from systemhtml import DartLibraryEmitter, Dart2JSBackend,\
61 HtmlDartInterfaceGenerator, DartLibrary, DartLibraries,\
62 HTML_LIBRARY_NAMES
63from systemnative import CPPLibraryEmitter, DartiumBackend
64from templateloader import TemplateLoader
65
66sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
67
68import utils
69
70_logger = logging.getLogger('dartdomgenerator')
71
72
73class GeneratorOptions(object):
74
75 def __init__(self, templates, database, type_registry, renamer, metadata,
76 dart_js_interop):
77 self.templates = templates
78 self.database = database
79 self.type_registry = type_registry
80 self.renamer = renamer
81 self.metadata = metadata
82 self.dart_js_interop = dart_js_interop
83
84
85def LoadDatabase(database_dir, use_database_cache):
86 common_database = database.Database(database_dir)
87 if use_database_cache:
88 common_database.LoadFromCache()
89 else:
90 common_database.Load()
91 return common_database
92
93
94def GenerateFromDatabase(common_database,
95 dart2js_output_dir,
96 update_dom_metadata=False,
97 logging_level=logging.WARNING,
98 dart_js_interop=False,
99 generate_static_extensions=False,
100 generate_prototype_events=False):
101 print('\n ----- Accessing DOM using %s -----\n' %
102 ('dart:js' if dart_js_interop else 'C++'))
103
104 start_time = time.time()
105
106 current_dir = os.path.dirname(__file__)
107 auxiliary_dir = os.path.join(current_dir, '..', 'src')
108 template_dir = os.path.join(current_dir, '..', 'templates')
109
110 _logger.setLevel(logging_level)
111
112 generator = dartgenerator.DartGenerator(logging_level)
113 generator.LoadAuxiliary(auxiliary_dir)
114
115 generator.FilterMembersWithUnidentifiedTypes(common_database)
116 webkit_database = common_database.Clone()
117
118 # Generate Dart interfaces for the WebKit DOM.
119 generator.FilterInterfaces(
120 database=webkit_database,
121 or_annotations=['WebKit', 'Dart'],
122 exclude_displaced=['WebKit'],
123 exclude_suppressed=['WebKit', 'Dart'])
124 generator.FixEventTargets(webkit_database)
125 generator.AddMissingArguments(webkit_database)
126 generator.CleanupOperationArguments(webkit_database)
127
128 emitters = multiemitter.MultiEmitter(logging_level)
129 metadata = DartMetadata(os.path.join(current_dir, '..', 'dom.json'),
130 os.path.join(current_dir, '..', 'docs.json'),
131 logging_level)
132 renamer = HtmlRenamer(webkit_database, metadata)
133 type_registry = TypeRegistry(webkit_database, renamer)
134
135 print('GenerateFromDatabase %s seconds' % round(
136 (time.time() - start_time), 2))
137
138 def RunGenerator(dart_libraries, dart_output_dir, template_loader,
139 backend_factory, dart_js_interop):
140 options = GeneratorOptions(template_loader, webkit_database,
141 type_registry, renamer, metadata,
142 dart_js_interop)
143 dart_library_emitter = DartLibraryEmitter(emitters, dart_output_dir,
144 dart_libraries)
145 event_generator = HtmlEventGenerator(webkit_database, renamer, metadata,
146 template_loader)
147 prototype_event_generator = Prototype_HtmlEventGenerator(
148 webkit_database, renamer, metadata, event_generator)
149
150 def generate_interface(interface, gl_constants=None):
151 backend = backend_factory(interface)
152 interface_generator = HtmlDartInterfaceGenerator(
153 options, dart_library_emitter, event_generator,
154 prototype_event_generator, interface, backend)
155 interface_generator.Generate()
156 if len(backend._gl_constants) > 0 and not (gl_constants is None):
157 gl_constants.extend(backend._gl_constants)
158
159 generator.Generate(webkit_database, common_database, generate_interface)
160 if generate_prototype_events:
161 dart_bin = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart')
162 prototype_event_generator.WriteFile(dart_bin)
163
164 dart_library_emitter.EmitLibraries(auxiliary_dir, dart_js_interop)
165
166 if dart2js_output_dir:
167 template_paths = ['html/dart2js', 'html/impl', 'html/interface', '']
168 template_loader = TemplateLoader(template_dir, template_paths, {
169 'DARTIUM': False,
170 'DART2JS': True,
171 'JSINTEROP': False,
172 'NNBD': True,
173 })
174 backend_options = GeneratorOptions(template_loader, webkit_database,
175 type_registry, renamer, metadata,
176 dart_js_interop)
177
178 backend_factory = lambda interface:\
179 Dart2JSBackend(interface, backend_options, logging_level, generate_static_extensions)
180
181 dart_output_dir = os.path.join(dart2js_output_dir, 'dart')
182 dart_libraries = DartLibraries(HTML_LIBRARY_NAMES, template_loader,
183 'dart2js', dart2js_output_dir,
184 dart_js_interop)
185 for file in generator._auxiliary_files.values():
186 if file.endswith('darttemplate'):
187 dart_libraries._libraries['html'].AddFile(file)
188
189 print('\nGenerating dart2js:\n')
190 start_time = time.time()
191
192 RunGenerator(dart_libraries, dart_output_dir, template_loader,
193 backend_factory, dart_js_interop)
194
195 print('Generated dart2js in %s seconds' %
196 round(time.time() - start_time, 2))
197
198 emitters.Flush()
199
200 if update_dom_metadata:
201 metadata.Flush()
202
203 monitored.FinishMonitoring(dart2js_output_dir, _logger)
204
205
206def GenerateSingleFile(library_path, output_dir, generated_output_dir=None, prefix=None):
207 library_dir = os.path.dirname(library_path)
208 library_filename = os.path.basename(library_path)
209 copy_dart_script = os.path.relpath('../../copy_dart.py', library_dir)
210 output_dir = os.path.relpath(output_dir, library_dir)
211 if not os.path.exists(library_dir):
212 os.makedirs(library_dir)
213 prefix_arg = 'export DART_HTML_PREFIX="' + prefix + '";' if prefix else ""
214 command = ' '.join([
215 prefix_arg, 'cd', library_dir, ';', copy_dart_script, output_dir, library_filename
216 ])
217 subprocess.call([command], shell=True)
218 dart_bin = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart')
219 sdk_file = os.path.join(library_dir, output_dir, library_filename)
220 formatCommand = ' '.join([dart_bin, 'format', sdk_file])
221 subprocess.call([formatCommand], shell=True)
222
223
225 """Regenerate the CssStyleDeclaration template file with the current CSS
226 properties."""
227 _logger.info('Updating Css Properties.')
228 css_code_generator.GenerateCssTemplateFile()
229
230
231CACHED_PATCHES = """
232// START_OF_CACHED_PATCHES
233// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
234// for details. All rights reserved. Use of this source code is governed by a
235// BSD-style license that can be found in the LICENSE file.
236
237// DO NOT EDIT GENERATED FILE.
238
239library cached_patches;
240
241var cached_patches = {
242 /********************************************************
243 ***** *****
244 ***** MUST RUN tools/dartium/generate_patches.sh *****
245 ***** *****
246 ********************************************************/
247};
248"""
249
250
251def main():
252 parser = optparse.OptionParser()
253 parser.add_option(
254 '--parallel',
255 dest='parallel',
256 action='store_true',
257 default=False,
258 help='Use fremontcut in parallel mode.')
259 parser.add_option(
260 '--systems',
261 dest='systems',
262 action='store',
263 type='string',
264 default='htmldart2js,htmldartium,_blink',
265 help='Systems to generate (htmldart2js, htmldartium, _blink)')
266 parser.add_option(
267 '--output-dir',
268 dest='output_dir',
269 action='store',
270 type='string',
271 default=None,
272 help='Directory to put the generated files')
273 parser.add_option(
274 '--use-database-cache',
275 dest='use_database_cache',
276 action='store_true',
277 default=False,
278 help='''Use the cached database from the previous run to
279 improve startup performance''')
280 parser.add_option(
281 '--update-dom-metadata',
282 dest='update_dom_metadata',
283 action='store_true',
284 default=False,
285 help='''Update the metadata list of DOM APIs''')
286 parser.add_option(
287 '--verbose',
288 dest='logging_level',
289 action='store_false',
290 default=logging.WARNING,
291 help='Output all informational messages')
292 parser.add_option(
293 '--examine',
294 dest='examine_idls',
295 action='store_true',
296 default=None,
297 help='Analyze IDL files')
298 parser.add_option(
299 '--logging',
300 dest='logging',
301 type='int',
302 action='store',
303 default=logging.NOTSET,
304 help='Level of logging 20 is Info, 30 is Warnings, 40 is Errors')
305 parser.add_option(
306 '--gen-interop',
307 dest='dart_js_interop',
308 action='store_true',
309 default=False,
310 help='Use JavaScript objects (dart:js) accessing the DOM in _blink')
311 parser.add_option(
312 '--no-cached-patches',
313 dest='no_cached_patches',
314 action='store_true',
315 default=False,
316 help='Do not generate the sdk/lib/js/cached_patches.dart file')
317 parser.add_option(
318 '--generate-static-extensions',
319 dest='generate_static_extensions',
320 action='store_true',
321 default=False,
322 help='Generate static extension members for dart:html classes')
323 parser.add_option(
324 '--generate-prototype-events',
325 dest='generate_prototype_events',
326 action='store_true',
327 default=False,
328 help='Generate event stream library for dart:html prototype')
329
330 (options, args) = parser.parse_args()
331
332 current_dir = os.path.dirname(__file__)
333 database_dir = os.path.join(current_dir, '..', 'database')
334 logging.config.fileConfig(os.path.join(current_dir, 'logging.conf'))
335 systems = options.systems.split(',')
336
337 output_dir = options.output_dir or os.path.join(
338 current_dir, '..', '..', '..', utils.GetBuildDir(utils.GuessOS()),
339 'generated')
340
341 dart2js_output_dir = None
342 if 'htmldart2js' in systems:
343 dart2js_output_dir = os.path.join(output_dir, 'dart2js')
344
345 logging_level = options.logging_level \
346 if options.logging == logging.NOTSET else options.logging
347
348 start_time = time.time()
349
351
352 # Parse the IDL and create the database.
353 database = fremontcutbuilder.main(
354 options.parallel,
355 logging_level=logging_level,
356 examine_idls=options.examine_idls)
357
358 GenerateFromDatabase(database, dart2js_output_dir,
359 options.update_dom_metadata, logging_level,
360 options.dart_js_interop,
361 options.generate_static_extensions,
362 options.generate_prototype_events)
363
364 file_generation_start_time = time.time()
365
366 if 'htmldart2js' in systems:
367 _logger.info('Generating dart2js single files.')
368
369 for library_name in HTML_LIBRARY_NAMES:
370 source = os.path.join(dart2js_output_dir,
371 '%s_dart2js.dart' % library_name)
373 source, os.path.join('..', '..', '..', 'sdk', 'lib',
374 library_name, 'dart2js'))
375
376 print('\nGenerating single file %s seconds' %
377 round(time.time() - file_generation_start_time, 2))
378
379 end_time = time.time()
380
381 print('\nDone (dartdomgenerator) %s seconds' %
382 round(end_time - start_time, 2))
383
384
385if __name__ == '__main__':
386 sys.exit(main())
static void round(SkPoint *p)
def __init__(self, templates, database, type_registry, renamer, metadata, dart_js_interop)
Definition: main.py:1
def print(*args, **kwargs)
Definition: run_tests.py:49
def GenerateFromDatabase(common_database, dart2js_output_dir, update_dom_metadata=False, logging_level=logging.WARNING, dart_js_interop=False, generate_static_extensions=False, generate_prototype_events=False)
def LoadDatabase(database_dir, use_database_cache)
def GenerateSingleFile(library_path, output_dir, generated_output_dir=None, prefix=None)
def GuessOS()
Definition: utils.py:21
def GetBuildDir(host_os)
Definition: utils.py:99
def CheckedInSdkPath()
Definition: utils.py:499
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741