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