1 import distutils.ccompiler
2 import sys, os, subprocess, glob
4 header = os.path.join('src', 'aubio.h')
5 output_path = os.path.join('python', 'gen')
7 source_header = """// this file is generated! do not modify
8 #include "aubio-types.h"
46 def get_preprocessor():
47 # findout which compiler to use
48 from distutils.sysconfig import customize_compiler
49 compiler_name = distutils.ccompiler.get_default_compiler()
50 compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
52 customize_compiler(compiler)
53 except AttributeError as e:
54 print("Warning: failed customizing compiler ({:s})".format(repr(e)))
56 if hasattr(compiler, 'initialize'):
59 except ValueError as e:
60 print("Warning: failed initializing compiler ({:s})".format(repr(e)))
63 if hasattr(compiler, 'preprocessor'): # for unixccompiler
64 cpp_cmd = compiler.preprocessor
65 elif hasattr(compiler, 'compiler'): # for ccompiler
66 cpp_cmd = compiler.compiler.split()
68 elif hasattr(compiler, 'cc'): # for msvccompiler
69 cpp_cmd = compiler.cc.split()
73 print("Warning: could not guess preprocessor, using env's CC")
74 cpp_cmd = os.environ.get('CC', 'cc').split()
79 def get_cpp_objects(header=header, usedouble=False):
80 cpp_cmd = get_preprocessor()
82 macros = [('AUBIO_UNSTABLE', 1)]
84 macros += [('HAVE_AUBIO_DOUBLE', 1)]
86 if not os.path.isfile(header):
87 raise Exception("could not find include file " + header)
89 includes = [os.path.dirname(header)]
90 cpp_cmd += distutils.ccompiler.gen_preprocess_options(macros, includes)
93 print("Running command: {:s}".format(" ".join(cpp_cmd)))
94 proc = subprocess.Popen(cpp_cmd,
95 stderr=subprocess.PIPE,
96 stdout=subprocess.PIPE)
97 assert proc, 'Proc was none'
98 cpp_output = proc.stdout.read()
99 err_output = proc.stderr.read()
101 raise Exception("preprocessor output is empty:\n%s" % err_output)
103 print ("Warning: preprocessor produced warnings:\n%s" % err_output)
104 if not isinstance(cpp_output, list):
105 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
107 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
108 cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
112 if i >= len(cpp_output): break
113 if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
114 cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
119 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
121 cpp_objects = [a.split()[3][:-1] for a in typedefs]
123 return cpp_output, cpp_objects
126 def analyze_cpp_output(cpp_objects, cpp_output):
129 for o in cpp_objects:
130 if o[:6] != 'aubio_':
133 if shortname in skip_objects:
135 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
136 lib[shortname]['longname'] = o
137 lib[shortname]['shortname'] = shortname
138 for fn in cpp_output:
140 #print "found", o[:-1], "in", fn
141 if 'typedef struct ' in fn:
142 lib[shortname]['struct'].append(fn)
144 lib[shortname]['do'].append(fn)
146 lib[shortname]['new'].append(fn)
148 lib[shortname]['del'].append(fn)
150 lib[shortname]['get'].append(fn)
152 lib[shortname]['set'].append(fn)
154 #print "no idea what to do about", fn
155 lib[shortname]['other'].append(fn)
158 def print_cpp_output_results(lib, cpp_output):
159 for fn in cpp_output:
162 for family in lib[o]:
163 if fn in lib[o][family]:
166 print ("missing", fn)
169 for family in lib[o]:
170 if type(lib[o][family]) == str:
171 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
172 elif len(lib[o][family]) == 1:
173 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
175 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
178 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
179 if not os.path.isdir(output_path): os.mkdir(output_path)
180 elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c')))
182 cpp_output, cpp_objects = get_cpp_objects(header, usedouble=usedouble)
184 lib = analyze_cpp_output(cpp_objects, cpp_output)
185 # print_cpp_output_results(lib, cpp_output)
189 from .gen_code import MappedObject
190 except (SystemError, ValueError):
191 from gen_code import MappedObject
194 mapped = MappedObject(lib[o], usedouble = usedouble)
195 out += mapped.gen_code()
196 output_file = os.path.join(output_path, 'gen-%s.c' % o)
197 with open(output_file, 'w') as f:
199 print ("wrote %s" % output_file )
200 sources_list.append(output_file)
203 out += "#include \"aubio-generated.h\""
204 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
207 int generated_types_ready (void)
209 return ({pycheck_types});
211 """.format(pycheck_types = check_types)
213 add_types = "".join(["""
214 Py_INCREF (&Py_{name}Type);
215 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
218 void add_generated_objects ( PyObject *m )
222 """.format(add_types = add_types)
224 output_file = os.path.join(output_path, 'aubio-generated.c')
225 with open(output_file, 'w') as f:
227 print ("wrote %s" % output_file )
228 sources_list.append(output_file)
230 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
231 out = """// generated list of objects created with gen_external.py
237 #ifndef HAVE_AUBIO_DOUBLE
238 #define HAVE_AUBIO_DOUBLE 1
243 int generated_objects ( void );
244 void add_generated_objects( PyObject *m );
245 """.format(objlist = objlist)
247 output_file = os.path.join(output_path, 'aubio-generated.h')
248 with open(output_file, 'w') as f:
250 print ("wrote %s" % output_file )
251 # no need to add header to list of sources
253 return sorted(sources_list)
255 if __name__ == '__main__':
256 if len(sys.argv) > 1: header = sys.argv[1]
257 if len(sys.argv) > 2: output_path = sys.argv[2]
258 generate_external(header, output_path)