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 assert proc, 'Proc was none'
125 cpp_output = proc.stdout.read()
126 err_output = proc.stderr.read()
128 print("Warning: preprocessor produced errors or warnings:\n%s" \
129 % err_output.decode('utf8'))
131 raise_msg = "preprocessor output is empty! Running command " \
132 + "\"%s\" failed" % " ".join(cpp_cmd)
134 raise_msg += " with stderr: \"%s\"" % err_output.decode('utf8')
136 raise_msg += " with no stdout or stderr"
137 raise Exception(raise_msg)
138 if not isinstance(cpp_output, list):
139 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
143 def filter_cpp_output(cpp_raw_output):
144 ''' prepare cpp-output for parsing '''
145 cpp_output = filter(lambda y: len(y) > 1, cpp_raw_output)
146 cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
150 if i >= len(cpp_output):
152 if ('{' in cpp_output[i - 1]) and ('}' not in cpp_output[i - 1]) or (';' not in cpp_output[i - 1]):
153 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
154 cpp_output.pop(i - 1)
155 elif ('}' in cpp_output[i]):
156 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
157 cpp_output.pop(i - 1)
161 # clean pointer notations
164 tmp += [l.replace(' *', ' * ')]
170 def get_cpp_objects_from_c_declarations(c_declarations, skip_objects=None):
171 if skip_objects is None:
172 skip_objects = default_skip_objects
173 typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations)
174 cpp_objects = [a.split()[3][:-1] for a in typedefs]
175 cpp_objects_filtered = filter(lambda y: not y[6:-2] in skip_objects, cpp_objects)
176 return cpp_objects_filtered
179 def get_all_func_names_from_lib(lib):
180 ''' return flat string of all function used in lib
183 for _, v in lib.items():
184 if isinstance(v, dict):
185 res += get_all_func_names_from_lib(v)
186 elif isinstance(v, list):
190 continue # not a function
191 fname_part = e[0].strip().split(' ')
192 fname = fname_part[-1]
196 raise NameError('gen_lib : weird function: ' + str(e))
201 def generate_lib_from_c_declarations(cpp_objects, c_declarations):
202 ''' returns a lib from given cpp_object names
204 a lib is a dict grouping functions by family (onset,pitch...)
205 each eement is itself a dict of functions grouped by puposes as :
206 struct, new, del, do, get, set and other
210 for o in cpp_objects:
212 if o[:6] == 'aubio_':
213 shortname = o[6:-2] # without aubio_ prefix and _t suffix
215 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'rdo': [], 'get': [], 'set': [], 'other': []}
216 lib[shortname]['longname'] = o
217 lib[shortname]['shortname'] = shortname
219 fullshortname = o[:-2] # name without _t suffix
221 for fn in c_declarations:
222 func_name = fn.split('(')[0].strip().split(' ')[-1]
223 if func_name.startswith(fullshortname + '_') or func_name.endswith(fullshortname):
224 # print "found", shortname, "in", fn
225 if 'typedef struct ' in fn:
226 lib[shortname]['struct'].append(fn)
228 lib[shortname]['do'].append(fn)
230 lib[shortname]['rdo'].append(fn)
232 lib[shortname]['new'].append(fn)
234 lib[shortname]['del'].append(fn)
236 lib[shortname]['get'].append(fn)
238 lib[shortname]['set'].append(fn)
240 # print "no idea what to do about", fn
241 lib[shortname]['other'].append(fn)
245 def print_c_declarations_results(lib, c_declarations):
246 for fn in c_declarations:
249 for family in lib[o]:
250 if fn in lib[o][family]:
256 for family in lib[o]:
257 if type(lib[o][family]) == str:
258 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
259 elif len(lib[o][family]) == 1:
260 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0]))
262 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
265 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
266 if not os.path.isdir(output_path):
267 os.mkdir(output_path)
269 return sorted(glob.glob(os.path.join(output_path, '*.c')))
271 c_declarations = get_c_declarations(header, usedouble=usedouble)
272 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)
274 lib = generate_lib_from_c_declarations(cpp_objects, c_declarations)
275 # print_c_declarations_results(lib, c_declarations)
280 mapped = MappedObject(lib[o], usedouble=usedouble)
281 out += mapped.gen_code()
282 output_file = os.path.join(output_path, 'gen-%s.c' % o)
283 with open(output_file, 'w') as f:
285 print("wrote %s" % output_file)
286 sources_list.append(output_file)
289 out += "#include \"aubio-generated.h\""
290 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
293 int generated_types_ready (void)
295 return ({pycheck_types});
297 """.format(pycheck_types=check_types)
299 add_types = "".join(["""
300 Py_INCREF (&Py_{name}Type);
301 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib])
304 void add_generated_objects ( PyObject *m )
308 """.format(add_types=add_types)
310 output_file = os.path.join(output_path, 'aubio-generated.c')
311 with open(output_file, 'w') as f:
313 print("wrote %s" % output_file)
314 sources_list.append(output_file)
316 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
317 out = """// generated list of objects created with gen_external.py
323 #ifndef HAVE_AUBIO_DOUBLE
324 #define HAVE_AUBIO_DOUBLE 1
329 int generated_objects ( void );
330 void add_generated_objects( PyObject *m );
331 """.format(objlist=objlist)
333 output_file = os.path.join(output_path, 'aubio-generated.h')
334 with open(output_file, 'w') as f:
336 print("wrote %s" % output_file)
337 # no need to add header to list of sources
339 return sorted(sources_list)
341 if __name__ == '__main__':
342 if len(sys.argv) > 1:
344 if len(sys.argv) > 2:
345 output_path = sys.argv[2]
346 generate_external(header, output_path)