]>
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')), | |
12f2f640 | 43 | ('sr_configcap', ('Capability', 'Configuration capability')), |
84c87085 ML |
44 | ('sr_datatype', ('DataType', 'Configuration data type')), |
45 | ('sr_channeltype', ('ChannelType', 'Channel type')), | |
3cd4b381 SA |
46 | ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type')), |
47 | ('sr_output_flag', ('OutputFlag', 'Flag applied to output modules'))]) | |
c23c8659 | 48 | |
3532ed01 | 49 | index = ElementTree.parse(index_file) |
c23c8659 ML |
50 | |
51 | # Build mapping between class names and enumerations. | |
3532ed01 ML |
52 | |
53 | classes = OrderedDict() | |
54 | ||
55 | for compound in index.findall('compound'): | |
56 | if compound.attrib['kind'] != 'file': | |
57 | continue | |
58 | filename = os.path.join( | |
59 | os.path.dirname(index_file), | |
60 | '%s.xml' % compound.attrib['refid']) | |
61 | doc = ElementTree.parse(filename) | |
62 | for section in doc.find('compounddef').findall('sectiondef'): | |
63 | if section.attrib["kind"] != 'enum': | |
64 | continue | |
65 | for member in section.findall('memberdef'): | |
66 | if member.attrib["kind"] != 'enum': | |
67 | continue | |
68 | name = member.find('name').text | |
69 | if name in mapping: | |
70 | classes[member] = mapping[name] | |
c23c8659 | 71 | |
bc81463b DE |
72 | header = open(os.path.join(outdirname, 'cxx/include/libsigrokcxx/enums.hpp'), 'w') |
73 | code = open(os.path.join(outdirname, 'cxx/enums.cpp'), 'w') | |
74 | swig = open(os.path.join(outdirname, 'swig/enums.i'), 'w') | |
c23c8659 ML |
75 | |
76 | for file in (header, code): | |
d2a929ab | 77 | print("/* Generated file - edit enums.py instead! */", file=file) |
c23c8659 | 78 | |
ace872d5 ML |
79 | print("namespace sigrok {", file=header) |
80 | ||
c23c8659 ML |
81 | # Template for beginning of class declaration and public members. |
82 | header_public_template = """ | |
35334baa UH |
83 | template<> const SR_API std::map<const enum {enumname}, const {classname} * const> EnumValue<{classname}, enum {enumname}>::_values; |
84 | ||
84c87085 | 85 | /** {brief} */ |
9d229ecb | 86 | class SR_API {classname} : public EnumValue<{classname}, enum {enumname}> |
c23c8659 ML |
87 | {{ |
88 | public: | |
c23c8659 ML |
89 | """ |
90 | ||
91 | # Template for beginning of private members. | |
92 | header_private_template = """ | |
9d229ecb ML |
93 | protected: |
94 | {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}} | |
c23c8659 ML |
95 | """ |
96 | ||
84c87085 ML |
97 | def get_text(node): |
98 | return str.join('\n\n', | |
99 | [p.text.rstrip() for p in node.findall('para')]) | |
100 | ||
101 | for enum, (classname, classbrief) in classes.items(): | |
3532ed01 ML |
102 | |
103 | enum_name = enum.find('name').text | |
84c87085 ML |
104 | members = enum.findall('enumvalue') |
105 | member_names = [m.find('name').text for m in members] | |
3532ed01 | 106 | trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names] |
84c87085 | 107 | briefs = [get_text(m.find('briefdescription')) for m in members] |
c23c8659 ML |
108 | |
109 | # Begin class and public declarations | |
d2a929ab ML |
110 | print(header_public_template.format( |
111 | brief=classbrief, classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
112 | |
113 | # Declare public pointers for each enum value | |
84c87085 ML |
114 | for trimmed_name, brief in zip(trimmed_names, briefs): |
115 | if brief: | |
d2a929ab ML |
116 | print('\t/** %s */' % brief, file=header) |
117 | print('\tstatic const %s * const %s;' % ( | |
118 | classname, trimmed_name), file=header) | |
c23c8659 ML |
119 | |
120 | # Declare additional methods if present | |
121 | filename = os.path.join(dirname, "%s_methods.hpp" % classname) | |
122 | if os.path.exists(filename): | |
d2a929ab | 123 | print(str.join('', open(filename).readlines()), file=header) |
c23c8659 ML |
124 | |
125 | # Begin private declarations | |
d2a929ab ML |
126 | print(header_private_template.format( |
127 | classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
128 | |
129 | # Declare private constants for each enum value | |
3532ed01 | 130 | for trimmed_name in trimmed_names: |
d2a929ab | 131 | print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header) |
c23c8659 ML |
132 | |
133 | # End class declaration | |
d2a929ab | 134 | print('};', file=header) |
c23c8659 | 135 | |
c23c8659 | 136 | # Define private constants for each enum value |
3532ed01 | 137 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
138 | print('const %s %s::_%s = %s(%s, "%s");' % ( |
139 | classname, classname, trimmed_name, classname, name, trimmed_name), | |
140 | file=code) | |
c23c8659 ML |
141 | |
142 | # Define public pointers for each enum value | |
3532ed01 | 143 | for trimmed_name in trimmed_names: |
d2a929ab ML |
144 | print('const %s * const %s::%s = &%s::_%s;' % ( |
145 | classname, classname, trimmed_name, classname, trimmed_name), | |
146 | file=code) | |
c23c8659 ML |
147 | |
148 | # Define map of enum values to constants | |
dc7125bb | 149 | print('template<> const SR_API std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % ( |
d2a929ab | 150 | enum_name, classname, classname, enum_name), file=code) |
3532ed01 | 151 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
152 | print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code) |
153 | print('};', file=code) | |
c23c8659 ML |
154 | |
155 | # Define additional methods if present | |
156 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
157 | if os.path.exists(filename): | |
d2a929ab | 158 | print(str.join('', open(filename).readlines()), file=code) |
fe4096fd | 159 | |
189461b2 | 160 | # Map EnumValue::id() and EnumValue::name() as SWIG attributes. |
d2a929ab ML |
161 | print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig) |
162 | print('%%attributestring(sigrok::%s, std::string, name, name);' % classname, | |
163 | file=swig) | |
189461b2 ML |
164 | |
165 | # Instantiate EnumValue template for SWIG | |
d2a929ab ML |
166 | print('%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % ( |
167 | classname, classname, enum_name), file=swig) | |
189461b2 | 168 | |
7a36ceac | 169 | # Apply any language-specific extras. |
d2a929ab | 170 | print('%%enumextras(%s);' % classname, file=swig) |
7a36ceac | 171 | |
e480df0c ML |
172 | # Declare additional attributes if present |
173 | filename = os.path.join(dirname, "%s_methods.i" % classname) | |
174 | if os.path.exists(filename): | |
d2a929ab | 175 | print(str.join('', open(filename).readlines()), file=swig) |
ace872d5 ML |
176 | |
177 | print("}", file=header) |