[19c3d75] | 1 | import distutils.ccompiler |
---|
[541ea280] | 2 | import sys |
---|
| 3 | import os |
---|
| 4 | import subprocess |
---|
| 5 | import glob |
---|
[ccb9fb5] | 6 | |
---|
[89b04e8] | 7 | header = os.path.join('src', 'aubio.h') |
---|
| 8 | output_path = os.path.join('python', 'gen') |
---|
[1167631] | 9 | |
---|
| 10 | source_header = """// this file is generated! do not modify |
---|
[ccb9fb5] | 11 | #include "aubio-types.h" |
---|
| 12 | """ |
---|
| 13 | |
---|
[51ca615] | 14 | default_skip_objects = [ |
---|
[541ea280] | 15 | # already in ext/ |
---|
| 16 | 'fft', |
---|
| 17 | 'pvoc', |
---|
| 18 | 'filter', |
---|
| 19 | 'filterbank', |
---|
| 20 | # AUBIO_UNSTABLE |
---|
| 21 | 'hist', |
---|
| 22 | 'parameter', |
---|
| 23 | 'scale', |
---|
| 24 | 'beattracking', |
---|
| 25 | 'resampler', |
---|
| 26 | 'peakpicker', |
---|
| 27 | 'pitchfcomb', |
---|
| 28 | 'pitchmcomb', |
---|
| 29 | 'pitchschmitt', |
---|
| 30 | 'pitchspecacf', |
---|
| 31 | 'pitchyin', |
---|
| 32 | 'pitchyinfft', |
---|
[9fa0ed1] | 33 | 'pitchyinfast', |
---|
[541ea280] | 34 | 'sink', |
---|
| 35 | 'sink_apple_audio', |
---|
| 36 | 'sink_sndfile', |
---|
| 37 | 'sink_wavwrite', |
---|
| 38 | #'mfcc', |
---|
| 39 | 'source', |
---|
| 40 | 'source_apple_audio', |
---|
| 41 | 'source_sndfile', |
---|
| 42 | 'source_avcodec', |
---|
| 43 | 'source_wavread', |
---|
| 44 | #'sampler', |
---|
| 45 | 'audio_unit', |
---|
| 46 | 'spectral_whitening', |
---|
| 47 | ] |
---|
| 48 | |
---|
[ccb9fb5] | 49 | |
---|
[19c3d75] | 50 | def get_preprocessor(): |
---|
| 51 | # findout which compiler to use |
---|
| 52 | from distutils.sysconfig import customize_compiler |
---|
| 53 | compiler_name = distutils.ccompiler.get_default_compiler() |
---|
| 54 | compiler = distutils.ccompiler.new_compiler(compiler=compiler_name) |
---|
| 55 | try: |
---|
| 56 | customize_compiler(compiler) |
---|
| 57 | except AttributeError as e: |
---|
| 58 | print("Warning: failed customizing compiler ({:s})".format(repr(e))) |
---|
| 59 | |
---|
| 60 | if hasattr(compiler, 'initialize'): |
---|
| 61 | try: |
---|
| 62 | compiler.initialize() |
---|
| 63 | except ValueError as e: |
---|
| 64 | print("Warning: failed initializing compiler ({:s})".format(repr(e))) |
---|
| 65 | |
---|
| 66 | cpp_cmd = None |
---|
[541ea280] | 67 | if hasattr(compiler, 'preprocessor'): # for unixccompiler |
---|
[19c3d75] | 68 | cpp_cmd = compiler.preprocessor |
---|
[541ea280] | 69 | elif hasattr(compiler, 'compiler'): # for ccompiler |
---|
[19c3d75] | 70 | cpp_cmd = compiler.compiler.split() |
---|
| 71 | cpp_cmd += ['-E'] |
---|
[541ea280] | 72 | elif hasattr(compiler, 'cc'): # for msvccompiler |
---|
[19c3d75] | 73 | cpp_cmd = compiler.cc.split() |
---|
| 74 | cpp_cmd += ['-E'] |
---|
| 75 | |
---|
| 76 | if not cpp_cmd: |
---|
| 77 | print("Warning: could not guess preprocessor, using env's CC") |
---|
| 78 | cpp_cmd = os.environ.get('CC', 'cc').split() |
---|
| 79 | cpp_cmd += ['-E'] |
---|
[dbdb48a] | 80 | if 'emcc' in cpp_cmd: |
---|
| 81 | cpp_cmd += ['-x', 'c'] # emcc defaults to c++, force C language |
---|
[19c3d75] | 82 | return cpp_cmd |
---|
[ccb9fb5] | 83 | |
---|
[150ec2d] | 84 | |
---|
| 85 | def get_c_declarations(header=header, usedouble=False): |
---|
| 86 | ''' return a dense and preprocessed string of all c declarations implied by aubio.h |
---|
| 87 | ''' |
---|
[19c3d75] | 88 | cpp_cmd = get_preprocessor() |
---|
| 89 | |
---|
| 90 | macros = [('AUBIO_UNSTABLE', 1)] |
---|
[3d14829] | 91 | if usedouble: |
---|
| 92 | macros += [('HAVE_AUBIO_DOUBLE', 1)] |
---|
[19c3d75] | 93 | |
---|
| 94 | if not os.path.isfile(header): |
---|
| 95 | raise Exception("could not find include file " + header) |
---|
| 96 | |
---|
| 97 | includes = [os.path.dirname(header)] |
---|
| 98 | cpp_cmd += distutils.ccompiler.gen_preprocess_options(macros, includes) |
---|
| 99 | cpp_cmd += [header] |
---|
| 100 | |
---|
| 101 | print("Running command: {:s}".format(" ".join(cpp_cmd))) |
---|
| 102 | proc = subprocess.Popen(cpp_cmd, |
---|
[541ea280] | 103 | stderr=subprocess.PIPE, |
---|
| 104 | stdout=subprocess.PIPE) |
---|
[19c3d75] | 105 | assert proc, 'Proc was none' |
---|
| 106 | cpp_output = proc.stdout.read() |
---|
| 107 | err_output = proc.stderr.read() |
---|
| 108 | if not cpp_output: |
---|
| 109 | raise Exception("preprocessor output is empty:\n%s" % err_output) |
---|
| 110 | elif err_output: |
---|
[541ea280] | 111 | print("Warning: preprocessor produced warnings:\n%s" % err_output) |
---|
[19c3d75] | 112 | if not isinstance(cpp_output, list): |
---|
| 113 | cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')] |
---|
[ccb9fb5] | 114 | |
---|
| 115 | cpp_output = filter(lambda y: len(y) > 1, cpp_output) |
---|
[19c3d75] | 116 | cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output)) |
---|
[ccb9fb5] | 117 | |
---|
| 118 | i = 1 |
---|
| 119 | while 1: |
---|
[541ea280] | 120 | if i >= len(cpp_output): |
---|
| 121 | break |
---|
[50853b0] | 122 | if ('{' in cpp_output[i - 1]) and ('}' not in cpp_output[i - 1]) or (';' not in cpp_output[i - 1]): |
---|
[541ea280] | 123 | cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i] |
---|
| 124 | cpp_output.pop(i - 1) |
---|
[15a43e0] | 125 | elif ('}' in cpp_output[i]): |
---|
| 126 | cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i] |
---|
| 127 | cpp_output.pop(i - 1) |
---|
[ccb9fb5] | 128 | else: |
---|
| 129 | i += 1 |
---|
| 130 | |
---|
[eea6101] | 131 | # clean pointer notations |
---|
| 132 | tmp = [] |
---|
| 133 | for l in cpp_output: |
---|
[2f5f1e4] | 134 | tmp += [l.replace(' *', ' * ')] |
---|
[541ea280] | 135 | cpp_output = tmp |
---|
[ccb9fb5] | 136 | |
---|
[150ec2d] | 137 | return cpp_output |
---|
[ccb9fb5] | 138 | |
---|
[541ea280] | 139 | |
---|
[dad51ce] | 140 | def get_cpp_objects_from_c_declarations(c_declarations, skip_objects=None): |
---|
[50853b0] | 141 | if skip_objects is None: |
---|
[51ca615] | 142 | skip_objects = default_skip_objects |
---|
[541ea280] | 143 | typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations) |
---|
[ccb9fb5] | 144 | cpp_objects = [a.split()[3][:-1] for a in typedefs] |
---|
[51ca615] | 145 | cpp_objects_filtered = filter(lambda y: not y[6:-2] in skip_objects, cpp_objects) |
---|
| 146 | return cpp_objects_filtered |
---|
[ccb9fb5] | 147 | |
---|
[6cbf34b] | 148 | |
---|
[50853b0] | 149 | def get_all_func_names_from_lib(lib): |
---|
[6cbf34b] | 150 | ''' return flat string of all function used in lib |
---|
| 151 | ''' |
---|
| 152 | res = [] |
---|
[50853b0] | 153 | for _, v in lib.items(): |
---|
[6cbf34b] | 154 | if isinstance(v, dict): |
---|
[50853b0] | 155 | res += get_all_func_names_from_lib(v) |
---|
[6cbf34b] | 156 | elif isinstance(v, list): |
---|
| 157 | for elem in v: |
---|
| 158 | e = elem.split('(') |
---|
| 159 | if len(e) < 2: |
---|
| 160 | continue # not a function |
---|
| 161 | fname_part = e[0].strip().split(' ') |
---|
| 162 | fname = fname_part[-1] |
---|
| 163 | if fname: |
---|
| 164 | res += [fname] |
---|
| 165 | else: |
---|
| 166 | raise NameError('gen_lib : weird function: ' + str(e)) |
---|
[ccb9fb5] | 167 | |
---|
[6cbf34b] | 168 | return res |
---|
[ccb9fb5] | 169 | |
---|
[41fc24f] | 170 | |
---|
[6cbf34b] | 171 | def generate_lib_from_c_declarations(cpp_objects, c_declarations): |
---|
| 172 | ''' returns a lib from given cpp_object names |
---|
| 173 | |
---|
| 174 | a lib is a dict grouping functions by family (onset,pitch...) |
---|
| 175 | each eement is itself a dict of functions grouped by puposes as : |
---|
| 176 | struct, new, del, do, get, set and other |
---|
| 177 | ''' |
---|
[ccb9fb5] | 178 | lib = {} |
---|
| 179 | |
---|
| 180 | for o in cpp_objects: |
---|
[0b2643b] | 181 | shortname = o |
---|
[5674833] | 182 | if o[:6] == 'aubio_': |
---|
[0b2643b] | 183 | shortname = o[6:-2] # without aubio_ prefix and _t suffix |
---|
[541ea280] | 184 | |
---|
[c96e6c0] | 185 | lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'rdo': [], 'get': [], 'set': [], 'other': []} |
---|
[ccb9fb5] | 186 | lib[shortname]['longname'] = o |
---|
| 187 | lib[shortname]['shortname'] = shortname |
---|
[5674833] | 188 | |
---|
[0b2643b] | 189 | fullshortname = o[:-2] # name without _t suffix |
---|
| 190 | |
---|
[150ec2d] | 191 | for fn in c_declarations: |
---|
[0b2643b] | 192 | func_name = fn.split('(')[0].strip().split(' ')[-1] |
---|
| 193 | if func_name.startswith(fullshortname + '_') or func_name.endswith(fullshortname): |
---|
[5674833] | 194 | # print "found", shortname, "in", fn |
---|
[ccb9fb5] | 195 | if 'typedef struct ' in fn: |
---|
| 196 | lib[shortname]['struct'].append(fn) |
---|
| 197 | elif '_do' in fn: |
---|
| 198 | lib[shortname]['do'].append(fn) |
---|
[c96e6c0] | 199 | elif '_rdo' in fn: |
---|
| 200 | lib[shortname]['rdo'].append(fn) |
---|
[ccb9fb5] | 201 | elif 'new_' in fn: |
---|
| 202 | lib[shortname]['new'].append(fn) |
---|
| 203 | elif 'del_' in fn: |
---|
| 204 | lib[shortname]['del'].append(fn) |
---|
| 205 | elif '_get_' in fn: |
---|
| 206 | lib[shortname]['get'].append(fn) |
---|
| 207 | elif '_set_' in fn: |
---|
| 208 | lib[shortname]['set'].append(fn) |
---|
| 209 | else: |
---|
[541ea280] | 210 | # print "no idea what to do about", fn |
---|
[ccb9fb5] | 211 | lib[shortname]['other'].append(fn) |
---|
[41fc24f] | 212 | return lib |
---|
[ccb9fb5] | 213 | |
---|
[541ea280] | 214 | |
---|
[150ec2d] | 215 | def print_c_declarations_results(lib, c_declarations): |
---|
| 216 | for fn in c_declarations: |
---|
[ccb9fb5] | 217 | found = 0 |
---|
| 218 | for o in lib: |
---|
| 219 | for family in lib[o]: |
---|
| 220 | if fn in lib[o][family]: |
---|
| 221 | found = 1 |
---|
| 222 | if found == 0: |
---|
[541ea280] | 223 | print("missing", fn) |
---|
[ccb9fb5] | 224 | |
---|
| 225 | for o in lib: |
---|
| 226 | for family in lib[o]: |
---|
| 227 | if type(lib[o][family]) == str: |
---|
[541ea280] | 228 | print("{:15s} {:10s} {:s}".format(o, family, lib[o][family])) |
---|
[ccb9fb5] | 229 | elif len(lib[o][family]) == 1: |
---|
[541ea280] | 230 | print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0])) |
---|
[a7f398d] | 231 | else: |
---|
[541ea280] | 232 | print("{:15s} {:10s} {:s}".format(o, family, lib[o][family])) |
---|
[41fc24f] | 233 | |
---|
[ccb9fb5] | 234 | |
---|
[41fc24f] | 235 | def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True): |
---|
[541ea280] | 236 | if not os.path.isdir(output_path): |
---|
| 237 | os.mkdir(output_path) |
---|
| 238 | elif not overwrite: |
---|
| 239 | return sorted(glob.glob(os.path.join(output_path, '*.c'))) |
---|
[41fc24f] | 240 | |
---|
[150ec2d] | 241 | c_declarations = get_c_declarations(header, usedouble=usedouble) |
---|
| 242 | cpp_objects = get_cpp_objects_from_c_declarations(c_declarations) |
---|
[41fc24f] | 243 | |
---|
[6cbf34b] | 244 | lib = generate_lib_from_c_declarations(cpp_objects, c_declarations) |
---|
[150ec2d] | 245 | # print_c_declarations_results(lib, c_declarations) |
---|
[41fc24f] | 246 | |
---|
| 247 | sources_list = [] |
---|
[1167631] | 248 | try: |
---|
| 249 | from .gen_code import MappedObject |
---|
[19c3d75] | 250 | except (SystemError, ValueError): |
---|
[1167631] | 251 | from gen_code import MappedObject |
---|
[ccb9fb5] | 252 | for o in lib: |
---|
[1167631] | 253 | out = source_header |
---|
[541ea280] | 254 | mapped = MappedObject(lib[o], usedouble=usedouble) |
---|
[ccb9fb5] | 255 | out += mapped.gen_code() |
---|
| 256 | output_file = os.path.join(output_path, 'gen-%s.c' % o) |
---|
| 257 | with open(output_file, 'w') as f: |
---|
| 258 | f.write(out) |
---|
[541ea280] | 259 | print("wrote %s" % output_file) |
---|
[ccb9fb5] | 260 | sources_list.append(output_file) |
---|
| 261 | |
---|
[1167631] | 262 | out = source_header |
---|
[ccb9fb5] | 263 | out += "#include \"aubio-generated.h\"" |
---|
| 264 | check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib]) |
---|
| 265 | out += """ |
---|
| 266 | |
---|
| 267 | int generated_types_ready (void) |
---|
| 268 | {{ |
---|
| 269 | return ({pycheck_types}); |
---|
| 270 | }} |
---|
[541ea280] | 271 | """.format(pycheck_types=check_types) |
---|
[ccb9fb5] | 272 | |
---|
| 273 | add_types = "".join([""" |
---|
| 274 | Py_INCREF (&Py_{name}Type); |
---|
[541ea280] | 275 | PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib]) |
---|
[ccb9fb5] | 276 | out += """ |
---|
| 277 | |
---|
| 278 | void add_generated_objects ( PyObject *m ) |
---|
| 279 | {{ |
---|
| 280 | {add_types} |
---|
| 281 | }} |
---|
[541ea280] | 282 | """.format(add_types=add_types) |
---|
[ccb9fb5] | 283 | |
---|
| 284 | output_file = os.path.join(output_path, 'aubio-generated.c') |
---|
| 285 | with open(output_file, 'w') as f: |
---|
| 286 | f.write(out) |
---|
[541ea280] | 287 | print("wrote %s" % output_file) |
---|
[ccb9fb5] | 288 | sources_list.append(output_file) |
---|
[a7f398d] | 289 | |
---|
[b6230d8] | 290 | objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib]) |
---|
[a89ed31] | 291 | out = """// generated list of objects created with gen_external.py |
---|
[ccb9fb5] | 292 | |
---|
[a89ed31] | 293 | #include <Python.h> |
---|
| 294 | """ |
---|
| 295 | if usedouble: |
---|
| 296 | out += """ |
---|
| 297 | #ifndef HAVE_AUBIO_DOUBLE |
---|
| 298 | #define HAVE_AUBIO_DOUBLE 1 |
---|
| 299 | #endif |
---|
| 300 | """ |
---|
| 301 | out += """ |
---|
[ccb9fb5] | 302 | {objlist} |
---|
| 303 | int generated_objects ( void ); |
---|
| 304 | void add_generated_objects( PyObject *m ); |
---|
[541ea280] | 305 | """.format(objlist=objlist) |
---|
[ccb9fb5] | 306 | |
---|
| 307 | output_file = os.path.join(output_path, 'aubio-generated.h') |
---|
| 308 | with open(output_file, 'w') as f: |
---|
| 309 | f.write(out) |
---|
[541ea280] | 310 | print("wrote %s" % output_file) |
---|
[ccb9fb5] | 311 | # no need to add header to list of sources |
---|
| 312 | |
---|
[ee7e543] | 313 | return sorted(sources_list) |
---|
[ccb9fb5] | 314 | |
---|
| 315 | if __name__ == '__main__': |
---|
[541ea280] | 316 | if len(sys.argv) > 1: |
---|
| 317 | header = sys.argv[1] |
---|
| 318 | if len(sys.argv) > 2: |
---|
| 319 | output_path = sys.argv[2] |
---|
[1167631] | 320 | generate_external(header, output_path) |
---|