def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True): if not os.path.isdir(output_path): os.mkdir(output_path) elif not overwrite: return glob.glob(os.path.join(output_path, "*.c")) sources_list = [] cpp_output, cpp_objects = get_cpp_objects(header) lib = {} for o in cpp_objects: if o[:6] != "aubio_": continue shortname = o[6:-2] if shortname in skip_objects: continue lib[shortname] = {"struct": [], "new": [], "del": [], "do": [], "get": [], "set": [], "other": []} lib[shortname]["longname"] = o lib[shortname]["shortname"] = shortname for fn in cpp_output: if o[:-1] in fn: # print "found", o[:-1], "in", fn if "typedef struct " in fn: lib[shortname]["struct"].append(fn) elif "_do" in fn: lib[shortname]["do"].append(fn) elif "new_" in fn: lib[shortname]["new"].append(fn) elif "del_" in fn: lib[shortname]["del"].append(fn) elif "_get_" in fn: lib[shortname]["get"].append(fn) elif "_set_" in fn: lib[shortname]["set"].append(fn) else: # print "no idea what to do about", fn lib[shortname]["other"].append(fn) """ for fn in cpp_output: found = 0 for o in lib: for family in lib[o]: if fn in lib[o][family]: found = 1 if found == 0: print "missing", fn for o in lib: for family in lib[o]: if type(lib[o][family]) == str: print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) ) elif len(lib[o][family]) == 1: print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) ) else: print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) ) """ try: from .gen_code import MappedObject except (SystemError, ValueError): from gen_code import MappedObject for o in lib: out = source_header mapped = MappedObject(lib[o], usedouble=usedouble) out += mapped.gen_code() output_file = os.path.join(output_path, "gen-%s.c" % o) with open(output_file, "w") as f: f.write(out) print("wrote %s" % output_file) sources_list.append(output_file) out = source_header out += '#include "aubio-generated.h"' check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib]) out += """ int generated_types_ready (void) {{ return ({pycheck_types}); }} """.format( pycheck_types=check_types ) add_types = "".join( [ """ Py_INCREF (&Py_{name}Type); PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format( name=o ) for o in lib ] ) out += """ void add_generated_objects ( PyObject *m ) {{ {add_types} }} """.format( add_types=add_types ) output_file = os.path.join(output_path, "aubio-generated.c") with open(output_file, "w") as f: f.write(out) print("wrote %s" % output_file) sources_list.append(output_file) objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib]) out = """// generated list of objects created with gen_external.py #include <Python.h> """ if usedouble: out += """ #ifndef HAVE_AUBIO_DOUBLE #define HAVE_AUBIO_DOUBLE 1 #endif """ out += """ {objlist} int generated_objects ( void ); void add_generated_objects( PyObject *m ); """.format( objlist=objlist ) output_file = os.path.join(output_path, "aubio-generated.h") with open(output_file, "w") as f: f.write(out) print("wrote %s" % output_file) # no need to add header to list of sources return sources_list
def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True): if not os.path.isdir(output_path): os.mkdir(output_path) elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c'))) c_declarations = get_c_declarations(header, usedouble=usedouble) cpp_objects = get_cpp_objects_from_c_declarations(c_declarations) lib = generate_lib_from_c_declarations(cpp_objects, c_declarations) # print_c_declarations_results(lib, c_declarations) sources_list = [] for o in lib: out = source_header mapped = MappedObject(lib[o], usedouble=usedouble) out += mapped.gen_code() output_file = os.path.join(output_path, 'gen-%s.c' % o) with open(output_file, 'w') as f: f.write(out) print("wrote %s" % output_file) sources_list.append(output_file) out = source_header out += "#include \"aubio-generated.h\"" check_types = "\n || ".join( ["PyType_Ready(&Py_%sType) < 0" % o for o in lib]) out += """ int generated_types_ready (void) {{ return ({pycheck_types}); }} """.format(pycheck_types=check_types) add_types = "".join([ """ Py_INCREF (&Py_{name}Type); PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format( name=o) for o in lib ]) out += """ void add_generated_objects ( PyObject *m ) {{ {add_types} }} """.format(add_types=add_types) output_file = os.path.join(output_path, 'aubio-generated.c') with open(output_file, 'w') as f: f.write(out) print("wrote %s" % output_file) sources_list.append(output_file) objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib]) out = """// generated list of objects created with gen_external.py #include <Python.h> """ if usedouble: out += """ #ifndef HAVE_AUBIO_DOUBLE #define HAVE_AUBIO_DOUBLE 1 #endif """ out += """ {objlist} int generated_objects ( void ); void add_generated_objects( PyObject *m ); """.format(objlist=objlist) output_file = os.path.join(output_path, 'aubio-generated.h') with open(output_file, 'w') as f: f.write(out) print("wrote %s" % output_file) # no need to add header to list of sources return sorted(sources_list)
def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True): if not os.path.isdir(output_path): os.mkdir(output_path) elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c'))) cpp_output, cpp_objects = get_cpp_objects(header, usedouble=usedouble) lib = analyze_cpp_output(cpp_objects, cpp_output) # print_cpp_output_results(lib, cpp_output) sources_list = [] try: from .gen_code import MappedObject except (SystemError, ValueError): from gen_code import MappedObject for o in lib: out = source_header mapped = MappedObject(lib[o], usedouble = usedouble) out += mapped.gen_code() output_file = os.path.join(output_path, 'gen-%s.c' % o) with open(output_file, 'w') as f: f.write(out) print ("wrote %s" % output_file ) sources_list.append(output_file) out = source_header out += "#include \"aubio-generated.h\"" check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib]) out += """ int generated_types_ready (void) {{ return ({pycheck_types}); }} """.format(pycheck_types = check_types) add_types = "".join([""" Py_INCREF (&Py_{name}Type); PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib]) out += """ void add_generated_objects ( PyObject *m ) {{ {add_types} }} """.format(add_types = add_types) output_file = os.path.join(output_path, 'aubio-generated.c') with open(output_file, 'w') as f: f.write(out) print ("wrote %s" % output_file ) sources_list.append(output_file) objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib]) out = """// generated list of objects created with gen_external.py #include <Python.h> """ if usedouble: out += """ #ifndef HAVE_AUBIO_DOUBLE #define HAVE_AUBIO_DOUBLE 1 #endif """ out += """ {objlist} int generated_objects ( void ); void add_generated_objects( PyObject *m ); """.format(objlist = objlist) output_file = os.path.join(output_path, 'aubio-generated.h') with open(output_file, 'w') as f: f.write(out) print ("wrote %s" % output_file ) # no need to add header to list of sources return sorted(sources_list)