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 from Version import get_aubio_version
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 """
17 # if a setuptools extension is passed, fill it with pkg-config results
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,
25 for package in packages:
26 print("checking for {:s}".format(package))
27 cmd = ['pkg-config', '--libs', '--cflags', package]
29 tokens = subprocess.check_output(cmd)
30 except Exception as e:
31 print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
33 tokens = tokens.decode('utf8').split()
40 arg = 'extra_link_args'
42 kw.setdefault(arg, []).append(value)
43 for key, value in iter(kw.items()): # remove duplicated
44 kw[key] = list(set(value))
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
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']
58 def add_local_aubio_sources(ext, usedouble = False):
59 """ build aubio inside python module instead of linking against libaubio """
60 print("Info: libaubio was not installed or built locally with waf, adding src/")
61 aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
62 aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c')))
63 ext.sources += aubio_sources
65 def add_local_macros(ext, usedouble = False):
66 # define macros (waf puts them in build/src/config.h)
67 for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
68 'HAVE_MATH_H', 'HAVE_STRING_H',
69 'HAVE_C99_VARARGS_MACROS',
70 'HAVE_LIMITS_H', 'HAVE_STDARG_H',
72 ext.define_macros += [(define_macro, 1)]
74 def add_external_deps(ext, usedouble = False):
75 # loof for additional packages
76 print("Info: looking for *optional* additional packages")
77 packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample',
83 # samplerate only works with float
84 if usedouble is False:
85 packages += ['samplerate']
87 print("Info: not adding libsamplerate in double precision mode")
88 add_packages(packages, ext=ext)
89 if 'avcodec' in ext.libraries \
90 and 'avformat' in ext.libraries \
91 and 'avutil' in ext.libraries \
92 and 'avresample' in ext.libraries:
93 ext.define_macros += [('HAVE_LIBAV', 1)]
94 if 'jack' in ext.libraries:
95 ext.define_macros += [('HAVE_JACK', 1)]
96 if 'sndfile' in ext.libraries:
97 ext.define_macros += [('HAVE_SNDFILE', 1)]
98 if 'samplerate' in ext.libraries:
99 ext.define_macros += [('HAVE_SAMPLERATE', 1)]
100 if 'fftw3f' in ext.libraries:
101 ext.define_macros += [('HAVE_FFTW3F', 1)]
102 ext.define_macros += [('HAVE_FFTW3', 1)]
104 # add accelerate on darwin
105 if sys.platform.startswith('darwin'):
106 ext.extra_link_args += ['-framework', 'Accelerate']
107 ext.define_macros += [('HAVE_ACCELERATE', 1)]
108 ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)]
109 ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)]
111 if sys.platform.startswith('win'):
112 ext.define_macros += [('HAVE_WIN_HACKS', 1)]
114 ext.define_macros += [('HAVE_WAVWRITE', 1)]
115 ext.define_macros += [('HAVE_WAVREAD', 1)]
119 ext.libraries += ['cblas']
120 ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
122 def add_system_aubio(ext):
123 # use pkg-config to find aubio's location
124 aubio_version = get_aubio_version()
125 add_packages(['aubio = ' + aubio_version], ext)
126 if 'aubio' not in ext.libraries:
127 print("Info: aubio " + aubio_version + " was not found by pkg-config")
129 print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
131 class CleanGenerated(distutils.command.clean.clean):
133 if os.path.isdir(output_path):
134 distutils.dir_util.remove_tree(output_path)
136 from distutils.command.build_ext import build_ext as _build_ext
137 class build_ext(_build_ext):
139 user_options = _build_ext.user_options + [
140 # The format is (long option, short option, description).
141 ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
144 def initialize_options(self):
145 _build_ext.initialize_options(self)
146 self.enable_double = False
148 def finalize_options(self):
149 _build_ext.finalize_options(self)
150 if self.enable_double:
152 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
153 level=distutils.log.INFO)
155 def build_extension(self, extension):
156 if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ:
157 extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
160 enable_double = False
161 # seack for aubio headers and lib in PKG_CONFIG_PATH
162 add_system_aubio(extension)
163 # the lib was not installed on this system
164 if 'aubio' not in extension.libraries:
165 # use local src/aubio.h
166 if os.path.isfile(os.path.join('src', 'aubio.h')):
167 add_local_aubio_header(extension)
168 add_local_macros(extension)
169 # look for a local waf build
170 if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')):
171 add_local_aubio_lib(extension)
173 # check for external dependencies
174 add_external_deps(extension, usedouble=enable_double)
175 # add libaubio sources and look for optional deps with pkg-config
176 add_local_aubio_sources(extension, usedouble=enable_double)
177 # generate files python/gen/*.c, python/gen/aubio-generated.h
178 extension.sources += generate_external(header, output_path, overwrite = False,
179 usedouble=enable_double)
180 return _build_ext.build_extension(self, extension)