source: python/lib/moresetuptools.py @ 79ef3b3

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

hide AUBIO_GIT_SHA in release
fix py3 syntax error

  • 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   
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()
39        if AUBIO_GIT_SHA:
40            AUBIO_VERSION_STATUS = '~git'+AUBIO_GIT_SHA
41
42    if AUBIO_VERSION_STATUS is not None :
43        verstr += AUBIO_VERSION_STATUS
44    return verstr
45
46def 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)
53        if gitsha:
54            verstr+='.git.'+gitsha
55    # TODO: add rc, .dev, and .post suffixes, add numbering
56    return verstr
57
58
59
60def get_git_revision_hash( short=True):
61    def which(program):
62   
63        def is_exe(fpath):
64            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
65
66        fpath, fname = os.path.split(program)
67        if fpath:
68            if is_exe(program):
69                return program
70        else:
71            for path in os.environ["PATH"].split(os.pathsep):
72                path = path.strip('"')
73                exe_file = os.path.join(path, program)
74                if is_exe(exe_file):
75                    return exe_file
76
77        return None
78    if not which('git'):
79        print ('Version : no git found on this system : can\'t get sha')
80        return ""
81
82    import subprocess
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']
89    if short:
90      gitcmd.append('--short')
91    gitcmd.append('HEAD')
92    return subprocess.check_output(gitcmd).strip()
93
94# inspired from https://gist.github.com/abergmeier/9488990
95def add_packages(packages, ext=None, **kw):
96    """ use pkg-config to search which of 'packages' are installed """
97    flag_map = {
98        '-I': 'include_dirs',
99        '-L': 'library_dirs',
100        '-l': 'libraries'}
101
102    # if a setuptools extension is passed, fill it with pkg-config results
103    if ext:
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,
108             }
109
110    for package in packages:
111        print("checking for {:s}".format(package))
112        cmd = ['pkg-config', '--libs', '--cflags', package]
113        try:
114            tokens = subprocess.check_output(cmd)
115        except Exception as e:
116            print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
117            continue
118        tokens = tokens.decode('utf8').split()
119        for token in tokens:
120            key = token[:2]
121            try:
122                arg = flag_map[key]
123                value = token[2:]
124            except KeyError:
125                arg = 'extra_link_args'
126                value = token
127            kw.setdefault(arg, []).append(value)
128    for key, value in iter(kw.items()): # remove duplicated
129        kw[key] = list(set(value))
130    return kw
131
132def 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
136
137def 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']
142
143def 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
149
150def 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)]
158
159def 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',
163                'jack',
164                'jack',
165                'sndfile',
166                #'fftw3f',
167               ]
168    # samplerate only works with float
169    if usedouble is False:
170        packages += ['samplerate']
171    else:
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)]
188
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)]
195
196    if sys.platform.startswith('win'):
197        ext.define_macros += [('HAVE_WIN_HACKS', 1)]
198
199    ext.define_macros += [('HAVE_WAVWRITE', 1)]
200    ext.define_macros += [('HAVE_WAVREAD', 1)]
201    # TODO:
202    # add cblas
203    if 0:
204        ext.libraries += ['cblas']
205        ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
206
207def 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")
213    else:
214        print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
215
216class CleanGenerated(distutils.command.clean.clean):
217    def run(self):
218        if os.path.isdir(output_path):
219            distutils.dir_util.remove_tree(output_path)
220
221from distutils.command.build_ext import build_ext as _build_ext
222class build_ext(_build_ext):
223
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)'),
227            ]
228
229    def initialize_options(self):
230        _build_ext.initialize_options(self)
231        self.enable_double = False
232
233    def finalize_options(self):
234        _build_ext.finalize_options(self)
235        if self.enable_double:
236            self.announce(
237                    'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
238                    level=distutils.log.INFO)
239
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)]
243            enable_double = True
244        else:
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)
257            else:
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)
Note: See TracBrowser for help on using the repository browser.