1 # a waf tool to add autoconf-like macros to the configure section
3 import Build, os, Options, preproc
5 from Configure import conf
6 from samba_utils import *
8 missing_headers = set()
10 ####################################################
11 # some autoconf like helpers, to make the transition
12 # to waf a bit easier for those used to autoconf
17 def DEFINE(conf, d, v, add_to_cflags=False):
18 '''define a config option'''
19 conf.define(d, v, quote=False)
21 conf.env.append_value('CCDEFINES', d + '=' + str(v))
23 def hlist_to_string(conf, headers=None):
24 '''convert a headers list to a set of #include lines'''
26 hlist = conf.env.hlist
29 hlist.extend(TO_LIST(headers))
31 hdrs += '#include <%s>\n' % h
37 '''using the nolink type in conf.check() allows us to avoid
38 the link stage of a test, thus speeding it up for tests
39 that where linking is not needed'''
43 def CHECK_HEADER(conf, h, add_headers=False, lib=None):
44 '''check for a header'''
45 if h in missing_headers:
47 d = h.upper().replace('/', '_')
48 d = d.replace('.', '_')
50 if CONFIG_SET(conf, d):
52 if not h in conf.env.hlist:
53 conf.env.hlist.append(h)
56 (ccflags, ldflags) = library_flags(conf, lib)
58 hdrs = hlist_to_string(conf, headers=h)
59 ret = conf.check(fragment='%s\nint main(void) { return 0; }' % hdrs,
63 msg="Checking for header %s" % h)
65 missing_headers.add(h)
69 if add_headers and not h in conf.env.hlist:
70 conf.env.hlist.append(h)
75 def CHECK_HEADERS(conf, headers, add_headers=False, together=False, lib=None):
76 '''check for a list of headers
78 when together==True, then the headers accumulate within this test.
79 This is useful for interdependent headers
82 if not add_headers and together:
83 saved_hlist = conf.env.hlist[:]
84 set_add_headers = True
86 set_add_headers = add_headers
87 for hdr in TO_LIST(headers):
88 if not CHECK_HEADER(conf, hdr, set_add_headers, lib=lib):
90 if not add_headers and together:
91 conf.env.hlist = saved_hlist
95 def header_list(conf, headers=None, lib=None):
96 '''form a list of headers which exist, as a string'''
98 if headers is not None:
99 for h in TO_LIST(headers):
100 if CHECK_HEADER(conf, h, add_headers=False, lib=lib):
102 return hlist_to_string(conf, headers=hlist)
106 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None, lib=None, msg=None):
107 '''check for a single type'''
109 define = 'HAVE_' + t.upper().replace(' ', '_')
111 msg='Checking for %s' % t
112 ret = CHECK_CODE(conf, '%s _x' % t,
120 if not ret and alternate:
121 conf.DEFINE(t, alternate)
126 def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None, lib=None):
127 '''check for a list of types'''
129 for t in TO_LIST(list):
130 if not CHECK_TYPE(conf, t, headers=headers,
131 define=define, alternate=alternate, lib=lib):
137 def CHECK_TYPE_IN(conf, t, headers=None, alternate=None, define=None):
138 '''check for a single type with a header'''
139 return CHECK_TYPE(conf, t, headers=headers, alternate=alternate, define=define)
143 def CHECK_VARIABLE(conf, v, define=None, always=False,
144 headers=None, msg=None, lib=None):
145 '''check for a variable declaration (or define)'''
147 define = 'HAVE_%s' % v.upper()
150 msg="Checking for variable %s" % v
152 return CHECK_CODE(conf,
155 void *_x; _x=(void *)&%s;
170 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
171 '''check a list of variable declarations, using the HAVE_DECL_xxx form
174 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
177 for v in TO_LIST(vars):
179 define='HAVE_DECL_%s' % v.upper()
181 define='HAVE_%s_DECL' % v.upper()
182 if not CHECK_VARIABLE(conf, v,
185 msg='Checking for declaration of %s' % v):
190 def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
191 '''check for a function'''
192 define='HAVE_%s' % f.upper()
194 # there are two ways to find a function. The first is
195 # to see if there is a declaration of the function, the
196 # 2nd is to try and link a program that calls the function
197 # unfortunately both strategies have problems.
198 # the 'check the declaration' approach works fine as long
199 # as the function has a declaraion in a header. If there is
200 # no header declaration we can get a false negative.
201 # The link method works fine as long as the compiler
202 # doesn't have a builtin for the function, which could cause
203 # a false negative due to mismatched parameters
204 # so to be sure, we need to try both
207 if link is None or link == True:
208 ret = CHECK_CODE(conf,
209 'int main(void) { extern void %s(void); %s(); return 0; }' % (f, f),
218 msg='Checking for %s' % f)
220 if not ret and (link is None or link == False):
221 ret = CHECK_VARIABLE(conf, f,
224 msg='Checking for declaration of %s' % f)
229 def CHECK_FUNCS(conf, list, link=None, lib=None, headers=None):
230 '''check for a list of functions'''
232 for f in TO_LIST(list):
233 if not CHECK_FUNC(conf, f, link=link, lib=lib, headers=headers):
239 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
240 '''check the size of a type'''
242 for v in TO_LIST(vars):
245 v_define = 'SIZEOF_%s' % v.upper().replace(' ', '_')
246 if not CHECK_CODE(conf,
247 'printf("%%u\\n", (unsigned)sizeof(%s))' % v,
254 msg="Checking size of %s" % v):
261 def CHECK_CODE(conf, code, define,
262 always=False, execute=False, addmain=True,
263 add_headers=True, mandatory=False,
264 headers=None, msg=None, cflags='', includes='# .',
265 local_include=True, lib=None, link=True,
266 define_ret=False, quote=False):
267 '''check if some code compiles and/or runs'''
269 if CONFIG_SET(conf, define):
272 if headers is not None:
273 CHECK_HEADERS(conf, headers=headers, lib=lib)
276 hdrs = header_list(conf, headers=headers, lib=lib)
285 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
287 fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
289 conf.write_config_header('__confdefs.h', top=True)
292 msg="Checking for %s" % define
294 # include the directory containing __confdefs.h
295 cflags += ' -I../../default'
298 cflags += ' -I%s' % conf.curdir
305 uselib = TO_LIST(lib)
307 (ccflags, ldflags) = library_flags(conf, uselib)
309 cflags = TO_LIST(cflags)
310 cflags.extend(ccflags)
312 ret = conf.check(fragment=fragment,
314 define_name = define,
315 mandatory = mandatory,
323 define_ret=define_ret)
324 if not ret and CONFIG_SET(conf, define):
325 # sometimes conf.check() returns false, but it
326 # sets the define. Maybe a waf bug?
330 conf.DEFINE(define, 1)
333 conf.DEFINE(define, 0)
339 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
340 always=False, define=None, headers=None):
341 '''check for a structure member'''
343 define = 'HAVE_%s' % member.upper()
344 return CHECK_CODE(conf,
345 '%s s; void *_x; _x=(void *)&s.%s' % (structname, member),
352 msg="Checking for member %s in %s" % (member, structname))
356 def CHECK_CFLAGS(conf, cflags):
357 '''check if the given cflags are accepted by the compiler
359 return conf.check(fragment='int main(void) { return 0; }\n',
363 msg="Checking compiler accepts %s" % cflags)
366 #################################################
367 # return True if a configuration option was found
369 def CONFIG_SET(conf, option):
370 return (option in conf.env) and (conf.env[option] != ())
371 Build.BuildContext.CONFIG_SET = CONFIG_SET
374 def library_flags(conf, libs):
375 '''work out flags from pkg_config'''
378 for lib in TO_LIST(libs):
380 inc_path = getattr(conf.env, 'CPPPATH_%s' % lib.upper(), [])
381 lib_path = getattr(conf.env, 'LIBPATH_%s' % lib.upper(), [])
383 ccflags.append('-I%s' % i)
385 ldflags.append('-L%s' % l)
386 return (ccflags, ldflags)
390 def CHECK_LIB(conf, libs, mandatory=False):
391 '''check if a set of libraries exist'''
393 liblist = TO_LIST(libs)
395 for lib in liblist[:]:
396 if GET_TARGET_TYPE(conf, lib):
399 (ccflags, ldflags) = library_flags(conf, lib)
401 if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
402 conf.ASSERT(not mandatory,
403 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
404 # if it isn't a mandatory library, then remove it from dependency lists
405 SET_TARGET_TYPE(conf, lib, 'EMPTY')
408 conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
409 conf.env['LIB_' + lib.upper()] = lib
410 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
415 ###########################################################
416 # check that the functions in 'list' are available in 'library'
417 # if they are, then make that library available as a dependency
419 # if the library is not available and mandatory==True, then
422 # If the library is not available and mandatory==False, then
423 # add the library to the list of dependencies to remove from
426 # optionally check for the functions first in libc
428 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, headers=None, link=None):
429 remaining = TO_LIST(list)
430 liblist = TO_LIST(library)
432 # check if some already found
433 for f in remaining[:]:
434 if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
437 # see if the functions are in libc
439 for f in remaining[:]:
440 if CHECK_FUNC(conf, f, link=True, headers=headers):
445 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
446 SET_TARGET_TYPE(conf, lib, 'EMPTY')
449 conf.CHECK_LIB(liblist)
450 for lib in liblist[:]:
451 if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
452 conf.ASSERT(not mandatory,
453 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
454 # if it isn't a mandatory library, then remove it from dependency lists
460 if not CHECK_FUNC(conf, f, lib=' '.join(liblist), headers=headers, link=link):
466 #################################################
467 # write out config.h in the right directory
469 def SAMBA_CONFIG_H(conf, path=None):
470 # we don't want to produce a config.h in places like lib/replace
471 # when we are building projects that depend on lib/replace
472 if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
475 if Options.options.developer:
476 # we add these here to ensure that -Wstrict-prototypes is not set during configure
477 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',
480 if Options.options.pedantic:
481 conf.ADD_CFLAGS('-W', testflags=True)
484 conf.write_config_header('config.h', top=True)
486 conf.write_config_header(path)
489 ##############################################################
490 # setup a configurable path
492 def CONFIG_PATH(conf, name, default):
493 if not name in conf.env:
494 if default[0] == '/':
495 conf.env[name] = default
497 conf.env[name] = conf.env['PREFIX'] + default
498 conf.define(name, conf.env[name], quote=True)
502 def ADD_CFLAGS(conf, flags, testflags=False):
503 '''add some CFLAGS to the command line
504 optionally set testflags to ensure all the flags work
508 for f in flags.split():
509 if CHECK_CFLAGS(conf, f):
512 if not 'EXTRA_CFLAGS' in conf.env:
513 conf.env['EXTRA_CFLAGS'] = []
514 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
517 ##############################################################
518 # add some extra include directories to all builds
520 def ADD_EXTRA_INCLUDES(conf, includes):
521 if not 'EXTRA_INCLUDES' in conf.env:
522 conf.env['EXTRA_INCLUDES'] = []
523 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
526 ##############################################################
527 # work out the current flags. local flags are added first
528 def CURRENT_CFLAGS(bld, target, cflags):
529 if not 'EXTRA_CFLAGS' in bld.env:
532 list = bld.env['EXTRA_CFLAGS'];
533 ret = TO_LIST(cflags)
538 def CHECK_RPATH_SUPPORT(conf):
539 '''see if the system supports rpath'''
540 return conf.CHECK_CODE('int x',
541 define='HAVE_RPATH_SUPPORT',
544 msg='Checking for rpath support',
545 cflags='-Wl,-rpath=.')
548 def CHECK_CC_ENV(conf):
549 '''trim whitespaces from 'CC'.
550 The build farm sometimes puts a space at the start'''
551 if os.environ.get('CC'):
552 conf.env.CC = TO_LIST(os.environ.get('CC'))
553 if len(conf.env.CC) == 1:
554 # make for nicer logs if just a single command
555 conf.env.CC = conf.env.CC[0]
559 def SETUP_CONFIGURE_CACHE(conf, enable):
560 '''enable/disable cache of configure results'''
562 # when -C is chosen, we will use a private cache and will
563 # not look into system includes. This roughtly matches what
564 # autoconf does with -C
565 cache_path = os.path.join(conf.blddir, '.confcache')
567 Options.cache_global = os.environ['WAFCACHE'] = cache_path
569 # when -C is not chosen we will not cache configure checks
570 # We set the recursion limit low to prevent waf from spending
571 # a lot of time on the signatures of the files.
572 Options.cache_global = os.environ['WAFCACHE'] = ''
573 preproc.recursion_limit = 1
574 # in either case we don't need to scan system includes
575 preproc.go_absolute = False