carl's wondrous DnD VBox patch - processor boxes are now vboxes and not listviews...
[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 Dave Robillard
5 # Copyright (C) 2008 Nedko Arnaudov
6
7 import os
8 import misc
9 import Configure
10 import Options
11 import Utils
12 import sys
13 from TaskGen import feature, before, after
14
15 global g_is_child
16 g_is_child = False
17
18 # Only run autowaf hooks once (even if sub projects call several times)
19 global g_step
20 g_step = 0
21
22 # Compute dependencies globally
23 #import preproc
24 #preproc.go_absolute = True
25
26 @feature('cc', 'cxx')
27 @after('apply_lib_vars')
28 @before('apply_obj_vars_cc', 'apply_obj_vars_cxx')
29 def include_config_h(self):
30         self.env.append_value('INC_PATHS', self.bld.srcnode)
31
32 def set_options(opt):
33         "Add standard autowaf options if they havn't been added yet"
34         global g_step
35         if g_step > 0:
36                 return
37         opt.tool_options('compiler_cc')
38         opt.tool_options('compiler_cxx')
39         opt.add_option('--debug', action='store_true', default=False, dest='debug',
40                         help="Build debuggable binaries [Default: False]")
41         opt.add_option('--strict', action='store_true', default=False, dest='strict',
42                         help="Use strict compiler flags and show all warnings [Default: False]")
43         opt.add_option('--build-docs', action='store_true', default=False, dest='build_docs',
44                         help="Build documentation - requires doxygen [Default: False]")
45         opt.add_option('--bundle', action='store_true', default=False,
46                         help="Build a self-contained bundle [Default: False]")
47         opt.add_option('--bindir', type='string',
48                         help="Executable programs [Default: PREFIX/bin]")
49         opt.add_option('--libdir', type='string',
50                         help="Libraries [Default: PREFIX/lib]")
51         opt.add_option('--includedir', type='string',
52                         help="Header files [Default: PREFIX/include]")
53         opt.add_option('--datadir', type='string',
54                         help="Shared data [Default: PREFIX/share]")
55         opt.add_option('--configdir', type='string',
56                         help="Configuration data [Default: PREFIX/etc]")
57         opt.add_option('--mandir', type='string',
58                         help="Manual pages [Default: DATADIR/man]")
59         opt.add_option('--htmldir', type='string',
60                         help="HTML documentation [Default: DATADIR/doc/PACKAGE]")
61         opt.add_option('--lv2-user', action='store_true', default=False, dest='lv2_user',
62                         help="Install LV2 bundles to user-local location [Default: False]")
63         if sys.platform == "darwin":
64                 opt.add_option('--lv2dir', type='string',
65                                 help="LV2 bundles [Default: /Library/Audio/Plug-Ins/LV2]")
66         else:
67                 opt.add_option('--lv2dir', type='string',
68                                 help="LV2 bundles [Default: LIBDIR/lv2]")
69         g_step = 1
70
71 def check_header(conf, name, define='', mandatory=False):
72         "Check for a header iff it hasn't been checked for yet"
73         if type(conf.env['AUTOWAF_HEADERS']) != dict:
74                 conf.env['AUTOWAF_HEADERS'] = {}
75
76         checked = conf.env['AUTOWAF_HEADERS']
77         if not name in checked:
78                 checked[name] = True
79                 if define != '':
80                         conf.check(header_name=name, define_name=define, mandatory=mandatory)
81                 else:
82                         conf.check(header_name=name, mandatory=mandatory)
83
84 def nameify(name):
85         return name.replace('/', '_').replace('++', 'PP').replace('-', '_').replace('.', '_')
86
87 def check_pkg(conf, name, **args):
88         if not 'mandatory' in args:
89                 args['mandatory'] = True
90         "Check for a package iff it hasn't been checked for yet"
91         var_name = 'HAVE_' + nameify(args['uselib_store'])
92         check = not var_name in conf.env
93         if not check and 'atleast_version' in args:
94                 # Re-check if version is newer than previous check
95                 checked_version = conf.env['VERSION_' + name]
96                 if checked_version and checked_version < args['atleast_version']:
97                         check = True;
98         if check:
99                 conf.check_cfg(package=name, args="--cflags --libs", **args)
100                 found = bool(conf.env[var_name])
101                 if found:
102                         conf.define(var_name, int(found))
103                         if 'atleast_version' in args:
104                                 conf.env['VERSION_' + name] = args['atleast_version']
105                 else:
106                         conf.undefine(var_name)
107                         if args['mandatory'] == True:
108                                 conf.fatal("Required package " + name + " not found")
109
110 def chop_prefix(conf, var):
111         name = conf.env[var][len(conf.env['PREFIX']):]
112         if len(name) > 0 and name[0] == '/':
113                 name = name[1:]
114         if name == "":
115                 name = "/"
116         return name;
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         conf.line_just = 43
126         conf.check_tool('misc')
127         conf.check_tool('compiler_cc')
128         conf.check_tool('compiler_cxx')
129         conf.env['BUILD_DOCS'] = Options.options.build_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
134         if Options.options.bundle:
135                 conf.env['BUNDLE'] = True
136                 conf.define('BUNDLE', 1)
137                 conf.env['BINDIR'] = conf.env['PREFIX']
138                 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'Headers')
139                 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'Libraries')
140                 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'Resources')
141                 conf.env['HTMLDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Documentation')
142                 conf.env['MANDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Man')
143                 conf.env['LV2DIR'] = os.path.join(conf.env['PREFIX'], 'PlugIns')
144         else:
145                 conf.env['BUNDLE'] = False
146                 if Options.options.bindir:
147                         conf.env['BINDIR'] = Options.options.bindir
148                 else:
149                         conf.env['BINDIR'] = os.path.join(conf.env['PREFIX'], 'bin')
150                 if Options.options.includedir:
151                         conf.env['INCLUDEDIR'] = Options.options.includedir
152                 else:
153                         conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'include')
154                 if Options.options.libdir:
155                         conf.env['LIBDIR'] = Options.options.libdir
156                 else:
157                         conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'lib')
158                 if Options.options.datadir:
159                         conf.env['DATADIR'] = Options.options.datadir
160                 else:
161                         conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'share')
162                 if Options.options.configdir:
163                         conf.env['CONFIGDIR'] = Options.options.configdir
164                 else:
165                         conf.env['CONFIGDIR'] = os.path.join(conf.env['PREFIX'], 'etc')
166                 if Options.options.htmldir:
167                         conf.env['HTMLDIR'] = Options.options.htmldir
168                 else:
169                         conf.env['HTMLDIR'] = os.path.join(conf.env['DATADIR'], 'doc', Utils.g_module.APPNAME)
170                 if Options.options.mandir:
171                         conf.env['MANDIR'] = Options.options.mandir
172                 else:
173                         conf.env['MANDIR'] = os.path.join(conf.env['DATADIR'], 'man')
174                 if Options.options.lv2dir:
175                         conf.env['LV2DIR'] = Options.options.lv2dir
176                 else:
177                         if Options.options.lv2_user:
178                                 if sys.platform == "darwin":
179                                         conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), 'Library/Audio/Plug-Ins/LV2')
180                                 else:
181                                         conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), '.lv2')
182                         else:
183                                 if sys.platform == "darwin":
184                                         conf.env['LV2DIR'] = '/Library/Audio/Plug-Ins/LV2'
185                                 else:
186                                         conf.env['LV2DIR'] = os.path.join(conf.env['LIBDIR'], 'lv2')
187                 
188         conf.env['BINDIRNAME'] = chop_prefix(conf, 'BINDIR')
189         conf.env['LIBDIRNAME'] = chop_prefix(conf, 'LIBDIR')
190         conf.env['DATADIRNAME'] = chop_prefix(conf, 'DATADIR')
191         conf.env['CONFIGDIRNAME'] = chop_prefix(conf, 'CONFIGDIR')
192         conf.env['LV2DIRNAME'] = chop_prefix(conf, 'LV2DIR')
193         
194         if Options.options.debug:
195                 conf.env['CCFLAGS'] = [ '-O0', '-g' ]
196                 conf.env['CXXFLAGS'] = [ '-O0',  '-g' ]
197         else:
198                 append_cxx_flags('-DNDEBUG')
199         if Options.options.strict:
200                 conf.env.append_value('CCFLAGS', [ '-std=c99', '-pedantic' ])
201                 conf.env.append_value('CXXFLAGS', [ '-ansi', '-Woverloaded-virtual'])
202                 append_cxx_flags('-Wall -Wextra -Wno-unused-parameter')
203         append_cxx_flags('-fPIC -DPIC -fshow-column')
204         g_step = 2
205         
206 def set_local_lib(conf, name, has_objects):
207         conf.define('HAVE_' + nameify(name.upper()), 1)
208         if has_objects:
209                 if type(conf.env['AUTOWAF_LOCAL_LIBS']) != dict:
210                         conf.env['AUTOWAF_LOCAL_LIBS'] = {}
211                 conf.env['AUTOWAF_LOCAL_LIBS'][name.lower()] = True
212         else:
213                 if type(conf.env['AUTOWAF_LOCAL_HEADERS']) != dict:
214                         conf.env['AUTOWAF_LOCAL_HEADERS'] = {}
215                 conf.env['AUTOWAF_LOCAL_HEADERS'][name.lower()] = True
216
217 def use_lib(bld, obj, libs):
218         abssrcdir = os.path.abspath('.')
219         libs_list = libs.split()
220         for l in libs_list:
221                 in_headers = l.lower() in bld.env['AUTOWAF_LOCAL_HEADERS']
222                 in_libs    = l.lower() in bld.env['AUTOWAF_LOCAL_LIBS']
223                 if in_libs:
224                         if hasattr(obj, 'uselib_local'):
225                                 obj.uselib_local += ' lib' + l.lower() + ' '
226                         else:
227                                 obj.uselib_local = 'lib' + l.lower() + ' '
228                 
229                 if in_headers or in_libs:
230                         inc_flag = '-iquote ' + os.path.join(abssrcdir, l.lower())
231                         for f in ['CCFLAGS', 'CXXFLAGS']:
232                                 if not inc_flag in bld.env[f]:
233                                         bld.env.append_value(f, inc_flag)
234                 else:
235                         if hasattr(obj, 'uselib'):
236                                 obj.uselib += ' ' + l
237                         else:
238                                 obj.uselib = l
239
240
241 def display_header(title):
242         Utils.pprint('BOLD', title)
243
244 def display_msg(conf, msg, status = None, color = None):
245         color = 'CYAN'
246         if type(status) == bool and status or status == "True":
247                 color = 'GREEN'
248         elif type(status) == bool and not status or status == "False":
249                 color = 'YELLOW'
250         Utils.pprint('NORMAL', "%s :" % msg.ljust(conf.line_just), sep='')
251         Utils.pprint(color, status)
252
253 def print_summary(conf):
254         global g_step
255         if g_step > 2:
256                 print
257                 return
258         e = conf.env
259         print
260         display_header('Global configuration')
261         display_msg(conf, "Install prefix", conf.env['PREFIX'])
262         display_msg(conf, "Debuggable build", str(conf.env['DEBUG']))
263         display_msg(conf, "Strict compiler flags", str(conf.env['STRICT']))
264         display_msg(conf, "Build documentation", str(conf.env['BUILD_DOCS']))
265         print
266         g_step = 3
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'           : '${exec_prefix}/lib',
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['BUILD_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 = file(source_path, 'w')
346                 o.write(text)
347                 o.close()
348         except IOError:
349                 print "Could not open", source_path, " for writing\n"
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 = file(header_path, 'w')
361                 o.write(text)
362                 o.close()
363         except IOError:
364                 print "Could not open", header_path, " for writing\n"
365                 sys.exit(-1)
366                 
367         return None
368
369 def shutdown():
370         # This isn't really correct (for packaging), but people asking is annoying
371         if Options.commands['install']:
372                 try: os.popen("/sbin/ldconfig")
373                 except: pass
374