[2675227] | 1 | """ A collection of function used from setup.py distutils script """ |
---|
| 2 | # |
---|
| 3 | import sys, os, glob, subprocess |
---|
[cb89e51] | 4 | import distutils, distutils.command.clean, distutils.dir_util |
---|
[1167631] | 5 | from .gen_external import generate_external, header, output_path |
---|
[a89ed31] | 6 | |
---|
[f432bb1] | 7 | def get_aubio_version(): |
---|
| 8 | # read from VERSION |
---|
| 9 | this_file_dir = os.path.dirname(os.path.abspath(__file__)) |
---|
| 10 | version_file = os.path.join(this_file_dir, '..', '..', 'VERSION') |
---|
| 11 | |
---|
| 12 | if not os.path.isfile(version_file): |
---|
| 13 | raise SystemError("VERSION file not found.") |
---|
| 14 | |
---|
| 15 | for l in open(version_file).readlines(): |
---|
| 16 | #exec (l.strip()) |
---|
| 17 | if l.startswith('AUBIO_MAJOR_VERSION'): |
---|
| 18 | AUBIO_MAJOR_VERSION = int(l.split('=')[1]) |
---|
| 19 | if l.startswith('AUBIO_MINOR_VERSION'): |
---|
| 20 | AUBIO_MINOR_VERSION = int(l.split('=')[1]) |
---|
| 21 | if l.startswith('AUBIO_PATCH_VERSION'): |
---|
| 22 | AUBIO_PATCH_VERSION = int(l.split('=')[1]) |
---|
| 23 | if l.startswith('AUBIO_VERSION_STATUS'): |
---|
| 24 | AUBIO_VERSION_STATUS = l.split('=')[1].strip()[1:-1] |
---|
| 25 | |
---|
| 26 | if AUBIO_MAJOR_VERSION is None or AUBIO_MINOR_VERSION is None \ |
---|
| 27 | or AUBIO_PATCH_VERSION is None: |
---|
| 28 | raise SystemError("Failed parsing VERSION file.") |
---|
| 29 | |
---|
| 30 | verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION, |
---|
| 31 | AUBIO_MINOR_VERSION, |
---|
| 32 | AUBIO_PATCH_VERSION])) |
---|
| 33 | |
---|
| 34 | if AUBIO_VERSION_STATUS is not None: |
---|
| 35 | verstr += AUBIO_VERSION_STATUS |
---|
| 36 | return verstr |
---|
| 37 | |
---|
| 38 | def get_aubio_pyversion(): |
---|
[f131ba8] | 39 | # convert to version for python according to pep 440 |
---|
| 40 | # see https://www.python.org/dev/peps/pep-0440/ |
---|
[f432bb1] | 41 | verstr = get_aubio_version() |
---|
| 42 | if '~alpha' in verstr: |
---|
| 43 | verstr = verstr.split('~')[0] + 'a1' |
---|
[f131ba8] | 44 | # TODO: add rc, .dev, and .post suffixes, add numbering |
---|
[f432bb1] | 45 | return verstr |
---|
| 46 | |
---|
[2675227] | 47 | # inspired from https://gist.github.com/abergmeier/9488990 |
---|
| 48 | def add_packages(packages, ext=None, **kw): |
---|
| 49 | """ use pkg-config to search which of 'packages' are installed """ |
---|
| 50 | flag_map = { |
---|
| 51 | '-I': 'include_dirs', |
---|
| 52 | '-L': 'library_dirs', |
---|
| 53 | '-l': 'libraries'} |
---|
| 54 | |
---|
| 55 | # if a setuptools extension is passed, fill it with pkg-config results |
---|
| 56 | if ext: |
---|
| 57 | kw = {'include_dirs': ext.include_dirs, |
---|
| 58 | 'extra_link_args': ext.extra_link_args, |
---|
| 59 | 'library_dirs': ext.library_dirs, |
---|
| 60 | 'libraries': ext.libraries, |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | for package in packages: |
---|
[7faef58] | 64 | print("checking for {:s}".format(package)) |
---|
[1772630] | 65 | cmd = ['pkg-config', '--libs', '--cflags', package] |
---|
[2675227] | 66 | try: |
---|
| 67 | tokens = subprocess.check_output(cmd) |
---|
[1772630] | 68 | except Exception as e: |
---|
| 69 | print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e))) |
---|
[2675227] | 70 | continue |
---|
| 71 | tokens = tokens.decode('utf8').split() |
---|
| 72 | for token in tokens: |
---|
| 73 | key = token[:2] |
---|
| 74 | try: |
---|
| 75 | arg = flag_map[key] |
---|
| 76 | value = token[2:] |
---|
| 77 | except KeyError: |
---|
| 78 | arg = 'extra_link_args' |
---|
| 79 | value = token |
---|
| 80 | kw.setdefault(arg, []).append(value) |
---|
| 81 | for key, value in iter(kw.items()): # remove duplicated |
---|
| 82 | kw[key] = list(set(value)) |
---|
| 83 | return kw |
---|
| 84 | |
---|
| 85 | def add_local_aubio_header(ext): |
---|
| 86 | """ use local "src/aubio.h", not <aubio/aubio.h>""" |
---|
| 87 | ext.define_macros += [('USE_LOCAL_AUBIO', 1)] |
---|
| 88 | ext.include_dirs += ['src'] # aubio.h |
---|
| 89 | |
---|
| 90 | def add_local_aubio_lib(ext): |
---|
| 91 | """ add locally built libaubio from build/src """ |
---|
| 92 | print("Info: using locally built libaubio") |
---|
| 93 | ext.library_dirs += [os.path.join('build', 'src')] |
---|
| 94 | ext.libraries += ['aubio'] |
---|
| 95 | |
---|
[45521d2] | 96 | def add_local_aubio_sources(ext, usedouble = False): |
---|
[2675227] | 97 | """ build aubio inside python module instead of linking against libaubio """ |
---|
[671eae1] | 98 | print("Info: libaubio was not installed or built locally with waf, adding src/") |
---|
[2e40231] | 99 | aubio_sources = sorted(glob.glob(os.path.join('src', '**.c'))) |
---|
| 100 | aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c'))) |
---|
[2675227] | 101 | ext.sources += aubio_sources |
---|
[7faef58] | 102 | |
---|
| 103 | def add_local_macros(ext, usedouble = False): |
---|
[2675227] | 104 | # define macros (waf puts them in build/src/config.h) |
---|
| 105 | for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H', |
---|
| 106 | 'HAVE_MATH_H', 'HAVE_STRING_H', |
---|
[999b1823] | 107 | 'HAVE_C99_VARARGS_MACROS', |
---|
[aa5828d] | 108 | 'HAVE_LIMITS_H', 'HAVE_STDARG_H', |
---|
| 109 | 'HAVE_MEMCPY_HACKS']: |
---|
[2675227] | 110 | ext.define_macros += [(define_macro, 1)] |
---|
| 111 | |
---|
[7faef58] | 112 | def add_external_deps(ext, usedouble = False): |
---|
[2675227] | 113 | # loof for additional packages |
---|
| 114 | print("Info: looking for *optional* additional packages") |
---|
| 115 | packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample', |
---|
| 116 | 'jack', |
---|
[45521d2] | 117 | 'jack', |
---|
| 118 | 'sndfile', |
---|
[2675227] | 119 | #'fftw3f', |
---|
| 120 | ] |
---|
[45521d2] | 121 | # samplerate only works with float |
---|
[f131ba8] | 122 | if usedouble is False: |
---|
[45521d2] | 123 | packages += ['samplerate'] |
---|
| 124 | else: |
---|
| 125 | print("Info: not adding libsamplerate in double precision mode") |
---|
[2675227] | 126 | add_packages(packages, ext=ext) |
---|
| 127 | if 'avcodec' in ext.libraries \ |
---|
| 128 | and 'avformat' in ext.libraries \ |
---|
| 129 | and 'avutil' in ext.libraries \ |
---|
| 130 | and 'avresample' in ext.libraries: |
---|
| 131 | ext.define_macros += [('HAVE_LIBAV', 1)] |
---|
| 132 | if 'jack' in ext.libraries: |
---|
| 133 | ext.define_macros += [('HAVE_JACK', 1)] |
---|
| 134 | if 'sndfile' in ext.libraries: |
---|
| 135 | ext.define_macros += [('HAVE_SNDFILE', 1)] |
---|
| 136 | if 'samplerate' in ext.libraries: |
---|
| 137 | ext.define_macros += [('HAVE_SAMPLERATE', 1)] |
---|
| 138 | if 'fftw3f' in ext.libraries: |
---|
| 139 | ext.define_macros += [('HAVE_FFTW3F', 1)] |
---|
| 140 | ext.define_macros += [('HAVE_FFTW3', 1)] |
---|
| 141 | |
---|
| 142 | # add accelerate on darwin |
---|
| 143 | if sys.platform.startswith('darwin'): |
---|
[bce913a] | 144 | ext.extra_link_args += ['-framework', 'Accelerate'] |
---|
[2675227] | 145 | ext.define_macros += [('HAVE_ACCELERATE', 1)] |
---|
[d392f3e] | 146 | ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)] |
---|
[652d72d] | 147 | ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)] |
---|
[2675227] | 148 | |
---|
[e27b6a1] | 149 | if sys.platform.startswith('win'): |
---|
| 150 | ext.define_macros += [('HAVE_WIN_HACKS', 1)] |
---|
| 151 | |
---|
[2675227] | 152 | ext.define_macros += [('HAVE_WAVWRITE', 1)] |
---|
| 153 | ext.define_macros += [('HAVE_WAVREAD', 1)] |
---|
| 154 | # TODO: |
---|
| 155 | # add cblas |
---|
| 156 | if 0: |
---|
| 157 | ext.libraries += ['cblas'] |
---|
| 158 | ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)] |
---|
| 159 | |
---|
| 160 | def add_system_aubio(ext): |
---|
| 161 | # use pkg-config to find aubio's location |
---|
[227aa1c] | 162 | aubio_version = get_aubio_version() |
---|
| 163 | add_packages(['aubio = ' + aubio_version], ext) |
---|
[2675227] | 164 | if 'aubio' not in ext.libraries: |
---|
[227aa1c] | 165 | print("Info: aubio " + aubio_version + " was not found by pkg-config") |
---|
| 166 | else: |
---|
| 167 | print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs)) |
---|
[2675227] | 168 | |
---|
[a89ed31] | 169 | class CleanGenerated(distutils.command.clean.clean): |
---|
| 170 | def run(self): |
---|
[4093c0c] | 171 | if os.path.isdir(output_path): |
---|
| 172 | distutils.dir_util.remove_tree(output_path) |
---|
[a89ed31] | 173 | |
---|
[8d09036] | 174 | from distutils.command.build_ext import build_ext as _build_ext |
---|
| 175 | class build_ext(_build_ext): |
---|
| 176 | |
---|
| 177 | user_options = _build_ext.user_options + [ |
---|
[a89ed31] | 178 | # The format is (long option, short option, description). |
---|
| 179 | ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'), |
---|
| 180 | ] |
---|
| 181 | |
---|
| 182 | def initialize_options(self): |
---|
[8d09036] | 183 | _build_ext.initialize_options(self) |
---|
[a89ed31] | 184 | self.enable_double = False |
---|
| 185 | |
---|
| 186 | def finalize_options(self): |
---|
[8d09036] | 187 | _build_ext.finalize_options(self) |
---|
[a89ed31] | 188 | if self.enable_double: |
---|
| 189 | self.announce( |
---|
| 190 | 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1', |
---|
| 191 | level=distutils.log.INFO) |
---|
| 192 | |
---|
[8d09036] | 193 | def build_extension(self, extension): |
---|
[3d14829] | 194 | if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ: |
---|
[8d09036] | 195 | extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)] |
---|
[3d14829] | 196 | enable_double = True |
---|
| 197 | else: |
---|
| 198 | enable_double = False |
---|
[7faef58] | 199 | # seack for aubio headers and lib in PKG_CONFIG_PATH |
---|
| 200 | add_system_aubio(extension) |
---|
| 201 | # the lib was not installed on this system |
---|
| 202 | if 'aubio' not in extension.libraries: |
---|
| 203 | # use local src/aubio.h |
---|
| 204 | if os.path.isfile(os.path.join('src', 'aubio.h')): |
---|
| 205 | add_local_aubio_header(extension) |
---|
| 206 | add_local_macros(extension) |
---|
| 207 | # look for a local waf build |
---|
| 208 | if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')): |
---|
[8d09036] | 209 | add_local_aubio_lib(extension) |
---|
| 210 | else: |
---|
[7faef58] | 211 | # check for external dependencies |
---|
[3d14829] | 212 | add_external_deps(extension, usedouble=enable_double) |
---|
[8d09036] | 213 | # add libaubio sources and look for optional deps with pkg-config |
---|
[3d14829] | 214 | add_local_aubio_sources(extension, usedouble=enable_double) |
---|
[8d09036] | 215 | # generate files python/gen/*.c, python/gen/aubio-generated.h |
---|
| 216 | extension.sources += generate_external(header, output_path, overwrite = False, |
---|
[3d14829] | 217 | usedouble=enable_double) |
---|
[8d09036] | 218 | return _build_ext.build_extension(self, extension) |
---|