3 header = """// this file is generated! do not modify
4 #include "aubio-types.h"
44 def get_cpp_objects():
46 cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 -I../build/src ../src/aubio.h').readlines()]
47 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/onset/onset.h').readlines()]
48 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/pitch/pitch.h').readlines()]
50 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
51 cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
52 cpp_output = list(cpp_output)
56 if i >= len(cpp_output): break
57 if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
58 cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
63 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
65 cpp_objects = [a.split()[3][:-1] for a in typedefs]
67 return cpp_output, cpp_objects
69 def generate_external(output_path, usedouble = False, overwrite = True):
70 if not os.path.isdir(output_path): os.mkdir(output_path)
71 elif overwrite == False: return glob.glob(os.path.join(output_path, '*.c'))
73 cpp_output, cpp_objects = get_cpp_objects()
80 if shortname in skip_objects:
82 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
83 lib[shortname]['longname'] = o
84 lib[shortname]['shortname'] = shortname
87 #print "found", o[:-1], "in", fn
88 if 'typedef struct ' in fn:
89 lib[shortname]['struct'].append(fn)
91 lib[shortname]['do'].append(fn)
93 lib[shortname]['new'].append(fn)
95 lib[shortname]['del'].append(fn)
97 lib[shortname]['get'].append(fn)
99 lib[shortname]['set'].append(fn)
101 #print "no idea what to do about", fn
102 lib[shortname]['other'].append(fn)
105 for fn in cpp_output:
108 for family in lib[o]:
109 if fn in lib[o][family]:
115 for family in lib[o]:
116 if type(lib[o][family]) == str:
117 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
118 elif len(lib[o][family]) == 1:
119 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
121 print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
124 from .gen_code import MappedObject
127 mapped = MappedObject(lib[o], usedouble = usedouble)
128 out += mapped.gen_code()
129 output_file = os.path.join(output_path, 'gen-%s.c' % o)
130 with open(output_file, 'w') as f:
132 print ("wrote %s" % output_file )
133 sources_list.append(output_file)
136 out += "#include \"aubio-generated.h\""
137 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
140 int generated_types_ready (void)
142 return ({pycheck_types});
144 """.format(pycheck_types = check_types)
146 add_types = "".join(["""
147 Py_INCREF (&Py_{name}Type);
148 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
151 void add_generated_objects ( PyObject *m )
155 """.format(add_types = add_types)
157 output_file = os.path.join(output_path, 'aubio-generated.c')
158 with open(output_file, 'w') as f:
160 print ("wrote %s" % output_file )
161 sources_list.append(output_file)
163 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
164 out = """// generated list of objects created with gen_external.py
170 #ifndef HAVE_AUBIO_DOUBLE
171 #define HAVE_AUBIO_DOUBLE 1
176 int generated_objects ( void );
177 void add_generated_objects( PyObject *m );
178 """.format(objlist = objlist)
180 output_file = os.path.join(output_path, 'aubio-generated.h')
181 with open(output_file, 'w') as f:
183 print ("wrote %s" % output_file )
184 # no need to add header to list of sources
188 if __name__ == '__main__':
190 generate_external(output_path)