python/lib/aubio/midiconv.py: improve unicode handling, skip UnicodeEncodeError on...
[aubio.git] / wscript
1 #! /usr/bin/python
2 #
3 # usage:
4 #   $ python waf --help
5 #
6 # example:
7 #   $ ./waf distclean configure build
8 #
9 # Note: aubio uses the waf build system, which relies on Python. Provided you
10 # have Python installed, you do *not* need to install anything to build aubio.
11 # For more info about waf, see http://code.google.com/p/waf/ .
12
13 import sys
14
15 APPNAME = 'aubio'
16
17 # source VERSION
18 for l in open('VERSION').readlines(): exec (l.strip())
19
20 VERSION = '.'.join ([str(x) for x in [
21     AUBIO_MAJOR_VERSION,
22     AUBIO_MINOR_VERSION,
23     AUBIO_PATCH_VERSION
24     ]]) + AUBIO_VERSION_STATUS
25
26 LIB_VERSION = '.'.join ([str(x) for x in [
27     LIBAUBIO_LT_CUR,
28     LIBAUBIO_LT_REV,
29     LIBAUBIO_LT_AGE]])
30
31 top = '.'
32 out = 'build'
33
34 def add_option_enable_disable(ctx, name, default = None,
35         help_str = None, help_disable_str = None):
36     if help_str == None:
37         help_str = 'enable ' + name + ' support'
38     if help_disable_str == None:
39         help_disable_str = 'do not ' + help_str
40     ctx.add_option('--enable-' + name, action = 'store_true',
41             default = default,
42             dest = 'enable_' + name.replace('-','_'),
43             help = help_str)
44     ctx.add_option('--disable-' + name, action = 'store_false',
45             #default = default,
46             dest = 'enable_' + name.replace('-','_'),
47             help = help_disable_str )
48
49 def options(ctx):
50     add_option_enable_disable(ctx, 'fftw3f', default = False,
51             help_str = 'compile with fftw3f instead of ooura (recommended)',
52             help_disable_str = 'do not compile with fftw3f')
53     add_option_enable_disable(ctx, 'fftw3', default = False,
54             help_str = 'compile with fftw3 instead of ooura',
55             help_disable_str = 'do not compile with fftw3')
56     add_option_enable_disable(ctx, 'complex', default = False,
57             help_str ='compile with C99 complex',
58             help_disable_str = 'do not use C99 complex (default)' )
59     add_option_enable_disable(ctx, 'jack', default = None,
60             help_str = 'compile with jack (auto)',
61             help_disable_str = 'disable jack support')
62     add_option_enable_disable(ctx, 'sndfile', default = None,
63             help_str = 'compile with sndfile (auto)',
64             help_disable_str = 'disable sndfile')
65     add_option_enable_disable(ctx, 'avcodec', default = None,
66             help_str = 'compile with libavcodec (auto)',
67             help_disable_str = 'disable libavcodec')
68     add_option_enable_disable(ctx, 'samplerate', default = None,
69             help_str = 'compile with samplerate (auto)',
70             help_disable_str = 'disable samplerate')
71     add_option_enable_disable(ctx, 'memcpy', default = True,
72             help_str = 'use memcpy hacks (default)',
73             help_disable_str = 'do not use memcpy hacks')
74     add_option_enable_disable(ctx, 'double', default = False,
75             help_str = 'compile in double precision mode',
76             help_disable_str = 'compile in single precision mode (default)')
77     add_option_enable_disable(ctx, 'fat', default = False,
78             help_str = 'build fat binaries (darwin only)',
79             help_disable_str = 'do not build fat binaries (default)')
80     add_option_enable_disable(ctx, 'accelerate', default = None,
81             help_str = 'use Accelerate framework (darwin only) (auto)',
82             help_disable_str = 'do not use Accelerate framework')
83     add_option_enable_disable(ctx, 'apple-audio', default = None,
84             help_str = 'use CoreFoundation (darwin only) (auto)',
85             help_disable_str = 'do not use CoreFoundation framework')
86     add_option_enable_disable(ctx, 'atlas', default = None,
87             help_str = 'use Atlas library (auto)',
88             help_disable_str = 'do not use Atlas library')
89     add_option_enable_disable(ctx, 'wavread', default = True,
90             help_str = 'compile with source_wavread (default)',
91             help_disable_str = 'do not compile source_wavread')
92     add_option_enable_disable(ctx, 'wavwrite', default = True,
93             help_str = 'compile with source_wavwrite (default)',
94             help_disable_str = 'do not compile source_wavwrite')
95
96     add_option_enable_disable(ctx, 'docs', default = None,
97             help_str = 'build documentation (auto)',
98             help_disable_str = 'do not build documentation')
99
100     ctx.add_option('--with-target-platform', type='string',
101             help='set target platform for cross-compilation', dest='target_platform')
102
103     ctx.load('compiler_c')
104     ctx.load('waf_unit_test')
105     ctx.load('gnu_dirs')
106
107 def configure(ctx):
108     from waflib import Options
109     ctx.load('compiler_c')
110     ctx.load('waf_unit_test')
111     ctx.load('gnu_dirs')
112
113     # check for common headers
114     ctx.check(header_name='stdlib.h')
115     ctx.check(header_name='stdio.h')
116     ctx.check(header_name='math.h')
117     ctx.check(header_name='string.h')
118     ctx.check(header_name='limits.h')
119     ctx.check(header_name='stdarg.h')
120     ctx.check(header_name='getopt.h', mandatory = False)
121     ctx.check(header_name='unistd.h', mandatory = False)
122
123     target_platform = sys.platform
124     if ctx.options.target_platform:
125         target_platform = ctx.options.target_platform
126     ctx.env['DEST_OS'] = target_platform
127
128     if ctx.env.CC_NAME != 'msvc':
129         ctx.env.CFLAGS += ['-g', '-Wall', '-Wextra']
130     else:
131         ctx.env.CFLAGS += ['/W4', '/MD']
132         ctx.env.CFLAGS += ['/D_CRT_SECURE_NO_WARNINGS']
133
134     ctx.check_cc(lib='m', uselib_store='M', mandatory=False)
135
136     if target_platform not in ['win32', 'win64']:
137         ctx.env.CFLAGS += ['-fPIC']
138     else:
139         ctx.define('HAVE_WIN_HACKS', 1)
140         ctx.env['cshlib_PATTERN'] = 'lib%s.dll'
141
142     if target_platform == 'darwin' and ctx.options.enable_fat:
143         ctx.env.CFLAGS += ['-arch', 'i386', '-arch', 'x86_64']
144         ctx.env.LINKFLAGS += ['-arch', 'i386', '-arch', 'x86_64']
145         MINSDKVER="10.4"
146         ctx.env.CFLAGS += [ '-mmacosx-version-min=' + MINSDKVER ]
147         ctx.env.LINKFLAGS += [ '-mmacosx-version-min=' + MINSDKVER ]
148
149     if target_platform in [ 'darwin', 'ios', 'iosimulator']:
150         if (ctx.options.enable_apple_audio != False):
151             ctx.env.FRAMEWORK += ['CoreFoundation', 'AudioToolbox']
152             ctx.define('HAVE_SOURCE_APPLE_AUDIO', 1)
153             ctx.define('HAVE_SINK_APPLE_AUDIO', 1)
154         if (ctx.options.enable_accelerate != False):
155             ctx.define('HAVE_ACCELERATE', 1)
156             ctx.env.FRAMEWORK += ['Accelerate']
157
158     if target_platform in [ 'ios', 'iosimulator' ]:
159         MINSDKVER="6.1"
160         ctx.env.CFLAGS += ['-std=c99']
161         if (ctx.options.enable_apple_audio != False):
162             ctx.define('HAVE_AUDIO_UNIT', 1)
163             #ctx.env.FRAMEWORK += ['CoreFoundation', 'AudioToolbox']
164         if target_platform == 'ios':
165             DEVROOT = "/Applications/Xcode.app/Contents"
166             DEVROOT += "/Developer/Platforms/iPhoneOS.platform/Developer"
167             SDKROOT = "%(DEVROOT)s/SDKs/iPhoneOS.sdk" % locals()
168             ctx.env.CFLAGS += [ '-fembed-bitcode' ]
169             ctx.env.CFLAGS += [ '-arch', 'arm64' ]
170             ctx.env.CFLAGS += [ '-arch', 'armv7' ]
171             ctx.env.CFLAGS += [ '-arch', 'armv7s' ]
172             ctx.env.LINKFLAGS += [ '-arch', 'arm64' ]
173             ctx.env.LINKFLAGS += ['-arch', 'armv7']
174             ctx.env.LINKFLAGS += ['-arch', 'armv7s']
175             ctx.env.CFLAGS += [ '-miphoneos-version-min=' + MINSDKVER ]
176             ctx.env.LINKFLAGS += [ '-miphoneos-version-min=' + MINSDKVER ]
177         else:
178             DEVROOT = "/Applications/Xcode.app/Contents"
179             DEVROOT += "/Developer/Platforms/iPhoneSimulator.platform/Developer"
180             SDKROOT = "%(DEVROOT)s/SDKs/iPhoneSimulator.sdk" % locals()
181             ctx.env.CFLAGS += [ '-arch', 'i386' ]
182             ctx.env.CFLAGS += [ '-arch', 'x86_64' ]
183             ctx.env.LINKFLAGS += ['-arch', 'i386']
184             ctx.env.LINKFLAGS += ['-arch', 'x86_64']
185             ctx.env.CFLAGS += [ '-mios-simulator-version-min=' + MINSDKVER ]
186             ctx.env.LINKFLAGS += [ '-mios-simulator-version-min=' + MINSDKVER ]
187         ctx.env.CFLAGS += [ '-isysroot' , SDKROOT]
188         ctx.env.LINKFLAGS += [ '-isysroot' , SDKROOT]
189
190     if target_platform == 'emscripten':
191         import os.path
192         ctx.env.CFLAGS += [ '-I' + os.path.join(os.environ['EMSCRIPTEN'], 'system', 'include') ]
193         ctx.env.CFLAGS += ['-Oz']
194         ctx.env.cprogram_PATTERN = "%s.js"
195         if (ctx.options.enable_atlas != True):
196             ctx.options.enable_atlas = False
197
198     # check support for C99 __VA_ARGS__ macros
199     check_c99_varargs = '''
200 #include <stdio.h>
201 #define AUBIO_ERR(...) fprintf(stderr, __VA_ARGS__)
202 '''
203
204     if ctx.check_cc(fragment = check_c99_varargs,
205             type='cstlib',
206             msg = 'Checking for C99 __VA_ARGS__ macro',
207             mandatory = False):
208         ctx.define('HAVE_C99_VARARGS_MACROS', 1)
209
210     # show a message about enable_double status
211     if (ctx.options.enable_double == True):
212         ctx.msg('Checking for size of smpl_t', 'double')
213         ctx.msg('Checking for size of lsmp_t', 'long double')
214     else:
215         ctx.msg('Checking for size of smpl_t', 'float')
216         ctx.msg('Checking for size of lsmp_t', 'double')
217
218     # optionally use complex.h
219     if (ctx.options.enable_complex == True):
220         ctx.check(header_name='complex.h')
221     else:
222         ctx.msg('Checking if complex.h is enabled', 'no')
223
224     # check for fftw3
225     if (ctx.options.enable_fftw3 != False or ctx.options.enable_fftw3f != False):
226         # one of fftwf or fftw3f
227         if (ctx.options.enable_fftw3f != False):
228             ctx.check_cfg(package = 'fftw3f', atleast_version = '3.0.0',
229                     args = '--cflags --libs',
230                     mandatory = ctx.options.enable_fftw3f)
231             if (ctx.options.enable_double == True):
232                 ctx.msg('Warning',
233                         'fftw3f enabled, but compiling in double precision!')
234         else:
235             # fftw3f disabled, take most sensible one according to
236             # enable_double
237             if (ctx.options.enable_double == True):
238                 ctx.check_cfg(package = 'fftw3', atleast_version = '3.0.0',
239                         args = '--cflags --libs', mandatory =
240                         ctx.options.enable_fftw3)
241             else:
242                 ctx.check_cfg(package = 'fftw3f', atleast_version = '3.0.0',
243                         args = '--cflags --libs',
244                         mandatory = ctx.options.enable_fftw3)
245         ctx.define('HAVE_FFTW3', 1)
246
247     # fftw not enabled, use vDSP or ooura
248     if 'HAVE_FFTW3F' in ctx.env.define_key:
249         ctx.msg('Checking for FFT implementation', 'fftw3f')
250     elif 'HAVE_FFTW3' in ctx.env.define_key:
251         ctx.msg('Checking for FFT implementation', 'fftw3')
252     elif 'HAVE_ACCELERATE' in ctx.env.define_key:
253         ctx.msg('Checking for FFT implementation', 'vDSP')
254     else:
255         ctx.msg('Checking for FFT implementation', 'ooura')
256
257     # check for libsndfile
258     if (ctx.options.enable_sndfile != False):
259         ctx.check_cfg(package = 'sndfile', atleast_version = '1.0.4',
260                 args = '--cflags --libs',
261                 mandatory = ctx.options.enable_sndfile)
262
263     # check for libsamplerate
264     if (ctx.options.enable_double):
265         if (ctx.options.enable_samplerate):
266             ctx.fatal("Could not compile aubio in double precision mode with libsamplerate")
267         else:
268             ctx.options.enable_samplerate = False
269             ctx.msg('Checking if using samplerate', 'no (disabled in double precision mode)',
270                     color = 'YELLOW')
271     if (ctx.options.enable_samplerate != False):
272         ctx.check_cfg(package = 'samplerate', atleast_version = '0.0.15',
273                 args = '--cflags --libs',
274                 mandatory = ctx.options.enable_samplerate)
275
276     # check for jack
277     if (ctx.options.enable_jack != False):
278         ctx.check_cfg(package = 'jack',
279                 args = '--cflags --libs',
280                 mandatory = ctx.options.enable_jack)
281
282     # check for libav
283     if (ctx.options.enable_avcodec != False):
284         ctx.check_cfg(package = 'libavcodec', atleast_version = '54.35.0',
285                 args = '--cflags --libs', uselib_store = 'AVCODEC',
286                 mandatory = ctx.options.enable_avcodec)
287         ctx.check_cfg(package = 'libavformat', atleast_version = '52.3.0',
288                 args = '--cflags --libs', uselib_store = 'AVFORMAT',
289                 mandatory = ctx.options.enable_avcodec)
290         ctx.check_cfg(package = 'libavutil', atleast_version = '52.3.0',
291                 args = '--cflags --libs', uselib_store = 'AVUTIL',
292                 mandatory = ctx.options.enable_avcodec)
293         ctx.check_cfg(package = 'libavresample', atleast_version = '1.0.1',
294                 args = '--cflags --libs', uselib_store = 'AVRESAMPLE',
295                 mandatory = ctx.options.enable_avcodec)
296         if all ( 'HAVE_' + i in ctx.env
297                 for i in ['AVCODEC', 'AVFORMAT', 'AVUTIL', 'AVRESAMPLE'] ):
298             ctx.define('HAVE_LIBAV', 1)
299             ctx.msg('Checking for all libav libraries', 'yes')
300         else:
301             ctx.msg('Checking for all libav libraries', 'not found', color = 'YELLOW')
302
303     if (ctx.options.enable_wavread != False):
304         ctx.define('HAVE_WAVREAD', 1)
305     ctx.msg('Checking if using source_wavread', ctx.options.enable_wavread and 'yes' or 'no')
306     if (ctx.options.enable_wavwrite!= False):
307         ctx.define('HAVE_WAVWRITE', 1)
308     ctx.msg('Checking if using sink_wavwrite', ctx.options.enable_wavwrite and 'yes' or 'no')
309
310     # use ATLAS
311     if (ctx.options.enable_atlas != False):
312         ctx.check(header_name = 'atlas/cblas.h', mandatory = ctx.options.enable_atlas)
313         #ctx.check(lib = 'lapack', uselib_store = 'LAPACK', mandatory = ctx.options.enable_atlas)
314         ctx.check(lib = 'cblas', uselib_store = 'BLAS', mandatory = ctx.options.enable_atlas)
315
316     # use memcpy hacks
317     if (ctx.options.enable_memcpy == True):
318         ctx.define('HAVE_MEMCPY_HACKS', 1)
319
320     # write configuration header
321     ctx.write_config_header('src/config.h')
322
323     # the following defines will be passed as arguments to the compiler
324     # instead of being written to src/config.h
325
326     # add some defines used in examples
327     ctx.define('AUBIO_PREFIX', ctx.env['PREFIX'])
328     ctx.define('PACKAGE', APPNAME)
329
330     # double precision mode
331     if (ctx.options.enable_double == True):
332         ctx.define('HAVE_AUBIO_DOUBLE', 1)
333
334     if (ctx.options.enable_docs != False):
335         # check if txt2man is installed, optional
336         try:
337           ctx.find_program('txt2man', var='TXT2MAN')
338         except ctx.errors.ConfigurationError:
339           ctx.to_log('txt2man was not found (ignoring)')
340
341         # check if doxygen is installed, optional
342         try:
343           ctx.find_program('doxygen', var='DOXYGEN')
344         except ctx.errors.ConfigurationError:
345           ctx.to_log('doxygen was not found (ignoring)')
346
347         # check if sphinx-build is installed, optional
348         try:
349           ctx.find_program('sphinx-build', var='SPHINX')
350         except ctx.errors.ConfigurationError:
351           ctx.to_log('sphinx-build was not found (ignoring)')
352
353 def build(bld):
354     bld.env['VERSION'] = VERSION
355     bld.env['LIB_VERSION'] = LIB_VERSION
356
357     # add sub directories
358     bld.recurse('src')
359     if bld.env['DEST_OS'] not in ['ios', 'iosimulator', 'android']:
360         bld.recurse('examples')
361         bld.recurse('tests')
362
363     bld( source = 'aubio.pc.in' )
364
365     # build manpages from txt files using txt2man
366     if bld.env['TXT2MAN']:
367         from waflib import TaskGen
368         if 'MANDIR' not in bld.env:
369             bld.env['MANDIR'] = bld.env['DATAROOTDIR'] + '/man'
370         rule_str = '${TXT2MAN} -t `basename ${TGT} | cut -f 1 -d . | tr a-z A-Z`'
371         rule_str += ' -r ${PACKAGE}\\ ${VERSION} -P ${PACKAGE}'
372         rule_str += ' -v ${PACKAGE}\\ User\\\'s\\ manual'
373         rule_str += ' -s 1 ${SRC} > ${TGT}'
374         TaskGen.declare_chain(
375                 name      = 'txt2man',
376                 rule      = rule_str,
377                 ext_in    = '.txt',
378                 ext_out   = '.1',
379                 reentrant = False,
380                 install_path =  '${MANDIR}/man1',
381                 )
382         bld( source = bld.path.ant_glob('doc/*.txt') )
383
384     # build documentation from source files using doxygen
385     if bld.env['DOXYGEN']:
386         bld( name = 'doxygen', rule = 'doxygen ${SRC} > /dev/null',
387                 source = 'doc/web.cfg',
388                 cwd = 'doc')
389         bld.install_files( '${DATAROOTDIR}' + '/doc/libaubio-doc',
390                 bld.path.ant_glob('doc/web/html/**'),
391                 cwd = bld.path.find_dir ('doc/web'),
392                 relative_trick = True)
393
394     # build documentation from source files using sphinx-build
395     if bld.env['SPHINX']:
396         bld( name = 'sphinx', rule = 'make html',
397                 source = ['doc/conf.py'] + bld.path.ant_glob('doc/**.rst'),
398                 cwd = 'doc')
399         bld.install_files( '${DATAROOTDIR}' + '/doc/libaubio-doc/sphinx',
400                 bld.path.ant_glob('doc/_build/html/**'),
401                 cwd = bld.path.find_dir ('doc/_build/html'),
402                 relative_trick = True)
403
404 def shutdown(bld):
405     from waflib import Logs
406     if bld.options.target_platform in ['ios', 'iosimulator']:
407         msg ='building for %s, contact the author for a commercial license' % bld.options.target_platform
408         Logs.pprint('RED', msg)
409         msg ='   Paul Brossier <piem@aubio.org>'
410         Logs.pprint('RED', msg)
411
412 def dist(ctx):
413     ctx.excl  = ' **/.waf-1* **/*~ **/*.pyc **/*.swp **/*.swo **/*.swn **/.lock-w* **/.git*'
414     ctx.excl += ' **/build/*'
415     ctx.excl += ' doc/_build'
416     ctx.excl += ' python/demos_*'
417     ctx.excl += ' **/python/gen **/python/build **/python/dist'
418     ctx.excl += ' **/python/ext/config.h'
419     ctx.excl += ' **/python/lib/aubio/_aubio.so'
420     ctx.excl += ' **.egg-info'
421     ctx.excl += ' **/**.zip **/**.tar.bz2'
422     ctx.excl += ' **/doc/full/* **/doc/web/*'
423     ctx.excl += ' **/python/*.db'
424     ctx.excl += ' **/python.old/*'
425     ctx.excl += ' **/python/*/*.old'
426     ctx.excl += ' **/python/tests/sounds'
427     ctx.excl += ' **/**.asc'
428     ctx.excl += ' **/dist*'
429     ctx.excl += ' **/.DS_Store'
430     ctx.excl += ' **/.travis.yml'
431     ctx.excl += ' **/.landscape.yml'
432     ctx.excl += ' **/.appveyor.yml'