[ec1ce52] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | """ This file generates a c file from a list of cpp prototypes. """ |
---|
| 4 | |
---|
[b0f2cc5] | 5 | import os, sys, shutil |
---|
[c71e405] | 6 | from gen_pyobject import write_msg, gen_new_init, gen_do, gen_members, gen_methods, gen_finish |
---|
| 7 | |
---|
| 8 | def get_cpp_objects(): |
---|
| 9 | |
---|
[25c9f9a] | 10 | cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 -I../build/src ../src/aubio.h').readlines()] |
---|
[c71e405] | 11 | |
---|
| 12 | cpp_output = filter(lambda y: len(y) > 1, cpp_output) |
---|
| 13 | cpp_output = filter(lambda y: not y.startswith('#'), cpp_output) |
---|
| 14 | |
---|
| 15 | i = 1 |
---|
| 16 | while 1: |
---|
| 17 | if i >= len(cpp_output): break |
---|
| 18 | if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'): |
---|
| 19 | cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i] |
---|
| 20 | cpp_output.pop(i-1) |
---|
| 21 | else: |
---|
| 22 | i += 1 |
---|
| 23 | |
---|
| 24 | typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output) |
---|
| 25 | |
---|
| 26 | cpp_objects = [a.split()[3][:-1] for a in typedefs] |
---|
| 27 | |
---|
| 28 | return cpp_output, cpp_objects |
---|
| 29 | |
---|
[ad5203c] | 30 | def generate_object_files(output_path): |
---|
| 31 | if os.path.isdir(output_path): shutil.rmtree(output_path) |
---|
| 32 | os.mkdir(output_path) |
---|
[c71e405] | 33 | |
---|
| 34 | generated_objects = [] |
---|
| 35 | cpp_output, cpp_objects = get_cpp_objects() |
---|
[8f86f0e] | 36 | skip_objects = [ |
---|
| 37 | # already in ext/ |
---|
| 38 | 'fft', |
---|
[9b1e101] | 39 | 'pvoc', |
---|
| 40 | 'filter', |
---|
| 41 | 'filterbank', |
---|
[8f86f0e] | 42 | #'resampler', |
---|
| 43 | # AUBIO_UNSTABLE |
---|
| 44 | 'hist', |
---|
[e97d0b3] | 45 | 'parameter', |
---|
[8f86f0e] | 46 | 'scale', |
---|
| 47 | 'beattracking', |
---|
[9b1e101] | 48 | 'resampler', |
---|
| 49 | 'sndfile', |
---|
[8f86f0e] | 50 | 'peakpicker', |
---|
| 51 | 'pitchfcomb', |
---|
| 52 | 'pitchmcomb', |
---|
| 53 | 'pitchschmitt', |
---|
[258d441] | 54 | 'pitchspecacf', |
---|
[8f86f0e] | 55 | 'pitchyin', |
---|
| 56 | 'pitchyinfft', |
---|
[0b5b7b2] | 57 | 'sink_apple_audio', |
---|
| 58 | 'sink_sndfile', |
---|
[d27634d] | 59 | 'source', |
---|
[9b1e101] | 60 | 'source_apple_audio', |
---|
[8f86f0e] | 61 | 'source_sndfile', |
---|
[e1cdb89] | 62 | 'source_avcodec', |
---|
[8f86f0e] | 63 | #'sampler', |
---|
[7609f6d] | 64 | 'audio_unit', |
---|
[8f86f0e] | 65 | ] |
---|
[c71e405] | 66 | |
---|
| 67 | write_msg("-- INFO: %d objects in total" % len(cpp_objects)) |
---|
| 68 | |
---|
| 69 | for this_object in cpp_objects: |
---|
| 70 | lint = 0 |
---|
[ad5203c] | 71 | |
---|
[c71e405] | 72 | if this_object[-2:] == '_t': |
---|
| 73 | object_name = this_object[:-2] |
---|
| 74 | else: |
---|
| 75 | object_name = this_object |
---|
| 76 | write_msg("-- WARNING: %s does not end in _t" % this_object) |
---|
| 77 | |
---|
| 78 | if object_name[:len('aubio_')] != 'aubio_': |
---|
| 79 | write_msg("-- WARNING: %s does not start n aubio_" % this_object) |
---|
| 80 | |
---|
| 81 | write_msg("-- INFO: looking at", object_name) |
---|
| 82 | object_methods = filter(lambda x: this_object in x, cpp_output) |
---|
| 83 | object_methods = [a.strip() for a in object_methods] |
---|
| 84 | object_methods = filter(lambda x: not x.startswith('typedef'), object_methods) |
---|
| 85 | #for method in object_methods: |
---|
| 86 | # write_msg(method) |
---|
| 87 | new_methods = filter(lambda x: 'new_'+object_name in x, object_methods) |
---|
| 88 | if len(new_methods) > 1: |
---|
| 89 | write_msg("-- WARNING: more than one new method for", object_name) |
---|
| 90 | for method in new_methods: |
---|
| 91 | write_msg(method) |
---|
| 92 | elif len(new_methods) < 1: |
---|
| 93 | write_msg("-- WARNING: no new method for", object_name) |
---|
| 94 | elif 0: |
---|
| 95 | for method in new_methods: |
---|
| 96 | write_msg(method) |
---|
| 97 | |
---|
| 98 | del_methods = filter(lambda x: 'del_'+object_name in x, object_methods) |
---|
| 99 | if len(del_methods) > 1: |
---|
| 100 | write_msg("-- WARNING: more than one del method for", object_name) |
---|
| 101 | for method in del_methods: |
---|
| 102 | write_msg(method) |
---|
| 103 | elif len(del_methods) < 1: |
---|
| 104 | write_msg("-- WARNING: no del method for", object_name) |
---|
| 105 | |
---|
| 106 | do_methods = filter(lambda x: object_name+'_do' in x, object_methods) |
---|
| 107 | if len(do_methods) > 1: |
---|
| 108 | pass |
---|
| 109 | #write_msg("-- WARNING: more than one do method for", object_name) |
---|
| 110 | #for method in do_methods: |
---|
| 111 | # write_msg(method) |
---|
| 112 | elif len(do_methods) < 1: |
---|
| 113 | write_msg("-- WARNING: no do method for", object_name) |
---|
| 114 | elif 0: |
---|
| 115 | for method in do_methods: |
---|
| 116 | write_msg(method) |
---|
| 117 | |
---|
| 118 | # check do methods return void |
---|
| 119 | for method in do_methods: |
---|
| 120 | if (method.split()[0] != 'void'): |
---|
| 121 | write_msg("-- ERROR: _do method does not return void:", method ) |
---|
| 122 | |
---|
| 123 | get_methods = filter(lambda x: object_name+'_get_' in x, object_methods) |
---|
| 124 | |
---|
| 125 | set_methods = filter(lambda x: object_name+'_set_' in x, object_methods) |
---|
| 126 | for method in set_methods: |
---|
| 127 | if (method.split()[0] != 'uint_t'): |
---|
| 128 | write_msg("-- ERROR: _set method does not return uint_t:", method ) |
---|
| 129 | |
---|
| 130 | other_methods = filter(lambda x: x not in new_methods, object_methods) |
---|
| 131 | other_methods = filter(lambda x: x not in del_methods, other_methods) |
---|
| 132 | other_methods = filter(lambda x: x not in do_methods, other_methods) |
---|
| 133 | other_methods = filter(lambda x: x not in get_methods, other_methods) |
---|
| 134 | other_methods = filter(lambda x: x not in set_methods, other_methods) |
---|
| 135 | |
---|
| 136 | if len(other_methods) > 0: |
---|
| 137 | write_msg("-- WARNING: some methods for", object_name, "were unidentified") |
---|
| 138 | for method in other_methods: |
---|
| 139 | write_msg(method) |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | # generate this_object |
---|
| 143 | short_name = object_name[len('aubio_'):] |
---|
| 144 | if short_name in skip_objects: |
---|
| 145 | write_msg("-- INFO: skipping object", short_name ) |
---|
| 146 | continue |
---|
| 147 | if 1: #try: |
---|
| 148 | s = gen_new_init(new_methods[0], short_name) |
---|
[ad5203c] | 149 | s += gen_do(do_methods[0], short_name) |
---|
[c71e405] | 150 | s += gen_members(new_methods[0], short_name) |
---|
| 151 | s += gen_methods(get_methods, set_methods, short_name) |
---|
| 152 | s += gen_finish(short_name) |
---|
[ad5203c] | 153 | generated_filepath = os.path.join(output_path,'gen-'+short_name+'.c') |
---|
[c71e405] | 154 | fd = open(generated_filepath, 'w') |
---|
| 155 | fd.write(s) |
---|
| 156 | #except Exception, e: |
---|
| 157 | # write_msg("-- ERROR:", type(e), str(e), "in", short_name) |
---|
| 158 | # continue |
---|
| 159 | generated_objects += [this_object] |
---|
| 160 | |
---|
| 161 | s = """// generated list of objects created with generator.py |
---|
[ec1ce52] | 162 | |
---|
[4bc098c] | 163 | """ |
---|
| 164 | |
---|
[c71e405] | 165 | types_ready = [] |
---|
| 166 | for each in generated_objects: |
---|
| 167 | types_ready.append(" PyType_Ready (&Py_%sType) < 0" % \ |
---|
| 168 | each.replace('aubio_','').replace('_t','') ) |
---|
| 169 | |
---|
[93acd9f] | 170 | s = """// generated list of objects created with generator.py |
---|
| 171 | |
---|
| 172 | #include "aubio-generated.h" |
---|
| 173 | """ |
---|
| 174 | |
---|
[c71e405] | 175 | s += """ |
---|
[93acd9f] | 176 | int generated_types_ready (void) |
---|
| 177 | { |
---|
| 178 | return ( |
---|
| 179 | """ |
---|
[c71e405] | 180 | s += ('\n ||').join(types_ready) |
---|
| 181 | s += """); |
---|
[93acd9f] | 182 | } |
---|
| 183 | """ |
---|
[c71e405] | 184 | |
---|
| 185 | s += """ |
---|
[93acd9f] | 186 | void add_generated_objects ( PyObject *m ) |
---|
| 187 | {""" |
---|
[c71e405] | 188 | for each in generated_objects: |
---|
[93acd9f] | 189 | s += """ |
---|
| 190 | Py_INCREF (&Py_%(name)sType); |
---|
| 191 | PyModule_AddObject (m, "%(name)s", (PyObject *) & Py_%(name)sType);""" % \ |
---|
| 192 | { 'name': ( each.replace('aubio_','').replace('_t','') ) } |
---|
[c71e405] | 193 | |
---|
| 194 | s += """ |
---|
[93acd9f] | 195 | }""" |
---|
| 196 | |
---|
| 197 | fd = open(os.path.join(output_path,'aubio-generated.c'), 'w') |
---|
| 198 | fd.write(s) |
---|
| 199 | |
---|
| 200 | s = """// generated list of objects created with generator.py |
---|
| 201 | |
---|
[8f86f0e] | 202 | #include <Python.h> |
---|
[93acd9f] | 203 | |
---|
| 204 | """ |
---|
| 205 | |
---|
| 206 | for each in generated_objects: |
---|
| 207 | s += "extern PyTypeObject Py_%sType;\n" % \ |
---|
| 208 | each.replace('aubio_','').replace('_t','') |
---|
| 209 | |
---|
| 210 | s+= "int generated_objects ( void );\n" |
---|
| 211 | s+= "void add_generated_objects( PyObject *m );\n" |
---|
[c71e405] | 212 | |
---|
[ad5203c] | 213 | fd = open(os.path.join(output_path,'aubio-generated.h'), 'w') |
---|
[c71e405] | 214 | fd.write(s) |
---|
| 215 | |
---|
| 216 | from os import listdir |
---|
[ad5203c] | 217 | generated_files = listdir(output_path) |
---|
[c71e405] | 218 | generated_files = filter(lambda x: x.endswith('.c'), generated_files) |
---|
[ad5203c] | 219 | generated_files = [output_path+'/'+f for f in generated_files] |
---|
[c71e405] | 220 | return generated_files |
---|
| 221 | |
---|
| 222 | if __name__ == '__main__': |
---|
[ad5203c] | 223 | generate_object_files('gen') |
---|