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