X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=source%2Fnew-driver;h=6e6977bad8a596774b9a4ce7d7f4dedff41745c4;hb=1fd5630e86bdbe3d476d9f5613fe20fd08112c5d;hp=b591b06be85b99483d35cace51542ed5d9ae2dca;hpb=4b527a01e5fbcf7fa69dcfa7a3ad2422bf9087b6;p=sigrok-util.git diff --git a/source/new-driver b/source/new-driver index b591b06..6e6977b 100755 --- a/source/new-driver +++ b/source/new-driver @@ -1,22 +1,22 @@ #!/usr/bin/python3 -# -# This file is part of the sigrok-util project. -# -# Copyright (C) 2012 Bert Vermeulen -# -# 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 . -# +## +## This file is part of the sigrok-util project. +## +## Copyright (C) 2012 Bert Vermeulen +## +## 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 . +## import os import sys @@ -27,19 +27,35 @@ 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]) +TMPL_AUTOCONF_AC_ARG_ENABLE = """\ +AC_ARG_ENABLE(${short}, AC_HELP_STRING([--enable-${short}], + [enable ${name} support [default=yes]]), + [HW_${upper}="$enableval"], + [HW_${upper}=$HW_ENABLED_DEFAULT]) 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]) + AC_DEFINE(HAVE_HW_${upper}, 1, [${name} support]) fi """ TMPL_AUTOCONF_AC_CONFIG_FILES = "\t\t hardware/${short}/Makefile\n" TMPL_AUTOCONF_SUMMARY = 'echo " - ${summary}"\n' TMPL_HWMAKE_SUBDIR = '\t${short}' +TMPL_HWMAKE_DRIVERLIB = """if HW_${upper} +libsigrokhardware_la_LIBADD += ${short}/libsigrok_hw_${lib}.la +endif + +""" +TMPL_HWDRIVER_EXTERN = """\ +#ifdef HAVE_HW_${upper} +extern SR_PRIV struct sr_dev_driver ${lib}_driver_info; +#endif +""" +TMPL_HWDRIVER_DIPTR = """\ +#ifdef HAVE_HW_${upper} + &${lib}_driver_info, +#endif +""" FILE_DRV_MAKEFILE = 'drv-Makefile.am' FILE_DRV_API = 'drv-api.c' FILE_DRV_PROTOCOL = 'drv-protocol.c' @@ -66,6 +82,7 @@ def new_driver(): raise Exception(err.decode()) gitdir = tmp + '/libsigrok/' do_configure_ac(gitdir) + do_hwdriver(gitdir) do_hwmake(gitdir) do_driverskel(gitdir) make_patch(gitdir) @@ -127,7 +144,7 @@ def do_configure_ac(gitdir): # add summary line out = '' state = 'copy' - names['summary'] = "%s%s $HW_%s" % (names['name'], + names['summary'] = "%s%s $HW_%s" % (names['short'], '.' * (32 - len(names['name'])), names['upper']) for line in configure_ac.split('\n')[:-1]: if state == 'copy': @@ -152,11 +169,48 @@ def do_configure_ac(gitdir): open(cacpath, 'w').write(configure_ac) +def do_hwdriver(gitdir): + path = gitdir + 'hwdriver.c' + hwdriver = open(path).read() + # add HAVE_HW_thing extern and pointers + out = '' + state = 'copy' + for line in hwdriver.split('\n')[:-1]: + if state == 'copy': + if line.find('/** @cond PRIVATE */') == 0: + state = 'extern' + elif line.find('static struct sr_dev_driver *drivers_list') == 0: + state = 'diptr' + elif state in ('extern', 'diptr'): + if state == 'extern': + entry = tmpl(TMPL_HWDRIVER_EXTERN) + next_state = 'copy' + else: + entry = tmpl(TMPL_HWDRIVER_DIPTR) + next_state = 'done' + m = re.match('#ifdef HAVE_.._(.*)', line) + if m: + drv = m.group(1) + if drv > names['upper']: + out += entry + state = next_state + elif not re.match('(extern|\t&|#endif)', line): + # new one at the end + out += entry + state = next_state + out += line + '\n' + if state != 'done': + raise Exception('HAVE_* markers not found in hwdriver.c') + hwdriver = out + + open(path, 'w').write(hwdriver) + + def do_hwmake(gitdir): path = gitdir + 'hardware/Makefile.am' hwmake = open(path).read() - # add AC_ARG_ENABLE option + # add SUBDIRS entry out = '' state = 'copy' for line in hwmake.split('\n')[:-1]: @@ -169,13 +223,23 @@ def do_hwmake(gitdir): drv_short = m.group(1) if drv_short.lower() > names['short']: out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n' - state = 'done' + state = 'driverlib' else: out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n' - state = 'done' + state = 'driverlib' + elif state == 'driverlib': + m = re.match('if [A-Z]{2}_(.*)$', line) + if m: + drv_short = m.group(1) + if drv_short > names['upper']: + out += tmpl(TMPL_HWMAKE_DRIVERLIB) + state = 'done' out += line + '\n' - if state != 'done': - raise Exception('SUBDIRS markers not found in hardware/Makefile.am') + if state == 'driverlib': + # reached end of file, add it in here + out += tmpl(TMPL_HWMAKE_DRIVERLIB).strip() + elif state != 'done': + raise Exception('markers not found in hardware/Makefile.am') hwmake = out open(path, 'w').write(hwmake) @@ -193,8 +257,8 @@ def do_driverskel(gitdir): 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'] + cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short'] + cmd += 'configure.ac hwdriver.c hardware/Makefile.am hardware/' + names['short'] command(cmd) cmd = "git format-patch HEAD~1" out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate() @@ -212,40 +276,40 @@ def command(cmd): 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 + 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 # # 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 ") + print("Usage: new-driver ") sys.exit() author, email = parse_gitconfig() @@ -256,10 +320,9 @@ names = { '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() -