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 d = 'HAVE_%s' % string.replace(h.upper(), '/', '_')
25 if CONFIG_SET(conf, d):
27 conf.env.hlist.append(h)
28 conf.env.hlist = unique_list(conf.env.hlist)
30 ret = conf.check(header_name=h)
31 if ret and add_headers:
32 conf.env.hlist.append(h)
33 conf.env.hlist = unique_list(conf.env.hlist)
38 def CHECK_HEADERS(conf, list, add_headers=True):
39 '''check for a list of headers'''
41 for hdr in TO_LIST(list):
42 if not CHECK_HEADER(conf, hdr, add_headers):
48 def CHECK_TYPES(conf, list):
49 '''check for a list of types'''
52 for t in TO_LIST(list):
53 if not conf.check(type_name=t, header_name=conf.env.hlist):
59 def CHECK_TYPE_IN(conf, t, hdr, define=None):
60 '''check for a type in a specific header'''
61 if conf.check(header_name=hdr):
63 ret = conf.check(type_name=t, header_name=hdr)
65 ret = conf.check(type_name=t, header_name=hdr, define_name=define)
71 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
72 '''check for a type with an alternate'''
74 headers = conf.env.hlist
75 if define is not None:
76 ret = conf.check(type_name=t, header_name=headers, define_name=define)
78 ret = conf.check(type_name=t, header_name=headers)
79 if not ret and alternate is not None:
80 conf.DEFINE(t, alternate)
85 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
86 '''check for a variable declaration (or define)'''
88 if headers is not None:
89 hlist = TO_LIST(headers)
91 hlist = conf.env.hlist
93 hdrs += '#include <%s>\n' % h
95 define = 'HAVE_%s' % v.upper()
96 if conf.check(fragment=
101 void *_x; _x=(void *)&%s;
107 msg="Checking for variable %s" % v):
108 conf.DEFINE(define, 1)
111 conf.DEFINE(define, 0)
115 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
116 '''check a list of variable declarations, using the HAVE_DECL_xxx form
119 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
122 for v in TO_LIST(vars):
124 define='HAVE_DECL_%s' % v.upper()
126 define='HAVE_%s_DECL' % v.upper()
127 if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
132 def CHECK_FUNC(conf, f, checklink=False, header=''):
133 '''check for a function'''
134 hlist = conf.env.hlist[:]
135 for h in TO_LIST(header):
136 if CHECK_HEADER(conf, h, add_headers=False):
138 define='HAVE_%s' % f.upper()
139 if CONFIG_SET(conf, define):
142 return CHECK_CODE(conf, 'void *x = (void *)%s' % f,
143 execute=False, define=define,
144 msg='Checking for %s' % f)
146 return conf.check_cc(function_name=f, header_name=hlist)
150 def CHECK_FUNCS(conf, list, checklink=False, header=''):
151 '''check for a list of functions'''
153 for f in TO_LIST(list):
154 if not CHECK_FUNC(conf, f, checklink=checklink, header=header):
160 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
161 '''check the size of a type'''
163 if headers is not None:
164 hlist = TO_LIST(headers)
166 hlist = conf.env.hlist
168 hdrs += '#include <%s>\n' % h
169 for v in TO_LIST(vars):
171 define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
178 printf("%%u\\n", (unsigned)sizeof(%s));
184 define_name=define_name,
186 msg="Checking size of %s" % v)
190 def CHECK_CODE(conf, code, define,
191 always=False, execute=False, addmain=True, mandatory=False,
192 headers=None, msg=None, cflags='', includes='# .',
194 '''check if some code compiles and/or runs'''
196 if headers is not None:
197 hlist = TO_LIST(headers)
199 hlist = conf.env.hlist
201 hdrs += '#include <%s>\n' % h
209 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
211 fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
213 conf.write_config_header('__confdefs.h', top=True)
216 msg="Checking for %s" % define
218 # include the directory containing __confdefs.h
219 cflags += ' -I../../default'
222 cflags += ' -I%s' % conf.curdir
224 if conf.check(fragment=fragment,
226 define_name = define,
227 mandatory = mandatory,
228 ccflags=TO_LIST(cflags),
231 conf.DEFINE(define, 1)
234 conf.DEFINE(define, 0)
240 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
241 always=False, define=None, headers=None):
242 '''check for a structure member'''
244 if headers is not None:
245 hlist = TO_LIST(headers)
247 hlist = conf.env.hlist
249 hdrs += '#include <%s>\n' % h
251 define = 'HAVE_%s' % member.upper()
252 if conf.check(fragment=
257 void *_x; _x=(void *)&s.%s;
260 ''' % (hdrs, structname, member),
262 msg="Checking for member %s in %s" % (member, structname)):
263 conf.DEFINE(define, 1)
266 conf.DEFINE(define, 0)
271 def CHECK_CFLAGS(conf, cflags, variable):
272 '''check if the given cflags are accepted by the compiler'''
273 if conf.check(fragment='int main(void) { return 0; }',
276 msg="Checking compiler accepts %s" % cflags):
277 conf.env[variable] = cflags
282 #################################################
283 # return True if a configuration option was found
285 def CONFIG_SET(conf, option):
286 return (option in conf.env) and (conf.env[option] != ())
287 Build.BuildContext.CONFIG_SET = CONFIG_SET
290 ###########################################################
291 # check that the functions in 'list' are available in 'library'
292 # if they are, then make that library available as a dependency
294 # if the library is not available and mandatory==True, then
297 # If the library is not available and mandatory==False, then
298 # add the library to the list of dependencies to remove from
301 # optionally check for the functions first in libc
303 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header=''):
304 remaining = TO_LIST(list)
305 liblist = TO_LIST(library)
307 hlist = conf.env.hlist[:]
308 for h in TO_LIST(header):
309 if CHECK_HEADER(conf, h, add_headers=False):
312 # check if some already found
313 for f in remaining[:]:
314 if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
317 # see if the functions are in libc
319 for f in remaining[:]:
320 if CHECK_FUNC(conf, f, checklink=True, header=header):
325 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
326 SET_TARGET_TYPE(conf, lib, 'EMPTY')
330 for lib in liblist[:]:
331 if GET_TARGET_TYPE(conf, lib):
333 if not conf.check(lib=lib, uselib_store=lib):
334 conf.ASSERT(not mandatory,
335 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
336 # if it isn't a mandatory library, then remove it from dependency lists
337 SET_TARGET_TYPE(conf, lib, 'EMPTY')
340 conf.define('HAVE_LIB%s' % string.replace(lib.upper(),'-','_'), 1)
341 conf.env['LIB_' + lib.upper()] = lib
342 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
349 if not conf.check_cc(function_name=f, lib=liblist, header_name=hlist):
355 #################################################
356 # write out config.h in the right directory
358 def SAMBA_CONFIG_H(conf, path=None):
359 # we don't want to produce a config.h in places like lib/replace
360 # when we are building projects that depend on lib/replace
361 if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
364 if Options.options.developer:
365 # we add these here to ensure that -Wstrict-prototypes is not set during configure
366 conf.ADD_CFLAGS('-Wall -g -Wfatal-errors -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Werror-implicit-function-declaration -Wformat=2 -Wno-format-y2k')
369 conf.write_config_header('config.h', top=True)
371 conf.write_config_header(path)
374 ##############################################################
375 # setup a configurable path
377 def CONFIG_PATH(conf, name, default):
378 if not name in conf.env:
379 if default[0] == '/':
380 conf.env[name] = default
382 conf.env[name] = conf.env['PREFIX'] + default
383 conf.define(name, conf.env[name], quote=True)
385 ##############################################################
386 # add some CFLAGS to the command line
388 def ADD_CFLAGS(conf, flags):
389 if not 'EXTRA_CFLAGS' in conf.env:
390 conf.env['EXTRA_CFLAGS'] = []
391 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
393 ##############################################################
394 # add some extra include directories to all builds
396 def ADD_EXTRA_INCLUDES(conf, includes):
397 if not 'EXTRA_INCLUDES' in conf.env:
398 conf.env['EXTRA_INCLUDES'] = []
399 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
402 ##############################################################
403 # work out the current flags. local flags are added first
404 def CURRENT_CFLAGS(bld, target, cflags):
405 if not 'EXTRA_CFLAGS' in bld.env:
408 list = bld.env['EXTRA_CFLAGS'];
409 ret = TO_LIST(cflags)
414 def CHECK_RPATH_SUPPORT(conf):
415 '''see if the system supports rpath'''
416 return conf.CHECK_CODE('int x',
417 define='HAVE_RPATH_SUPPORT',
419 msg='Checking for rpath support',
420 cflags='-Wl,-rpath=.')