Revert "swr/rast: Archrast codegen updates"
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / gen_archrast.py
1 # Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
12 # Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE.
21
22 # Python source
23 from __future__ import print_function
24 import os
25 import sys
26 import re
27 from gen_common import *
28
29 def parse_event_fields(lines, idx, event_dict):
30 field_names = []
31 field_types = []
32 end_of_event = False
33
34 num_fields = 0
35
36 # record all fields in event definition.
37 # note: we don't check if there's a leading brace.
38 while not end_of_event and idx < len(lines):
39 line = lines[idx].rstrip()
40 idx += 1
41
42 field = re.match(r'(\s*)(\w+)(\s*)(\w+)', line)
43
44 if field:
45 field_types.append(field.group(2))
46 field_names.append(field.group(4))
47 num_fields += 1
48
49 end_of_event = re.match(r'(\s*)};', line)
50
51 event_dict['field_types'] = field_types
52 event_dict['field_names'] = field_names
53 event_dict['num_fields'] = num_fields
54
55 return idx
56
57 def parse_enums(lines, idx, event_dict):
58 enum_names = []
59 end_of_enum = False
60
61 # record all enum values in enumeration
62 # note: we don't check if there's a leading brace.
63 while not end_of_enum and idx < len(lines):
64 line = lines[idx].rstrip()
65 idx += 1
66
67 preprocessor = re.search(r'#if|#endif', line)
68
69 if not preprocessor:
70 enum = re.match(r'(\s*)(\w+)(\s*)', line)
71
72 if enum:
73 enum_names.append(line)
74
75 end_of_enum = re.match(r'(\s*)};', line)
76
77 event_dict['names'] = enum_names
78 return idx
79
80 def parse_protos(protos, filename):
81
82 with open(filename, 'r') as f:
83 lines=f.readlines()
84
85 idx = 0
86
87 eventId = 0
88 raw_text = []
89 while idx < len(lines):
90 line = lines[idx].rstrip()
91 idx += 1
92
93 # search for event definitions.
94 match = re.match(r'(\s*)event(\s*)(\w+)', line)
95
96 if match:
97 eventId += 1
98 event_name = match.group(3)
99 protos['event_names'].append(event_name)
100
101 protos['events'][event_name] = {}
102 protos['events'][event_name]['event_id'] = eventId
103 idx = parse_event_fields(lines, idx, protos['events'][event_name])
104
105 # search for enums.
106 match = re.match(r'(\s*)enum(\s*)(\w+)', line)
107
108 if match:
109 enum_name = match.group(3)
110 protos['enum_names'].append(enum_name)
111
112 protos['enums'][enum_name] = {}
113 idx = parse_enums(lines, idx, protos['enums'][enum_name])
114
115 def main():
116
117 # Parse args...
118 parser = ArgumentParser()
119 parser.add_argument('--proto', '-p', help='Path to proto file', required=True)
120 parser.add_argument('--proto_private', '-pp', help='Path to private proto file', required=True)
121 parser.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required=True)
122 parser.add_argument('--gen_event_hpp', help='Generate event header', action='store_true', default=False)
123 parser.add_argument('--gen_event_cpp', help='Generate event cpp', action='store_true', default=False)
124 parser.add_argument('--gen_eventhandler_hpp', help='Generate eventhandler header', action='store_true', default=False)
125 parser.add_argument('--gen_eventhandlerfile_hpp', help='Generate eventhandler header for writing to files', action='store_true', default=False)
126 args = parser.parse_args()
127
128 proto_filename = args.proto
129 proto_private_filename = args.proto_private
130
131 (output_dir, output_filename) = os.path.split(args.output)
132
133 if not output_dir:
134 output_dir = '.'
135
136 #print('output_dir = %s' % output_dir, file=sys.stderr)
137 #print('output_filename = %s' % output_filename, file=sys.stderr)
138
139 if not os.path.exists(proto_filename):
140 print('Error: Could not find proto file %s' % proto_filename, file=sys.stderr)
141 return 1
142
143 if not os.path.exists(proto_private_filename):
144 print('Error: Could not find private proto file %s' % proto_private_filename, file=sys.stderr)
145 return 1
146
147 final_output_dir = output_dir
148 MakeDir(final_output_dir)
149 output_dir = MakeTmpDir('_codegen')
150
151 protos = {}
152 protos['events'] = {} # event dictionary containing events with their fields
153 protos['event_names'] = [] # needed to keep events in order parsed. dict is not ordered.
154 protos['enums'] = {}
155 protos['enum_names'] = []
156
157 parse_protos(protos, proto_filename)
158 parse_protos(protos, proto_private_filename)
159
160 rval = 0
161
162 try:
163 # Generate event header
164 if args.gen_event_hpp:
165 curdir = os.path.dirname(os.path.abspath(__file__))
166 template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp'])
167 output_fullpath = os.sep.join([output_dir, output_filename])
168
169 MakoTemplateWriter.to_file(template_file, output_fullpath,
170 cmdline=sys.argv,
171 filename=output_filename,
172 protos=protos)
173
174 # Generate event implementation
175 if args.gen_event_cpp:
176 curdir = os.path.dirname(os.path.abspath(__file__))
177 template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp'])
178 output_fullpath = os.sep.join([output_dir, output_filename])
179
180 MakoTemplateWriter.to_file(template_file, output_fullpath,
181 cmdline=sys.argv,
182 filename=output_filename,
183 protos=protos)
184
185 # Generate event handler header
186 if args.gen_eventhandler_hpp:
187 curdir = os.path.dirname(os.path.abspath(__file__))
188 template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandler.hpp'])
189 output_fullpath = os.sep.join([output_dir, output_filename])
190
191 MakoTemplateWriter.to_file(template_file, output_fullpath,
192 cmdline=sys.argv,
193 filename=output_filename,
194 event_header='gen_ar_event.hpp',
195 protos=protos)
196
197 # Generate event handler header
198 if args.gen_eventhandlerfile_hpp:
199 curdir = os.path.dirname(os.path.abspath(__file__))
200 template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandlerfile.hpp'])
201 output_fullpath = os.sep.join([output_dir, output_filename])
202
203 MakoTemplateWriter.to_file(template_file, output_fullpath,
204 cmdline=sys.argv,
205 filename=output_filename,
206 event_header='gen_ar_eventhandler.hpp',
207 protos=protos)
208
209 rval = CopyDirFilesIfDifferent(output_dir, final_output_dir)
210
211 except:
212 rval = 1
213
214 finally:
215 DeleteDirTree(output_dir)
216
217 return rval
218
219 if __name__ == '__main__':
220 sys.exit(main())
221