1 import distutils.ccompiler
6 from distutils.sysconfig import customize_compiler
7 from gen_code import MappedObject
9 header = os.path.join('src', 'aubio.h')
10 output_path = os.path.join('python', 'gen')
12 source_header = """// this file is generated! do not modify
13 #include "aubio-types.h"
16 default_skip_objects = [
52 def get_preprocessor():
53 # findout which compiler to use
54 compiler_name = distutils.ccompiler.get_default_compiler()
55 compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
57 customize_compiler(compiler)
58 except AttributeError as e:
59 print("Warning: failed customizing compiler ({:s})".format(repr(e)))
61 if hasattr(compiler, 'initialize'):
64 except ValueError as e:
65 print("Warning: failed initializing compiler ({:s})".format(repr(e)))
68 if hasattr(compiler, 'preprocessor'): # for unixccompiler
69 cpp_cmd = compiler.preprocessor
70 elif hasattr(compiler, 'compiler'): # for ccompiler
71 cpp_cmd = compiler.compiler.split()
73 elif hasattr(compiler, 'cc'): # for msvccompiler
74 cpp_cmd = compiler.cc.split()
77 # On win-amd64 (py3.x), the default compiler is cross-compiling, from x86
78 # to amd64 with %WIN_SDK_ROOT%\x86_amd64\cl.exe, but using this binary as a
79 # pre-processor generates no output, so we use %WIN_SDK_ROOT%\cl.exe
81 if len(cpp_cmd) > 1 and 'cl.exe' in cpp_cmd[-2]:
82 plat = os.path.basename(os.path.dirname(cpp_cmd[-2]))
83 if plat == 'x86_amd64':
84 print('workaround on win64 to avoid empty pre-processor output')
85 cpp_cmd[-2] = cpp_cmd[-2].replace('x86_amd64', '')
86 elif True in ['amd64' in f for f in cpp_cmd]:
87 print('warning: not using workaround for', cpp_cmd[0], plat)
90 print("Warning: could not guess preprocessor, using env's CC")
91 cpp_cmd = os.environ.get('CC', 'cc').split()
94 cpp_cmd += ['-x', 'c'] # emcc defaults to c++, force C language
98 def get_c_declarations(header=header, usedouble=False):
99 ''' return a dense and preprocessed string of all c declarations implied by aubio.h
101 cpp_output = get_cpp_output(header=header, usedouble=usedouble)
102 return filter_cpp_output (cpp_output)
105 def get_cpp_output(header=header, usedouble=False):
106 ''' find and run a C pre-processor on aubio.h '''
107 cpp_cmd = get_preprocessor()
109 macros = [('AUBIO_UNSTABLE', 1)]
111 macros += [('HAVE_AUBIO_DOUBLE', 1)]
113 if not os.path.isfile(header):
114 raise Exception("could not find include file " + header)
116 includes = [os.path.dirname(header)]
117 cpp_cmd += distutils.ccompiler.gen_preprocess_options(macros, includes)
120 print("Running command: {:s}".format(" ".join(cpp_cmd)))
121 proc = subprocess.Popen(cpp_cmd,
122 stderr=subprocess.PIPE,
123 stdout=subprocess.PIPE,
124 universal_newlines=True)
125 assert proc, 'Proc was none'
126 cpp_output = proc.stdout.read()
127 err_output = proc.stderr.read()
129 print("Warning: preprocessor produced errors or warnings:\n%s" \
132 raise_msg = "preprocessor output is empty! Running command " \
133 + "\"%s\" failed" % " ".join(cpp_cmd)
135 raise_msg += " with stderr: \"%s\"" % err_output
137 raise_msg += " with no stdout or stderr"
138 raise Exception(raise_msg)
139 if not isinstance(cpp_output, list):
140 cpp_output = [l.strip() for l in cpp_output.split('\n')]
144 def filter_cpp_output(cpp_raw_output):
145 ''' prepare cpp-output for parsing '''
146 cpp_output = filter(lambda y: len(y) > 1, cpp_raw_output)
147 cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
151 if i >= len(cpp_output):
153 if ('{' in cpp_output[i - 1]) and ('}' not in cpp_output[i - 1]) or (';' not in cpp_output[i - 1]):
154 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
155 cpp_output.pop(i - 1)
156 elif ('}' in cpp_output[i]):
157 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
158 cpp_output.pop(i - 1)
162 # clean pointer notations
165 tmp += [l.replace(' *', ' * ')]
171 def get_cpp_objects_from_c_declarations(c_declarations, skip_objects=None):
172 if skip_objects is None:
173 skip_objects = default_skip_objects
174 typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations)
175 cpp_objects = [a.split()[3][:-1] for a in typedefs]
176 cpp_objects_filtered = filter(lambda y: not y[6:-2] in skip_objects, cpp_objects)
177 return cpp_objects_filtered
180 def get_all_func_names_from_lib(lib):
181 ''' return flat string of all function used in lib
184 for _, v in lib.items():
185 if isinstance(v, dict):
186 res += get_all_func_names_from_lib(v)
187 elif isinstance(v, list):
191 continue # not a function
192 fname_part = e[0].strip().split(' ')
193 fname = fname_part[-1]
197 raise NameError('gen_lib : weird function: ' + str(e))
202 def generate_lib_from_c_declarations(cpp_objects, c_declarations):
203 ''' returns a lib from given cpp_object names
205 a lib is a dict grouping functions by family (onset,pitch...)
206 each eement is itself a dict of functions grouped by puposes as :
207 struct, new, del, do, get, set and other
211 for o in cpp_objects:
213 if o[:6] == 'aubio_':
214 shortname = o[6:-2] # without aubio_ prefix and _t suffix
216 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'rdo': [], 'get': [], 'set': [], 'other': []}
217 lib[shortname]['longname'] = o
218 lib[shortname]['shortname'] = shortname
220 fullshortname = o[:-2] # name without _t suffix
222 for fn in c_declarations:
223 func_name = fn.split('(')[0].strip().split(' ')[-1]
224 if func_name.startswith(fullshortname + '_') or func_name.endswith(fullshortname):
225 # print "found", shortname, "in", fn
226 if 'typedef struct ' in fn:
227 lib[shortname]['struct'].append(fn)
229 lib[shortname]['do'].append(fn)
231 lib[shortname]['rdo'].append(fn)
233 lib[shortname]['new'].append(fn)
235 lib[shortname]['del'].append(fn)
237 lib[shortname]['get'].append(fn)
239 lib[shortname]['set'].append(fn)
241 # print "no idea what to do about", fn
242 lib[shortname]['other'].append(fn)
246 def print_c_declarations_results(lib, c_declarations):
247 for fn in c_declarations:
250 for family in lib[o]:
251 if fn in lib[o][family]:
257 for family in lib[o]:
258 if type(lib[o][family]) == str:
259 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
260 elif len(lib[o][family]) == 1:
261 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0]))
263 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
266 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
267 if not os.path.isdir(output_path):
268 os.mkdir(output_path)
270 return sorted(glob.glob(os.path.join(output_path, '*.c')))
272 c_declarations = get_c_declarations(header, usedouble=usedouble)
273 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)
275 lib = generate_lib_from_c_declarations(cpp_objects, c_declarations)
276 # print_c_declarations_results(lib, c_declarations)
281 mapped = MappedObject(lib[o], usedouble=usedouble)
282 out += mapped.gen_code()
283 output_file = os.path.join(output_path, 'gen-%s.c' % o)
284 with open(output_file, 'w') as f:
286 print("wrote %s" % output_file)
287 sources_list.append(output_file)
290 out += "#include \"aubio-generated.h\""
291 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
294 int generated_types_ready (void)
296 return ({pycheck_types});
298 """.format(pycheck_types=check_types)
300 add_types = "".join(["""
301 Py_INCREF (&Py_{name}Type);
302 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib])
305 void add_generated_objects ( PyObject *m )
309 """.format(add_types=add_types)
311 output_file = os.path.join(output_path, 'aubio-generated.c')
312 with open(output_file, 'w') as f:
314 print("wrote %s" % output_file)
315 sources_list.append(output_file)
317 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
318 out = """// generated list of objects created with gen_external.py
324 #ifndef HAVE_AUBIO_DOUBLE
325 #define HAVE_AUBIO_DOUBLE 1
330 int generated_objects ( void );
331 void add_generated_objects( PyObject *m );
332 """.format(objlist=objlist)
334 output_file = os.path.join(output_path, 'aubio-generated.h')
335 with open(output_file, 'w') as f:
337 print("wrote %s" % output_file)
338 # no need to add header to list of sources
340 return sorted(sources_list)
342 if __name__ == '__main__':
343 if len(sys.argv) > 1:
345 if len(sys.argv) > 2:
346 output_path = sys.argv[2]
347 generate_external(header, output_path)