3 # This file is part of the sigrok 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 = """AC_ARG_ENABLE(${short}, AC_HELP_STRING([--enable-${short}],
31 [enable ${name} driver support [default=yes]]),
32 [HW_${upper}="$enableval"],
34 AM_CONDITIONAL(HW_${upper}, test x$HW_${upper} = xyes)
35 if test "x$HW_${upper}" = "xyes"; then
36 AC_DEFINE(HAVE_HW_${upper}, 1, [${name} driver support])
40 TMPL_AUTOCONF_AC_CONFIG_FILES = "\t\t hardware/${short}/Makefile\n"
41 TMPL_AUTOCONF_SUMMARY = 'echo " - ${summary}"\n'
42 TMPL_HWMAKE_SUBDIR = '\t${short}'
43 FILE_DRV_MAKEFILE = 'drv-Makefile.am'
44 FILE_DRV_API = 'drv-api.c'
45 FILE_DRV_PROTOCOL = 'drv-protocol.c'
46 FILE_DRV_PROTOCOL_H = 'drv-protocol.h'
49 out = re.sub(r'\${([^}]+)}', lambda x: str(names[x.group(1)]), template)
54 def tmpl_file(filename):
55 template = open(TMPLDIR + '/' + filename).read()
61 tmp = tempfile.mkdtemp()
64 out, err = Popen("git clone " + LIBSR, shell=True, stderr=PIPE).communicate()
66 raise Exception(err.decode())
67 gitdir = tmp + '/libsigrok/'
68 do_configure_ac(gitdir)
72 except Exception as e:
77 def do_configure_ac(gitdir):
78 cacpath = gitdir + 'configure.ac'
79 configure_ac = open(cacpath).read()
81 # add AC_ARG_ENABLE option
84 for line in configure_ac.split('\n')[:-1]:
86 if line == "# Hardware support '--enable' options.":
88 elif state == 'acarg':
89 m = re.match('AC_ARG_ENABLE\(([^,]+)', line)
91 drv_short = m.group(1)
92 if drv_short.lower() > names['short']:
93 out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
95 if line == '# Checks for libraries.':
97 out += tmpl(TMPL_AUTOCONF_AC_ARG_ENABLE)
101 raise Exception('AC_ARG_ENABLE markers not found in configure.ac')
104 # add driver Makefile to AC_CONFIG_FILES
107 for line in configure_ac.split('\n')[:-1]:
109 if line.find("AC_CONFIG_FILES([Makefile") > -1:
111 elif state == 'acconf':
112 m = re.match('\t\t hardware/([^/]+)/Makefile', line)
114 drv_short = m.group(1)
115 if drv_short.lower() > names['short']:
116 out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
120 out += tmpl(TMPL_AUTOCONF_AC_CONFIG_FILES)
124 raise Exception('AC_CONFIG_FILES marker not found in configure.ac')
130 names['summary'] = "%s%s $HW_%s" % (names['name'],
131 '.' * (32 - len(names['name'])), names['upper'])
132 for line in configure_ac.split('\n')[:-1]:
134 if line.find('Enabled hardware drivers') > -1:
136 elif state == 'echo':
137 m = re.match('echo " - ([^\.]+)', line)
139 drv_short = m.group(1)
140 if drv_short.lower() > names['name'].lower():
141 out += tmpl(TMPL_AUTOCONF_SUMMARY)
145 out += tmpl(TMPL_AUTOCONF_SUMMARY)
149 raise Exception('summary marker not found in configure.ac')
152 open(cacpath, 'w').write(configure_ac)
155 def do_hwmake(gitdir):
156 path = gitdir + 'hardware/Makefile.am'
157 hwmake = open(path).read()
159 # add AC_ARG_ENABLE option
162 for line in hwmake.split('\n')[:-1]:
164 if line.find('SUBDIRS =') > -1:
166 elif state == 'subdirs':
167 m = re.match('\t([^ \\\]+\s*\\\)', line)
169 drv_short = m.group(1)
170 if drv_short.lower() > names['short']:
171 out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
174 out += tmpl(TMPL_HWMAKE_SUBDIR) + ' \\\n'
178 raise Exception('SUBDIRS markers not found in hardware/Makefile.am')
181 open(path, 'w').write(hwmake)
184 def do_driverskel(gitdir):
185 drvdir = gitdir + 'hardware/' + names['short']
187 open(drvdir + '/Makefile.am', 'w').write(tmpl_file(FILE_DRV_MAKEFILE))
188 open(drvdir + '/api.c', 'w').write(tmpl_file(FILE_DRV_API))
189 open(drvdir + '/protocol.c', 'w').write(tmpl_file(FILE_DRV_PROTOCOL))
190 open(drvdir + '/protocol.h', 'w').write(tmpl_file(FILE_DRV_PROTOCOL_H))
193 def make_patch(gitdir):
195 command('git add hardware/' + names['short'])
196 cmd = 'git commit -m "%s: initial driver skeleton" ' % names['short']
197 cmd += 'configure.ac hardware/Makefile.am hardware/' + names['short']
199 cmd = "git format-patch HEAD~1"
200 out, err = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
202 raise Exception(err.decode())
203 patch = out.decode().strip()
204 shutil.move(gitdir + '/' + patch, scriptdir + '/' + patch)
209 out, err = Popen(cmd, shell=True, stderr=PIPE).communicate()
211 raise Exception(err.decode())
214 def parse_gitconfig():
216 author = email = None
217 for line in open(os.environ['HOME'] + '/.gitconfig').readlines():
218 m = re.match('\s*(\w+)\s*=\s*(.*)\s*$', line)
220 key, value = m.groups()
229 if not author or not email:
230 print("Please put your name and email in ~/.gitconfig")
239 scriptdir = os.getcwd()
240 if socket.gethostname() == 'sigrok':
241 LIBSR = '/data/git/libsigrok'
242 TMPLDIR = '/data/tools/tmpl'
244 LIBSR = 'git://sigrok.org/libsigrok'
247 if len(sys.argv) < 2:
248 print("Usage: new-driver.py <name>")
251 author, email = parse_gitconfig()
252 name = ' '.join(sys.argv[1:])
255 'short': re.sub('[^a-z0-9]', '-', name.lower()),
256 'lib': re.sub('[^a-z0-9]', '_', name.lower()),
257 'upper': re.sub('[^A-Z0-9]', '_', name.upper()),
258 'libupper': re.sub('[^A-Z0-9]', '', name.upper()),
259 'year': datetime.datetime.now().year,