Merge branch 'yin-optimizations' of https://github.com/emuell/aubio
[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     ctx.add_option('--build-type', action = 'store',
51             default = "release",
52             choices = ('debug', 'release'),
53             dest = 'build_type',
54             help = 'whether to compile with (--build_type=release) or without (--build_type=debug) '\
55               ' compiler opimizations [default: release]')
56     add_option_enable_disable(ctx, 'fftw3f', default = False,
57             help_str = 'compile with fftw3f instead of ooura (recommended)',
58             help_disable_str = 'do not compile with fftw3f')
59     add_option_enable_disable(ctx, 'fftw3', default = False,
60             help_str = 'compile with fftw3 instead of ooura',
61             help_disable_str = 'do not compile with fftw3')
62     add_option_enable_disable(ctx, 'complex', default = False,
63             help_str ='compile with C99 complex',
64             help_disable_str = 'do not use C99 complex (default)' )
65     add_option_enable_disable(ctx, 'jack', default = None,
66             help_str = 'compile with jack (auto)',
67             help_disable_str = 'disable jack support')
68     add_option_enable_disable(ctx, 'sndfile', default = None,
69             help_str = 'compile with sndfile (auto)',
70             help_disable_str = 'disable sndfile')
71     add_option_enable_disable(ctx, 'avcodec', default = None,
72             help_str = 'compile with libavcodec (auto)',
73             help_disable_str = 'disable libavcodec')
74     add_option_enable_disable(ctx, 'samplerate', default = None,
75             help_str = 'compile with samplerate (auto)',
76             help_disable_str = 'disable samplerate')
77     add_option_enable_disable(ctx, 'memcpy', default = True,
78             help_str = 'use memcpy hacks (default)',
79             help_disable_str = 'do not use memcpy hacks')
80     add_option_enable_disable(ctx, 'double', default = False,
81             help_str = 'compile in double precision mode',
82             help_disable_str = 'compile in single precision mode (default)')
83     add_option_enable_disable(ctx, 'fat', default = False,
84             help_str = 'build fat binaries (darwin only)',
85             help_disable_str = 'do not build fat binaries (default)')
86     add_option_enable_disable(ctx, 'accelerate', default = None,
87             help_str = 'use Accelerate framework (darwin only) (auto)',
88             help_disable_str = 'do not use Accelerate framework')
89     add_option_enable_disable(ctx, 'apple-audio', default = None,
90             help_str = 'use CoreFoundation (darwin only) (auto)',
91             help_disable_str = 'do not use CoreFoundation framework')
92     add_option_enable_disable(ctx, 'atlas', default = False,
93             help_str = 'use Atlas library (no)',
94             help_disable_str = 'do not use Atlas library')
95     add_option_enable_disable(ctx, 'wavread', default = True,
96             help_str = 'compile with source_wavread (default)',
97             help_disable_str = 'do not compile source_wavread')
98     add_option_enable_disable(ctx, 'wavwrite', default = True,
99             help_str = 'compile with source_wavwrite (default)',
100             help_disable_str = 'do not compile source_wavwrite')
101
102     add_option_enable_disable(ctx, 'docs', default = None,
103             help_str = 'build documentation (auto)',
104             help_disable_str = 'do not build documentation')
105
106     ctx.add_option('--with-target-platform', type='string',
107             help='set target platform for cross-compilation', dest='target_platform')
108
109     ctx.load('compiler_c')
110     ctx.load('waf_unit_test')
111     ctx.load('gnu_dirs')
112
113 def configure(ctx):
114     from waflib import Options
115     ctx.load('compiler_c')
116     ctx.load('waf_unit_test')
117     ctx.load('gnu_dirs')
118
119     # check for common headers
120     ctx.check(header_name='stdlib.h')
121     ctx.check(header_name='stdio.h')
122     ctx.check(header_name='math.h')
123     ctx.check(header_name='string.h')
124     ctx.check(header_name='limits.h')
125     ctx.check(header_name='stdarg.h')
126     ctx.check(header_name='getopt.h', mandatory = False)
127     ctx.check(header_name='unistd.h', mandatory = False)
128
129     target_platform = sys.platform
130     if ctx.options.target_platform:
131         target_platform = ctx.options.target_platform
132     ctx.env['DEST_OS'] = target_platform
133
134     if ctx.options.build_type == "debug":
135         ctx.define('DEBUG', 1)
136     else:
137         ctx.define('NDEBUG', 1)
138     
139     if ctx.env.CC_NAME != 'msvc':
140         # enable debug symbols and configure warnings
141         ctx.env.CFLAGS += ['-g', '-Wall', '-Wextra']
142         if ctx.options.build_type == "release":
143             # set optimization level 
144             ctx.env.CFLAGS += ['-O2']
145     else:
146         # enable debug symbols
147         ctx.env.CFLAGS += ['/Z7', '/FS']
148         ctx.env.LINKFLAGS += ['/DEBUG', '/INCREMENTAL:NO']
149         # configure warnings
150         ctx.env.CFLAGS += ['/W4', '/D_CRT_SECURE_NO_WARNINGS']
151         # set optimization level and runtime libs 
152         if (ctx.options.build_type == "release"):
153             ctx.env.CFLAGS += ['/Ox']
154             ctx.env.CFLAGS += ['/MD']
155         else:
156             assert(ctx.options.build_type == "debug")
157             ctx.env.CFLAGS += ['/MDd']
158         
159     ctx.check_cc(lib='m', uselib_store='M', mandatory=False)
160
161     if target_platform not in ['win32', 'win64']:
162         ctx.env.CFLAGS += ['-fPIC']
163     else:
164         ctx.define('HAVE_WIN_HACKS', 1)
165         ctx.env['cshlib_PATTERN'] = 'lib%s.dll'
166
167     if target_platform == 'darwin' and ctx.options.enable_fat:
168         ctx.env.CFLAGS += ['-arch', 'i386', '-arch', 'x86_64']
169         ctx.env.LINKFLAGS += ['-arch', 'i386', '-arch', 'x86_64']
170         MINSDKVER="10.4"
171         ctx.env.CFLAGS += [ '-mmacosx-version-min=' + MINSDKVER ]
172         ctx.env.LINKFLAGS += [ '-mmacosx-version-min=' + MINSDKVER ]
173
174     if target_platform in [ 'darwin', 'ios', 'iosimulator']:
175         if (ctx.options.enable_apple_audio != False):
176             ctx.env.FRAMEWORK += ['CoreFoundation', 'AudioToolbox']
177             ctx.define('HAVE_SOURCE_APPLE_AUDIO', 1)
178             ctx.define('HAVE_SINK_APPLE_AUDIO', 1)
179             ctx.msg('Checking for AudioToolbox.framework', 'yes')
180         else:
181             ctx.msg('Checking for AudioToolbox.framework', 'no (disabled)', color = 'YELLOW')
182         if (ctx.options.enable_accelerate != False):
183             ctx.define('HAVE_ACCELERATE', 1)
184             ctx.env.FRAMEWORK += ['Accelerate']
185             ctx.msg('Checking for Accelerate framework', 'yes')
186         else:
187             ctx.msg('Checking for Accelerate framework', 'no (disabled)', color = 'YELLOW')
188
189     if target_platform in [ 'ios', 'iosimulator' ]:
190         MINSDKVER="6.1"
191         ctx.env.CFLAGS += ['-std=c99']
192         if (ctx.options.enable_apple_audio != False):
193             ctx.define('HAVE_AUDIO_UNIT', 1)
194             #ctx.env.FRAMEWORK += ['CoreFoundation', 'AudioToolbox']
195         if target_platform == 'ios':
196             DEVROOT = "/Applications/Xcode.app/Contents"
197             DEVROOT += "/Developer/Platforms/iPhoneOS.platform/Developer"
198             SDKROOT = "%(DEVROOT)s/SDKs/iPhoneOS.sdk" % locals()
199             ctx.env.CFLAGS += [ '-fembed-bitcode' ]
200             ctx.env.CFLAGS += [ '-arch', 'arm64' ]
201             ctx.env.CFLAGS += [ '-arch', 'armv7' ]
202             ctx.env.CFLAGS += [ '-arch', 'armv7s' ]
203             ctx.env.LINKFLAGS += [ '-arch', 'arm64' ]
204             ctx.env.LINKFLAGS += ['-arch', 'armv7']
205             ctx.env.LINKFLAGS += ['-arch', 'armv7s']
206             ctx.env.CFLAGS += [ '-miphoneos-version-min=' + MINSDKVER ]
207             ctx.env.LINKFLAGS += [ '-miphoneos-version-min=' + MINSDKVER ]
208         else:
209             DEVROOT = "/Applications/Xcode.app/Contents"
210             DEVROOT += "/Developer/Platforms/iPhoneSimulator.platform/Developer"
211             SDKROOT = "%(DEVROOT)s/SDKs/iPhoneSimulator.sdk" % locals()
212             ctx.env.CFLAGS += [ '-arch', 'i386' ]
213             ctx.env.CFLAGS += [ '-arch', 'x86_64' ]
214             ctx.env.LINKFLAGS += ['-arch', 'i386']
215             ctx.env.LINKFLAGS += ['-arch', 'x86_64']
216             ctx.env.CFLAGS += [ '-mios-simulator-version-min=' + MINSDKVER ]
217             ctx.env.LINKFLAGS += [ '-mios-simulator-version-min=' + MINSDKVER ]
218         ctx.env.CFLAGS += [ '-isysroot' , SDKROOT]
219         ctx.env.LINKFLAGS += [ '-isysroot' , SDKROOT]
220
221     if target_platform == 'emscripten':
222         import os.path
223         ctx.env.CFLAGS += [ '-I' + os.path.join(os.environ['EMSCRIPTEN'], 'system', 'include') ]
224         ctx.env.CFLAGS += ['-Oz']
225         ctx.env.cprogram_PATTERN = "%s.js"
226         if (ctx.options.enable_atlas != True):
227             ctx.options.enable_atlas = False
228
229     # check support for C99 __VA_ARGS__ macros
230     check_c99_varargs = '''
231 #include <stdio.h>
232 #define AUBIO_ERR(...) fprintf(stderr, __VA_ARGS__)
233 '''
234
235     if ctx.check_cc(fragment = check_c99_varargs,
236             type='cstlib',
237             msg = 'Checking for C99 __VA_ARGS__ macro',
238             mandatory = False):
239         ctx.define('HAVE_C99_VARARGS_MACROS', 1)
240
241     # show a message about enable_double status
242     if (ctx.options.enable_double == True):
243         ctx.msg('Checking for size of smpl_t', 'double')
244         ctx.msg('Checking for size of lsmp_t', 'long double')
245     else:
246         ctx.msg('Checking for size of smpl_t', 'float')
247         ctx.msg('Checking for size of lsmp_t', 'double')
248
249     # optionally use complex.h
250     if (ctx.options.enable_complex == True):
251         ctx.check(header_name='complex.h')
252     else:
253         ctx.msg('Checking if complex.h is enabled', 'no')
254
255     # check for fftw3
256     if (ctx.options.enable_fftw3 != False or ctx.options.enable_fftw3f != False):
257         # one of fftwf or fftw3f
258         if (ctx.options.enable_fftw3f != False):
259             ctx.check_cfg(package = 'fftw3f', atleast_version = '3.0.0',
260                     args = '--cflags --libs',
261                     mandatory = ctx.options.enable_fftw3f)
262             if (ctx.options.enable_double == True):
263                 ctx.msg('Warning',
264                         'fftw3f enabled, but compiling in double precision!')
265         else:
266             # fftw3f disabled, take most sensible one according to
267             # enable_double
268             if (ctx.options.enable_double == True):
269                 ctx.check_cfg(package = 'fftw3', atleast_version = '3.0.0',
270                         args = '--cflags --libs', mandatory =
271                         ctx.options.enable_fftw3)
272             else:
273                 ctx.check_cfg(package = 'fftw3f', atleast_version = '3.0.0',
274                         args = '--cflags --libs',
275                         mandatory = ctx.options.enable_fftw3)
276         ctx.define('HAVE_FFTW3', 1)
277
278     # fftw not enabled, use vDSP or ooura
279     if 'HAVE_FFTW3F' in ctx.env.define_key:
280         ctx.msg('Checking for FFT implementation', 'fftw3f')
281     elif 'HAVE_FFTW3' in ctx.env.define_key:
282         ctx.msg('Checking for FFT implementation', 'fftw3')
283     elif 'HAVE_ACCELERATE' in ctx.env.define_key:
284         ctx.msg('Checking for FFT implementation', 'vDSP')
285     else:
286         ctx.msg('Checking for FFT implementation', 'ooura')
287
288     # check for libsndfile
289     if (ctx.options.enable_sndfile != False):
290         ctx.check_cfg(package = 'sndfile', atleast_version = '1.0.4',
291                 args = '--cflags --libs',
292                 mandatory = ctx.options.enable_sndfile)
293
294     # check for libsamplerate
295     if (ctx.options.enable_double):
296         if (ctx.options.enable_samplerate):
297             ctx.fatal("Could not compile aubio in double precision mode with libsamplerate")
298         else:
299             ctx.options.enable_samplerate = False
300             ctx.msg('Checking if using samplerate', 'no (disabled in double precision mode)',
301                     color = 'YELLOW')
302     if (ctx.options.enable_samplerate != False):
303         ctx.check_cfg(package = 'samplerate', atleast_version = '0.0.15',
304                 args = '--cflags --libs',
305                 mandatory = ctx.options.enable_samplerate)
306
307     # check for jack
308     if (ctx.options.enable_jack != False):
309         ctx.check_cfg(package = 'jack',
310                 args = '--cflags --libs',
311                 mandatory = ctx.options.enable_jack)
312
313     # check for libav
314     if (ctx.options.enable_avcodec != False):
315         ctx.check_cfg(package = 'libavcodec', atleast_version = '54.35.0',
316                 args = '--cflags --libs', uselib_store = 'AVCODEC',
317                 mandatory = ctx.options.enable_avcodec)
318         ctx.check_cfg(package = 'libavformat', atleast_version = '52.3.0',
319                 args = '--cflags --libs', uselib_store = 'AVFORMAT',
320                 mandatory = ctx.options.enable_avcodec)
321         ctx.check_cfg(package = 'libavutil', atleast_version = '52.3.0',
322                 args = '--cflags --libs', uselib_store = 'AVUTIL',
323                 mandatory = ctx.options.enable_avcodec)
324         ctx.check_cfg(package = 'libavresample', atleast_version = '1.0.1',
325                 args = '--cflags --libs', uselib_store = 'AVRESAMPLE',
326                 mandatory = ctx.options.enable_avcodec)
327         if all ( 'HAVE_' + i in ctx.env
328                 for i in ['AVCODEC', 'AVFORMAT', 'AVUTIL', 'AVRESAMPLE'] ):
329             ctx.define('HAVE_LIBAV', 1)
330             ctx.msg('Checking for all libav libraries', 'yes')
331         else:
332             ctx.msg('Checking for all libav libraries', 'not found', color = 'YELLOW')
333
334     if (ctx.options.enable_wavread != False):
335         ctx.define('HAVE_WAVREAD', 1)
336     ctx.msg('Checking if using source_wavread', ctx.options.enable_wavread and 'yes' or 'no')
337     if (ctx.options.enable_wavwrite!= False):
338         ctx.define('HAVE_WAVWRITE', 1)
339     ctx.msg('Checking if using sink_wavwrite', ctx.options.enable_wavwrite and 'yes' or 'no')
340
341     # use ATLAS
342     if (ctx.options.enable_atlas != False):
343         ctx.check(header_name = 'atlas/cblas.h', mandatory = ctx.options.enable_atlas)
344         #ctx.check(lib = 'lapack', uselib_store = 'LAPACK', mandatory = ctx.options.enable_atlas)
345         ctx.check(lib = 'cblas', uselib_store = 'BLAS', mandatory = ctx.options.enable_atlas)
346
347     # use memcpy hacks
348     if (ctx.options.enable_memcpy == True):
349         ctx.define('HAVE_MEMCPY_HACKS', 1)
350
351     # write configuration header
352     ctx.write_config_header('src/config.h')
353
354     # the following defines will be passed as arguments to the compiler
355     # instead of being written to src/config.h
356     ctx.define('HAVE_CONFIG_H', 1)
357
358     # add some defines used in examples
359     ctx.define('AUBIO_PREFIX', ctx.env['PREFIX'])
360     ctx.define('PACKAGE', APPNAME)
361
362     # double precision mode
363     if (ctx.options.enable_double == True):
364         ctx.define('HAVE_AUBIO_DOUBLE', 1)
365
366     if (ctx.options.enable_docs != False):
367         # check if txt2man is installed, optional
368         try:
369           ctx.find_program('txt2man', var='TXT2MAN')
370         except ctx.errors.ConfigurationError:
371           ctx.to_log('txt2man was not found (ignoring)')
372
373         # check if doxygen is installed, optional
374         try:
375           ctx.find_program('doxygen', var='DOXYGEN')
376         except ctx.errors.ConfigurationError:
377           ctx.to_log('doxygen was not found (ignoring)')
378
379         # check if sphinx-build is installed, optional
380         try:
381           ctx.find_program('sphinx-build', var='SPHINX')
382         except ctx.errors.ConfigurationError:
383           ctx.to_log('sphinx-build was not found (ignoring)')
384
385 def build(bld):
386     bld.env['VERSION'] = VERSION
387     bld.env['LIB_VERSION'] = LIB_VERSION
388
389     # main source
390     bld.recurse('src')
391
392     # add sub directories
393     if bld.env['DEST_OS'] not in ['ios', 'iosimulator', 'android']:
394         bld.recurse('examples')
395         bld.recurse('tests')
396
397     # pkg-config template
398     bld( source = 'aubio.pc.in' )
399
400     # documentation
401     txt2man(bld)
402     doxygen(bld)
403     sphinx(bld)
404
405 def txt2man(bld):
406     # build manpages from txt files using txt2man
407     if bld.env['TXT2MAN']:
408         from waflib import TaskGen
409         if 'MANDIR' not in bld.env:
410             bld.env['MANDIR'] = bld.env['DATAROOTDIR'] + '/man'
411         rule_str = '${TXT2MAN} -t `basename ${TGT} | cut -f 1 -d . | tr a-z A-Z`'
412         rule_str += ' -r ${PACKAGE}\\ ${VERSION} -P ${PACKAGE}'
413         rule_str += ' -v ${PACKAGE}\\ User\\\'s\\ manual'
414         rule_str += ' -s 1 ${SRC} > ${TGT}'
415         TaskGen.declare_chain(
416                 name      = 'txt2man',
417                 rule      = rule_str,
418                 ext_in    = '.txt',
419                 ext_out   = '.1',
420                 reentrant = False,
421                 install_path =  '${MANDIR}/man1',
422                 )
423         bld( source = bld.path.ant_glob('doc/*.txt') )
424
425 def doxygen(bld):
426     # build documentation from source files using doxygen
427     if bld.env['DOXYGEN']:
428         bld( name = 'doxygen', rule = 'doxygen ${SRC} > /dev/null',
429                 source = 'doc/web.cfg',
430                 cwd = 'doc')
431         bld.install_files( '${DATAROOTDIR}' + '/doc/libaubio-doc',
432                 bld.path.ant_glob('doc/web/html/**'),
433                 cwd = bld.path.find_dir ('doc/web'),
434                 relative_trick = True)
435
436 def sphinx(bld):
437     # build documentation from source files using sphinx-build
438     # note: build in ../doc/_build/html, otherwise waf wont install unsigned files
439     if bld.env['SPHINX']:
440         bld( name = 'sphinx',
441                 rule = '${SPHINX} -b html -a -q `dirname ${SRC}` `dirname ${TGT}`',
442                 source = 'doc/conf.py',
443                 target = '../doc/_build/html/index.html')
444         bld.install_files( '${DATAROOTDIR}' + '/doc/libaubio-doc/sphinx',
445                 bld.path.ant_glob('doc/_build/html/**'),
446                 cwd = bld.path.find_dir('doc/_build/html'),
447                 relative_trick = True)
448
449 # register the previous rules as build rules
450 from waflib.Build import BuildContext
451
452 class build_txt2man(BuildContext):
453     cmd = 'txt2man'
454     fun = 'txt2man'
455
456 class build_manpages(BuildContext):
457     cmd = 'manpages'
458     fun = 'txt2man'
459
460 class build_sphinx(BuildContext):
461     cmd = 'sphinx'
462     fun = 'sphinx'
463
464 class build_doxygen(BuildContext):
465     cmd = 'doxygen'
466     fun = 'doxygen'
467
468 def shutdown(bld):
469     from waflib import Logs
470     if bld.options.target_platform in ['ios', 'iosimulator']:
471         msg ='building for %s, contact the author for a commercial license' % bld.options.target_platform
472         Logs.pprint('RED', msg)
473         msg ='   Paul Brossier <piem@aubio.org>'
474         Logs.pprint('RED', msg)
475
476 def dist(ctx):
477     ctx.excl  = ' **/.waf* **/*~ **/*.pyc **/*.swp **/*.swo **/*.swn **/.lock-w* **/.git*'
478     ctx.excl += ' **/build/*'
479     ctx.excl += ' doc/_build'
480     ctx.excl += ' python/demos_*'
481     ctx.excl += ' **/python/gen **/python/build **/python/dist'
482     ctx.excl += ' **/python/ext/config.h'
483     ctx.excl += ' **/python/lib/aubio/_aubio.so'
484     ctx.excl += ' **.egg-info'
485     ctx.excl += ' **/**.zip **/**.tar.bz2'
486     ctx.excl += ' **.tar.bz2'
487     ctx.excl += ' **/doc/full/* **/doc/web/*'
488     ctx.excl += ' **/doc/full.cfg'
489     ctx.excl += ' **/python/*.db'
490     ctx.excl += ' **/python.old/*'
491     ctx.excl += ' **/python/*/*.old'
492     ctx.excl += ' **/python/tests/sounds'
493     ctx.excl += ' **/**.asc'
494     ctx.excl += ' **/dist*'
495     ctx.excl += ' **/.DS_Store'
496     ctx.excl += ' **/.travis.yml'
497     ctx.excl += ' **/.landscape.yml'
498     ctx.excl += ' **/.appveyor.yml'