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