#!/usr/bin/python3
-#
-# This file is part of the sigrok project.
-#
-# Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
+##
+## This file is part of the sigrok-util project.
+##
+## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
import os
import sys
import tempfile
-from subprocess import Popen, PIPE
+from subprocess import Popen, PIPE, check_output
import shutil
import re
import socket
import datetime
-TMPL_AUTOCONF_AC_ARG_ENABLE = """AC_ARG_ENABLE(${short}, AC_HELP_STRING([--enable-${short}],
- [enable ${name} driver support [default=yes]]),
- [HW_${upper}="$enableval"],
- [HW_${upper}=yes])
-AM_CONDITIONAL(HW_${upper}, test x$HW_${upper} = xyes)
-if test "x$HW_${upper}" = "xyes"; then
- AC_DEFINE(HAVE_HW_${upper}, 1, [${name} driver support])
-fi
+TMPL_AUTOCONF_DRIVER = "SR_DRIVER([${name}], [${short}])\n"
+TMPL_HWMAKE_DRIVERLIB = """if HW_${upper}
+libsigrok_la_SOURCES += \\
+ src/hardware/${short}/protocol.h \\
+ src/hardware/${short}/protocol.c \\
+ src/hardware/${short}/api.c
+endif
+"""
+TMPL_DRIVERS_EXTERN = """\
+#ifdef HAVE_HW_${upper}
+extern SR_PRIV struct sr_dev_driver ${lib}_driver_info;
+#endif
+"""
+TMPL_DRIVERS_POINTER = """\
+#ifdef HAVE_HW_${upper}
+ (DRVS) {&${lib}_driver_info, NULL},
+#endif
"""
-TMPL_AUTOCONF_AC_CONFIG_FILES = "\t\t hardware/${short}/Makefile\n"
-TMPL_AUTOCONF_SUMMARY = 'echo " - ${summary}"\n'
-TMPL_HWMAKE_SUBDIR = '\t${short}'
-FILE_DRV_MAKEFILE = 'drv-Makefile.am'
FILE_DRV_API = 'drv-api.c'
FILE_DRV_PROTOCOL = 'drv-protocol.c'
FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
tmp = tempfile.mkdtemp()
try:
os.chdir(tmp)
- out, err = Popen("git clone " + LIBSR, shell=True, stderr=PIPE).communicate()
- if err:
+ process = Popen("git clone --depth=1 " + LIBSR, shell=True, stderr=PIPE)
+ out, err = process.communicate()
+ if process.returncode:
raise Exception(err.decode())
gitdir = tmp + '/libsigrok/'
- do_configure_ac(gitdir)
- do_hwmake(gitdir)
+ do_autoconf(gitdir)
+ do_drivers(gitdir)
+ do_automake(gitdir)
do_driverskel(gitdir)
make_patch(gitdir)
except Exception as e:
shutil.rmtree(tmp)
-def do_configure_ac(gitdir):
+# add DRIVER and DRIVER2 entries to configure.ac
+def do_autoconf(gitdir):
cacpath = gitdir + 'configure.ac'
configure_ac = open(cacpath).read()
- # add AC_ARG_ENABLE option
out = ''
- state = 'copy'
+ state = 'driver'
+ active = False
for line in configure_ac.split('\n')[:-1]:
- if state == 'copy':
- if line == "# Hardware support '--enable' options.":
- state = 'acarg'
- elif state == 'acarg':
- m = re.match('AC_ARG_ENABLE\(([^,]+)', line)
+ if state == 'driver':
+ m = re.match(r'SR_DRIVER\(\[([^\]]+)', line)
if m:
- drv_short = m.group(1)
- if drv_short.lower() > names['short']:
- out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
+ active = True
+ if active:
+ if (m and m.group(1).upper() > names['name'].upper()) or m is None:
+ out += tmpl(TMPL_AUTOCONF_DRIVER)
state = 'done'
- if line == '# Checks for libraries.':
- # new one at the end
- out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
- state = 'done'
+ active = False
out += line + '\n'
if state != 'done':
- raise Exception('AC_ARG_ENABLE markers not found in configure.ac')
- configure_ac = out
+ raise Exception('No SR_DRIVER entries found in configure.ac')
+ open(cacpath, 'w').write(out)
- # add driver Makefile to AC_CONFIG_FILES
- out = ''
- state = 'copy'
- for line in configure_ac.split('\n')[:-1]:
- if state == 'copy':
- if line.find("AC_CONFIG_FILES([Makefile") > -1:
- state = 'acconf'
- elif state == 'acconf':
- m = re.match('\t\t hardware/([^/]+)/Makefile', line)
- if m:
- drv_short = m.group(1)
- if drv_short.lower() > names['short']:
- out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
- state = 'done'
- else:
- # new one at the end
- out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
- state = 'done'
- out += line + '\n'
- if state != 'done':
- raise Exception('AC_CONFIG_FILES marker not found in configure.ac')
- configure_ac = out
- # add summary line
+# add HAVE_HW_ extern and pointers to drivers.c
+def do_drivers(gitdir):
+ path = gitdir + 'src/drivers.c'
+ source = open(path).read()
out = ''
- state = 'copy'
- names['summary'] = "%s%s $HW_%s" % (names['name'],
- '.' * (32 - len(names['name'])), names['upper'])
- for line in configure_ac.split('\n')[:-1]:
- if state == 'copy':
- if line.find('Enabled hardware drivers') > -1:
- state = 'echo'
- elif state == 'echo':
- m = re.match('echo " - ([^\.]+)', line)
- if m:
- drv_short = m.group(1)
- if drv_short.lower() > names['name'].lower():
- out += tmpl(TMPL_AUTOCONF_SUMMARY)
- state = 'done'
- else:
- # new one at the end
- out += tmpl(TMPL_AUTOCONF_SUMMARY)
- state = 'done'
+ iflevel = 0
+ state = 'extern'
+ first_entry = ''
+ for line in source.split('\n')[:-1]:
+ m = re.match(r'\s*#\s*if(?:def)?\s+HAVE_HW_(\w+)', line)
+ if m:
+ if iflevel == 0:
+ if not first_entry:
+ first_entry = m.group(1)
+ elif m.group(1) == first_entry:
+ # second time we see this, so we're past the externs
+ if state != 'idle':
+ # tack driver on to the end of the list
+ out += tmpl(TMPL_DRIVERS_EXTERN)
+ state = 'pointer'
+ if state == 'extern':
+ if m.group(1) > names['upper']:
+ out += tmpl(TMPL_DRIVERS_EXTERN)
+ state = 'idle'
+ elif state == 'pointer':
+ if m.group(1) > names['upper']:
+ out += tmpl(TMPL_DRIVERS_POINTER)
+ state = 'done'
+ iflevel += 1
+ elif re.match(r'\s*#\s*endif\b', line):
+ iflevel -= 1
+ elif iflevel == 0 and state == 'pointer':
+ # we passed the last entry
+ out += tmpl(TMPL_DRIVERS_POINTER)
+ state = 'done'
out += line + '\n'
if state != 'done':
- raise Exception('summary marker not found in configure.ac')
- configure_ac = out
+ raise Exception('No "HAVE_HW_*" markers found in drivers.c' + state)
+ open(path, 'w').write(out)
- open(cacpath, 'w').write(configure_ac)
-
-def do_hwmake(gitdir):
- path = gitdir + 'hardware/Makefile.am'
+# add HW_ entry to Makefile.am
+def do_automake(gitdir):
+ path = gitdir + 'Makefile.am'
hwmake = open(path).read()
- # add AC_ARG_ENABLE option
out = ''
state = 'copy'
for line in hwmake.split('\n')[:-1]:
- if state == 'copy':
- if line.find('SUBDIRS =') > -1:
- state = 'subdirs'
- elif state == 'subdirs':
- m = re.match('\t([^ \\\]+\s*\\\)', line)
+ if state == 'copy' and re.match(r'if\s+HW_\w+$', line):
+ state = 'drivers'
+ if state == 'drivers':
+ m = re.match(r'if\s+HW_(\w+)$', line)
if m:
drv_short = m.group(1)
- if drv_short.lower() > names['short']:
- out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
+ if drv_short > names['upper']:
+ out += tmpl(TMPL_HWMAKE_DRIVERLIB)
state = 'done'
- else:
- out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
+ elif not re.match(r'\s*libsigrok_la_SOURCES\b|\s*src/hardware/|endif\b', line):
+ print("[%s]" % line.strip())
+ # we passed the last entry
+ out += tmpl(TMPL_HWMAKE_DRIVERLIB)
state = 'done'
out += line + '\n'
if state != 'done':
- raise Exception('SUBDIRS markers not found in hardware/Makefile.am')
- hwmake = out
-
- open(path, 'w').write(hwmake)
+ raise Exception('No "if HW_" markers found in Makefile.am')
+ open(path, 'w').write(out)
def do_driverskel(gitdir):
- drvdir = gitdir + 'hardware/' + names['short']
+ drvdir = gitdir + 'src/hardware/' + names['short']
os.mkdir(drvdir)
- open(drvdir + '/Makefile.am', 'w').write(tmpl_file(FILE_DRV_MAKEFILE))
open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
def make_patch(gitdir):
os.chdir(gitdir)
- command('git add hardware/' + names['short'])
- cmd = 'git commit -m "%s: initial driver skeleton" ' % names['short']
- cmd += 'configure.ac hardware/Makefile.am hardware/' + names['short']
+ command('git add src/hardware/' + names['short'])
+ cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short']
+ cmd += 'configure.ac Makefile.am src/drivers.c src/hardware/' + names['short']
command(cmd)
cmd = "git format-patch HEAD~1"
out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
def parse_gitconfig():
- try:
- author = email = None
- for line in open(os.environ['HOME'] + '/.gitconfig').readlines():
- m = re.match('\s*(\w+)\s*=\s*(.*)\s*$', line)
- if m:
- key, value = m.groups()
- if key == 'name':
- author = value
- elif key == 'email':
- email = value
- if author and email:
- break
- except:
- pass
- if not author or not email:
- print("Please put your name and email in ~/.gitconfig")
- sys.exit()
-
- return author, email
+ author = email = None
+ try:
+ author = check_output(["git", "config", "user.name"]).decode().strip();
+ email = check_output(["git", "config", "user.email"]).decode().strip();
+ except:
+ print("Please set your name and email in your git config")
+ sys.exit()
+ return author, email
#
# main
#
scriptdir = os.getcwd()
-if socket.gethostname() == 'sigrok':
- LIBSR = '/data/git/libsigrok'
- TMPLDIR = '/data/tools/tmpl'
-else:
- LIBSR = 'git://sigrok.org/libsigrok'
- TMPLDIR = scriptdir
+if scriptdir.split('/')[-2:] != ['sigrok-util', 'source']:
+ print("Please call this script from the 'source' directory.")
+ sys.exit(1)
+
+LIBSR = 'git://sigrok.org/libsigrok'
+TMPLDIR = scriptdir
if len(sys.argv) < 2:
- print("Usage: new-driver.py <name>")
+ print("Usage: new-driver <name>")
sys.exit()
author, email = parse_gitconfig()
'short': re.sub('[^a-z0-9]', '-', name.lower()),
'lib': re.sub('[^a-z0-9]', '_', name.lower()),
'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
- 'libupper': re.sub('[^A-Z0-9]', '', name.upper()),
- 'year': datetime.datetime.now().year,
- 'author': author,
- 'email': email,
+ 'year': datetime.datetime.now().year,
+ 'author': author,
+ 'email': email,
}
new_driver()
-