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