Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_compiler.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (C) 2013 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29"""Compile an .idl file to Blink C++ bindings (.h and .cpp files) for Dart:HTML.
30
31Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
32"""
33
34import abc
35from optparse import OptionParser
36import os
37import pickle
38
39from idl_reader import IdlReader
40from utilities import write_file
41
42# TODO(terry): Temporary whitelist of IDL files to skip code generating. e.g.,
43# adding 'Animation.idl' to this list will skip that IDL file.
44SKIP_IDL_FILES = ['']
45
46
48 parser = OptionParser()
49 parser.add_option(
50 '--idl-attributes-file',
51 help="location of bindings/IDLExtendedAttributes.txt")
52 parser.add_option('--output-directory')
53 parser.add_option('--interfaces-info-file')
54 parser.add_option('--write-file-only-if-changed', type='int')
55 # ensure output comes last, so command line easy to parse via regexes
56 parser.disable_interspersed_args()
57
58 options, args = parser.parse_args()
59 if options.output_directory is None:
60 parser.error('Must specify output directory using --output-directory.')
61 options.write_file_only_if_changed = bool(
62 options.write_file_only_if_changed)
63 if len(args) != 1:
64 parser.error(
65 'Must specify exactly 1 input file as argument, but %d given.' %
66 len(args))
67 idl_filename = os.path.realpath(args[0])
68 return options, idl_filename
69
70
72 basename = os.path.basename(idl_filename)
73 interface_name, _ = os.path.splitext(basename)
74 return interface_name
75
76
77class IdlCompiler(object):
78 """Abstract Base Class for IDL compilers.
79
80 In concrete classes:
81 * self.code_generator must be set, implementing generate_code()
82 (returning a list of output code), and
83 * compile_file() must be implemented (handling output filenames).
84 """
85 __metaclass__ = abc.ABCMeta
86
87 def __init__(self,
88 output_directory,
89 code_generator=None,
90 interfaces_info=None,
91 interfaces_info_filename='',
92 only_if_changed=False):
93 """
94 Args:
95 interfaces_info:
96 interfaces_info dict
97 (avoids auxiliary file in run-bindings-tests)
98 interfaces_info_file: filename of pickled interfaces_info
99 """
100 self.code_generator = code_generator
101 if interfaces_info_filename:
102 with open(interfaces_info_filename) as interfaces_info_file:
103 interfaces_info = pickle.load(interfaces_info_file)
104 self.interfaces_info = interfaces_info
105
106 self.only_if_changed = only_if_changed
107 self.output_directory = output_directory
108 self.reader = IdlReader(interfaces_info, output_directory, True)
109
110 def compile_and_write(self, idl_filename, output_filenames):
111 # Only compile the IDL file and return the AST.
112 definitions = self.reader.read_idl_definitions(idl_filename)
113 return definitions
114
115 def generate_global_and_write(self, output_filenames):
116 pass
117
118 @abc.abstractmethod
119 def compile_file(self, idl_filename):
120 pass
__init__(self, output_directory, code_generator=None, interfaces_info=None, interfaces_info_filename='', only_if_changed=False)
generate_global_and_write(self, output_filenames)
compile_and_write(self, idl_filename, output_filenames)
compile_file(self, idl_filename)
idl_filename_to_interface_name(idl_filename)