source: python/lib/moresetuptools.py @ 1eb8c0e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 1eb8c0e was 1eb8c0e, checked in by Martin Hermant <martin.hermant@gmail.com>, 7 years ago

aubio version :
aubio-c / aubio-py add git commit support

  • Property mode set to 100644
File size: 10.0 KB
Line 
1""" A collection of function used from setup.py distutils script """
2#
3import sys, os, glob, subprocess
4import distutils, distutils.command.clean, distutils.dir_util
5from .gen_external import generate_external, header, output_path
6
7def get_aubio_version():
8    # read from VERSION
9    this_file_dir = os.path.dirname(os.path.abspath(__file__))
10    version_file = os.path.join(this_file_dir, '..', '..', 'VERSION')
11
12    if not os.path.isfile(version_file):
13        raise SystemError("VERSION file not found.")
14
15    for l in open(version_file).readlines():
16        #exec (l.strip())
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]
25
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.")
29
30    verstr = '.'.join(map(str, [AUBIO_MAJOR_VERSION,
31                                     AUBIO_MINOR_VERSION,
32                                     AUBIO_PATCH_VERSION]))
33
34    AUBIO_GIT_SHA = get_git_revision_hash()
35    """ append sha to version in alpha release
36    """
37    if '~alpha' in AUBIO_VERSION_STATUS :
38        if AUBIO_GIT_SHA:
39            AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
40    if AUBIO_VERSION_STATUS is not None :
41        verstr += AUBIO_VERSION_STATUS
42    return verstr
43
44def 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)
51        if gitsha:
52            verstr+='.git.'+gitsha
53    # TODO: add rc, .dev, and .post suffixes, add numbering
54    return verstr
55
56
57
58def get_git_revision_hash( short=True):
59    def which(program):
60   
61        def is_exe(fpath):
62            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
63
64        fpath, fname = os.path.split(program)
65        if fpath:
66            if is_exe(program):
67                return program
68        else:
69            for path in os.environ["PATH"].split(os.pathsep):
70                path = path.strip('"')
71                exe_file = os.path.join(path, program)
72                if is_exe(exe_file):
73                    return exe_file
74
75        return None
76    if not which('git'):
77        print 'no git found on this system : can\'t get sha'
78        return ""
79
80    import subprocess
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']
87    if short:
88      gitcmd.append('--short')
89    gitcmd.append('HEAD')
90    return subprocess.check_output(gitcmd).strip()
91
92# inspired from https://gist.github.com/abergmeier/9488990
93def add_packages(packages, ext=None, **kw):
94    """ use pkg-config to search which of 'packages' are installed """
95    flag_map = {
96        '-I': 'include_dirs',
97        '-L': 'library_dirs',
98        '-l': 'libraries'}
99
100    # if a setuptools extension is passed, fill it with pkg-config results
101    if ext:
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,
106             }
107
108    for package in packages:
109        print("checking for {:s}".format(package))
110        cmd = ['pkg-config', '--libs', '--cflags', package]
111        try:
112            tokens = subprocess.check_output(cmd)
113        except Exception as e:
114            print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
115            continue
116        tokens = tokens.decode('utf8').split()
117        for token in tokens:
118            key = token[:2]
119            try:
120                arg = flag_map[key]
121                value = token[2:]
122            except KeyError:
123                arg = 'extra_link_args'
124                value = token
125            kw.setdefault(arg, []).append(value)
126    for key, value in iter(kw.items()): # remove duplicated
127        kw[key] = list(set(value))
128    return kw
129
130def 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
134
135def 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']
140
141def 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
147
148def 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)]
156
157def 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',
161                'jack',
162                'jack',
163                'sndfile',
164                #'fftw3f',
165               ]
166    # samplerate only works with float
167    if usedouble is False:
168        packages += ['samplerate']
169    else:
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)]
186
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)]
193
194    if sys.platform.startswith('win'):
195        ext.define_macros += [('HAVE_WIN_HACKS', 1)]
196
197    ext.define_macros += [('HAVE_WAVWRITE', 1)]
198    ext.define_macros += [('HAVE_WAVREAD', 1)]
199    # TODO:
200    # add cblas
201    if 0:
202        ext.libraries += ['cblas']
203        ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
204
205def 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")
211    else:
212        print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
213
214class CleanGenerated(distutils.command.clean.clean):
215    def run(self):
216        if os.path.isdir(output_path):
217            distutils.dir_util.remove_tree(output_path)
218
219from distutils.command.build_ext import build_ext as _build_ext
220class build_ext(_build_ext):
221
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)'),
225            ]
226
227    def initialize_options(self):
228        _build_ext.initialize_options(self)
229        self.enable_double = False
230
231    def finalize_options(self):
232        _build_ext.finalize_options(self)
233        if self.enable_double:
234            self.announce(
235                    'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
236                    level=distutils.log.INFO)
237
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)]
241            enable_double = True
242        else:
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)
255            else:
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)
Note: See TracBrowser for help on using the repository browser.