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