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