OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 14 matching lines...) Expand all Loading... |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 |
29 import os.path | 29 import os.path |
30 import shlex | 30 import shlex |
31 import shutil | 31 import shutil |
32 import optparse | 32 import optparse |
33 | 33 |
34 from in_file import InFile | 34 from in_file import InFile |
35 import template_expander | |
36 | 35 |
37 | 36 |
38 class Writer(object): | 37 class Writer(object): |
39 # Subclasses should override. | 38 # Subclasses should override. |
40 class_name = None | 39 class_name = None |
41 defaults = None | 40 defaults = None |
42 valid_values = None | 41 valid_values = None |
43 default_parameters = None | 42 default_parameters = None |
44 | 43 |
45 def __init__(self, in_files, enabled_conditions): | 44 def __init__(self, in_files, enabled_conditions): |
46 if isinstance(in_files, basestring): | 45 if isinstance(in_files, basestring): |
47 in_files = [in_files] | 46 in_files = [in_files] |
48 self.in_file = InFile.load_from_files(in_files, self.defaults, self.vali
d_values, self.default_parameters) | 47 self.in_file = InFile.load_from_files(in_files, self.defaults, self.vali
d_values, self.default_parameters) |
49 self._enabled_conditions = enabled_conditions | 48 self._enabled_conditions = enabled_conditions |
50 | 49 self._outputs = {} # file_name -> generator |
51 # Subclasses should override. | |
52 def generate_header(self): | |
53 return '' | |
54 | |
55 # Subclasses should override. | |
56 def generate_interfaces_header(self): | |
57 return '' | |
58 | |
59 # Subclasses should override. | |
60 def generate_headers_header(self): | |
61 return '' | |
62 | |
63 # Subclasses should override. | |
64 def generate_implementation(self): | |
65 return '' | |
66 | |
67 # Subclasses should override. | |
68 def generate_idl(self): | |
69 return '' | |
70 | 50 |
71 def wrap_with_condition(self, string, condition): | 51 def wrap_with_condition(self, string, condition): |
72 if not condition: | 52 if not condition: |
73 return string | 53 return string |
74 return "#if ENABLE(%(condition)s)\n%(string)s\n#endif" % { 'condition' :
condition, 'string' : string } | 54 return "#if ENABLE(%(condition)s)\n%(string)s\n#endif" % { 'condition' :
condition, 'string' : string } |
75 | 55 |
76 def _forcibly_create_text_file_at_path_with_contents(self, file_path, conten
ts): | 56 def _forcibly_create_text_file_at_path_with_contents(self, file_path, conten
ts): |
77 # FIXME: This method can be made less force-full anytime after 6/1/2013. | 57 # FIXME: This method can be made less force-full anytime after 6/1/2013. |
78 # A gyp error was briefly checked into the tree, causing | 58 # A gyp error was briefly checked into the tree, causing |
79 # a directory to have been generated in place of one of | 59 # a directory to have been generated in place of one of |
80 # our output files. Clean up after that error so that | 60 # our output files. Clean up after that error so that |
81 # all users don't need to clobber their output directories. | 61 # all users don't need to clobber their output directories. |
82 shutil.rmtree(file_path, ignore_errors=True) | 62 shutil.rmtree(file_path, ignore_errors=True) |
83 # The build system should ensure our output directory exists, but just i
n case. | 63 # The build system should ensure our output directory exists, but just i
n case. |
84 directory = os.path.dirname(file_path) | 64 directory = os.path.dirname(file_path) |
85 if not os.path.exists(directory): | 65 if not os.path.exists(directory): |
86 os.makedirs(directory) | 66 os.makedirs(directory) |
87 | 67 |
88 with open(file_path, "w") as file_to_write: | 68 with open(file_path, "w") as file_to_write: |
89 file_to_write.write(contents) | 69 file_to_write.write(contents) |
90 | 70 |
91 def _write_file(self, output_dir, generator, file_name): | 71 def _write_file(self, output_dir, contents, file_name): |
92 contents = generator() | |
93 if type(contents) is dict: | |
94 contents = template_expander.apply_template(file_name + ".tmpl", con
tents) | |
95 if not contents: | |
96 return | |
97 path = os.path.join(output_dir, file_name) | 72 path = os.path.join(output_dir, file_name) |
98 self._forcibly_create_text_file_at_path_with_contents(path, contents) | 73 self._forcibly_create_text_file_at_path_with_contents(path, contents) |
99 | 74 |
100 def write_header(self, output_dir): | 75 def write_files(self, output_dir): |
101 self._write_file(output_dir, self.generate_header, self.class_name + '.h
') | 76 for file_name, generator in self._outputs.items(): |
102 | 77 self._write_file(output_dir, generator(), file_name) |
103 def write_headers_header(self, output_dir): | |
104 self._write_file(output_dir, self.generate_headers_header, self.class_na
me + 'Headers.h') | |
105 | |
106 def write_interfaces_header(self, output_dir): | |
107 self._write_file(output_dir, self.generate_interfaces_header, self.class
_name + 'Interfaces.h') | |
108 | |
109 def write_implmentation(self, output_dir): | |
110 self._write_file(output_dir, self.generate_implementation, self.class_na
me + '.cpp') | |
111 | |
112 def write_idl(self, output_dir): | |
113 self._write_file(output_dir, self.generate_idl, self.class_name + '.idl'
) | |
114 | 78 |
115 | 79 |
116 class Maker(object): | 80 class Maker(object): |
117 def __init__(self, writer_class): | 81 def __init__(self, writer_class): |
118 self._writer_class = writer_class | 82 self._writer_class = writer_class |
119 | 83 |
120 def _enabled_conditions_from_defines(self, defines_arg_string): | 84 def _enabled_conditions_from_defines(self, defines_arg_string): |
121 if not defines_arg_string: | 85 if not defines_arg_string: |
122 return [] | 86 return [] |
123 | 87 |
(...skipping 21 matching lines...) Expand all Loading... |
145 exit(1) | 109 exit(1) |
146 | 110 |
147 parser = optparse.OptionParser() | 111 parser = optparse.OptionParser() |
148 parser.add_option("--defines") | 112 parser.add_option("--defines") |
149 parser.add_option("--output_dir", default=os.getcwd()) | 113 parser.add_option("--output_dir", default=os.getcwd()) |
150 (options, args) = parser.parse_args() | 114 (options, args) = parser.parse_args() |
151 | 115 |
152 enabled_conditions = self._enabled_conditions_from_defines(options.defin
es) | 116 enabled_conditions = self._enabled_conditions_from_defines(options.defin
es) |
153 | 117 |
154 writer = self._writer_class(args, enabled_conditions) | 118 writer = self._writer_class(args, enabled_conditions) |
155 writer.write_header(options.output_dir) | 119 writer.write_files(options.output_dir) |
156 writer.write_headers_header(options.output_dir) | |
157 writer.write_interfaces_header(options.output_dir) | |
158 writer.write_implmentation(options.output_dir) | |
159 writer.write_idl(options.output_dir) | |
OLD | NEW |