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 *
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(protos
, filename
):
82 with
open(filename
, 'r') as f
:
89 while idx
< len(lines
):
90 line
= lines
[idx
].rstrip()
93 # search for event definitions.
94 match
= re
.match(r
'(\s*)event(\s*)(\w+)', line
)
98 event_name
= match
.group(3)
99 protos
['event_names'].append(event_name
)
101 protos
['events'][event_name
] = {}
102 protos
['events'][event_name
]['event_id'] = eventId
103 idx
= parse_event_fields(lines
, idx
, protos
['events'][event_name
])
106 match
= re
.match(r
'(\s*)enum(\s*)(\w+)', line
)
109 enum_name
= match
.group(3)
110 protos
['enum_names'].append(enum_name
)
112 protos
['enums'][enum_name
] = {}
113 idx
= parse_enums(lines
, idx
, protos
['enums'][enum_name
])
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()
128 proto_filename
= args
.proto
129 proto_private_filename
= args
.proto_private
131 (output_dir
, output_filename
) = os
.path
.split(args
.output
)
136 #print('output_dir = %s' % output_dir, file=sys.stderr)
137 #print('output_filename = %s' % output_filename, file=sys.stderr)
139 if not os
.path
.exists(proto_filename
):
140 print('Error: Could not find proto file %s' % proto_filename
, file=sys
.stderr
)
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
)
147 final_output_dir
= output_dir
148 MakeDir(final_output_dir
)
149 output_dir
= MakeTmpDir('_codegen')
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.
155 protos
['enum_names'] = []
157 parse_protos(protos
, proto_filename
)
158 parse_protos(protos
, proto_private_filename
)
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
])
169 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
171 filename
=output_filename
,
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
])
180 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
182 filename
=output_filename
,
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
])
191 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
193 filename
=output_filename
,
194 event_header
='gen_ar_event.hpp',
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
])
203 MakoTemplateWriter
.to_file(template_file
, output_fullpath
,
205 filename
=output_filename
,
206 event_header
='gen_ar_eventhandler.hpp',
209 rval
= CopyDirFilesIfDifferent(output_dir
, final_output_dir
)
215 DeleteDirTree(output_dir
)
219 if __name__
== '__main__':