Changeset 19c3d75 for python


Ignore:
Timestamp:
May 16, 2016, 7:12:15 AM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
f465088
Parents:
e0c74c2
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/gen_external.py

    re0c74c2 r19c3d75  
    1 import os, glob
     1import distutils.ccompiler
     2import sys, os, subprocess, glob
    23
    34header = os.path.join('src', 'aubio.h')
     
    4445  ]
    4546
     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
    4679
    4780def get_cpp_objects(header=header):
    48 
    49     cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 {:s}'.format(header)).readlines()]
    50     #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/onset/onset.h').readlines()]
    51     #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/pitch/pitch.h').readlines()]
     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')]
    52105
    53106    cpp_output = filter(lambda y: len(y) > 1, cpp_output)
    54     cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
    55     cpp_output = list(cpp_output)
     107    cpp_output = list(filter(lambda y: not y.startswith('#'), cpp_output))
    56108
    57109    i = 1
     
    127179    try:
    128180        from .gen_code import MappedObject
    129     except (SystemError, ValueError) as e:
     181    except (SystemError, ValueError):
    130182        from gen_code import MappedObject
    131183    for o in lib:
     
    193245
    194246if __name__ == '__main__':
    195     import sys
    196247    if len(sys.argv) > 1: header = sys.argv[1]
    197248    if len(sys.argv) > 2: output_path = sys.argv[2]
Note: See TracChangeset for help on using the changeset viewer.