+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)
+
+