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