make IO::disconnect (port,port) symmetric with IO::connect (port,port) by requiring...
[ardour.git] / autowaf.py
1 #!/usr/bin/env python
2 # Waf utilities for easily building standard unixey packages/libraries
3 # Licensed under the GNU GPL v2 or later, see COPYING file for details.
4 # Copyright (C) 2008 David Robillard
5 # Copyright (C) 2008 Nedko Arnaudov
6
7 import Configure
8 import Options
9 import Utils
10 import misc
11 import os
12 import subprocess
13 import sys
14 import glob
15
16 from TaskGen import feature, before, after
17
18 global g_is_child
19 g_is_child = False
20
21 # Only run autowaf hooks once (even if sub projects call several times)
22 global g_step
23 g_step = 0
24
25 # Compute dependencies globally
26 #import preproc
27 #preproc.go_absolute = True
28
29 @feature('cc', 'cxx')
30 @after('apply_lib_vars')
31 @before('apply_obj_vars_cc', 'apply_obj_vars_cxx')
32 def include_config_h(self):
33         self.env.append_value('INC_PATHS', self.bld.srcnode)
34
35 def set_options(opt):
36         "Add standard autowaf options if they havn't been added yet"
37         global g_step
38         if g_step > 0:
39                 return
40         opt.tool_options('compiler_cc')
41         opt.tool_options('compiler_cxx')
42         opt.add_option('--debug', action='store_true', default=False, dest='debug',
43                         help="Build debuggable binaries [Default: False]")
44         opt.add_option('--strict', action='store_true', default=False, dest='strict',
45                         help="Use strict compiler flags and show all warnings [Default: False]")
46         opt.add_option('--docs', action='store_true', default=False, dest='docs',
47                         help="Build documentation - requires doxygen [Default: False]")
48         opt.add_option('--bundle', action='store_true', default=False,
49                         help="Build a self-contained bundle [Default: False]")
50         opt.add_option('--bindir', type='string',
51                         help="Executable programs [Default: PREFIX/bin]")
52         opt.add_option('--libdir', type='string',
53                         help="Libraries [Default: PREFIX/lib]")
54         opt.add_option('--includedir', type='string',
55                         help="Header files [Default: PREFIX/include]")
56         opt.add_option('--datadir', type='string',
57                         help="Shared data [Default: PREFIX/share]")
58         opt.add_option('--configdir', type='string',
59                         help="Configuration data [Default: PREFIX/etc]")
60         opt.add_option('--mandir', type='string',
61                         help="Manual pages [Default: DATADIR/man]")
62         opt.add_option('--htmldir', type='string',
63                         help="HTML documentation [Default: DATADIR/doc/PACKAGE]")
64         opt.add_option('--lv2-user', action='store_true', default=False, dest='lv2_user',
65                         help="Install LV2 bundles to user-local location [Default: False]")
66         if sys.platform == "darwin":
67                 opt.add_option('--lv2dir', type='string',
68                                 help="LV2 bundles [Default: /Library/Audio/Plug-Ins/LV2]")
69         else:
70                 opt.add_option('--lv2dir', type='string',
71                                 help="LV2 bundles [Default: LIBDIR/lv2]")
72         g_step = 1
73
74 def check_header(conf, name, define='', mandatory=False):
75         "Check for a header iff it hasn't been checked for yet"
76         if type(conf.env['AUTOWAF_HEADERS']) != dict:
77                 conf.env['AUTOWAF_HEADERS'] = {}
78
79         checked = conf.env['AUTOWAF_HEADERS']
80         if not name in checked:
81                 checked[name] = True
82                 includes = '' # search default system include paths
83                 if sys.platform == "darwin":
84                         includes = '/opt/local/include'
85                 if define != '':
86                         conf.check(header_name=name, includes=includes, define_name=define, mandatory=mandatory)
87                 else:
88                         conf.check(header_name=name, includes=includes, mandatory=mandatory)
89
90 def nameify(name):
91         return name.replace('/', '_').replace('++', 'PP').replace('-', '_').replace('.', '_')
92
93 def check_pkg(conf, name, **args):
94         if not 'mandatory' in args:
95                 args['mandatory'] = True
96         "Check for a package iff it hasn't been checked for yet"
97         var_name = 'HAVE_' + nameify(args['uselib_store'])
98         check = not var_name in conf.env
99         if not check and 'atleast_version' in args:
100                 # Re-check if version is newer than previous check
101                 checked_version = conf.env['VERSION_' + name]
102                 if checked_version and checked_version < args['atleast_version']:
103                         check = True;
104         if check:
105                 conf.check_cfg(package=name, args="--cflags --libs", **args)
106                 found = bool(conf.env[var_name])
107                 if found:
108                         conf.define(var_name, int(found))
109                         if 'atleast_version' in args:
110                                 conf.env['VERSION_' + name] = args['atleast_version']
111                 else:
112                         conf.undefine(var_name)
113                         if args['mandatory'] == True:
114                                 conf.fatal("Required package " + name + " not found")
115
116 def configure(conf):
117         global g_step
118         if g_step > 1:
119                 return
120         def append_cxx_flags(vals):
121                 conf.env.append_value('CCFLAGS', vals.split())
122                 conf.env.append_value('CXXFLAGS', vals.split())
123         display_header('Global Configuration')
124         conf.check_tool('misc')
125         conf.check_tool('compiler_cc')
126         conf.check_tool('compiler_cxx')
127         conf.env['DOCS'] = Options.options.docs
128         conf.env['DEBUG'] = Options.options.debug
129         conf.env['STRICT'] = Options.options.strict
130         conf.env['PREFIX'] = os.path.abspath(os.path.expanduser(os.path.normpath(conf.env['PREFIX'])))
131         if Options.options.bundle:
132                 conf.env['BUNDLE'] = True
133                 conf.define('BUNDLE', 1)
134                 conf.env['BINDIR'] = conf.env['PREFIX']
135                 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'Headers')
136                 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'Libraries')
137                 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'Resources')
138                 conf.env['HTMLDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Documentation')
139                 conf.env['MANDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Man')
140                 conf.env['LV2DIR'] = os.path.join(conf.env['PREFIX'], 'PlugIns')
141         else:
142                 conf.env['BUNDLE'] = False
143                 if Options.options.bindir:
144                         conf.env['BINDIR'] = Options.options.bindir
145                 else:
146                         conf.env['BINDIR'] = os.path.join(conf.env['PREFIX'], 'bin')
147                 if Options.options.includedir:
148                         conf.env['INCLUDEDIR'] = Options.options.includedir
149                 else:
150                         conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'include')
151                 if Options.options.libdir:
152                         conf.env['LIBDIR'] = Options.options.libdir
153                 else:
154                         conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'lib')
155                 if Options.options.datadir:
156                         conf.env['DATADIR'] = Options.options.datadir
157                 else:
158                         conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'share')
159                 if Options.options.configdir:
160                         conf.env['CONFIGDIR'] = Options.options.configdir
161                 else:
162                         conf.env['CONFIGDIR'] = os.path.join(conf.env['PREFIX'], 'etc')
163                 if Options.options.htmldir:
164                         conf.env['HTMLDIR'] = Options.options.htmldir
165                 else:
166                         conf.env['HTMLDIR'] = os.path.join(conf.env['DATADIR'], 'doc', Utils.g_module.APPNAME)
167                 if Options.options.mandir:
168                         conf.env['MANDIR'] = Options.options.mandir
169                 else:
170                         conf.env['MANDIR'] = os.path.join(conf.env['DATADIR'], 'man')
171                 if Options.options.lv2dir:
172                         conf.env['LV2DIR'] = Options.options.lv2dir
173                 else:
174                         if Options.options.lv2_user:
175                                 if sys.platform == "darwin":
176                                         conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), 'Library/Audio/Plug-Ins/LV2')
177                                 else:
178                                         conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), '.lv2')
179                         else:
180                                 if sys.platform == "darwin":
181                                         conf.env['LV2DIR'] = '/Library/Audio/Plug-Ins/LV2'
182                                 else:
183                                         conf.env['LV2DIR'] = os.path.join(conf.env['LIBDIR'], 'lv2')
184                 
185         conf.env['BINDIRNAME'] = os.path.basename(conf.env['BINDIR'])
186         conf.env['LIBDIRNAME'] = os.path.basename(conf.env['LIBDIR'])
187         conf.env['DATADIRNAME'] = os.path.basename(conf.env['DATADIR'])
188         conf.env['CONFIGDIRNAME'] = os.path.basename(conf.env['CONFIGDIR'])
189         conf.env['LV2DIRNAME'] = os.path.basename(conf.env['LV2DIR'])
190
191         if Options.options.docs:
192                 doxygen = conf.find_program('doxygen')
193                 if not doxygen:
194                         conf.fatal("Doxygen is required to build documentation, configure without --docs")
195
196                 dot = conf.find_program('dot')
197                 if not dot:
198                         conf.fatal("Graphviz (dot) is required to build documentation, configure without --docs")
199                 
200         if Options.options.debug:
201                 conf.env['CCFLAGS'] = [ '-O0', '-g' ]
202                 conf.env['CXXFLAGS'] = [ '-O0',  '-g' ]
203         else:
204                 append_cxx_flags('-DNDEBUG')
205
206         if Options.options.strict:
207                 conf.env.append_value('CCFLAGS', [ '-std=c99', '-pedantic' ])
208                 conf.env.append_value('CXXFLAGS', [ '-ansi', '-Woverloaded-virtual', '-Wnon-virtual-dtor'])
209                 append_cxx_flags('-Wall -Wextra -Wno-unused-parameter')
210
211         append_cxx_flags('-fPIC -DPIC -fshow-column')
212
213         display_msg(conf, "Install prefix", conf.env['PREFIX'])
214         display_msg(conf, "Debuggable build", str(conf.env['DEBUG']))
215         display_msg(conf, "Strict compiler flags", str(conf.env['STRICT']))
216         display_msg(conf, "Build documentation", str(conf.env['DOCS']))
217         print()
218
219         g_step = 2
220         
221 def set_local_lib(conf, name, has_objects):
222         conf.define('HAVE_' + nameify(name.upper()), 1)
223         if has_objects:
224                 if type(conf.env['AUTOWAF_LOCAL_LIBS']) != dict:
225                         conf.env['AUTOWAF_LOCAL_LIBS'] = {}
226                 conf.env['AUTOWAF_LOCAL_LIBS'][name.lower()] = True
227         else:
228                 if type(conf.env['AUTOWAF_LOCAL_HEADERS']) != dict:
229                         conf.env['AUTOWAF_LOCAL_HEADERS'] = {}
230                 conf.env['AUTOWAF_LOCAL_HEADERS'][name.lower()] = True
231
232 def use_lib(bld, obj, libs):
233         abssrcdir = os.path.abspath('.')
234         libs_list = libs.split()
235         for l in libs_list:
236                 in_headers = l.lower() in bld.env['AUTOWAF_LOCAL_HEADERS']
237                 in_libs    = l.lower() in bld.env['AUTOWAF_LOCAL_LIBS']
238                 if in_libs:
239                         if hasattr(obj, 'uselib_local'):
240                                 obj.uselib_local += ' lib' + l.lower() + ' '
241                         else:
242                                 obj.uselib_local = 'lib' + l.lower() + ' '
243                 
244                 if in_headers or in_libs:
245                         inc_flag = '-iquote ' + os.path.join(abssrcdir, l.lower())
246                         for f in ['CCFLAGS', 'CXXFLAGS']:
247                                 if not inc_flag in bld.env[f]:
248                                         bld.env.append_value(f, inc_flag)
249                 else:
250                         if hasattr(obj, 'uselib'):
251                                 obj.uselib += ' ' + l
252                         else:
253                                 obj.uselib = l
254
255
256 def display_header(title):
257         Utils.pprint('BOLD', title)
258
259 def display_msg(conf, msg, status = None, color = None):
260         color = 'CYAN'
261         if type(status) == bool and status or status == "True":
262                 color = 'GREEN'
263         elif type(status) == bool and not status or status == "False":
264                 color = 'YELLOW'
265         Utils.pprint('BOLD', "%s :" % msg.ljust(conf.line_just), sep='')
266         Utils.pprint(color, status)
267
268 def link_flags(env, lib):
269         return ' '.join(map(lambda x: env['LIB_ST'] % x, env['LIB_' + lib]))
270
271 def compile_flags(env, lib):
272         return ' '.join(map(lambda x: env['CPPPATH_ST'] % x, env['CPPPATH_' + lib]))
273
274 def set_recursive():
275         global g_is_child
276         g_is_child = True
277
278 def is_child():
279         global g_is_child
280         return g_is_child
281
282 # Pkg-config file
283 def build_pc(bld, name, version, libs):
284         '''Build a pkg-config file for a library.
285         name    -- uppercase variable name     (e.g. 'SOMENAME')
286         version -- version string              (e.g. '1.2.3')
287         libs    -- string/list of dependencies (e.g. 'LIBFOO GLIB')
288         '''
289
290         obj              = bld.new_task_gen('subst')
291         obj.source       = name.lower() + '.pc.in'
292         obj.target       = name.lower() + '.pc'
293         obj.install_path = '${PREFIX}/${LIBDIRNAME}/pkgconfig'
294         pkg_prefix       = bld.env['PREFIX'] 
295         if pkg_prefix[-1] == '/':
296                 pkg_prefix = pkg_prefix[:-1]
297         obj.dict = {
298                 'prefix'           : pkg_prefix,
299                 'exec_prefix'      : '${prefix}',
300                 'libdir'           : '${prefix}/' + bld.env['LIBDIRNAME'],
301                 'includedir'       : '${prefix}/include',
302                 name + '_VERSION'  : version,
303         }
304         if type(libs) != list:
305                 libs = libs.split()
306         for i in libs:
307                 obj.dict[i + '_LIBS']   = link_flags(bld.env, i)
308                 obj.dict[i + '_CFLAGS'] = compile_flags(bld.env, i)
309
310 # Doxygen API documentation
311 def build_dox(bld, name, version, srcdir, blddir):
312         if not bld.env['DOCS']:
313                 return
314         obj = bld.new_task_gen('subst')
315         obj.source = 'doc/reference.doxygen.in'
316         obj.target = 'doc/reference.doxygen'
317         if is_child():
318                 src_dir = os.path.join(srcdir, name.lower())
319                 doc_dir = os.path.join(blddir, 'default', name.lower(), 'doc')
320         else:
321                 src_dir = srcdir
322                 doc_dir = os.path.join(blddir, 'default', 'doc')
323         obj.dict = {
324                 name + '_VERSION' : version,
325                 name + '_SRCDIR'  : os.path.abspath(src_dir),
326                 name + '_DOC_DIR' : os.path.abspath(doc_dir)
327         }
328         obj.install_path = ''
329         out1 = bld.new_task_gen('command-output')
330         out1.dependencies = [obj]
331         out1.stdout = '/doc/doxygen.out'
332         out1.stdin = '/doc/reference.doxygen' # whatever..
333         out1.command = 'doxygen'
334         out1.argv = [os.path.abspath(doc_dir) + '/reference.doxygen']
335         out1.command_is_external = True
336
337 # Version code file generation
338 def build_version_files(header_path, source_path, domain, major, minor, micro):
339         header_path = os.path.abspath(header_path)
340         source_path = os.path.abspath(source_path)
341         text  = "int " + domain + "_major_version = " + str(major) + ";\n"
342         text += "int " + domain + "_minor_version = " + str(minor) + ";\n"
343         text += "int " + domain + "_micro_version = " + str(micro) + ";\n"
344         try:
345                 o = open(source_path, 'w')
346                 o.write(text)
347                 o.close()
348         except IOError:
349                 print("Could not open %s for writing\n", source_path)
350                 sys.exit(-1)
351
352         text  = "#ifndef __" + domain + "_version_h__\n"
353         text += "#define __" + domain + "_version_h__\n"
354         text += "extern const char* " + domain + "_revision;\n"
355         text += "extern int " + domain + "_major_version;\n"
356         text += "extern int " + domain + "_minor_version;\n"
357         text += "extern int " + domain + "_micro_version;\n"
358         text += "#endif /* __" + domain + "_version_h__ */\n"
359         try:
360                 o = open(header_path, 'w')
361                 o.write(text)
362                 o.close()
363         except IOError:
364                 print("Could not open %s for writing\n", header_path)
365                 sys.exit(-1)
366                 
367         return None
368
369 def run_tests(ctx, appname, tests):
370         orig_dir = os.path.abspath(os.curdir)
371         failures = 0
372         base = '..'
373
374         top_level = os.path.abspath(ctx.curdir) != os.path.abspath(os.curdir)
375         if top_level:
376                 os.chdir('./build/default/' + appname)
377                 base = '../..'
378         else:
379                 os.chdir('./build/default')
380
381         lcov = True
382         lcov_log = open('lcov.log', 'w')
383         try:
384                 # Clear coverage data
385                 subprocess.call('lcov -d ./src -z'.split(),
386                                 stdout=lcov_log, stderr=lcov_log)
387         except:
388                 lcov = False
389                 print("Failed to run lcov, no coverage report will be generated")
390
391
392         # Run all tests
393         for i in tests:
394                 print()
395                 Utils.pprint('BOLD', 'Running %s test %s' % (appname, i))
396                 if subprocess.call(i) == 0:
397                         Utils.pprint('GREEN', 'Passed %s %s' % (appname, i))
398                 else:
399                         failures += 1
400                         Utils.pprint('RED', 'Failed %s %s' % (appname, i))
401
402         if lcov:
403                 # Generate coverage data
404                 coverage_lcov = open('coverage.lcov', 'w')
405                 subprocess.call(('lcov -c -d ./src -d ./test -b ' + base).split(),
406                                 stdout=coverage_lcov, stderr=lcov_log)
407                 coverage_lcov.close()
408                 
409                 # Strip out unwanted stuff
410                 coverage_stripped_lcov = open('coverage-stripped.lcov', 'w')
411                 subprocess.call('lcov --remove coverage.lcov *boost* c++*'.split(),
412                                 stdout=coverage_stripped_lcov, stderr=lcov_log)
413                 coverage_stripped_lcov.close()
414         
415                 # Generate HTML coverage output
416                 if not os.path.isdir('./coverage'):
417                         os.makedirs('./coverage')
418                 subprocess.call('genhtml -o coverage coverage-stripped.lcov'.split(),
419                                 stdout=lcov_log, stderr=lcov_log)
420         
421         lcov_log.close()
422
423         print()
424         Utils.pprint('BOLD', 'Summary:', sep=''),
425         if failures == 0:
426                 Utils.pprint('GREEN', 'All ' + appname + ' tests passed')
427         else:
428                 Utils.pprint('RED', str(failures) + ' ' + appname + ' test(s) failed')
429
430         Utils.pprint('BOLD', 'Coverage:', sep='')
431         print(os.path.abspath('coverage/index.html'))
432
433         os.chdir(orig_dir)
434
435 def shutdown():
436         # This isn't really correct (for packaging), but people asking is annoying
437         if Options.commands['install']:
438                 try: os.popen("/sbin/ldconfig")
439                 except: pass
440
441 def build_i18n(bld,srcdir,dir,name,sources):
442         pwd = bld.get_curdir()
443         os.chdir(os.path.join (srcdir, dir))
444
445         pot_file = '%s.pot' % name
446
447         args = [ 'xgettext',
448                  '--keyword=_',
449                  '--keyword=N_',
450                  '--from-code=UTF-8',
451                  '-o', pot_file,
452                  '--copyright-holder="Paul Davis"' ]
453         args += sources
454         print 'Updating ', pot_file
455         os.spawnvp (os.P_WAIT, 'xgettext', args)
456         
457         po_files = glob.glob ('po/*.po')
458         languages = [ po.replace ('.po', '') for po in po_files ]
459         
460         for po_file in po_files:
461                 args = [ 'msgmerge',
462                          '--update',
463                          po_file,
464                          pot_file ]
465                 print 'Updating ', po_file
466                 os.spawnvp (os.P_WAIT, 'msgmerge', args)
467                 
468         for po_file in po_files:
469                 mo_file = po_file.replace ('.po', '.mo')
470                 args = [ 'msgfmt',
471                          '-c',
472                          '-o',
473                          mo_file,
474                          po_file ]
475                 print 'Generating ', po_file
476                 os.spawnvp (os.P_WAIT, 'msgfmt', args)
477         os.chdir (pwd)