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 AUBIO_GIT_SHA = get_git_revision_hash()
35 """ append sha to version in alpha release
37 if '~alpha' in AUBIO_VERSION_STATUS :
39 AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
40 if AUBIO_VERSION_STATUS is not None :
41 verstr += AUBIO_VERSION_STATUS
44 def get_aubio_pyversion():
45 # convert to version for python according to pep 440
46 # see https://www.python.org/dev/peps/pep-0440/
47 verstr = get_aubio_version()
48 if '~alpha' in verstr or '~git' in verstr:
49 verstr = verstr.split('~')[0] + '+a1'
50 gitsha = get_git_revision_hash(short=True)
52 verstr+='.git.'+gitsha
53 # TODO: add rc, .dev, and .post suffixes, add numbering
58 def get_git_revision_hash( short=True):
62 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
64 fpath, fname = os.path.split(program)
69 for path in os.environ["PATH"].split(os.pathsep):
70 path = path.strip('"')
71 exe_file = os.path.join(path, program)
77 print 'no git found on this system : can\'t get sha'
81 this_file_dir = os.path.dirname(os.path.abspath(__file__))
82 aubio_dir = os.path.join(this_file_dir, '..', '..')
83 aubio_dir = os.path.abspath(aubio_dir)
84 if not os.path.exists(aubio_dir):
85 raise SystemError("git / root folder not found")
86 gitcmd = ['git','-C',aubio_dir ,'rev-parse']
88 gitcmd.append('--short')
90 return subprocess.check_output(gitcmd).strip()
92 # inspired from https://gist.github.com/abergmeier/9488990
93 def add_packages(packages, ext=None, **kw):
94 """ use pkg-config to search which of 'packages' are installed """
100 # if a setuptools extension is passed, fill it with pkg-config results
102 kw = {'include_dirs': ext.include_dirs,
103 'extra_link_args': ext.extra_link_args,
104 'library_dirs': ext.library_dirs,
105 'libraries': ext.libraries,
108 for package in packages:
109 print("checking for {:s}".format(package))
110 cmd = ['pkg-config', '--libs', '--cflags', package]
112 tokens = subprocess.check_output(cmd)
113 except Exception as e:
114 print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
116 tokens = tokens.decode('utf8').split()
123 arg = 'extra_link_args'
125 kw.setdefault(arg, []).append(value)
126 for key, value in iter(kw.items()): # remove duplicated
127 kw[key] = list(set(value))
130 def add_local_aubio_header(ext):
131 """ use local "src/aubio.h", not <aubio/aubio.h>"""
132 ext.define_macros += [('USE_LOCAL_AUBIO', 1)]
133 ext.include_dirs += ['src'] # aubio.h
135 def add_local_aubio_lib(ext):
136 """ add locally built libaubio from build/src """
137 print("Info: using locally built libaubio")
138 ext.library_dirs += [os.path.join('build', 'src')]
139 ext.libraries += ['aubio']
141 def add_local_aubio_sources(ext, usedouble = False):
142 """ build aubio inside python module instead of linking against libaubio """
143 print("Info: libaubio was not installed or built locally with waf, adding src/")
144 aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
145 aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c')))
146 ext.sources += aubio_sources
148 def add_local_macros(ext, usedouble = False):
149 # define macros (waf puts them in build/src/config.h)
150 for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
151 'HAVE_MATH_H', 'HAVE_STRING_H',
152 'HAVE_C99_VARARGS_MACROS',
153 'HAVE_LIMITS_H', 'HAVE_STDARG_H',
154 'HAVE_MEMCPY_HACKS']:
155 ext.define_macros += [(define_macro, 1)]
157 def add_external_deps(ext, usedouble = False):
158 # loof for additional packages
159 print("Info: looking for *optional* additional packages")
160 packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample',
166 # samplerate only works with float
167 if usedouble is False:
168 packages += ['samplerate']
170 print("Info: not adding libsamplerate in double precision mode")
171 add_packages(packages, ext=ext)
172 if 'avcodec' in ext.libraries \
173 and 'avformat' in ext.libraries \
174 and 'avutil' in ext.libraries \
175 and 'avresample' in ext.libraries:
176 ext.define_macros += [('HAVE_LIBAV', 1)]
177 if 'jack' in ext.libraries:
178 ext.define_macros += [('HAVE_JACK', 1)]
179 if 'sndfile' in ext.libraries:
180 ext.define_macros += [('HAVE_SNDFILE', 1)]
181 if 'samplerate' in ext.libraries:
182 ext.define_macros += [('HAVE_SAMPLERATE', 1)]
183 if 'fftw3f' in ext.libraries:
184 ext.define_macros += [('HAVE_FFTW3F', 1)]
185 ext.define_macros += [('HAVE_FFTW3', 1)]
187 # add accelerate on darwin
188 if sys.platform.startswith('darwin'):
189 ext.extra_link_args += ['-framework', 'Accelerate']
190 ext.define_macros += [('HAVE_ACCELERATE', 1)]
191 ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)]
192 ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)]
194 if sys.platform.startswith('win'):
195 ext.define_macros += [('HAVE_WIN_HACKS', 1)]
197 ext.define_macros += [('HAVE_WAVWRITE', 1)]
198 ext.define_macros += [('HAVE_WAVREAD', 1)]
202 ext.libraries += ['cblas']
203 ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
205 def add_system_aubio(ext):
206 # use pkg-config to find aubio's location
207 aubio_version = get_aubio_version()
208 add_packages(['aubio = ' + aubio_version], ext)
209 if 'aubio' not in ext.libraries:
210 print("Info: aubio " + aubio_version + " was not found by pkg-config")
212 print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
214 class CleanGenerated(distutils.command.clean.clean):
216 if os.path.isdir(output_path):
217 distutils.dir_util.remove_tree(output_path)
219 from distutils.command.build_ext import build_ext as _build_ext
220 class build_ext(_build_ext):
222 user_options = _build_ext.user_options + [
223 # The format is (long option, short option, description).
224 ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
227 def initialize_options(self):
228 _build_ext.initialize_options(self)
229 self.enable_double = False
231 def finalize_options(self):
232 _build_ext.finalize_options(self)
233 if self.enable_double:
235 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
236 level=distutils.log.INFO)
238 def build_extension(self, extension):
239 if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ:
240 extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
243 enable_double = False
244 # seack for aubio headers and lib in PKG_CONFIG_PATH
245 add_system_aubio(extension)
246 # the lib was not installed on this system
247 if 'aubio' not in extension.libraries:
248 # use local src/aubio.h
249 if os.path.isfile(os.path.join('src', 'aubio.h')):
250 add_local_aubio_header(extension)
251 add_local_macros(extension)
252 # look for a local waf build
253 if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')):
254 add_local_aubio_lib(extension)
256 # check for external dependencies
257 add_external_deps(extension, usedouble=enable_double)
258 # add libaubio sources and look for optional deps with pkg-config
259 add_local_aubio_sources(extension, usedouble=enable_double)
260 # generate files python/gen/*.c, python/gen/aubio-generated.h
261 extension.sources += generate_external(header, output_path, overwrite = False,
262 usedouble=enable_double)
263 return _build_ext.build_extension(self, extension)