]>
Commit | Line | Data |
---|---|---|
c23c8659 ML |
1 | ## |
2 | ## This file is part of the libsigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li> | |
5 | ## | |
6 | ## This program is free software: you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation, either version 3 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
d2a929ab | 20 | from __future__ import print_function |
3532ed01 | 21 | from xml.etree import ElementTree |
c23c8659 ML |
22 | from collections import OrderedDict |
23 | import sys, os, re | |
24 | ||
3532ed01 | 25 | index_file = sys.argv[1] |
6884b52b | 26 | |
c23c8659 ML |
27 | # Get directory this script is in. |
28 | dirname = os.path.dirname(os.path.realpath(__file__)) | |
29 | ||
bc81463b DE |
30 | outdirname = "bindings" |
31 | if not os.path.exists(os.path.join(outdirname, 'cxx/include/libsigrokcxx')): | |
32 | os.makedirs(os.path.join(outdirname, 'cxx/include/libsigrokcxx')) | |
33 | if not os.path.exists(os.path.join(outdirname, 'swig')): | |
34 | os.makedirs(os.path.join(outdirname, 'swig')) | |
33c84e81 | 35 | |
c23c8659 | 36 | mapping = dict([ |
84c87085 ML |
37 | ('sr_loglevel', ('LogLevel', 'Log verbosity level')), |
38 | ('sr_packettype', ('PacketType', 'Type of datafeed packet')), | |
39 | ('sr_mq', ('Quantity', 'Measured quantity')), | |
40 | ('sr_unit', ('Unit', 'Unit of measurement')), | |
41 | ('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')), | |
42 | ('sr_configkey', ('ConfigKey', 'Configuration key')), | |
43 | ('sr_datatype', ('DataType', 'Configuration data type')), | |
44 | ('sr_channeltype', ('ChannelType', 'Channel type')), | |
3cd4b381 SA |
45 | ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type')), |
46 | ('sr_output_flag', ('OutputFlag', 'Flag applied to output modules'))]) | |
c23c8659 | 47 | |
3532ed01 | 48 | index = ElementTree.parse(index_file) |
c23c8659 ML |
49 | |
50 | # Build mapping between class names and enumerations. | |
3532ed01 ML |
51 | |
52 | classes = OrderedDict() | |
53 | ||
54 | for compound in index.findall('compound'): | |
55 | if compound.attrib['kind'] != 'file': | |
56 | continue | |
57 | filename = os.path.join( | |
58 | os.path.dirname(index_file), | |
59 | '%s.xml' % compound.attrib['refid']) | |
60 | doc = ElementTree.parse(filename) | |
61 | for section in doc.find('compounddef').findall('sectiondef'): | |
62 | if section.attrib["kind"] != 'enum': | |
63 | continue | |
64 | for member in section.findall('memberdef'): | |
65 | if member.attrib["kind"] != 'enum': | |
66 | continue | |
67 | name = member.find('name').text | |
68 | if name in mapping: | |
69 | classes[member] = mapping[name] | |
c23c8659 | 70 | |
bc81463b DE |
71 | header = open(os.path.join(outdirname, 'cxx/include/libsigrokcxx/enums.hpp'), 'w') |
72 | code = open(os.path.join(outdirname, 'cxx/enums.cpp'), 'w') | |
73 | swig = open(os.path.join(outdirname, 'swig/enums.i'), 'w') | |
c23c8659 ML |
74 | |
75 | for file in (header, code): | |
d2a929ab | 76 | print("/* Generated file - edit enums.py instead! */", file=file) |
c23c8659 ML |
77 | |
78 | # Template for beginning of class declaration and public members. | |
79 | header_public_template = """ | |
84c87085 | 80 | /** {brief} */ |
9d229ecb | 81 | class SR_API {classname} : public EnumValue<{classname}, enum {enumname}> |
c23c8659 ML |
82 | {{ |
83 | public: | |
c23c8659 ML |
84 | """ |
85 | ||
86 | # Template for beginning of private members. | |
87 | header_private_template = """ | |
9d229ecb ML |
88 | protected: |
89 | {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}} | |
c23c8659 ML |
90 | """ |
91 | ||
84c87085 ML |
92 | def get_text(node): |
93 | return str.join('\n\n', | |
94 | [p.text.rstrip() for p in node.findall('para')]) | |
95 | ||
96 | for enum, (classname, classbrief) in classes.items(): | |
3532ed01 ML |
97 | |
98 | enum_name = enum.find('name').text | |
84c87085 ML |
99 | members = enum.findall('enumvalue') |
100 | member_names = [m.find('name').text for m in members] | |
3532ed01 | 101 | trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names] |
84c87085 | 102 | briefs = [get_text(m.find('briefdescription')) for m in members] |
c23c8659 ML |
103 | |
104 | # Begin class and public declarations | |
d2a929ab ML |
105 | print(header_public_template.format( |
106 | brief=classbrief, classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
107 | |
108 | # Declare public pointers for each enum value | |
84c87085 ML |
109 | for trimmed_name, brief in zip(trimmed_names, briefs): |
110 | if brief: | |
d2a929ab ML |
111 | print('\t/** %s */' % brief, file=header) |
112 | print('\tstatic const %s * const %s;' % ( | |
113 | classname, trimmed_name), file=header) | |
c23c8659 ML |
114 | |
115 | # Declare additional methods if present | |
116 | filename = os.path.join(dirname, "%s_methods.hpp" % classname) | |
117 | if os.path.exists(filename): | |
d2a929ab | 118 | print(str.join('', open(filename).readlines()), file=header) |
c23c8659 ML |
119 | |
120 | # Begin private declarations | |
d2a929ab ML |
121 | print(header_private_template.format( |
122 | classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
123 | |
124 | # Declare private constants for each enum value | |
3532ed01 | 125 | for trimmed_name in trimmed_names: |
d2a929ab | 126 | print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header) |
c23c8659 ML |
127 | |
128 | # End class declaration | |
d2a929ab | 129 | print('};', file=header) |
c23c8659 | 130 | |
c23c8659 | 131 | # Define private constants for each enum value |
3532ed01 | 132 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
133 | print('const %s %s::_%s = %s(%s, "%s");' % ( |
134 | classname, classname, trimmed_name, classname, name, trimmed_name), | |
135 | file=code) | |
c23c8659 ML |
136 | |
137 | # Define public pointers for each enum value | |
3532ed01 | 138 | for trimmed_name in trimmed_names: |
d2a929ab ML |
139 | print('const %s * const %s::%s = &%s::_%s;' % ( |
140 | classname, classname, trimmed_name, classname, trimmed_name), | |
141 | file=code) | |
c23c8659 ML |
142 | |
143 | # Define map of enum values to constants | |
dc7125bb | 144 | print('template<> const SR_API std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % ( |
d2a929ab | 145 | enum_name, classname, classname, enum_name), file=code) |
3532ed01 | 146 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
147 | print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code) |
148 | print('};', file=code) | |
c23c8659 ML |
149 | |
150 | # Define additional methods if present | |
151 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
152 | if os.path.exists(filename): | |
d2a929ab | 153 | print(str.join('', open(filename).readlines()), file=code) |
fe4096fd | 154 | |
189461b2 | 155 | # Map EnumValue::id() and EnumValue::name() as SWIG attributes. |
d2a929ab ML |
156 | print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig) |
157 | print('%%attributestring(sigrok::%s, std::string, name, name);' % classname, | |
158 | file=swig) | |
189461b2 ML |
159 | |
160 | # Instantiate EnumValue template for SWIG | |
d2a929ab ML |
161 | print('%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % ( |
162 | classname, classname, enum_name), file=swig) | |
189461b2 | 163 | |
7a36ceac | 164 | # Apply any language-specific extras. |
d2a929ab | 165 | print('%%enumextras(%s);' % classname, file=swig) |
7a36ceac | 166 | |
e480df0c ML |
167 | # Declare additional attributes if present |
168 | filename = os.path.join(dirname, "%s_methods.i" % classname) | |
169 | if os.path.exists(filename): | |
d2a929ab | 170 | print(str.join('', open(filename).readlines()), file=swig) |