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
13 from TaskGen import feature, before, after
18 # Only run autowaf hooks once (even if sub projects call several times)
22 # Compute dependencies globally
24 #preproc.go_absolute = True
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)
33 "Add standard autowaf options if they havn't been added yet"
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]")
67 opt.add_option('--lv2dir', type='string',
68 help="LV2 bundles [Default: LIBDIR/lv2]")
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'] = {}
76 checked = conf.env['AUTOWAF_HEADERS']
77 if not name in checked:
80 conf.check(header_name=name, define_name=define, mandatory=mandatory)
82 conf.check(header_name=name, mandatory=mandatory)
84 def check_tool(conf, name):
85 "Check for a tool iff it hasn't been checked for yet"
86 if type(conf.env['AUTOWAF_TOOLS']) != dict:
87 conf.env['AUTOWAF_TOOLS'] = {}
89 checked = conf.env['AUTOWAF_TOOLS']
90 if not name in checked:
95 return name.replace('/', '_').replace('++', 'PP').replace('-', '_')
97 def check_pkg(conf, name, **args):
98 if not 'mandatory' in args:
99 args['mandatory'] = True
100 "Check for a package iff it hasn't been checked for yet"
101 var_name = 'HAVE_' + nameify(args['uselib_store'])
102 check = not var_name in conf.env
103 if not check and 'atleast_version' in args:
104 # Re-check if version is newer than previous check
105 checked_version = conf.env['VERSION_' + name]
106 if checked_version and checked_version < args['atleast_version']:
109 conf.check_cfg(package=name, args="--cflags --libs", **args)
110 found = bool(conf.env[var_name])
112 conf.define(var_name, int(found))
113 if 'atleast_version' in args:
114 conf.env['VERSION_' + name] = args['atleast_version']
116 conf.undefine(var_name)
117 if args['mandatory'] == True:
118 conf.fatal("Required package " + name + " not found")
120 def chop_prefix(conf, var):
121 name = conf.env[var][len(conf.env['PREFIX']):]
122 if len(name) > 0 and name[0] == '/':
132 def append_cxx_flags(vals):
133 conf.env.append_value('CCFLAGS', vals.split())
134 conf.env.append_value('CXXFLAGS', vals.split())
136 check_tool(conf, 'misc')
137 check_tool(conf, 'compiler_cc')
138 check_tool(conf, 'compiler_cxx')
139 conf.env['BUILD_DOCS'] = Options.options.build_docs
140 conf.env['DEBUG'] = Options.options.debug
141 conf.env['STRICT'] = Options.options.strict
142 conf.env['PREFIX'] = os.path.abspath(os.path.expanduser(os.path.normpath(conf.env['PREFIX'])))
143 if Options.options.bundle:
144 conf.env['BUNDLE'] = True
145 conf.define('BUNDLE', 1)
146 conf.env['BINDIR'] = conf.env['PREFIX']
147 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'Headers')
148 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'Libraries')
149 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'Resources')
150 conf.env['HTMLDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Documentation')
151 conf.env['MANDIR'] = os.path.join(conf.env['PREFIX'], 'Resources/Man')
152 conf.env['LV2DIR'] = os.path.join(conf.env['PREFIX'], 'PlugIns')
154 conf.env['BUNDLE'] = False
155 if Options.options.bindir:
156 conf.env['BINDIR'] = Options.options.bindir
158 conf.env['BINDIR'] = os.path.join(conf.env['PREFIX'], 'bin')
159 if Options.options.includedir:
160 conf.env['INCLUDEDIR'] = Options.options.includedir
162 conf.env['INCLUDEDIR'] = os.path.join(conf.env['PREFIX'], 'include')
163 if Options.options.libdir:
164 conf.env['LIBDIR'] = Options.options.libdir
166 conf.env['LIBDIR'] = os.path.join(conf.env['PREFIX'], 'lib')
167 if Options.options.datadir:
168 conf.env['DATADIR'] = Options.options.datadir
170 conf.env['DATADIR'] = os.path.join(conf.env['PREFIX'], 'share')
171 if Options.options.configdir:
172 conf.env['CONFIGDIR'] = Options.options.configdir
174 conf.env['CONFIGDIR'] = os.path.join(conf.env['PREFIX'], 'etc')
175 if Options.options.htmldir:
176 conf.env['HTMLDIR'] = Options.options.htmldir
178 conf.env['HTMLDIR'] = os.path.join(conf.env['DATADIR'], 'doc', Utils.g_module.APPNAME)
179 if Options.options.mandir:
180 conf.env['MANDIR'] = Options.options.mandir
182 conf.env['MANDIR'] = os.path.join(conf.env['DATADIR'], 'man')
183 if Options.options.lv2dir:
184 conf.env['LV2DIR'] = Options.options.lv2dir
186 if Options.options.lv2_user:
187 if sys.platform == "darwin":
188 conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), 'Library/Audio/Plug-Ins/LV2')
190 conf.env['LV2DIR'] = os.path.join(os.getenv('HOME'), '.lv2')
192 if sys.platform == "darwin":
193 conf.env['LV2DIR'] = '/Library/Audio/Plug-Ins/LV2'
195 conf.env['LV2DIR'] = os.path.join(conf.env['LIBDIR'], 'lv2')
197 conf.env['BINDIRNAME'] = chop_prefix(conf, 'BINDIR')
198 conf.env['LIBDIRNAME'] = chop_prefix(conf, 'LIBDIR')
199 conf.env['DATADIRNAME'] = chop_prefix(conf, 'DATADIR')
200 conf.env['CONFIGDIRNAME'] = chop_prefix(conf, 'CONFIGDIR')
201 conf.env['LV2DIRNAME'] = chop_prefix(conf, 'LV2DIR')
203 if Options.options.debug:
204 conf.env['CCFLAGS'] = [ '-O0', '-g' ]
205 conf.env['CXXFLAGS'] = [ '-O0', '-g' ]
207 append_cxx_flags('-DNDEBUG')
208 if Options.options.strict:
209 conf.env.append_value('CCFLAGS', [ '-std=c99', '-pedantic' ])
210 conf.env.append_value('CXXFLAGS', [ '-ansi', '-Woverloaded-virtual'])
211 append_cxx_flags('-Wall -Wextra -Wno-unused-parameter')
212 append_cxx_flags('-fPIC -DPIC -fshow-column')
215 def set_local_lib(conf, name, has_objects):
216 conf.define('HAVE_' + nameify(name.upper()), 1)
218 if type(conf.env['AUTOWAF_LOCAL_LIBS']) != dict:
219 conf.env['AUTOWAF_LOCAL_LIBS'] = {}
220 conf.env['AUTOWAF_LOCAL_LIBS'][name.lower()] = True
222 if type(conf.env['AUTOWAF_LOCAL_HEADERS']) != dict:
223 conf.env['AUTOWAF_LOCAL_HEADERS'] = {}
224 conf.env['AUTOWAF_LOCAL_HEADERS'][name.lower()] = True
226 def use_lib(bld, obj, libs):
227 abssrcdir = os.path.abspath('.')
228 libs_list = libs.split()
230 in_headers = l.lower() in bld.env['AUTOWAF_LOCAL_HEADERS']
231 in_libs = l.lower() in bld.env['AUTOWAF_LOCAL_LIBS']
233 if hasattr(obj, 'uselib_local'):
234 obj.uselib_local += ' lib' + l.lower() + ' '
236 obj.uselib_local = 'lib' + l.lower() + ' '
238 if in_headers or in_libs:
239 inc_flag = '-iquote ' + os.path.join(abssrcdir, l.lower())
240 for f in ['CCFLAGS', 'CXXFLAGS']:
241 if not inc_flag in bld.env[f]:
242 bld.env.prepend_value(f, inc_flag)
244 if hasattr(obj, 'uselib'):
245 obj.uselib += ' ' + l
250 def display_header(title):
251 Utils.pprint('BOLD', title)
253 def display_msg(conf, msg, status = None, color = None):
255 if type(status) == bool and status or status == "True":
257 elif type(status) == bool and not status or status == "False":
259 Utils.pprint('NORMAL', "%s :" % msg.ljust(conf.line_just), sep='')
260 Utils.pprint(color, status)
262 def print_summary(conf):
269 display_header('Global configuration')
270 display_msg(conf, "Install prefix", conf.env['PREFIX'])
271 display_msg(conf, "Debuggable build", str(conf.env['DEBUG']))
272 display_msg(conf, "Strict compiler flags", str(conf.env['STRICT']))
273 display_msg(conf, "Build documentation", str(conf.env['BUILD_DOCS']))
277 def link_flags(env, lib):
278 return ' '.join(map(lambda x: env['LIB_ST'] % x, env['LIB_' + lib]))
280 def compile_flags(env, lib):
281 return ' '.join(map(lambda x: env['CPPPATH_ST'] % x, env['CPPPATH_' + lib]))
292 def build_pc(bld, name, version, libs):
293 '''Build a pkg-config file for a library.
294 name -- uppercase variable name (e.g. 'SOMENAME')
295 version -- version string (e.g. '1.2.3')
296 libs -- string/list of dependencies (e.g. 'LIBFOO GLIB')
299 obj = bld.new_task_gen('subst')
300 obj.source = name.lower() + '.pc.in'
301 obj.target = name.lower() + '.pc'
302 obj.install_path = '${PREFIX}/${LIBDIRNAME}/pkgconfig'
303 pkg_prefix = bld.env['PREFIX']
304 if pkg_prefix[-1] == '/':
305 pkg_prefix = pkg_prefix[:-1]
307 'prefix' : pkg_prefix,
308 'exec_prefix' : '${prefix}',
309 'libdir' : '${exec_prefix}/lib',
310 'includedir' : '${prefix}/include',
311 name + '_VERSION' : version,
313 if type(libs) != list:
316 obj.dict[i + '_LIBS'] = link_flags(bld.env, i)
317 obj.dict[i + '_CFLAGS'] = compile_flags(bld.env, i)
319 # Doxygen API documentation
320 def build_dox(bld, name, version, srcdir, blddir):
321 if not bld.env['BUILD_DOCS']:
323 obj = bld.new_task_gen('subst')
324 obj.source = 'doc/reference.doxygen.in'
325 obj.target = 'doc/reference.doxygen'
327 src_dir = os.path.join(srcdir, name.lower())
328 doc_dir = os.path.join(blddir, 'default', name.lower(), 'doc')
331 doc_dir = os.path.join(blddir, 'default', 'doc')
333 name + '_VERSION' : version,
334 name + '_SRCDIR' : os.path.abspath(src_dir),
335 name + '_DOC_DIR' : os.path.abspath(doc_dir)
337 obj.install_path = ''
338 out1 = bld.new_task_gen('command-output')
339 out1.stdout = '/doc/doxygen.out'
340 out1.stdin = '/doc/reference.doxygen' # whatever..
341 out1.command = 'doxygen'
342 out1.argv = [os.path.abspath(doc_dir) + '/reference.doxygen']
343 out1.command_is_external = True
345 # Version code file generation
346 def build_version_files(header_path, source_path, domain, major, minor, micro):
347 header_path = os.path.abspath(header_path)
348 source_path = os.path.abspath(source_path)
349 text = "int " + domain + "_major_version = " + str(major) + ";\n"
350 text += "int " + domain + "_minor_version = " + str(minor) + ";\n"
351 text += "int " + domain + "_micro_version = " + str(micro) + ";\n"
353 o = file(source_path, 'w')
357 print "Could not open", source_path, " for writing\n"
360 text = "#ifndef __" + domain + "_version_h__\n"
361 text += "#define __" + domain + "_version_h__\n"
362 text += "extern const char* " + domain + "_revision;\n"
363 text += "extern int " + domain + "_major_version;\n"
364 text += "extern int " + domain + "_minor_version;\n"
365 text += "extern int " + domain + "_micro_version;\n"
366 text += "#endif /* __" + domain + "_version_h__ */\n"
368 o = file(header_path, 'w')
372 print "Could not open", header_path, " for writing\n"
378 # This isn't really correct (for packaging), but people asking is annoying
379 if Options.commands['install']:
380 try: os.popen("/sbin/ldconfig")