Fix spacey tabs...
[ardour2.git] / autowaf.py
blob93f0910eb284322ac3bfaa6dd1619a8ecbe09b3b
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
7 import os
8 import misc
9 import Configure
10 import Options
11 import Utils
12 import sys
13 import glob
14 from TaskGen import feature, before, after
16 global g_is_child
17 g_is_child = False
19 # Only run autowaf hooks once (even if sub projects call several times)
20 global g_step
21 g_step = 0
23 # Compute dependencies globally
24 #import preproc
25 #preproc.go_absolute = True
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)
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('--build-docs', action='store_true', default=False, dest='build_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
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'] = {}
77 checked = conf.env['AUTOWAF_HEADERS']
78 if not name in checked:
79 checked[name] = True
80 if define != '':
81 conf.check(header_name=name, define_name=define, mandatory=mandatory)
82 else:
83 conf.check(header_name=name, mandatory=mandatory)
85 def nameify(name):
86 return name.replace('/', '_').replace('++', 'PP').replace('-', '_').replace('.', '_')
88 def check_pkg(conf, name, **args):
89 if not 'mandatory' in args:
90 args['mandatory'] = True
91 "Check for a package iff it hasn't been checked for yet"
92 var_name = 'HAVE_' + nameify(args['uselib_store'])
93 check = not var_name in conf.env
94 if not check and 'atleast_version' in args:
95 # Re-check if version is newer than previous check
96 checked_version = conf.env['VERSION_' + name]
97 if checked_version and checked_version < args['atleast_version']:
98 check = True;
99 if check:
100 conf.check_cfg(package=name, args="--cflags --libs", **args)
101 found = bool(conf.env[var_name])
102 if found:
103 conf.define(var_name, int(found))
104 if 'atleast_version' in args:
105 conf.env['VERSION_' + name] = args['atleast_version']
106 else:
107 conf.undefine(var_name)
108 if args['mandatory'] == True:
109 conf.fatal("Required package " + name + " not found")
111 def chop_prefix(conf, var):
112 name = conf.env[var][len(conf.env['PREFIX']):]
113 if len(name) > 0 and name[0] == '/':
114 name = name[1:]
115 if name == "":
116 name = "/"
117 return name;
119 def configure(conf):
120 global g_step
121 if g_step > 1:
122 return
123 def append_cxx_flags(vals):
124 conf.env.append_value('CCFLAGS', vals.split())
125 conf.env.append_value('CXXFLAGS', vals.split())
126 conf.line_just = 43
127 conf.check_tool('misc')
128 conf.check_tool('compiler_cc')
129 conf.check_tool('compiler_cxx')
130 conf.env['BUILD_DOCS'] = Options.options.build_docs
131 conf.env['DEBUG'] = Options.options.debug
132 conf.env['STRICT'] = Options.options.strict
133 conf.env['PREFIX'] = os.path.abspath(os.path.expanduser(os.path.normpath(conf.env['PREFIX'])))
135 if Options.options.bundle:
136 conf.env['BUNDLE'] = True
137 conf.define('BUNDLE', 1)
138 conf.env['BINDIR'] = conf.env['PREFIX']
139 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'Headers')
140 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'Libraries')
141 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'Resources')
142 conf.env['HTMLDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Documentation')
143 conf.env['MANDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Man')
144 conf.env['LV2DIR'] = os.path.join(conf.env['PREFIX'], 'PlugIns')
145 else:
146 conf.env['BUNDLE'] = False
147 if Options.options.bindir:
148 conf.env['BINDIR'] = Options.options.bindir
149 else:
150 conf.env['BINDIR'] = os.path.join(conf.env['PREFIX'], 'bin')
151 if Options.options.includedir:
152 conf.env['INCLUDEDIR'] = Options.options.includedir
153 else:
154 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'include')
155 if Options.options.libdir:
156 conf.env['LIBDIR'] = Options.options.libdir
157 else:
158 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'lib')
159 if Options.options.datadir:
160 conf.env['DATADIR'] = Options.options.datadir
161 else:
162 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'share')
163 if Options.options.configdir:
164 conf.env['CONFIGDIR'] = Options.options.configdir
165 else:
166 conf.env['CONFIGDIR'] = os.path.join(conf.env['PREFIX'], 'etc')
167 if Options.options.htmldir:
168 conf.env['HTMLDIR'] = Options.options.htmldir
169 else:
170 conf.env['HTMLDIR'] = os.path.join(conf.env['DATADIR'], 'doc', Utils.g_module.APPNAME)
171 if Options.options.mandir:
172 conf.env['MANDIR'] = Options.options.mandir
173 else:
174 conf.env['MANDIR'] = os.path.join(conf.env['DATADIR'], 'man')
175 if Options.options.lv2dir:
176 conf.env['LV2DIR'] = Options.options.lv2dir
177 else:
178 if Options.options.lv2_user:
179 if sys.platform == "darwin":
180 conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), 'Library/Audio/Plug-Ins/LV2')
181 else:
182 conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), '.lv2')
183 else:
184 if sys.platform == "darwin":
185 conf.env['LV2DIR'] = '/Library/Audio/Plug-Ins/LV2'
186 else:
187 conf.env['LV2DIR'] = os.path.join(conf.env['LIBDIR'], 'lv2')
189 conf.env['BINDIRNAME'] = chop_prefix(conf, 'BINDIR')
190 conf.env['LIBDIRNAME'] = chop_prefix(conf, 'LIBDIR')
191 conf.env['DATADIRNAME'] = chop_prefix(conf, 'DATADIR')
192 conf.env['CONFIGDIRNAME'] = chop_prefix(conf, 'CONFIGDIR')
193 conf.env['LV2DIRNAME'] = chop_prefix(conf, 'LV2DIR')
195 if Options.options.debug:
196 conf.env['CCFLAGS'] = [ '-O0', '-g' ]
197 conf.env['CXXFLAGS'] = [ '-O0', '-g' ]
198 else:
199 append_cxx_flags('-DNDEBUG')
200 if Options.options.strict:
201 conf.env.append_value('CCFLAGS', [ '-std=c99', '-pedantic' ])
202 conf.env.append_value('CXXFLAGS', [ '-ansi', '-Woverloaded-virtual'])
203 append_cxx_flags('-Wall -Wextra -Wno-unused-parameter')
204 append_cxx_flags('-fPIC -DPIC -fshow-column')
205 g_step = 2
207 def set_local_lib(conf, name, has_objects):
208 conf.define('HAVE_' + nameify(name.upper()), 1)
209 if has_objects:
210 if type(conf.env['AUTOWAF_LOCAL_LIBS']) != dict:
211 conf.env['AUTOWAF_LOCAL_LIBS'] = {}
212 conf.env['AUTOWAF_LOCAL_LIBS'][name.lower()] = True
213 else:
214 if type(conf.env['AUTOWAF_LOCAL_HEADERS']) != dict:
215 conf.env['AUTOWAF_LOCAL_HEADERS'] = {}
216 conf.env['AUTOWAF_LOCAL_HEADERS'][name.lower()] = True
218 def use_lib(bld, obj, libs):
219 abssrcdir = os.path.abspath('.')
220 libs_list = libs.split()
221 for l in libs_list:
222 in_headers = l.lower() in bld.env['AUTOWAF_LOCAL_HEADERS']
223 in_libs = l.lower() in bld.env['AUTOWAF_LOCAL_LIBS']
224 if in_libs:
225 if hasattr(obj, 'uselib_local'):
226 obj.uselib_local += ' lib' + l.lower() + ' '
227 else:
228 obj.uselib_local = 'lib' + l.lower() + ' '
230 if in_headers or in_libs:
231 inc_flag = '-iquote ' + os.path.join(abssrcdir, l.lower())
232 for f in ['CCFLAGS', 'CXXFLAGS']:
233 if not inc_flag in bld.env[f]:
234 bld.env.append_value(f, inc_flag)
235 else:
236 if hasattr(obj, 'uselib'):
237 obj.uselib += ' ' + l
238 else:
239 obj.uselib = l
242 def display_header(title):
243 Utils.pprint('BOLD', title)
245 def display_msg(conf, msg, status = None, color = None):
246 color = 'CYAN'
247 if type(status) == bool and status or status == "True":
248 color = 'GREEN'
249 elif type(status) == bool and not status or status == "False":
250 color = 'YELLOW'
251 Utils.pprint('NORMAL', "%s :" % msg.ljust(conf.line_just), sep='')
252 Utils.pprint(color, status)
254 def print_summary(conf):
255 global g_step
256 if g_step > 2:
257 print
258 return
259 e = conf.env
260 print
261 display_header('Global configuration')
262 display_msg(conf, "Install prefix", conf.env['PREFIX'])
263 display_msg(conf, "Debuggable build", str(conf.env['DEBUG']))
264 display_msg(conf, "Strict compiler flags", str(conf.env['STRICT']))
265 display_msg(conf, "Build documentation", str(conf.env['BUILD_DOCS']))
266 print
267 g_step = 3
269 def link_flags(env, lib):
270 return ' '.join(map(lambda x: env['LIB_ST'] % x, env['LIB_' + lib]))
272 def compile_flags(env, lib):
273 return ' '.join(map(lambda x: env['CPPPATH_ST'] % x, env['CPPPATH_' + lib]))
275 def set_recursive():
276 global g_is_child
277 g_is_child = True
279 def is_child():
280 global g_is_child
281 return g_is_child
283 # Pkg-config file
284 def build_pc(bld, name, version, libs):
285 '''Build a pkg-config file for a library.
286 name -- uppercase variable name (e.g. 'SOMENAME')
287 version -- version string (e.g. '1.2.3')
288 libs -- string/list of dependencies (e.g. 'LIBFOO GLIB')
291 obj = bld.new_task_gen('subst')
292 obj.source = name.lower() + '.pc.in'
293 obj.target = name.lower() + '.pc'
294 obj.install_path = '${PREFIX}/${LIBDIRNAME}/pkgconfig'
295 pkg_prefix = bld.env['PREFIX']
296 if pkg_prefix[-1] == '/':
297 pkg_prefix = pkg_prefix[:-1]
298 obj.dict = {
299 'prefix' : pkg_prefix,
300 'exec_prefix' : '${prefix}',
301 'libdir' : '${exec_prefix}/lib',
302 'includedir' : '${prefix}/include',
303 name + '_VERSION' : version,
305 if type(libs) != list:
306 libs = libs.split()
307 for i in libs:
308 obj.dict[i + '_LIBS'] = link_flags(bld.env, i)
309 obj.dict[i + '_CFLAGS'] = compile_flags(bld.env, i)
311 # Doxygen API documentation
312 def build_dox(bld, name, version, srcdir, blddir):
313 if not bld.env['BUILD_DOCS']:
314 return
315 obj = bld.new_task_gen('subst')
316 obj.source = 'doc/reference.doxygen.in'
317 obj.target = 'doc/reference.doxygen'
318 if is_child():
319 src_dir = os.path.join(srcdir, name.lower())
320 doc_dir = os.path.join(blddir, 'default', name.lower(), 'doc')
321 else:
322 src_dir = srcdir
323 doc_dir = os.path.join(blddir, 'default', 'doc')
324 obj.dict = {
325 name + '_VERSION' : version,
326 name + '_SRCDIR' : os.path.abspath(src_dir),
327 name + '_DOC_DIR' : os.path.abspath(doc_dir)
329 obj.install_path = ''
330 out1 = bld.new_task_gen('command-output')
331 out1.dependencies = [obj]
332 out1.stdout = '/doc/doxygen.out'
333 out1.stdin = '/doc/reference.doxygen' # whatever..
334 out1.command = 'doxygen'
335 out1.argv = [os.path.abspath(doc_dir) + '/reference.doxygen']
336 out1.command_is_external = True
338 # Version code file generation
339 def build_version_files(header_path, source_path, domain, major, minor, micro):
340 header_path = os.path.abspath(header_path)
341 source_path = os.path.abspath(source_path)
342 text = "int " + domain + "_major_version = " + str(major) + ";\n"
343 text += "int " + domain + "_minor_version = " + str(minor) + ";\n"
344 text += "int " + domain + "_micro_version = " + str(micro) + ";\n"
345 try:
346 o = file(source_path, 'w')
347 o.write(text)
348 o.close()
349 except IOError:
350 print "Could not open", source_path, " for writing\n"
351 sys.exit(-1)
353 text = "#ifndef __" + domain + "_version_h__\n"
354 text += "#define __" + domain + "_version_h__\n"
355 text += "extern const char* " + domain + "_revision;\n"
356 text += "extern int " + domain + "_major_version;\n"
357 text += "extern int " + domain + "_minor_version;\n"
358 text += "extern int " + domain + "_micro_version;\n"
359 text += "#endif /* __" + domain + "_version_h__ */\n"
360 try:
361 o = file(header_path, 'w')
362 o.write(text)
363 o.close()
364 except IOError:
365 print "Could not open", header_path, " for writing\n"
366 sys.exit(-1)
368 return None
370 # Internationalisation (gettext)
371 def build_i18n(bld,dir,name,sources):
372 pwd = bld.get_curdir()
373 os.chdir(pwd)
375 pot_file = '%s.pot' % name
377 args = [ 'xgettext',
378 '--keyword=_',
379 '--keyword=N_',
380 '--from-code=UTF-8',
381 '-o', pot_file,
382 '--copyright-holder="Paul Davis"' ]
383 args += sources
384 Utils.pprint('GREEN', 'Updating ' + pot_file, sep='')
385 os.spawnvp (os.P_WAIT, 'xgettext', args)
387 po_files = glob.glob ('po/*.po')
389 for po_file in po_files:
390 args = [ 'msgmerge',
391 '--update',
392 po_file,
393 pot_file ]
394 Utils.pprint('GREEN', 'Updating ' + po_file, sep='')
395 os.spawnvp (os.P_WAIT, 'msgmerge', args)
397 for po_file in po_files:
398 mo_file = po_file.replace ('.po', '.mo')
399 args = [ 'msgfmt',
400 '-c',
401 '-o',
402 mo_file,
403 po_file ]
404 Utils.pprint('GREEN', 'Generating ' + po_file)
405 os.spawnvp (os.P_WAIT, 'msgfmt', args)
407 def shutdown():
408 # This isn't really correct (for packaging), but people asking is annoying
409 if Options.commands['install']:
410 try: os.popen("/sbin/ldconfig")
411 except: pass