1 import distutils.ccompiler
7 header = os.path.join('src', 'aubio.h')
8 output_path = os.path.join('python', 'gen')
10 source_header = """// this file is generated! do not modify
11 #include "aubio-types.h"
14 default_skip_objects = [
50 def get_preprocessor():
51 # findout which compiler to use
52 from distutils.sysconfig import customize_compiler
53 compiler_name = distutils.ccompiler.get_default_compiler()
54 compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
56 customize_compiler(compiler)
57 except AttributeError as e:
58 print("Warning: failed customizing compiler ({:s})".format(repr(e)))
60 if hasattr(compiler, 'initialize'):
63 except ValueError as e:
64 print("Warning: failed initializing compiler ({:s})".format(repr(e)))
67 if hasattr(compiler, 'preprocessor'): # for unixccompiler
68 cpp_cmd = compiler.preprocessor
69 elif hasattr(compiler, 'compiler'): # for ccompiler
70 cpp_cmd = compiler.compiler.split()
72 elif hasattr(compiler, 'cc'): # for msvccompiler
73 cpp_cmd = compiler.cc.split()
77 print("Warning: could not guess preprocessor, using env's CC")
78 cpp_cmd = os.environ.get('CC', 'cc').split()
80 cpp_cmd += ['-x', 'c'] # force C language (emcc defaults to c++)
84 def get_c_declarations(header=header, usedouble=False):
85 ''' return a dense and preprocessed string of all c declarations implied by aubio.h
87 cpp_cmd = get_preprocessor()
89 macros = [('AUBIO_UNSTABLE', 1)]
91 macros += [('HAVE_AUBIO_DOUBLE', 1)]
93 if not os.path.isfile(header):
94 raise Exception("could not find include file " + header)
96 includes = [os.path.dirname(header)]
97 cpp_cmd += distutils.ccompiler.gen_preprocess_options(macros, includes)
100 print("Running command: {:s}".format(" ".join(cpp_cmd)))
101 proc = subprocess.Popen(cpp_cmd,
102 stderr=subprocess.PIPE,
103 stdout=subprocess.PIPE)
104 assert proc, 'Proc was none'
105 cpp_output = proc.stdout.read()
106 err_output = proc.stderr.read()
108 raise Exception("preprocessor output is empty:\n%s" % err_output)
110 print("Warning: preprocessor produced warnings:\n%s" % err_output)
111 if not isinstance(cpp_output, list):
112 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
114 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
115 cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
119 if i >= len(cpp_output):
121 if ('{' in cpp_output[i - 1]) and ('}' not in cpp_output[i - 1]) or (';' not in cpp_output[i - 1]):
122 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
123 cpp_output.pop(i - 1)
124 elif ('}' in cpp_output[i]):
125 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
126 cpp_output.pop(i - 1)
130 # clean pointer notations
133 tmp += [l.replace(' *', ' * ')]
139 def get_cpp_objects_from_c_declarations(c_declarations, skip_objects=None):
140 if skip_objects is None:
141 skip_objects = default_skip_objects
142 typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations)
143 cpp_objects = [a.split()[3][:-1] for a in typedefs]
144 cpp_objects_filtered = filter(lambda y: not y[6:-2] in skip_objects, cpp_objects)
145 return cpp_objects_filtered
148 def get_all_func_names_from_lib(lib):
149 ''' return flat string of all function used in lib
152 for _, v in lib.items():
153 if isinstance(v, dict):
154 res += get_all_func_names_from_lib(v)
155 elif isinstance(v, list):
159 continue # not a function
160 fname_part = e[0].strip().split(' ')
161 fname = fname_part[-1]
165 raise NameError('gen_lib : weird function: ' + str(e))
170 def generate_lib_from_c_declarations(cpp_objects, c_declarations):
171 ''' returns a lib from given cpp_object names
173 a lib is a dict grouping functions by family (onset,pitch...)
174 each eement is itself a dict of functions grouped by puposes as :
175 struct, new, del, do, get, set and other
179 for o in cpp_objects:
181 if o[:6] == 'aubio_':
182 shortname = o[6:-2] # without aubio_ prefix and _t suffix
184 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
185 lib[shortname]['longname'] = o
186 lib[shortname]['shortname'] = shortname
188 fullshortname = o[:-2] # name without _t suffix
190 for fn in c_declarations:
191 func_name = fn.split('(')[0].strip().split(' ')[-1]
192 if func_name.startswith(fullshortname + '_') or func_name.endswith(fullshortname):
193 # print "found", shortname, "in", fn
194 if 'typedef struct ' in fn:
195 lib[shortname]['struct'].append(fn)
197 lib[shortname]['do'].append(fn)
199 lib[shortname]['new'].append(fn)
201 lib[shortname]['del'].append(fn)
203 lib[shortname]['get'].append(fn)
205 lib[shortname]['set'].append(fn)
207 # print "no idea what to do about", fn
208 lib[shortname]['other'].append(fn)
212 def print_c_declarations_results(lib, c_declarations):
213 for fn in c_declarations:
216 for family in lib[o]:
217 if fn in lib[o][family]:
223 for family in lib[o]:
224 if type(lib[o][family]) == str:
225 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
226 elif len(lib[o][family]) == 1:
227 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0]))
229 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
232 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
233 if not os.path.isdir(output_path):
234 os.mkdir(output_path)
236 return sorted(glob.glob(os.path.join(output_path, '*.c')))
238 c_declarations = get_c_declarations(header, usedouble=usedouble)
239 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)
241 lib = generate_lib_from_c_declarations(cpp_objects, c_declarations)
242 # print_c_declarations_results(lib, c_declarations)
246 from .gen_code import MappedObject
247 except (SystemError, ValueError):
248 from gen_code import MappedObject
251 mapped = MappedObject(lib[o], usedouble=usedouble)
252 out += mapped.gen_code()
253 output_file = os.path.join(output_path, 'gen-%s.c' % o)
254 with open(output_file, 'w') as f:
256 print("wrote %s" % output_file)
257 sources_list.append(output_file)
260 out += "#include \"aubio-generated.h\""
261 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
264 int generated_types_ready (void)
266 return ({pycheck_types});
268 """.format(pycheck_types=check_types)
270 add_types = "".join(["""
271 Py_INCREF (&Py_{name}Type);
272 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib])
275 void add_generated_objects ( PyObject *m )
279 """.format(add_types=add_types)
281 output_file = os.path.join(output_path, 'aubio-generated.c')
282 with open(output_file, 'w') as f:
284 print("wrote %s" % output_file)
285 sources_list.append(output_file)
287 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
288 out = """// generated list of objects created with gen_external.py
294 #ifndef HAVE_AUBIO_DOUBLE
295 #define HAVE_AUBIO_DOUBLE 1
300 int generated_objects ( void );
301 void add_generated_objects( PyObject *m );
302 """.format(objlist=objlist)
304 output_file = os.path.join(output_path, 'aubio-generated.h')
305 with open(output_file, 'w') as f:
307 print("wrote %s" % output_file)
308 # no need to add header to list of sources
310 return sorted(sources_list)
312 if __name__ == '__main__':
313 if len(sys.argv) > 1:
315 if len(sys.argv) > 2:
316 output_path = sys.argv[2]
317 generate_external(header, output_path)