1 # a waf tool to add autoconf-like macros to the configure section
3 import Build, os, Options
5 from Configure import conf
6 from samba_utils import *
8 ####################################################
9 # some autoconf like helpers, to make the transition
10 # to waf a bit easier for those used to autoconf
15 def DEFINE(conf, d, v, add_to_cflags=False):
16 '''define a config option'''
17 conf.define(d, v, quote=False)
19 conf.env.append_value('CCDEFINES', d + '=' + str(v))
22 def CHECK_HEADER(conf, h, add_headers=True):
23 '''check for a header'''
24 if conf.check(header_name=h) and add_headers:
25 conf.env.hlist.append(h)
31 def CHECK_HEADERS(conf, list, add_headers=True):
32 '''check for a list of headers'''
34 for hdr in TO_LIST(list):
35 if not CHECK_HEADER(conf, hdr, add_headers):
41 def CHECK_TYPES(conf, list):
42 '''check for a list of types'''
45 for t in TO_LIST(list):
46 if not conf.check(type_name=t, header_name=conf.env.hlist):
52 def CHECK_TYPE_IN(conf, t, hdr, define=None):
53 '''check for a type in a specific header'''
54 if conf.check(header_name=hdr):
56 ret = conf.check(type_name=t, header_name=hdr)
58 ret = conf.check(type_name=t, header_name=hdr, define_name=define)
64 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
65 '''check for a type with an alternate'''
67 headers = conf.env.hlist
68 if define is not None:
69 ret = conf.check(type_name=t, header_name=headers, define_name=define)
71 ret = conf.check(type_name=t, header_name=headers)
72 if not ret and alternate is not None:
73 conf.DEFINE(t, alternate)
78 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
79 '''check for a variable declaration (or define)'''
81 if headers is not None:
82 hlist = TO_LIST(headers)
84 hlist = conf.env.hlist
86 hdrs += '#include <%s>\n' % h
88 define = 'HAVE_%s' % v.upper()
89 if conf.check(fragment=
94 void *_x; _x=(void *)&%s;
100 msg="Checking for variable %s" % v):
101 conf.DEFINE(define, 1)
104 conf.DEFINE(define, 0)
108 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
109 '''check a list of variable declarations, using the HAVE_DECL_xxx form
112 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
115 for v in TO_LIST(vars):
117 define='HAVE_DECL_%s' % v.upper()
119 define='HAVE_%s_DECL' % v.upper()
120 if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
126 def CHECK_FUNC(conf, f, checklink=False):
127 '''check for a function'''
128 define='HAVE_%s' % f.upper()
129 if CONFIG_SET(conf, define):
132 return CHECK_CODE(conf, '%s()' % f, execute=False, define=define)
133 return conf.check(function_name=f, header_name=conf.env.hlist)
137 def CHECK_FUNCS(conf, list, checklink=False):
138 '''check for a list of functions'''
140 for f in TO_LIST(list):
141 if not CHECK_FUNC(conf, f, checklink):
147 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
148 '''check the size of a type'''
150 if headers is not None:
151 hlist = TO_LIST(headers)
153 hlist = conf.env.hlist
155 hdrs += '#include <%s>\n' % h
156 for v in TO_LIST(vars):
158 define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
165 printf("%%u\\n", (unsigned)sizeof(%s));
171 define_name=define_name,
173 msg="Checking size of %s" % v)
177 def CHECK_CODE(conf, code, define,
178 always=False, execute=False, addmain=True, mandatory=False,
179 headers=None, msg=None, cflags='', includes='# .',
181 '''check if some code compiles and/or runs'''
183 if headers is not None:
184 hlist = TO_LIST(headers)
186 hlist = conf.env.hlist
188 hdrs += '#include <%s>\n' % h
196 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
198 fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
200 conf.write_config_header('__confdefs.h', top=True)
203 msg="Checking for %s" % define
205 # include the directory containing __confdefs.h
206 cflags += ' -I../../default'
209 cflags += ' -I%s' % conf.curdir
211 if conf.check(fragment=fragment,
213 define_name = define,
214 mandatory = mandatory,
215 ccflags=TO_LIST(cflags),
218 conf.DEFINE(define, 1)
221 conf.DEFINE(define, 0)
227 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
228 always=False, define=None, headers=None):
229 '''check for a structure member'''
231 if headers is not None:
232 hlist = TO_LIST(headers)
234 hlist = conf.env.hlist
236 hdrs += '#include <%s>\n' % h
238 define = 'HAVE_%s' % member.upper()
239 if conf.check(fragment=
244 void *_x; _x=(void *)&s.%s;
247 ''' % (hdrs, structname, member),
249 msg="Checking for member %s in %s" % (member, structname)):
250 conf.DEFINE(define, 1)
253 conf.DEFINE(define, 0)
258 def CHECK_CFLAGS(conf, cflags, variable):
259 '''check if the given cflags are accepted by the compiler'''
260 if conf.check(fragment='int main(void) { return 0; }',
263 msg="Checking compiler accepts %s" % cflags):
264 conf.env[variable] = cflags
269 #################################################
270 # return True if a configuration option was found
272 def CONFIG_SET(conf, option):
273 return (option in conf.env) and (conf.env[option] != ())
274 Build.BuildContext.CONFIG_SET = CONFIG_SET
277 ###########################################################
278 # check that the functions in 'list' are available in 'library'
279 # if they are, then make that library available as a dependency
281 # if the library is not available and mandatory==True, then
284 # If the library is not available and mandatory==False, then
285 # add the library to the list of dependencies to remove from
288 # optionally check for the functions first in libc
290 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
291 remaining = TO_LIST(list)
292 liblist = TO_LIST(library)
294 # check if some already found
295 for f in remaining[:]:
296 if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
299 # see if the functions are in libc
301 for f in remaining[:]:
302 if CHECK_FUNC(conf, f):
307 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
308 SET_TARGET_TYPE(conf, lib, 'EMPTY')
312 for lib in liblist[:]:
313 if GET_TARGET_TYPE(conf, lib):
315 if not conf.check(lib=lib, uselib_store=lib):
316 conf.ASSERT(not mandatory,
317 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
318 # if it isn't a mandatory library, then remove it from dependency lists
319 SET_TARGET_TYPE(conf, lib, 'EMPTY')
322 conf.define('HAVE_LIB%s' % string.replace(lib.upper(),'-','_'), 1)
329 if not conf.check(function_name=f, lib=liblist, header_name=conf.env.hlist):
336 if GET_TARGET_TYPE(conf, lib):
338 conf.env['LIB_' + lib.upper()] = lib
339 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
344 #################################################
345 # write out config.h in the right directory
347 def SAMBA_CONFIG_H(conf, path=None):
348 # we don't want to produce a config.h in places like lib/replace
349 # when we are building projects that depend on lib/replace
350 if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
353 conf.write_config_header('config.h', top=True)
355 conf.write_config_header(path)
358 ##############################################################
359 # setup a configurable path
361 def CONFIG_PATH(conf, name, default):
362 if not name in conf.env:
363 if default[0] == '/':
364 conf.env[name] = default
366 conf.env[name] = conf.env['PREFIX'] + default
367 conf.define(name, conf.env[name], quote=True)
369 ##############################################################
370 # add some CFLAGS to the command line
372 def ADD_CFLAGS(conf, flags):
373 if not 'EXTRA_CFLAGS' in conf.env:
374 conf.env['EXTRA_CFLAGS'] = []
375 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
377 ##############################################################
378 # add some extra include directories to all builds
380 def ADD_EXTRA_INCLUDES(conf, includes):
381 if not 'EXTRA_INCLUDES' in conf.env:
382 conf.env['EXTRA_INCLUDES'] = []
383 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
386 ##############################################################
387 # work out the current flags. local flags are added first
388 def CURRENT_CFLAGS(bld, target, cflags):
389 if not 'EXTRA_CFLAGS' in bld.env:
392 list = bld.env['EXTRA_CFLAGS'];
393 ret = TO_LIST(cflags)