1 """ A collection of function used from setup.py distutils script """
3 import sys, os, glob, subprocess
4 import distutils, distutils.command.clean, distutils.dir_util
5 from .gen_external import generate_external, header, output_path
7 def get_aubio_version():
9 this_file_dir = os.path.dirname(os.path.abspath(__file__))
10 version_file = os.path.join(this_file_dir, '..', '..', 'VERSION')
12 if not os.path.isfile(version_file):
13 raise SystemError("VERSION file not found.")
15 for l in open(version_file).readlines():
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]
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.")
30 verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
32 AUBIO_PATCH_VERSION]))
34 if AUBIO_VERSION_STATUS is not None:
35 verstr += AUBIO_VERSION_STATUS
38 def get_aubio_pyversion():
39 # convert to version for python according to pep 440
40 # see https://www.python.org/dev/peps/pep-0440/
41 verstr = get_aubio_version()
42 if '~alpha' in verstr:
43 verstr = verstr.split('~')[0] + 'a1'
44 # TODO: add rc, .dev, and .post suffixes, add numbering
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 """
55 # if a setuptools extension is passed, fill it with pkg-config results
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,
63 for package in packages:
64 print("checking for {:s}".format(package))
65 cmd = ['pkg-config', '--libs', '--cflags', package]
67 tokens = subprocess.check_output(cmd)
68 except Exception as e:
69 print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
71 tokens = tokens.decode('utf8').split()
78 arg = 'extra_link_args'
80 kw.setdefault(arg, []).append(value)
81 for key, value in iter(kw.items()): # remove duplicated
82 kw[key] = list(set(value))
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
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']
96 def add_local_aubio_sources(ext):
97 """ build aubio inside python module instead of linking against libaubio """
98 print("Info: libaubio was not installed or built locally with waf, adding src/")
99 aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
100 aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c')))
101 ext.sources += aubio_sources
103 def add_local_macros(ext, usedouble = False):
105 ext.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
106 # define macros (waf puts them in build/src/config.h)
107 for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
108 'HAVE_MATH_H', 'HAVE_STRING_H',
109 'HAVE_C99_VARARGS_MACROS',
110 'HAVE_LIMITS_H', 'HAVE_STDARG_H',
111 'HAVE_MEMCPY_HACKS']:
112 ext.define_macros += [(define_macro, 1)]
114 def add_external_deps(ext, usedouble = False):
115 # loof for additional packages
116 print("Info: looking for *optional* additional packages")
117 packages = ['libavcodec', 'libavformat', 'libavutil',
118 'libswresample', 'libavresample',
122 # samplerate only works with float
123 if usedouble is False:
124 packages += ['samplerate']
126 print("Info: not adding libsamplerate in double precision mode")
127 add_packages(packages, ext=ext)
128 if 'avcodec' in ext.libraries \
129 and 'avformat' in ext.libraries \
130 and 'avutil' in ext.libraries:
131 if 'swresample' in ext.libraries:
132 ext.define_macros += [('HAVE_SWRESAMPLE', 1)]
133 elif 'avresample' in ext.libraries:
134 ext.define_macros += [('HAVE_AVRESAMPLE', 1)]
135 if 'swresample' in ext.libraries or 'avresample' in ext.libraries:
136 ext.define_macros += [('HAVE_LIBAV', 1)]
137 if 'sndfile' in ext.libraries:
138 ext.define_macros += [('HAVE_SNDFILE', 1)]
139 if 'samplerate' in ext.libraries:
140 ext.define_macros += [('HAVE_SAMPLERATE', 1)]
141 if 'fftw3f' in ext.libraries:
142 ext.define_macros += [('HAVE_FFTW3F', 1)]
143 ext.define_macros += [('HAVE_FFTW3', 1)]
145 # add accelerate on darwin
146 if sys.platform.startswith('darwin'):
147 ext.extra_link_args += ['-framework', 'Accelerate']
148 ext.define_macros += [('HAVE_ACCELERATE', 1)]
149 ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)]
150 ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)]
152 if sys.platform.startswith('win'):
153 ext.define_macros += [('HAVE_WIN_HACKS', 1)]
155 ext.define_macros += [('HAVE_WAVWRITE', 1)]
156 ext.define_macros += [('HAVE_WAVREAD', 1)]
160 ext.libraries += ['cblas']
161 ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
163 def add_system_aubio(ext):
164 # use pkg-config to find aubio's location
165 aubio_version = get_aubio_version()
166 add_packages(['aubio = ' + aubio_version], ext)
167 if 'aubio' not in ext.libraries:
168 print("Info: aubio " + aubio_version + " was not found by pkg-config")
170 print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
172 class CleanGenerated(distutils.command.clean.clean):
174 if os.path.isdir(output_path):
175 distutils.dir_util.remove_tree(output_path)
177 from distutils.command.build_ext import build_ext as _build_ext
178 class build_ext(_build_ext):
180 user_options = _build_ext.user_options + [
181 # The format is (long option, short option, description).
182 ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
185 def initialize_options(self):
186 _build_ext.initialize_options(self)
187 self.enable_double = False
189 def finalize_options(self):
190 _build_ext.finalize_options(self)
191 if self.enable_double:
193 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
194 level=distutils.log.INFO)
196 def build_extension(self, extension):
197 if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ:
200 enable_double = False
201 # seack for aubio headers and lib in PKG_CONFIG_PATH
202 add_system_aubio(extension)
203 # the lib was not installed on this system
204 if 'aubio' not in extension.libraries:
205 # use local src/aubio.h
206 if os.path.isfile(os.path.join('src', 'aubio.h')):
207 add_local_aubio_header(extension)
208 add_local_macros(extension, usedouble=enable_double)
209 # look for a local waf build
210 if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')):
211 add_local_aubio_lib(extension)
213 # check for external dependencies
214 add_external_deps(extension, usedouble=enable_double)
215 # add libaubio sources and look for optional deps with pkg-config
216 add_local_aubio_sources(extension)
217 # generate files python/gen/*.c, python/gen/aubio-generated.h
218 extension.sources += generate_external(header, output_path, overwrite = False,
219 usedouble=enable_double)
220 return _build_ext.build_extension(self, extension)