1 # Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
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:
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
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
23 from __future__
import print_function
27 from gen_common
import ArgumentParser
, MakoTemplateWriter
29 def parse_event_fields(lines
, idx
, event_dict
):
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()
42 field
= re
.match(r
'(\s*)(\w+)(\s*)(\w+)', line
)
45 field_types
.append(field
.group(2))
46 field_names
.append(field
.group(4))
49 end_of_event
= re
.match(r
'(\s*)};', line
)
51 event_dict
['field_types'] = field_types
52 event_dict
['field_names'] = field_names
53 event_dict
['num_fields'] = num_fields
57 def parse_enums(lines
, idx
, event_dict
):
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()
67 preprocessor
= re
.search(r
'#if|#endif', line
)
70 enum
= re
.match(r
'(\s*)(\w+)(\s*)', line
)
73 enum_names
.append(line
)
75 end_of_enum
= re
.match(r
'(\s*)};', line
)
77 event_dict
['names'] = enum_names
80 def parse_protos(filename
):
83 with
open(filename
, 'r') as f
:
88 protos
['events'] = {} # event dictionary containing events with their fields
89 protos
['event_names'] = [] # needed to keep events in order parsed. dict is not ordered.
91 protos
['enum_names'] = []
95 while idx
< len(lines
):
96 line
= lines
[idx
].rstrip()
99 # search for event definitions.
100 match
= re
.match(r
'(\s*)event(\s*)(\w+)', line
)
104 event_name
= match
.group(3)
105 protos
['event_names'].append(event_name
)
107 protos
['events'][event_name
] = {}
108 protos
['events'][event_name
]['event_id'] = eventId
109 idx
= parse_event_fields(lines
, idx
, protos
['events'][event_name
])
112 match
= re
.match(r
'(\s*)enum(\s*)(\w+)', line
)
115 enum_name
= match
.group(3)
116 protos
['enum_names'].append(enum_name
)
118 protos
['enums'][enum_name
] = {}
119 idx
= parse_enums(lines
, idx
, protos
['enums'][enum_name
])
126 parser
= ArgumentParser()
127 parser
.add_argument('--proto', '-p', help='Path to proto file', required
=True)
128 parser
.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required
=True)
129 parser
.add_argument('--gen_event_hpp', help='Generate event header', action
='store_true', default
=False)
130 parser
.add_argument('--gen_event_cpp', help='Generate event cpp', action
='store_true', default
=False)
131 parser
.add_argument('--gen_eventhandler_hpp', help='Generate eventhandler header', action
='store_true', default
=False)
132 parser
.add_argument('--gen_eventhandlerfile_hpp', help='Generate eventhandler header for writing to files', action
='store_true', default
=False)
133 args
= parser
.parse_args()
135 proto_filename
= args
.proto
137 (output_dir
, output_filename
) = os
.path
.split(args
.output
)
142 #print('output_dir = %s' % output_dir, file=sys.stderr)
143 #print('output_filename = %s' % output_filename, file=sys.stderr)
145 if not os
.path
.exists(proto_filename
):
146 print('Error: Could not find proto file %s' % proto_filename
, file=sys
.stderr
)
149 protos
= parse_protos(proto_filename
)
151 # Generate event header
152 if args
.gen_event_hpp
:
153 curdir
= os
.path
.dirname(os
.path
.abspath(__file__
))
154 template_file
= os
.sep
.join([curdir
, 'templates', 'gen_ar_event.hpp'])
155 output_fullpath
= os
.sep
.join([output_dir
, output_filename
])
157 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
159 filename
=output_filename
,
162 # Generate event implementation
163 if args
.gen_event_cpp
:
164 curdir
= os
.path
.dirname(os
.path
.abspath(__file__
))
165 template_file
= os
.sep
.join([curdir
, 'templates', 'gen_ar_event.cpp'])
166 output_fullpath
= os
.sep
.join([output_dir
, output_filename
])
168 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
170 filename
=output_filename
,
173 # Generate event handler header
174 if args
.gen_eventhandler_hpp
:
175 curdir
= os
.path
.dirname(os
.path
.abspath(__file__
))
176 template_file
= os
.sep
.join([curdir
, 'templates', 'gen_ar_eventhandler.hpp'])
177 output_fullpath
= os
.sep
.join([output_dir
, output_filename
])
179 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
181 filename
=output_filename
,
182 event_header
='gen_ar_event.hpp',
185 # Generate event handler header
186 if args
.gen_eventhandlerfile_hpp
:
187 curdir
= os
.path
.dirname(os
.path
.abspath(__file__
))
188 template_file
= os
.sep
.join([curdir
, 'templates', 'gen_ar_eventhandlerfile.hpp'])
189 output_fullpath
= os
.sep
.join([output_dir
, output_filename
])
191 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
193 filename
=output_filename
,
194 event_header
='gen_ar_eventhandler.hpp',
199 if __name__
== '__main__':