]> sigrok.org Git - sigrok-util.git/blob - source/new-driver
sysclk-lwla: Add custom protocol dissector for use with Wireshark.
[sigrok-util.git] / source / new-driver
1 #!/usr/bin/python3
2 ##
3 ## This file is part of the sigrok-util project.
4 ##
5 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 ##
7 ## This program is free software: you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation, either version 3 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ## GNU General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 ##
20
21 import os
22 import sys
23 import tempfile
24 from subprocess import Popen, PIPE
25 import shutil
26 import re
27 import socket
28 import datetime
29
30 TMPL_AUTOCONF_AC_ARG_ENABLE = """\
31 AC_ARG_ENABLE(${short}, AC_HELP_STRING([--enable-${short}],
32         [enable ${name} support [default=yes]]),
33         [HW_${upper}="$enableval"],
34         [HW_${upper}=$HW_ENABLED_DEFAULT])
35 AM_CONDITIONAL(HW_${upper}, test x$HW_${upper} = xyes)
36 if test "x$HW_${upper}" = "xyes"; then
37         AC_DEFINE(HAVE_HW_${upper}, 1, [${name} support])
38 fi
39
40 """
41 TMPL_AUTOCONF_AC_CONFIG_FILES = "\t\t hardware/${short}/Makefile\n"
42 TMPL_AUTOCONF_SUMMARY = 'echo "  - ${summary}"\n'
43 TMPL_HWMAKE_SUBDIR = '\t${short}'
44 TMPL_HWMAKE_DRIVERLIB = """if HW_${upper}
45 libsigrokhardware_la_LIBADD += ${short}/libsigrok_hw_${lib}.la
46 endif
47
48 """
49 TMPL_HWDRIVER_EXTERN = """\
50 #ifdef HAVE_HW_${upper}
51 extern SR_PRIV struct sr_dev_driver ${lib}_driver_info;
52 #endif
53 """
54 TMPL_HWDRIVER_DIPTR = """\
55 #ifdef HAVE_HW_${upper}
56         &${lib}_driver_info,
57 #endif
58 """
59 FILE_DRV_MAKEFILE = 'drv-Makefile.am'
60 FILE_DRV_API = 'drv-api.c'
61 FILE_DRV_PROTOCOL = 'drv-protocol.c'
62 FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
63
64 def tmpl(template):
65     out = re.sub(r'\${([^}]+)}', lambda x: str(names[x.group(1)]), template)
66
67     return out
68
69
70 def tmpl_file(filename):
71     template = open(TMPLDIR + '/' + filename).read()
72
73     return tmpl(template)
74
75
76 def new_driver():
77     tmp = tempfile.mkdtemp()
78     try:
79         os.chdir(tmp)
80         process = Popen("git clone " + LIBSR, shell=True, stderr=PIPE)
81         out, err = process.communicate()
82         if process.returncode:
83             raise Exception(err.decode())
84         gitdir = tmp + '/libsigrok/'
85         do_configure_ac(gitdir)
86         do_hwdriver(gitdir)
87         do_hwmake(gitdir)
88         do_driverskel(gitdir)
89         make_patch(gitdir)
90     except Exception as e:
91         print(e)
92     shutil.rmtree(tmp)
93
94
95 def do_configure_ac(gitdir):
96     cacpath = gitdir + 'configure.ac'
97     configure_ac = open(cacpath).read()
98
99     # add AC_ARG_ENABLE option
100     out = ''
101     state = 'copy'
102     for line in configure_ac.split('\n')[:-1]:
103         if state == 'copy':
104             if line == "# Hardware support '--enable' options.":
105                 state = 'acarg'
106         elif state == 'acarg':
107             m = re.match('AC_ARG_ENABLE\(([^,]+)', line)
108             if m:
109                 drv_short = m.group(1)
110                 if drv_short.lower() > names['short']:
111                     out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
112                     state = 'done'
113             if line == '# Checks for libraries.':
114                 # new one at the end
115                 out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
116                 state = 'done'
117         out += line + '\n'
118     if state != 'done':
119         raise Exception('AC_ARG_ENABLE markers not found in configure.ac')
120     configure_ac = out
121
122     # add driver Makefile to AC_CONFIG_FILES
123     out = ''
124     state = 'copy'
125     for line in configure_ac.split('\n')[:-1]:
126         if state == 'copy':
127             if line.find("AC_CONFIG_FILES([Makefile") > -1:
128                 state = 'acconf'
129         elif state == 'acconf':
130             m = re.match('\t\t hardware/([^/]+)/Makefile', line)
131             if m:
132                 drv_short = m.group(1)
133                 if drv_short.lower() > names['short']:
134                     out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
135                     state = 'done'
136             else:
137                 # new one at the end
138                 out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
139                 state = 'done'
140         out += line + '\n'
141     if state != 'done':
142         raise Exception('AC_CONFIG_FILES marker not found in configure.ac')
143     configure_ac = out
144
145     # add summary line
146     out = ''
147     state = 'copy'
148     names['summary'] = "%s%s $HW_%s" % (names['short'],
149             '.' * (32 - len(names['name'])), names['upper'])
150     for line in configure_ac.split('\n')[:-1]:
151         if state == 'copy':
152             if line.find('Enabled hardware drivers') > -1:
153                 state = 'echo'
154         elif state == 'echo':
155             m = re.match('echo "  - ([^\.]+)', line)
156             if m:
157                 drv_short = m.group(1)
158                 if drv_short.lower() > names['name'].lower():
159                     out += tmpl(TMPL_AUTOCONF_SUMMARY)
160                     state = 'done'
161             else:
162                 # new one at the end
163                 out += tmpl(TMPL_AUTOCONF_SUMMARY)
164                 state = 'done'
165         out += line + '\n'
166     if state != 'done':
167         raise Exception('summary marker not found in configure.ac')
168     configure_ac = out
169
170     open(cacpath, 'w').write(configure_ac)
171
172
173 def do_hwdriver(gitdir):
174     path = gitdir + 'hwdriver.c'
175     hwdriver = open(path).read()
176     # add HAVE_HW_thing extern and pointers
177     out = ''
178     state = 'copy'
179     for line in hwdriver.split('\n')[:-1]:
180         if state == 'copy':
181             if line.find('/** @cond PRIVATE */') == 0:
182                 state = 'extern'
183             elif line.find('static struct sr_dev_driver *drivers_list') == 0:
184                 state = 'diptr'
185         elif state in ('extern', 'diptr'):
186             if state == 'extern':
187                 entry = tmpl(TMPL_HWDRIVER_EXTERN)
188                 next_state = 'copy'
189             else:
190                 entry = tmpl(TMPL_HWDRIVER_DIPTR)
191                 next_state = 'done'
192             m = re.match('#ifdef HAVE_.._(.*)', line)
193             if m:
194                 drv = m.group(1)
195                 if drv > names['upper']:
196                     out += entry
197                     state = next_state
198             elif not re.match('(extern|\t&|#endif)', line):
199                 # new one at the end
200                 out += entry
201                 state = next_state
202         out += line + '\n'
203     if state != 'done':
204         raise Exception('HAVE_* markers not found in hwdriver.c')
205     hwdriver = out
206
207     open(path, 'w').write(hwdriver)
208
209
210 def do_hwmake(gitdir):
211     path = gitdir + 'hardware/Makefile.am'
212     hwmake = open(path).read()
213
214     # add SUBDIRS entry
215     out = ''
216     state = 'copy'
217     for line in hwmake.split('\n')[:-1]:
218         if state == 'copy':
219             if line.find('SUBDIRS =') > -1:
220                 state = 'subdirs'
221         elif state == 'subdirs':
222             m = re.match('\t([^ \\\]+\s*\\\)', line)
223             if m:
224                 drv_short = m.group(1)
225                 if drv_short.lower() > names['short']:
226                     out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
227                     state = 'driverlib'
228             else:
229                 out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
230                 state = 'driverlib'
231         elif state == 'driverlib':
232             m = re.match('if [A-Z]{2}_(.*)$', line)
233             if m:
234                 drv_short = m.group(1)
235                 if drv_short > names['upper']:
236                     out += tmpl(TMPL_HWMAKE_DRIVERLIB)
237                     state = 'done'
238         out += line + '\n'
239     if state == 'driverlib':
240         # reached end of file, add it in here
241         out += tmpl(TMPL_HWMAKE_DRIVERLIB).strip()
242     elif state != 'done':
243         raise Exception('markers not found in hardware/Makefile.am')
244     hwmake = out
245
246     open(path, 'w').write(hwmake)
247
248
249 def do_driverskel(gitdir):
250     drvdir = gitdir + 'hardware/' + names['short']
251     os.mkdir(drvdir)
252     open(drvdir + '/Makefile.am', 'w').write(tmpl_file(FILE_DRV_MAKEFILE))
253     open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
254     open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
255     open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
256
257
258 def make_patch(gitdir):
259     os.chdir(gitdir)
260     command('git add hardware/' + names['short'])
261     cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short']
262     cmd += 'configure.ac hwdriver.c hardware/Makefile.am hardware/' + names['short']
263     command(cmd)
264     cmd = "git format-patch HEAD~1"
265     out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
266     if err:
267         raise Exception(err.decode())
268     patch = out.decode().strip()
269     shutil.move(gitdir + '/' + patch, scriptdir + '/' + patch)
270     print(patch)
271
272
273 def command(cmd):
274     out, err = Popen(cmd, shell=True, stderr=PIPE).communicate()
275     if err:
276         raise Exception(err.decode())
277
278
279 def parse_gitconfig():
280     try:
281         author = email = None
282         for line in open(os.environ['HOME'] + '/.gitconfig').readlines():
283             m = re.match('\s*(\w+)\s*=\s*(.*)\s*$', line)
284             if m:
285                 key, value = m.groups()
286                 if key == 'name':
287                     author = value
288                 elif key == 'email':
289                     email = value
290                 if author and email:
291                     break
292     except:
293         pass
294     if not author or not email:
295         print("Please put your name and email in ~/.gitconfig")
296         sys.exit()
297
298     return author, email
299
300 #
301 # main
302 #
303
304 scriptdir = os.getcwd()
305 if scriptdir.split('/')[-2:] != ['sigrok-util', 'source']:
306         print("Please call this script from the 'source' directory.")
307         sys.exit(1)
308
309 LIBSR = 'git://sigrok.org/libsigrok'
310 TMPLDIR = scriptdir
311
312 if len(sys.argv) < 2:
313     print("Usage: new-driver <name>")
314     sys.exit()
315
316 author, email = parse_gitconfig()
317 name = ' '.join(sys.argv[1:])
318 names = {
319     'name': name,
320     'short': re.sub('[^a-z0-9]', '-', name.lower()),
321     'lib': re.sub('[^a-z0-9]', '_', name.lower()),
322     'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
323     'libupper': re.sub('[^A-Z0-9]', '', name.upper()),
324     'year': datetime.datetime.now().year,
325     'author': author,
326     'email': email,
327 }
328 new_driver()
329