source: python/lib/gen_external.py @ 19c3d75

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 19c3d75 was 19c3d75, checked in by Paul Brossier <piem@piem.org>, 4 years ago

python/lib/gen_external.py: improve compiler detection, fixes build on windows (#55)

  • Property mode set to 100644
File size: 7.5 KB
Line 
1import distutils.ccompiler
2import sys, os, subprocess, glob
3
4header = os.path.join('src', 'aubio.h')
5output_path = os.path.join('python', 'gen')
6
7source_header = """// this file is generated! do not modify
8#include "aubio-types.h"
9"""
10
11skip_objects = [
12  # already in ext/
13  'fft',
14  'pvoc',
15  'filter',
16  'filterbank',
17  #'resampler',
18  # AUBIO_UNSTABLE
19  'hist',
20  'parameter',
21  'scale',
22  'beattracking',
23  'resampler',
24  'peakpicker',
25  'pitchfcomb',
26  'pitchmcomb',
27  'pitchschmitt',
28  'pitchspecacf',
29  'pitchyin',
30  'pitchyinfft',
31  'sink',
32  'sink_apple_audio',
33  'sink_sndfile',
34  'sink_wavwrite',
35  #'mfcc',
36  'source',
37  'source_apple_audio',
38  'source_sndfile',
39  'source_avcodec',
40  'source_wavread',
41  #'sampler',
42  'audio_unit',
43
44  'tss',
45  ]
46
47def get_preprocessor():
48    # findout which compiler to use
49    from distutils.sysconfig import customize_compiler
50    compiler_name = distutils.ccompiler.get_default_compiler()
51    compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
52    try:
53        customize_compiler(compiler)
54    except AttributeError as e:
55        print("Warning: failed customizing compiler ({:s})".format(repr(e)))
56
57    if hasattr(compiler, 'initialize'):
58        try:
59            compiler.initialize()
60        except ValueError as e:
61            print("Warning: failed initializing compiler ({:s})".format(repr(e)))
62
63    cpp_cmd = None
64    if hasattr(compiler, 'preprocessor'): # for unixccompiler
65        cpp_cmd = compiler.preprocessor
66    elif hasattr(compiler, 'compiler'): # for ccompiler
67        cpp_cmd = compiler.compiler.split()
68        cpp_cmd += ['-E']
69    elif hasattr(compiler, 'cc'): # for msvccompiler
70        cpp_cmd = compiler.cc.split()
71        cpp_cmd += ['-E']
72
73    if not cpp_cmd:
74        print("Warning: could not guess preprocessor, using env's CC")
75        cpp_cmd = os.environ.get('CC', 'cc').split()
76        cpp_cmd += ['-E']
77
78    return cpp_cmd
79
80def get_cpp_objects(header=header):
81    cpp_cmd = get_preprocessor()
82
83    macros = [('AUBIO_UNSTABLE', 1)]
84
85    if not os.path.isfile(header):
86        raise Exception("could not find include file " + header)
87
88    includes = [os.path.dirname(header)]
89    cpp_cmd += distutils.ccompiler.gen_preprocess_options(macros, includes)
90    cpp_cmd += [header]
91
92    print("Running command: {:s}".format(" ".join(cpp_cmd)))
93    proc = subprocess.Popen(cpp_cmd,
94            stderr=subprocess.PIPE,
95            stdout=subprocess.PIPE)
96    assert proc, 'Proc was none'
97    cpp_output = proc.stdout.read()
98    err_output = proc.stderr.read()
99    if not cpp_output:
100        raise Exception("preprocessor output is empty:\n%s" % err_output)
101    elif err_output:
102        print ("Warning: preprocessor produced warnings:\n%s" % err_output)
103    if not isinstance(cpp_output, list):
104        cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
105
106    cpp_output = filter(lambda y: len(y) > 1, cpp_output)
107    cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
108
109    i = 1
110    while 1:
111        if i >= len(cpp_output): break
112        if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
113            cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
114            cpp_output.pop(i-1)
115        else:
116            i += 1
117
118    typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
119
120    cpp_objects = [a.split()[3][:-1] for a in typedefs]
121
122    return cpp_output, cpp_objects
123
124def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
125    if not os.path.isdir(output_path): os.mkdir(output_path)
126    elif not overwrite: return glob.glob(os.path.join(output_path, '*.c'))
127    sources_list = []
128    cpp_output, cpp_objects = get_cpp_objects(header)
129    lib = {}
130
131    for o in cpp_objects:
132        if o[:6] != 'aubio_':
133            continue
134        shortname = o[6:-2]
135        if shortname in skip_objects:
136            continue
137        lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
138        lib[shortname]['longname'] = o
139        lib[shortname]['shortname'] = shortname
140        for fn in cpp_output:
141            if o[:-1] in fn:
142                #print "found", o[:-1], "in", fn
143                if 'typedef struct ' in fn:
144                    lib[shortname]['struct'].append(fn)
145                elif '_do' in fn:
146                    lib[shortname]['do'].append(fn)
147                elif 'new_' in fn:
148                    lib[shortname]['new'].append(fn)
149                elif 'del_' in fn:
150                    lib[shortname]['del'].append(fn)
151                elif '_get_' in fn:
152                    lib[shortname]['get'].append(fn)
153                elif '_set_' in fn:
154                    lib[shortname]['set'].append(fn)
155                else:
156                    #print "no idea what to do about", fn
157                    lib[shortname]['other'].append(fn)
158
159    """
160    for fn in cpp_output:
161        found = 0
162        for o in lib:
163            for family in lib[o]:
164                if fn in lib[o][family]:
165                    found = 1
166        if found == 0:
167            print "missing", fn
168
169    for o in lib:
170        for family in lib[o]:
171            if type(lib[o][family]) == str:
172                print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
173            elif len(lib[o][family]) == 1:
174                print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
175            else:
176                print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
177    """
178
179    try:
180        from .gen_code import MappedObject
181    except (SystemError, ValueError):
182        from gen_code import MappedObject
183    for o in lib:
184        out = source_header
185        mapped = MappedObject(lib[o], usedouble = usedouble)
186        out += mapped.gen_code()
187        output_file = os.path.join(output_path, 'gen-%s.c' % o)
188        with open(output_file, 'w') as f:
189            f.write(out)
190            print ("wrote %s" % output_file )
191            sources_list.append(output_file)
192
193    out = source_header
194    out += "#include \"aubio-generated.h\""
195    check_types = "\n     ||  ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
196    out += """
197
198int generated_types_ready (void)
199{{
200  return ({pycheck_types});
201}}
202""".format(pycheck_types = check_types)
203
204    add_types = "".join(["""
205  Py_INCREF (&Py_{name}Type);
206  PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
207    out += """
208
209void add_generated_objects ( PyObject *m )
210{{
211{add_types}
212}}
213""".format(add_types = add_types)
214
215    output_file = os.path.join(output_path, 'aubio-generated.c')
216    with open(output_file, 'w') as f:
217        f.write(out)
218        print ("wrote %s" % output_file )
219        sources_list.append(output_file)
220
221    objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
222    out = """// generated list of objects created with gen_external.py
223
224#include <Python.h>
225"""
226    if usedouble:
227        out += """
228#ifndef HAVE_AUBIO_DOUBLE
229#define HAVE_AUBIO_DOUBLE 1
230#endif
231"""
232    out += """
233{objlist}
234int generated_objects ( void );
235void add_generated_objects( PyObject *m );
236""".format(objlist = objlist)
237
238    output_file = os.path.join(output_path, 'aubio-generated.h')
239    with open(output_file, 'w') as f:
240        f.write(out)
241        print ("wrote %s" % output_file )
242        # no need to add header to list of sources
243
244    return sources_list
245
246if __name__ == '__main__':
247    if len(sys.argv) > 1: header = sys.argv[1]
248    if len(sys.argv) > 2: output_path = sys.argv[2]
249    generate_external(header, output_path)
Note: See TracBrowser for help on using the repository browser.