[ec1ce52] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | """ This file generates a c file from a list of cpp prototypes. """ |
---|
| 4 | |
---|
| 5 | import os, sys |
---|
| 6 | |
---|
[b8ee46c] | 7 | skip_objects = ['fft', 'pvoc', 'filter', 'filterbank', 'resampler'] |
---|
[ec1ce52] | 8 | |
---|
[7c4cf20] | 9 | cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 -I ../../build/default/src ../../src/aubio.h').readlines()] |
---|
[ec1ce52] | 10 | |
---|
| 11 | cpp_output = filter(lambda y: len(y) > 1, cpp_output) |
---|
| 12 | cpp_output = filter(lambda y: not y.startswith('#'), cpp_output) |
---|
| 13 | |
---|
| 14 | i = 1 |
---|
| 15 | while 1: |
---|
[c85da04] | 16 | if i >= len(cpp_output): break |
---|
| 17 | if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'): |
---|
| 18 | cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i] |
---|
| 19 | cpp_output.pop(i-1) |
---|
| 20 | else: |
---|
| 21 | i += 1 |
---|
[ec1ce52] | 22 | |
---|
| 23 | typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output) |
---|
| 24 | |
---|
| 25 | objects = [a.split()[3][:-1] for a in typedefs] |
---|
| 26 | |
---|
| 27 | print "-- INFO: %d objects in total" % len(objects) |
---|
| 28 | |
---|
[4bc098c] | 29 | generated_objects = [] |
---|
| 30 | |
---|
| 31 | for this_object in objects: |
---|
[c85da04] | 32 | lint = 0 |
---|
| 33 | |
---|
[4bc098c] | 34 | if this_object[-2:] == '_t': |
---|
| 35 | object_name = this_object[:-2] |
---|
[c85da04] | 36 | else: |
---|
[4bc098c] | 37 | object_name = this_object |
---|
| 38 | print "-- WARNING: %s does not end in _t" % this_object |
---|
[c85da04] | 39 | |
---|
| 40 | if object_name[:len('aubio_')] != 'aubio_': |
---|
[4bc098c] | 41 | print "-- WARNING: %s does not start n aubio_" % this_object |
---|
[c85da04] | 42 | |
---|
| 43 | print "-- INFO: looking at", object_name |
---|
[4bc098c] | 44 | object_methods = filter(lambda x: this_object in x, cpp_output) |
---|
[c85da04] | 45 | object_methods = [a.strip() for a in object_methods] |
---|
| 46 | object_methods = filter(lambda x: not x.startswith('typedef'), object_methods) |
---|
| 47 | #for method in object_methods: |
---|
| 48 | # print method |
---|
| 49 | |
---|
| 50 | new_methods = filter(lambda x: 'new_'+object_name in x, object_methods) |
---|
| 51 | if len(new_methods) > 1: |
---|
| 52 | print "-- WARNING: more than one new method for", object_name |
---|
| 53 | for method in new_methods: |
---|
| 54 | print method |
---|
| 55 | elif len(new_methods) < 1: |
---|
| 56 | print "-- WARNING: no new method for", object_name |
---|
| 57 | elif 0: |
---|
| 58 | for method in new_methods: |
---|
| 59 | print method |
---|
| 60 | |
---|
| 61 | del_methods = filter(lambda x: 'del_'+object_name in x, object_methods) |
---|
| 62 | if len(del_methods) > 1: |
---|
| 63 | print "-- WARNING: more than one del method for", object_name |
---|
| 64 | for method in del_methods: |
---|
| 65 | print method |
---|
| 66 | elif len(del_methods) < 1: |
---|
| 67 | print "-- WARNING: no del method for", object_name |
---|
| 68 | |
---|
| 69 | do_methods = filter(lambda x: object_name+'_do' in x, object_methods) |
---|
| 70 | if len(do_methods) > 1: |
---|
| 71 | pass |
---|
| 72 | #print "-- WARNING: more than one do method for", object_name |
---|
| 73 | #for method in do_methods: |
---|
| 74 | # print method |
---|
| 75 | elif len(do_methods) < 1: |
---|
| 76 | print "-- WARNING: no do method for", object_name |
---|
| 77 | elif 0: |
---|
| 78 | for method in do_methods: |
---|
| 79 | print method |
---|
| 80 | |
---|
| 81 | # check do methods return void |
---|
[ec1ce52] | 82 | for method in do_methods: |
---|
[c85da04] | 83 | if (method.split()[0] != 'void'): |
---|
| 84 | print "-- ERROR: _do method does not return void:", method |
---|
| 85 | |
---|
| 86 | get_methods = filter(lambda x: object_name+'_get_' in x, object_methods) |
---|
| 87 | |
---|
| 88 | set_methods = filter(lambda x: object_name+'_set_' in x, object_methods) |
---|
| 89 | for method in set_methods: |
---|
| 90 | if (method.split()[0] != 'uint_t'): |
---|
| 91 | print "-- ERROR: _set method does not return uint_t:", method |
---|
| 92 | |
---|
| 93 | other_methods = filter(lambda x: x not in new_methods, object_methods) |
---|
| 94 | other_methods = filter(lambda x: x not in del_methods, other_methods) |
---|
| 95 | other_methods = filter(lambda x: x not in do_methods, other_methods) |
---|
| 96 | other_methods = filter(lambda x: x not in get_methods, other_methods) |
---|
| 97 | other_methods = filter(lambda x: x not in set_methods, other_methods) |
---|
| 98 | |
---|
| 99 | if len(other_methods) > 0: |
---|
| 100 | print "-- WARNING: some methods for", object_name, "were unidentified" |
---|
| 101 | for method in other_methods: |
---|
| 102 | print method |
---|
| 103 | |
---|
[4bc098c] | 104 | # generate this_object |
---|
[c85da04] | 105 | if not os.path.isdir('generated'): os.mkdir('generated') |
---|
| 106 | from gen_pyobject import * |
---|
| 107 | short_name = object_name[len('aubio_'):] |
---|
| 108 | if short_name in skip_objects: |
---|
| 109 | print "-- INFO: skipping object", short_name |
---|
| 110 | continue |
---|
| 111 | if 1: #try: |
---|
| 112 | s = gen_new_init(new_methods[0], short_name) |
---|
| 113 | s += gen_do(do_methods[0], short_name) |
---|
| 114 | s += gen_members(new_methods[0], short_name) |
---|
| 115 | s += gen_methods(get_methods, set_methods, short_name) |
---|
| 116 | s += gen_finish(short_name) |
---|
| 117 | fd = open('generated/gen-'+short_name+'.c', 'w') |
---|
| 118 | fd.write(s) |
---|
| 119 | #except Exception, e: |
---|
| 120 | # print "-- ERROR:", type(e), str(e), "in", short_name |
---|
| 121 | # continue |
---|
[4bc098c] | 122 | generated_objects += [this_object] |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | s = """// generated list of generated objects |
---|
| 126 | |
---|
| 127 | """ |
---|
| 128 | |
---|
| 129 | for each in generated_objects: |
---|
| 130 | s += "extern PyTypeObject Py_%sType;\n" % \ |
---|
| 131 | each.replace('aubio_','').replace('_t','') |
---|
| 132 | |
---|
| 133 | types_ready = [] |
---|
| 134 | for each in generated_objects: |
---|
| 135 | types_ready.append(" PyType_Ready (&Py_%sType) < 0" % \ |
---|
| 136 | each.replace('aubio_','').replace('_t','') ) |
---|
| 137 | |
---|
| 138 | s += """ |
---|
| 139 | int |
---|
| 140 | generated_types_ready (void) |
---|
| 141 | { |
---|
| 142 | return (""" |
---|
| 143 | s += ('||\n').join(types_ready) |
---|
| 144 | s += """); |
---|
| 145 | } |
---|
| 146 | """ |
---|
| 147 | |
---|
| 148 | s += """ |
---|
| 149 | void |
---|
| 150 | add_generated_objects ( PyObject *m ) |
---|
| 151 | {""" |
---|
| 152 | for each in generated_objects: |
---|
| 153 | s += """ Py_INCREF (&Py_%(name)sType); |
---|
| 154 | PyModule_AddObject (m, "%(name)s", (PyObject *) & Py_%(name)sType);""" % \ |
---|
| 155 | { 'name': ( each.replace('aubio_','').replace('_t','') ) } |
---|
| 156 | |
---|
| 157 | s += """ |
---|
| 158 | }""" |
---|
| 159 | |
---|
| 160 | fd = open('generated/aubio-generated.h', 'w') |
---|
| 161 | fd.write(s) |
---|