Update autowaf.py
[ladi-patchage.git] / autowaf.py
blobdaf3083077a18f0c639ff044ddc71abb87dd8d99
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 <nedko@arnaudov.name>
7 import os
8 import misc
9 import Params
10 import Configure
11 import Utils
12 import sys
14 Configure.g_maxlen = 55
15 g_is_child = False
17 # Only run autowaf hooks once (even if sub projects call several times)
18 global g_step
19 g_step = 0
21 global g_features
23 def set_options(opt, docs_available = True, dev_available = True, lv2_available = True):
24 "Add standard autowaf options if they havn't been added yet"
25 global g_step
26 global g_features
27 if g_step > 0:
28 return
30 g_features = {}
31 g_features['docs'] = docs_available
32 g_features['dev'] = dev_available
33 g_features['lv2'] = lv2_available
35 opt.tool_options('compiler_cc')
36 opt.tool_options('compiler_cxx')
37 opt.add_option('--debug', action='store_true', default=False, dest='debug',
38 help="Build debuggable binaries [Default: False]")
39 opt.add_option('--strict', action='store_true', default=False, dest='strict',
40 help="Use strict compiler flags and show all warnings [Default: False]")
41 if g_features['docs']:
42 opt.add_option('--build-docs', action='store_true', default=False, dest='build_docs',
43 help="Build documentation - requires doxygen [Default: False]")
44 opt.add_option('--bundle', action='store_true', default=False,
45 help="Build a self-contained bundle [Default: False]")
46 opt.add_option('--bindir', type='string', help="Executable programs [Default: PREFIX/bin]")
47 if g_features['dev']:
48 opt.add_option('--libdir', type='string', help="Libraries [Default: PREFIX/lib]")
49 opt.add_option('--includedir', type='string', help="Header files [Default: PREFIX/include]")
50 opt.add_option('--datadir', type='string', help="Shared data [Default: PREFIX/share]")
51 if g_features['docs']:
52 opt.add_option('--mandir', type='string', help="Manual pages [Default: DATADIR/man]")
53 opt.add_option('--htmldir', type='string', help="HTML documentation [Default: DATADIR/doc/PACKAGE]")
54 if g_features['lv2']:
55 if sys.platform == "darwin":
56 opt.add_option('--lv2dir', type='string', help="LV2 bundles [Default: /Library/Audio/Plug-Ins/LV2]")
57 else:
58 opt.add_option('--lv2dir', type='string', help="LV2 bundles [Default: LIBDIR/lv2]")
59 opt.add_option('--lv2-user', action='store_true', default=False, dest='lv2_user',
60 help="Install LV2 bundles to user-local location [Default: False]")
61 g_step = 1
63 def check_header(conf, name, define='', **args):
64 "Check for a header iff it hasn't been checked for yet"
65 if type(conf.env['AUTOWAF_HEADERS']) != dict:
66 conf.env['AUTOWAF_HEADERS'] = {}
68 checked = conf.env['AUTOWAF_HEADERS']
69 if not name in checked:
70 conf.check_header(name, define, **args)
71 checked[name] = True
73 def check_tool(conf, name):
74 "Check for a tool iff it hasn't been checked for yet"
75 if type(conf.env['AUTOWAF_TOOLS']) != dict:
76 conf.env['AUTOWAF_TOOLS'] = {}
78 checked = conf.env['AUTOWAF_TOOLS']
79 if not name in checked:
80 conf.check_tool(name)
81 checked[name] = True
83 def check_pkg(conf, name, **args):
84 "Check for a package iff it hasn't been checked for yet"
85 if not 'HAVE_' + args['destvar'] in conf.env:
86 if not conf.check_pkg(name, **args):
87 conf.env['HAVE_' + args['destvar']] = False
88 else:
89 conf.env['HAVE_' + args['destvar']] = 1
91 def chop_prefix(conf, var):
92 name = conf.env[var][len(conf.env['PREFIX']):]
93 if len(name) > 0 and name[0] == '/':
94 name = name[1:]
95 if name == "":
96 name = "/"
97 return name;
99 def configure(conf):
100 global g_step
101 global g_features
102 if g_step > 1:
103 return
104 def append_cxx_flags(val):
105 conf.env.append_value('CCFLAGS', val)
106 conf.env.append_value('CXXFLAGS', val)
107 check_tool(conf, 'misc')
108 if g_features['docs']:
109 conf.env['BUILD_DOCS'] = Params.g_options.build_docs
110 else:
111 conf.env['BUILD_DOCS'] = False
112 conf.env['DEBUG'] = Params.g_options.debug
113 conf.env['PREFIX'] = os.path.abspath(os.path.expanduser(os.path.normpath(conf.env['PREFIX'])))
114 if Params.g_options.bundle:
115 conf.env['BUNDLE'] = True
116 conf.define('BUNDLE', 1)
117 conf.env['BINDIR'] = conf.env['PREFIX']
118 if g_features['dev']:
119 conf.env['INCLUDEDIR'] = conf.env['PREFIX'] + '/Headers/'
120 conf.env['LIBDIR'] = conf.env['PREFIX'] + '/Libraries/'
121 conf.env['DATADIR'] = conf.env['PREFIX'] + '/Resources/'
122 if g_features['docs']:
123 conf.env['HTMLDIR'] = conf.env['PREFIX'] + '/Resources/Documenation/'
124 conf.env['MANDIR'] = conf.env['PREFIX'] + '/Resources/Man/'
125 if g_features['lv2']:
126 conf.env['LV2DIR'] = conf.env['PREFIX'] + '/PlugIns/'
127 else:
128 conf.env['BUNDLE'] = False
129 if Params.g_options.bindir:
130 conf.env['BINDIR'] = Params.g_options.bindir
131 else:
132 conf.env['BINDIR'] = conf.env['PREFIX'] + '/bin/'
133 if g_features['dev']:
134 if Params.g_options.includedir:
135 conf.env['INCLUDEDIR'] = Params.g_options.includedir
136 else:
137 conf.env['INCLUDEDIR'] = conf.env['PREFIX'] + '/include/'
138 if Params.g_options.libdir:
139 conf.env['LIBDIR'] = Params.g_options.libdir
140 else:
141 conf.env['LIBDIR'] = conf.env['PREFIX'] + '/lib/'
142 else:
143 conf.env['LIBDIR'] = conf.env['PREFIX'] + '/lib/'
144 if Params.g_options.datadir:
145 conf.env['DATADIR'] = Params.g_options.datadir
146 else:
147 conf.env['DATADIR'] = conf.env['PREFIX'] + '/share/'
148 if g_features['docs']:
149 if Params.g_options.htmldir:
150 conf.env['HTMLDIR'] = Params.g_options.htmldir
151 else:
152 conf.env['HTMLDIR'] = conf.env['DATADIR'] + 'doc/' + Utils.g_module.APPNAME + '/'
153 if Params.g_options.mandir:
154 conf.env['MANDIR'] = Params.g_options.mandir
155 else:
156 conf.env['MANDIR'] = conf.env['DATADIR'] + 'man/'
157 if g_features['lv2']:
158 if Params.g_options.lv2dir:
159 conf.env['LV2DIR'] = Params.g_options.lv2dir
160 else:
161 if Params.g_options.lv2_user:
162 if sys.platform == "darwin":
163 conf.env['LV2DIR'] = os.getenv('HOME') + '/Library/Audio/Plug-Ins/LV2'
164 else:
165 conf.env['LV2DIR'] = os.getenv('HOME') + '/.lv2'
166 else:
167 if sys.platform == "darwin":
168 conf.env['LV2DIR'] = '/Library/Audio/Plug-Ins/LV2'
169 else:
170 conf.env['LV2DIR'] = conf.env['LIBDIR'] + 'lv2/'
172 conf.env['BINDIRNAME'] = chop_prefix(conf, 'BINDIR')
173 conf.env['LIBDIRNAME'] = chop_prefix(conf, 'LIBDIR')
174 conf.env['DATADIRNAME'] = chop_prefix(conf, 'DATADIR')
175 if g_features['lv2']:
176 conf.env['LV2DIRNAME'] = chop_prefix(conf, 'LV2DIR')
178 if Params.g_options.debug:
179 conf.env['CCFLAGS'] = '-O0 -g -std=c99'
180 conf.env['CXXFLAGS'] = '-O0 -g -ansi'
181 if Params.g_options.strict:
182 conf.env['CCFLAGS'] = '-O0 -g -std=c99 -pedantic'
183 append_cxx_flags('-Wall -Wextra -Wno-unused-parameter')
184 append_cxx_flags('-fPIC -DPIC')
185 g_step = 2
187 def set_local_lib(conf, name, has_objects):
188 conf.define('HAVE_' + name.upper(), True)
189 if has_objects:
190 if type(conf.env['AUTOWAF_LOCAL_LIBS']) != dict:
191 conf.env['AUTOWAF_LOCAL_LIBS'] = {}
192 conf.env['AUTOWAF_LOCAL_LIBS'][name.lower()] = True
193 else:
194 if type(conf.env['AUTOWAF_LOCAL_HEADERS']) != dict:
195 conf.env['AUTOWAF_LOCAL_HEADERS'] = {}
196 conf.env['AUTOWAF_LOCAL_HEADERS'][name.lower()] = True
198 def use_lib(bld, obj, libs):
199 abssrcdir = os.path.abspath('.')
200 libs_list = libs.split()
201 for l in libs_list:
202 in_headers = l.lower() in bld.env()['AUTOWAF_LOCAL_HEADERS']
203 in_libs = l.lower() in bld.env()['AUTOWAF_LOCAL_LIBS']
204 if in_libs:
205 obj.uselib_local += ' lib' + l.lower() + ' '
207 if in_headers or in_libs:
208 inc_flag = '-iquote ' + abssrcdir + '/' + l.lower()
209 for f in ['CCFLAGS', 'CXXFLAGS']:
210 if not inc_flag in bld.env()[f]:
211 bld.env().prepend_value(f, inc_flag)
212 else:
213 obj.uselib += ' ' + l
216 def display_header(title):
217 Params.pprint('BOLD', title)
219 def display_msg(msg, status = None, color = None):
220 Configure.g_maxlen = max(Configure.g_maxlen, len(msg))
221 if status:
222 print "%s :" % msg.ljust(Configure.g_maxlen),
223 Params.pprint(color, status)
224 else:
225 print "%s" % msg.ljust(Configure.g_maxlen)
227 def display_feature(msg, build):
228 if build:
229 display_msg(msg, "yes", 'GREEN')
230 else:
231 display_msg(msg, "no", 'YELLOW')
233 def print_summary(conf):
234 global g_step
235 global g_features
236 if g_step > 2:
237 print
238 return
239 e = conf.env
240 print
241 display_header('Global configuration')
242 display_msg("Install prefix", conf.env['PREFIX'], 'CYAN')
243 display_msg("Debuggable build", str(conf.env['DEBUG']), 'YELLOW')
244 if g_features['docs']:
245 display_msg("Build documentation", str(conf.env['BUILD_DOCS']), 'YELLOW')
246 print
247 g_step = 3
249 def link_flags(env, lib):
250 return ' '.join(map(lambda x: env['LIB_ST'] % x, env['LIB_' + lib]))
252 def compile_flags(env, lib):
253 return ' '.join(map(lambda x: env['CPPPATH_ST'] % x, env['CPPPATH_' + lib]))
255 def set_recursive():
256 global g_is_child
257 g_is_child = True
259 def is_child():
260 global g_is_child
261 return g_is_child
263 # Pkg-config file
264 def build_pc(bld, name, version, libs):
265 '''Build a pkg-config file for a library.
266 name -- uppercase variable name (e.g. 'SOMENAME')
267 version -- version string (e.g. '1.2.3')
268 libs -- string/list of dependencies (e.g. 'LIBFOO GLIB')
271 obj = bld.create_obj('subst')
272 obj.source = name.lower() + '.pc.in'
273 obj.target = name.lower() + '.pc'
274 obj.inst_var = 'PREFIX'
275 obj.inst_dir = bld.env()['LIBDIRNAME'] + 'pkgconfig'
276 pkg_prefix = bld.env()['PREFIX']
277 if pkg_prefix[-1] == '/':
278 pkg_prefix = pkg_prefix[:-1]
279 obj.dict = {
280 'prefix' : pkg_prefix,
281 'exec_prefix' : '${prefix}',
282 'libdir' : '${exec_prefix}/lib',
283 'includedir' : '${prefix}/include',
284 name + '_VERSION' : version,
286 if type(libs) != list:
287 libs = libs.split()
288 for i in libs:
289 obj.dict[i + '_LIBS'] = link_flags(bld.env(), i)
290 obj.dict[i + '_CFLAGS'] = compile_flags(bld.env(), i)
292 # Wrapper script (for bundle)
293 def build_wrapper(bld, template, prog):
294 if not bld.env()['BUNDLE']:
295 return
296 obj = bld.create_obj('subst')
297 obj.chmod = 0755
298 obj.source = template
299 obj.inst_var = 'PREFIX'
300 obj.inst_dir = '/'
301 obj.dict = {
302 'EXECUTABLE' : prog.target + ".bin",
303 'LIB_DIR_NAME' : bld.env()['LIBDIRNAME']
305 prog.target = prog.target + '.bin'
307 # Doxygen API documentation
308 def build_dox(bld, name, version, srcdir, blddir):
309 if not bld.env()['BUILD_DOCS']:
310 return
311 obj = bld.create_obj('subst')
312 obj.source = 'doc/reference.doxygen.in'
313 obj.target = 'doc/reference.doxygen'
314 if is_child():
315 src_dir = srcdir + '/' + name.lower()
316 doc_dir = blddir + '/default/' + name.lower() + '/doc'
317 else:
318 src_dir = srcdir
319 doc_dir = blddir + '/default/doc'
320 obj.dict = {
321 name + '_VERSION' : version,
322 name + '_SRCDIR' : os.path.abspath(src_dir),
323 name + '_DOC_DIR' : os.path.abspath(doc_dir)
325 obj.inst_var = 0
326 out1 = bld.create_obj('command-output')
327 out1.stdout = '/doc/doxygen.out'
328 out1.stdin = '/doc/reference.doxygen' # whatever..
329 out1.command = 'doxygen'
330 out1.argv = [os.path.abspath(doc_dir) + '/reference.doxygen']
331 out1.command_is_external = True
333 def shutdown():
334 # This isn't really correct (for packaging), but people asking is annoying
335 if Params.g_commands['install']:
336 try: os.popen("/sbin/ldconfig")
337 except: pass