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