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 | cmd = ['pkg-config', '--libs', '--cflags', package] |
---|
25 | try: |
---|
26 | tokens = subprocess.check_output(cmd) |
---|
27 | except Exception as e: |
---|
28 | print("Running \"{:s}\" failed: {:s}".format(' '.join(cmd), repr(e))) |
---|
29 | continue |
---|
30 | tokens = tokens.decode('utf8').split() |
---|
31 | for token in tokens: |
---|
32 | key = token[:2] |
---|
33 | try: |
---|
34 | arg = flag_map[key] |
---|
35 | value = token[2:] |
---|
36 | except KeyError: |
---|
37 | arg = 'extra_link_args' |
---|
38 | value = token |
---|
39 | kw.setdefault(arg, []).append(value) |
---|
40 | for key, value in iter(kw.items()): # remove duplicated |
---|
41 | kw[key] = list(set(value)) |
---|
42 | return kw |
---|
43 | |
---|
44 | def add_local_aubio_header(ext): |
---|
45 | """ use local "src/aubio.h", not <aubio/aubio.h>""" |
---|
46 | ext.define_macros += [('USE_LOCAL_AUBIO', 1)] |
---|
47 | ext.include_dirs += ['src'] # aubio.h |
---|
48 | |
---|
49 | def add_local_aubio_lib(ext): |
---|
50 | """ add locally built libaubio from build/src """ |
---|
51 | print("Info: using locally built libaubio") |
---|
52 | ext.library_dirs += [os.path.join('build', 'src')] |
---|
53 | ext.libraries += ['aubio'] |
---|
54 | |
---|
55 | def add_local_aubio_sources(ext): |
---|
56 | """ build aubio inside python module instead of linking against libaubio """ |
---|
57 | print("Warning: libaubio was not built with waf, adding src/") |
---|
58 | # create an empty header, macros will be passed on the command line |
---|
59 | fake_config_header = os.path.join('python', 'ext', 'config.h') |
---|
60 | distutils.file_util.write_file(fake_config_header, "") |
---|
61 | aubio_sources = glob.glob(os.path.join('src', '**.c')) |
---|
62 | aubio_sources += glob.glob(os.path.join('src', '*', '**.c')) |
---|
63 | ext.sources += aubio_sources |
---|
64 | # define macros (waf puts them in build/src/config.h) |
---|
65 | for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H', |
---|
66 | 'HAVE_MATH_H', 'HAVE_STRING_H', |
---|
67 | 'HAVE_C99_VARARGS_MACROS', |
---|
68 | 'HAVE_LIMITS_H', 'HAVE_MEMCPY_HACKS']: |
---|
69 | ext.define_macros += [(define_macro, 1)] |
---|
70 | |
---|
71 | # loof for additional packages |
---|
72 | print("Info: looking for *optional* additional packages") |
---|
73 | packages = ['libavcodec', 'libavformat', 'libavutil', 'libavresample', |
---|
74 | 'jack', |
---|
75 | 'sndfile', 'samplerate', |
---|
76 | #'fftw3f', |
---|
77 | ] |
---|
78 | add_packages(packages, ext=ext) |
---|
79 | if 'avcodec' in ext.libraries \ |
---|
80 | and 'avformat' in ext.libraries \ |
---|
81 | and 'avutil' in ext.libraries \ |
---|
82 | and 'avresample' in ext.libraries: |
---|
83 | ext.define_macros += [('HAVE_LIBAV', 1)] |
---|
84 | if 'jack' in ext.libraries: |
---|
85 | ext.define_macros += [('HAVE_JACK', 1)] |
---|
86 | if 'sndfile' in ext.libraries: |
---|
87 | ext.define_macros += [('HAVE_SNDFILE', 1)] |
---|
88 | if 'samplerate' in ext.libraries: |
---|
89 | ext.define_macros += [('HAVE_SAMPLERATE', 1)] |
---|
90 | if 'fftw3f' in ext.libraries: |
---|
91 | ext.define_macros += [('HAVE_FFTW3F', 1)] |
---|
92 | ext.define_macros += [('HAVE_FFTW3', 1)] |
---|
93 | |
---|
94 | # add accelerate on darwin |
---|
95 | if sys.platform.startswith('darwin'): |
---|
96 | ext.extra_link_args += ['-framework', 'Accelerate'] |
---|
97 | ext.define_macros += [('HAVE_ACCELERATE', 1)] |
---|
98 | ext.define_macros += [('HAVE_SOURCE_APPLE_AUDIO', 1)] |
---|
99 | ext.define_macros += [('HAVE_SINK_APPLE_AUDIO', 1)] |
---|
100 | |
---|
101 | if sys.platform.startswith('win'): |
---|
102 | ext.define_macros += [('HAVE_WIN_HACKS', 1)] |
---|
103 | |
---|
104 | ext.define_macros += [('HAVE_WAVWRITE', 1)] |
---|
105 | ext.define_macros += [('HAVE_WAVREAD', 1)] |
---|
106 | # TODO: |
---|
107 | # add cblas |
---|
108 | if 0: |
---|
109 | ext.libraries += ['cblas'] |
---|
110 | ext.define_macros += [('HAVE_ATLAS_CBLAS_H', 1)] |
---|
111 | |
---|
112 | def add_system_aubio(ext): |
---|
113 | # use pkg-config to find aubio's location |
---|
114 | add_packages(['aubio'], ext) |
---|
115 | if 'aubio' not in ext.libraries: |
---|
116 | print("Error: libaubio not found") |
---|
117 | |
---|
118 | class CleanGenerated(distutils.command.clean.clean): |
---|
119 | def run(self): |
---|
120 | distutils.dir_util.remove_tree(output_path) |
---|
121 | distutils.command.clean.clean.run(self) |
---|
122 | |
---|
123 | class GenerateCommand(distutils.cmd.Command): |
---|
124 | description = 'generate gen/gen-*.c files from ../src/aubio.h' |
---|
125 | user_options = [ |
---|
126 | # The format is (long option, short option, description). |
---|
127 | ('enable-double', None, 'use HAVE_AUBIO_DOUBLE=1 (default: 0)'), |
---|
128 | ] |
---|
129 | |
---|
130 | def initialize_options(self): |
---|
131 | self.enable_double = False |
---|
132 | |
---|
133 | def finalize_options(self): |
---|
134 | if self.enable_double: |
---|
135 | self.announce( |
---|
136 | 'will generate code for aubio compiled with HAVE_AUBIO_DOUBLE=1', |
---|
137 | level=distutils.log.INFO) |
---|
138 | |
---|
139 | def run(self): |
---|
140 | self.announce( 'Generating code', level=distutils.log.INFO) |
---|
141 | generated_object_files = generate_external(header, output_path, usedouble=self.enable_double) |
---|