3 ## This file is part of the sigrok-util project.
5 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 from subprocess import Popen, PIPE
30 TMPL_AUTOCONF_AC_ARG_ENABLE = """\
31 AC_ARG_ENABLE(${short}, AC_HELP_STRING([--enable-${short}],
32 [enable ${name} driver support [default=yes]]),
33 [HW_${upper}="$enableval"],
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} driver support])
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 FILE_DRV_MAKEFILE = 'drv-Makefile.am'
45 FILE_DRV_API = 'drv-api.c'
46 FILE_DRV_PROTOCOL = 'drv-protocol.c'
47 FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
50 out = re.sub(r'\${([^}]+)}', lambda x: str(names[x.group(1)]), template)
55 def tmpl_file(filename):
56 template = open(TMPLDIR + '/' + filename).read()
62 tmp = tempfile.mkdtemp()
65 out, err = Popen("git clone " + LIBSR, shell=True, stderr=PIPE).communicate()
67 raise Exception(err.decode())
68 gitdir = tmp + '/libsigrok/'
69 do_configure_ac(gitdir)
73 except Exception as e:
78 def do_configure_ac(gitdir):
79 cacpath = gitdir + 'configure.ac'
80 configure_ac = open(cacpath).read()
82 # add AC_ARG_ENABLE option
85 for line in configure_ac.split('\n')[:-1]:
87 if line == "# Hardware support '--enable' options.":
89 elif state == 'acarg':
90 m = re.match('AC_ARG_ENABLE\(([^,]+)', line)
92 drv_short = m.group(1)
93 if drv_short.lower() > names['short']:
94 out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
96 if line == '# Checks for libraries.':
98 out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
102 raise Exception('AC_ARG_ENABLE markers not found in configure.ac')
105 # add driver Makefile to AC_CONFIG_FILES
108 for line in configure_ac.split('\n')[:-1]:
110 if line.find("AC_CONFIG_FILES([Makefile") > -1:
112 elif state == 'acconf':
113 m = re.match('\t\t hardware/([^/]+)/Makefile', line)
115 drv_short = m.group(1)
116 if drv_short.lower() > names['short']:
117 out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
121 out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
125 raise Exception('AC_CONFIG_FILES marker not found in configure.ac')
131 names['summary'] = "%s%s $HW_%s" % (names['name'],
132 '.' * (32 - len(names['name'])), names['upper'])
133 for line in configure_ac.split('\n')[:-1]:
135 if line.find('Enabled hardware drivers') > -1:
137 elif state == 'echo':
138 m = re.match('echo " - ([^\.]+)', line)
140 drv_short = m.group(1)
141 if drv_short.lower() > names['name'].lower():
142 out += tmpl(TMPL_AUTOCONF_SUMMARY)
146 out += tmpl(TMPL_AUTOCONF_SUMMARY)
150 raise Exception('summary marker not found in configure.ac')
153 open(cacpath, 'w').write(configure_ac)
156 def do_hwmake(gitdir):
157 path = gitdir + 'hardware/Makefile.am'
158 hwmake = open(path).read()
160 # add AC_ARG_ENABLE option
163 for line in hwmake.split('\n')[:-1]:
165 if line.find('SUBDIRS =') > -1:
167 elif state == 'subdirs':
168 m = re.match('\t([^ \\\]+\s*\\\)', line)
170 drv_short = m.group(1)
171 if drv_short.lower() > names['short']:
172 out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
175 out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
179 raise Exception('SUBDIRS markers not found in hardware/Makefile.am')
182 open(path, 'w').write(hwmake)
185 def do_driverskel(gitdir):
186 drvdir = gitdir + 'hardware/' + names['short']
188 open(drvdir + '/Makefile.am', 'w').write(tmpl_file(FILE_DRV_MAKEFILE))
189 open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
190 open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
191 open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
194 def make_patch(gitdir):
196 command('git add hardware/' + names['short'])
197 cmd = 'git commit -m "%s: Initial driver skeleton." ' % names['short']
198 cmd += 'configure.ac hardware/Makefile.am hardware/' + names['short']
200 cmd = "git format-patch HEAD~1"
201 out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
203 raise Exception(err.decode())
204 patch = out.decode().strip()
205 shutil.move(gitdir + '/' + patch, scriptdir + '/' + patch)
210 out, err = Popen(cmd, shell=True, stderr=PIPE).communicate()
212 raise Exception(err.decode())
215 def parse_gitconfig():
217 author = email = None
218 for line in open(os.environ['HOME'] + '/.gitconfig').readlines():
219 m = re.match('\s*(\w+)\s*=\s*(.*)\s*$', line)
221 key, value = m.groups()
230 if not author or not email:
231 print("Please put your name and email in ~/.gitconfig")
240 scriptdir = os.getcwd()
241 if socket.gethostname() == 'sigrok':
242 LIBSR = '/data/git/libsigrok'
243 TMPLDIR = '/data/tools/tmpl'
245 LIBSR = 'git://sigrok.org/libsigrok'
248 if len(sys.argv) < 2:
249 print("Usage: new-driver <name>")
252 author, email = parse_gitconfig()
253 name = ' '.join(sys.argv[1:])
256 'short': re.sub('[^a-z0-9]', '-', name.lower()),
257 'lib': re.sub('[^a-z0-9]', '_', name.lower()),
258 'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
259 'libupper': re.sub('[^A-Z0-9]', '', name.upper()),
260 'year': datetime.datetime.now().year,