Flutter Engine
The Flutter Engine
compiler.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (C) 2014 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 Dart bindings (.h and .cpp files).
30
31Design doc: ??????
32"""
33
34from optparse import OptionParser
35import os
36import sys
37
38dart_script_path = os.path.dirname(os.path.abspath(__file__))
39script_path = os.path.join(
40 os.path.dirname(os.path.dirname(dart_script_path)), 'scripts')
41sys.path.extend([script_path])
42
43from dart_compiler import IdlCompiler
44from code_generator_dart import CodeGeneratorDart
45
46
48 parser = OptionParser()
49 parser.add_option('--output-directory')
50 parser.add_option('--interfaces-info-file')
51 parser.add_option('--write-file-only-if-changed', type='int', default='1')
52 parser.add_option('--generate-global', type='int')
53
54 # ensure output comes last, so command line easy to parse via regexes
55 parser.disable_interspersed_args()
56
57 options, args = parser.parse_args()
58 if options.output_directory is None:
59 parser.error('Must specify output directory using --output-directory.')
60 options.write_file_only_if_changed = bool(
61 options.write_file_only_if_changed)
62 options.generate_global = bool(options.generate_global)
63 if len(args) != 1:
64 # parser.error('Must specify exactly 1 input file as argument, but %d given.' % len(args))
65 return options, None
66 idl_filename = os.path.realpath(args[0])
67 return options, idl_filename
68
69
71 basename = os.path.basename(idl_filename)
72 interface_name, _ = os.path.splitext(basename)
73 return interface_name
74
75
77
78 def __init__(self, *args, **kwargs):
79 IdlCompiler.__init__(self, *args, **kwargs)
80
81 interfaces_info = self.interfaces_info
83
86
87 def compile_file(self, idl_filename):
88 interface_name = idl_filename_to_interface_name(idl_filename)
89 header_filename = os.path.join(self.output_directoryoutput_directory,
90 'Dart%s.h' % interface_name)
91 cpp_filename = os.path.join(self.output_directoryoutput_directory,
92 'Dart%s.cpp' % interface_name)
93 return self.compile_and_write(idl_filename,
94 (header_filename, cpp_filename))
95
96 def generate_global(self):
97 global_header_filename = os.path.join(self.output_directoryoutput_directory,
98 'DartWebkitClassIds.h')
99 global_cpp_filename = os.path.join(self.output_directoryoutput_directory,
100 'DartWebkitClassIds.cpp')
101 self.generate_global_and_write((global_header_filename,
102 global_cpp_filename))
103
104
105def main():
106 options, idl_filename = parse_options()
107
108 if options.generate_global:
109 idl_compiler = IdlCompilerDart(
110 options.output_directory,
111 interfaces_info_filename=options.interfaces_info_file,
112 only_if_changed=options.write_file_only_if_changed)
113 idl_compiler.generate_global()
114 else:
115 idl_compiler = IdlCompilerDart(
116 options.output_directory,
117 interfaces_info_filename=options.interfaces_info_file,
118 only_if_changed=options.write_file_only_if_changed)
119 idl_compiler.compile_file(idl_filename)
120
121
122if __name__ == '__main__':
123 sys.exit(main())
def compile_file(self, idl_filename)
Definition: compiler.py:87
def __init__(self, *args, **kwargs)
Definition: compiler.py:78
def generate_global(self)
Definition: compiler.py:96
def compile_and_write(self, idl_filename, output_filenames)
def generate_global_and_write(self, output_filenames)
def main()
Definition: compiler.py:105
def parse_options()
Definition: compiler.py:47
def idl_filename_to_interface_name(idl_filename)
Definition: compiler.py:70
Definition: main.py:1