source: python/lib/moresetuptools.py @ 04d0c89

sampler
Last change on this file since 04d0c89 was 04d0c89, checked in by Paul Brossier <piem@piem.org>, 7 years ago

python/lib/moresetuptools.py: assume pthread is available except on windows

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[2675227]1""" A collection of function used from setup.py distutils script """
2#
3import sys, os, glob, subprocess
[cb89e51]4import distutils, distutils.command.clean, distutils.dir_util
[1167631]5from .gen_external import generate_external, header, output_path
[a89ed31]6
[f77820d]7from this_version import get_aubio_version
[f432bb1]8
[2675227]9# inspired from https://gist.github.com/abergmeier/9488990
10def add_packages(packages, ext=None, **kw):
11    """ use pkg-config to search which of 'packages' are installed """
12    flag_map = {
13        '-I': 'include_dirs',
14        '-L': 'library_dirs',
15        '-l': 'libraries'}
16
17    # if a setuptools extension is passed, fill it with pkg-config results
18    if ext:
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,
23             }
24
25    for package in packages:
[7faef58]26        print("checking for {:s}".format(package))
[1772630]27        cmd = ['pkg-config', '--libs', '--cflags', package]
[2675227]28        try:
29            tokens = subprocess.check_output(cmd)
[1772630]30        except Exception as e:
31            print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e)))
[2675227]32            continue
33        tokens = tokens.decode('utf8').split()
34        for token in tokens:
35            key = token[:2]
36            try:
37                arg = flag_map[key]
38                value = token[2:]
39            except KeyError:
40                arg = 'extra_link_args'
41                value = token
42            kw.setdefault(arg, []).append(value)
43    for key, value in iter(kw.items()): # remove duplicated
44        kw[key] = list(set(value))
45    return kw
46
47def 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
51
52def 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']
57
[4cb48d2]58def add_local_aubio_sources(ext):
[2675227]59    """ build aubio inside python module instead of linking against libaubio """
[671eae1]60    print("Info: libaubio was not installed or built locally with waf, adding src/")
[ac971a51]61    aubio_sources = sorted(glob.glob(os.path.join('src', '**.c')))
62    aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c')))
[2675227]63    ext.sources += aubio_sources
[7faef58]64
65def add_local_macros(ext, usedouble = False):
[4cb48d2]66    if usedouble:
67        ext.define_macros += [('HAVE_AUBIO_DOUBLE', 1)]
[2675227]68    # define macros (waf puts them in build/src/config.h)
69    for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
70                         'HAVE_MATH_H', 'HAVE_STRING_H',
[999b1823]71                         'HAVE_C99_VARARGS_MACROS',
[aa5828d]72                         'HAVE_LIMITS_H', 'HAVE_STDARG_H',
73                         'HAVE_MEMCPY_HACKS']:
[2675227]74        ext.define_macros += [(define_macro, 1)]
[04d0c89]75    # assume pthread is available except on windows
76    if os.name != 'nt':
77        ext.define_macros += [('HAVE_PTHREAD_H', 1)]
[2675227]78
[7faef58]79def add_external_deps(ext, usedouble = False):
[2675227]80    # loof for additional packages
81    print("Info: looking for *optional* additional packages")
[218f670]82    packages = ['libavcodec', 'libavformat', 'libavutil',
83                'libswresample', 'libavresample',
[45521d2]84                'sndfile',
[b83449d]85                'rubberband',
[2675227]86                #'fftw3f',
87               ]
[45521d2]88    # samplerate only works with float
[f131ba8]89    if usedouble is False:
[45521d2]90        packages += ['samplerate']
91    else:
92        print("Info: not adding libsamplerate in double precision mode")
[2675227]93    add_packages(packages, ext=ext)
94    if 'avcodec' in ext.libraries \
95            and 'avformat' in ext.libraries \
[c7d444a]96            and 'avutil' in ext.libraries:
97        if 'swresample' in ext.libraries:
98            ext.define_macros += [('HAVE_SWRESAMPLE', 1)]
99        elif 'avresample' in ext.libraries:
100            ext.define_macros += [('HAVE_AVRESAMPLE', 1)]
101        if 'swresample' in ext.libraries or 'avresample' in ext.libraries:
102            ext.define_macros += [('HAVE_LIBAV', 1)]
[2675227]103    if 'sndfile' in ext.libraries:
104        ext.define_macros += [('HAVE_SNDFILE', 1)]
105    if 'samplerate' in ext.libraries:
106        ext.define_macros += [('HAVE_SAMPLERATE', 1)]
[b83449d]107    if 'rubberband' in ext.libraries:
108        ext.define_macros += [('HAVE_RUBBERBAND', 1)]
[2675227]109    if 'fftw3f' in ext.libraries:
110        ext.define_macros += [('HAVE_FFTW3F', 1)]
111        ext.define_macros += [('HAVE_FFTW3', 1)]
112
113    # add accelerate on darwin
114    if sys.platform.startswith('darwin'):
[bce913a]115        ext.extra_link_args += ['-framework', 'Accelerate']
[2675227]116        ext.define_macros += [('HAVE_ACCELERATE', 1)]
[d392f3e]117        ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)]
[652d72d]118        ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)]
[2675227]119
[e27b6a1]120    if sys.platform.startswith('win'):
121        ext.define_macros += [('HAVE_WIN_HACKS', 1)]
122
[2675227]123    ext.define_macros += [('HAVE_WAVWRITE', 1)]
124    ext.define_macros += [('HAVE_WAVREAD', 1)]
[67a3053]125
126    # TODO: add cblas
[2675227]127    if 0:
128        ext.libraries += ['cblas']
129        ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)]
130
131def add_system_aubio(ext):
132    # use pkg-config to find aubio's location
[227aa1c]133    aubio_version = get_aubio_version()
134    add_packages(['aubio = ' + aubio_version], ext)
[2675227]135    if 'aubio' not in ext.libraries:
[227aa1c]136        print("Info: aubio " + aubio_version + " was not found by pkg-config")
137    else:
138        print("Info: using system aubio " + aubio_version + " found in " + ' '.join(ext.library_dirs))
[2675227]139
[67a3053]140def add_libav_on_win(ext):
141    """ no pkg-config on windows, simply assume these libs are available """
142    ext.libraries += ['avformat', 'avutil', 'avcodec', 'swresample']
143    for define_macro in ['HAVE_LIBAV', 'HAVE_SWRESAMPLE']:
144        ext.define_macros += [(define_macro, 1)]
145
[a89ed31]146class CleanGenerated(distutils.command.clean.clean):
147    def run(self):
[4093c0c]148        if os.path.isdir(output_path):
149            distutils.dir_util.remove_tree(output_path)
[a89ed31]150
[5a7c000]151from distutils.command.build_ext import build_ext as _build_ext
152class build_ext(_build_ext):
153
154    user_options = _build_ext.user_options + [
[a89ed31]155            # The format is (long option, short option, description).
156            ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'),
157            ]
158
159    def initialize_options(self):
[5a7c000]160        _build_ext.initialize_options(self)
[a89ed31]161        self.enable_double = False
162
163    def finalize_options(self):
[5a7c000]164        _build_ext.finalize_options(self)
[a89ed31]165        if self.enable_double:
166            self.announce(
167                    'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1',
168                    level=distutils.log.INFO)
169
[5a7c000]170    def build_extension(self, extension):
[3d14829]171        if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ:
172            enable_double = True
173        else:
174            enable_double = False
[7faef58]175        # seack for aubio headers and lib in PKG_CONFIG_PATH
176        add_system_aubio(extension)
177        # the lib was not installed on this system
178        if 'aubio' not in extension.libraries:
179            # use local src/aubio.h
180            if os.path.isfile(os.path.join('src', 'aubio.h')):
181                add_local_aubio_header(extension)
[4cb48d2]182            add_local_macros(extension, usedouble=enable_double)
[7faef58]183            # look for a local waf build
184            if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')):
[5a7c000]185                add_local_aubio_lib(extension)
186            else:
[7faef58]187                # check for external dependencies
[3d14829]188                add_external_deps(extension, usedouble=enable_double)
[67a3053]189                # force adding libav on windows
190                if os.name == 'nt' and ('WITH_LIBAV' in os.environ \
191                        or 'CONDA_PREFIX' in os.environ):
192                    add_libav_on_win(extension)
[5a7c000]193                # add libaubio sources and look for optional deps with pkg-config
[4cb48d2]194                add_local_aubio_sources(extension)
[5a7c000]195        # generate files python/gen/*.c, python/gen/aubio-generated.h
[c7d3c2a]196        extension.include_dirs += [ output_path ]
[5a7c000]197        extension.sources += generate_external(header, output_path, overwrite = False,
[3d14829]198                usedouble=enable_double)
[5a7c000]199        return _build_ext.build_extension(self, extension)
Note: See TracBrowser for help on using the repository browser.