3 header = os.path.join('src', 'aubio.h')
4 output_path = os.path.join('python', 'gen')
6 source_header = """// this file is generated! do not modify
7 #include "aubio-types.h"
47 def get_cpp_objects(header=header):
49 cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 {:s}'.format(header)).readlines()]
50 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/onset/onset.h').readlines()]
51 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/pitch/pitch.h').readlines()]
53 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
54 cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
55 cpp_output = list(cpp_output)
59 if i >= len(cpp_output): break
60 if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
61 cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
66 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
68 cpp_objects = [a.split()[3][:-1] for a in typedefs]
70 return cpp_output, cpp_objects
72 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
73 if not os.path.isdir(output_path): os.mkdir(output_path)
74 elif overwrite == False: return glob.glob(os.path.join(output_path, '*.c'))
76 cpp_output, cpp_objects = get_cpp_objects(header)
83 if shortname in skip_objects:
85 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
86 lib[shortname]['longname'] = o
87 lib[shortname]['shortname'] = shortname
90 #print "found", o[:-1], "in", fn
91 if 'typedef struct ' in fn:
92 lib[shortname]['struct'].append(fn)
94 lib[shortname]['do'].append(fn)
96 lib[shortname]['new'].append(fn)
98 lib[shortname]['del'].append(fn)
100 lib[shortname]['get'].append(fn)
102 lib[shortname]['set'].append(fn)
104 #print "no idea what to do about", fn
105 lib[shortname]['other'].append(fn)
108 for fn in cpp_output:
111 for family in lib[o]:
112 if fn in lib[o][family]:
118 for family in lib[o]:
119 if type(lib[o][family]) == str:
120 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
121 elif len(lib[o][family]) == 1:
122 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
124 print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
128 from .gen_code import MappedObject
129 except (SystemError, ValueError) as e:
130 from gen_code import MappedObject
133 mapped = MappedObject(lib[o], usedouble = usedouble)
134 out += mapped.gen_code()
135 output_file = os.path.join(output_path, 'gen-%s.c' % o)
136 with open(output_file, 'w') as f:
138 print ("wrote %s" % output_file )
139 sources_list.append(output_file)
142 out += "#include \"aubio-generated.h\""
143 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
146 int generated_types_ready (void)
148 return ({pycheck_types});
150 """.format(pycheck_types = check_types)
152 add_types = "".join(["""
153 Py_INCREF (&Py_{name}Type);
154 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
157 void add_generated_objects ( PyObject *m )
161 """.format(add_types = add_types)
163 output_file = os.path.join(output_path, 'aubio-generated.c')
164 with open(output_file, 'w') as f:
166 print ("wrote %s" % output_file )
167 sources_list.append(output_file)
169 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
170 out = """// generated list of objects created with gen_external.py
176 #ifndef HAVE_AUBIO_DOUBLE
177 #define HAVE_AUBIO_DOUBLE 1
182 int generated_objects ( void );
183 void add_generated_objects( PyObject *m );
184 """.format(objlist = objlist)
186 output_file = os.path.join(output_path, 'aubio-generated.h')
187 with open(output_file, 'w') as f:
189 print ("wrote %s" % output_file )
190 # no need to add header to list of sources
194 if __name__ == '__main__':
196 if len(sys.argv) > 1: header = sys.argv[1]
197 if len(sys.argv) > 2: output_path = sys.argv[2]
198 generate_external(header, output_path)