1 | """ A collection of function used from setup.py distutils script """ |
---|
2 | # |
---|
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 |
---|
6 | |
---|
7 | # inspired from https://gist.github.com/abergmeier/9488990 |
---|
8 | def add_packages(packages, ext=None, **kw): |
---|
9 | """ use pkg-config to search which of 'packages' are installed """ |
---|
10 | flag_map = { |
---|
11 | '-I': 'include_dirs', |
---|
12 | '-L': 'library_dirs', |
---|
13 | '-l': 'libraries'} |
---|
14 | |
---|
15 | # if a setuptools extension is passed, fill it with pkg-config results |
---|
16 | if ext: |
---|
17 | kw = {'include_dirs': ext.include_dirs, |
---|
18 | 'extra_link_args': ext.extra_link_args, |
---|
19 | 'library_dirs': ext.library_dirs, |
---|
20 | 'libraries': ext.libraries, |
---|
21 | } |
---|
22 | |
---|
23 | for package in packages: |
---|
24 | print("checking for {:s}".format(package)) |
---|
25 | cmd = ['pkg-config', '--libs', '--cflags', package] |
---|
26 | try: |
---|
27 | tokens = subprocess.check_output(cmd) |
---|
28 | except Exception as e: |
---|
29 | print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e))) |
---|
30 | continue |
---|
31 | tokens = tokens.decode('utf8').split() |
---|
32 | for token in tokens: |
---|
33 | key = token[:2] |
---|
34 | try: |
---|
35 | arg = flag_map[key] |
---|
36 | value = token[2:] |
---|
37 | except KeyError: |
---|
38 | arg = 'extra_link_args' |
---|
39 | value = token |
---|
40 | kw.setdefault(arg, []).append(value) |
---|
41 | for key, value in iter(kw.items()): # remove duplicated |
---|
42 | kw[key] = list(set(value)) |
---|
43 | return kw |
---|
44 | |
---|
45 | def add_local_aubio_header(ext): |
---|
46 | """ use local "src/aubio.h", not <aubio/aubio.h>""" |
---|
47 | ext.define_macros += [('USE_LOCAL_AUBIO', 1)] |
---|
48 | ext.include_dirs += ['src'] # aubio.h |
---|
49 | |
---|
50 | def add_local_aubio_lib(ext): |
---|
51 | """ add locally built libaubio from build/src """ |
---|
52 | print("Info: using locally built libaubio") |
---|
53 | ext.library_dirs += [os.path.join('build', 'src')] |
---|
54 | ext.libraries += ['aubio'] |
---|
55 | |
---|
56 | def add_local_aubio_sources(ext, usedouble = False): |
---|
57 | """ build aubio inside python module instead of linking against libaubio """ |
---|
58 | print("Warning: libaubio was not built with waf, adding src/") |
---|
59 | # create an empty header, macros will be passed on the command line |
---|
60 | fake_config_header = os.path.join('python', 'ext', 'config.h') |
---|
61 | distutils.file_util.write_file(fake_config_header, "") |
---|
62 | aubio_sources = sorted(glob.glob(os.path.join('src', '**.c'))) |
---|
63 | aubio_sources += sorted(glob.glob(os.path.join('src', '*', '**.c'))) |
---|
64 | ext.sources += aubio_sources |
---|
65 | |
---|
66 | def add_local_macros(ext, usedouble = False): |
---|
67 | # define macros (waf puts them in build/src/config.h) |
---|
68 | for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H', |
---|
69 | 'HAVE_MATH_H', 'HAVE_STRING_H', |
---|
70 | 'HAVE_C99_VARARGS_MACROS', |
---|
71 | 'HAVE_LIMITS_H', 'HAVE_STDARG_H', |
---|
72 | 'HAVE_MEMCPY_HACKS']: |
---|
73 | ext.define_macros += [(define_macro, 1)] |
---|
74 | |
---|
75 | def add_external_deps(ext, usedouble = False): |
---|
76 | # loof for additional packages |
---|
77 | print("Info: looking for *optional* additional packages") |
---|
78 | packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample', |
---|
79 | 'jack', |
---|
80 | 'jack', |
---|
81 | 'sndfile', |
---|
82 | #'fftw3f', |
---|
83 | ] |
---|
84 | # samplerate only works with float |
---|
85 | if usedouble == False: |
---|
86 | packages += ['samplerate'] |
---|
87 | else: |
---|
88 | print("Info: not adding libsamplerate in double precision mode") |
---|
89 | add_packages(packages, ext=ext) |
---|
90 | if 'avcodec' in ext.libraries \ |
---|
91 | and 'avformat' in ext.libraries \ |
---|
92 | and 'avutil' in ext.libraries \ |
---|
93 | and 'avresample' in ext.libraries: |
---|
94 | ext.define_macros += [('HAVE_LIBAV', 1)] |
---|
95 | if 'jack' in ext.libraries: |
---|
96 | ext.define_macros += [('HAVE_JACK', 1)] |
---|
97 | if 'sndfile' in ext.libraries: |
---|
98 | ext.define_macros += [('HAVE_SNDFILE', 1)] |
---|
99 | if 'samplerate' in ext.libraries: |
---|
100 | ext.define_macros += [('HAVE_SAMPLERATE', 1)] |
---|
101 | if 'fftw3f' in ext.libraries: |
---|
102 | ext.define_macros += [('HAVE_FFTW3F', 1)] |
---|
103 | ext.define_macros += [('HAVE_FFTW3', 1)] |
---|
104 | |
---|
105 | # add accelerate on darwin |
---|
106 | if sys.platform.startswith('darwin'): |
---|
107 | ext.extra_link_args += ['-framework', 'Accelerate'] |
---|
108 | ext.define_macros += [('HAVE_ACCELERATE', 1)] |
---|
109 | ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)] |
---|
110 | ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)] |
---|
111 | |
---|
112 | if sys.platform.startswith('win'): |
---|
113 | ext.define_macros += [('HAVE_WIN_HACKS', 1)] |
---|
114 | |
---|
115 | ext.define_macros += [('HAVE_WAVWRITE', 1)] |
---|
116 | ext.define_macros += [('HAVE_WAVREAD', 1)] |
---|
117 | # TODO: |
---|
118 | # add cblas |
---|
119 | if 0: |
---|
120 | ext.libraries += ['cblas'] |
---|
121 | ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)] |
---|
122 | |
---|
123 | def add_system_aubio(ext): |
---|
124 | # use pkg-config to find aubio's location |
---|
125 | add_packages(['aubio'], ext) |
---|
126 | if 'aubio' not in ext.libraries: |
---|
127 | print("Error: libaubio not found") |
---|
128 | |
---|
129 | class CleanGenerated(distutils.command.clean.clean): |
---|
130 | def run(self): |
---|
131 | if os.path.isdir(output_path): |
---|
132 | distutils.dir_util.remove_tree(output_path) |
---|
133 | |
---|
134 | from distutils.command.build_ext import build_ext as _build_ext |
---|
135 | class build_ext(_build_ext): |
---|
136 | |
---|
137 | user_options = _build_ext.user_options + [ |
---|
138 | # The format is (long option, short option, description). |
---|
139 | ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'), |
---|
140 | ] |
---|
141 | |
---|
142 | def initialize_options(self): |
---|
143 | _build_ext.initialize_options(self) |
---|
144 | self.enable_double = False |
---|
145 | |
---|
146 | def finalize_options(self): |
---|
147 | _build_ext.finalize_options(self) |
---|
148 | if self.enable_double: |
---|
149 | self.announce( |
---|
150 | 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1', |
---|
151 | level=distutils.log.INFO) |
---|
152 | |
---|
153 | def build_extension(self, extension): |
---|
154 | if self.enable_double or 'HAVE_AUBIO_DOUBLE' in os.environ: |
---|
155 | extension.define_macros += [('HAVE_AUBIO_DOUBLE', 1)] |
---|
156 | enable_double = True |
---|
157 | else: |
---|
158 | enable_double = False |
---|
159 | # seack for aubio headers and lib in PKG_CONFIG_PATH |
---|
160 | add_system_aubio(extension) |
---|
161 | # the lib was not installed on this system |
---|
162 | if 'aubio' not in extension.libraries: |
---|
163 | # use local src/aubio.h |
---|
164 | if os.path.isfile(os.path.join('src', 'aubio.h')): |
---|
165 | add_local_aubio_header(extension) |
---|
166 | add_local_macros(extension) |
---|
167 | # look for a local waf build |
---|
168 | if os.path.isfile(os.path.join('build','src', 'fvec.c.1.o')): |
---|
169 | add_local_aubio_lib(extension) |
---|
170 | else: |
---|
171 | # check for external dependencies |
---|
172 | add_external_deps(extension, usedouble=enable_double) |
---|
173 | # add libaubio sources and look for optional deps with pkg-config |
---|
174 | add_local_aubio_sources(extension, usedouble=enable_double) |
---|
175 | # generate files python/gen/*.c, python/gen/aubio-generated.h |
---|
176 | extension.sources += generate_external(header, output_path, overwrite = False, |
---|
177 | usedouble=enable_double) |
---|
178 | return _build_ext.build_extension(self, extension) |
---|