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):
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 hdrs = hlist_to_string(conf, headers=h)
57 ret = conf.check(fragment='%s\nint main(void) { return 0; }' % hdrs,
60 msg="Checking for header %s" % h)
62 missing_headers.add(h)
66 if add_headers and not h in conf.env.hlist:
67 conf.env.hlist.append(h)
72 def CHECK_HEADERS(conf, headers, add_headers=False, together=False):
73 '''check for a list of headers
75 when together==True, then the headers accumulate within this test.
76 This is useful for interdependent headers
79 if not add_headers and together:
80 saved_hlist = conf.env.hlist[:]
81 set_add_headers = True
83 set_add_headers = add_headers
84 for hdr in TO_LIST(headers):
85 if not CHECK_HEADER(conf, hdr, set_add_headers):
87 if not add_headers and together:
88 conf.env.hlist = saved_hlist
92 def header_list(conf, headers=None):
93 '''form a list of headers which exist, as a string'''
95 if headers is not None:
96 for h in TO_LIST(headers):
97 if CHECK_HEADER(conf, h, add_headers=False):
99 return hlist_to_string(conf, headers=hlist)
103 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
104 '''check for a single type'''
106 define = 'HAVE_' + t.upper().replace(' ', '_')
107 ret = CHECK_CODE(conf, '%s _x' % t,
111 msg='Checking for %s' % t,
114 if not ret and alternate:
115 conf.DEFINE(t, alternate)
120 def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None):
121 '''check for a list of types'''
123 for t in TO_LIST(list):
124 if not CHECK_TYPE(conf, t, headers=headers, define=define, alternate=alternate):
130 def CHECK_TYPE_IN(conf, t, headers=None, alternate=None, define=None):
131 '''check for a single type with a header'''
132 return CHECK_TYPE(conf, t, headers=headers, alternate=alternate, define=define)
136 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None, msg=None):
137 '''check for a variable declaration (or define)'''
139 define = 'HAVE_%s' % v.upper()
142 msg="Checking for variable %s" % v
144 return CHECK_CODE(conf,
147 void *_x; _x=(void *)&%s;
161 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
162 '''check a list of variable declarations, using the HAVE_DECL_xxx form
165 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
168 for v in TO_LIST(vars):
170 define='HAVE_DECL_%s' % v.upper()
172 define='HAVE_%s_DECL' % v.upper()
173 if not CHECK_VARIABLE(conf, v,
176 msg='Checking for declaration of %s' % v):
181 def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
182 '''check for a function'''
183 define='HAVE_%s' % f.upper()
185 # there are two ways to find a function. The first is
186 # to see if there is a declaration of the function, the
187 # 2nd is to try and link a program that calls the function
188 # unfortunately both strategies have problems.
189 # the 'check the declaration' approach works fine as long
190 # as the function has a declaraion in a header. If there is
191 # no header declaration we can get a false negative.
192 # The link method works fine as long as the compiler
193 # doesn't have a builtin for the function, which could cause
194 # a false negative due to mismatched parameters
195 # so to be sure, we need to try both
198 if link is None or link == True:
199 ret = CHECK_CODE(conf,
200 'int main(void) { extern void %s(void); %s(); return 0; }' % (f, f),
209 msg='Checking for %s' % f)
211 if not ret and (link is None or link == False):
212 ret = CHECK_VARIABLE(conf, f,
215 msg='Checking for declaration of %s' % f)
220 def CHECK_FUNCS(conf, list, link=None, lib=None, headers=None):
221 '''check for a list of functions'''
223 for f in TO_LIST(list):
224 if not CHECK_FUNC(conf, f, link=link, lib=lib, headers=headers):
230 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
231 '''check the size of a type'''
233 for v in TO_LIST(vars):
236 v_define = 'SIZEOF_%s' % v.upper().replace(' ', '_')
237 if not CHECK_CODE(conf,
238 'printf("%%u\\n", (unsigned)sizeof(%s))' % v,
245 msg="Checking size of %s" % v):
252 def CHECK_CODE(conf, code, define,
253 always=False, execute=False, addmain=True,
254 add_headers=True, mandatory=False,
255 headers=None, msg=None, cflags='', includes='# .',
256 local_include=True, lib=None, link=True,
257 define_ret=False, quote=False):
258 '''check if some code compiles and/or runs'''
260 if CONFIG_SET(conf, define):
263 if headers is not None:
264 CHECK_HEADERS(conf, headers=headers)
267 hdrs = header_list(conf, headers=headers)
276 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
278 fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
280 conf.write_config_header('__confdefs.h', top=True)
283 msg="Checking for %s" % define
285 # include the directory containing __confdefs.h
286 cflags += ' -I../../default'
289 cflags += ' -I%s' % conf.curdir
297 uselib = TO_LIST(lib)
301 ret = conf.check(fragment=fragment,
303 define_name = define,
304 mandatory = mandatory,
305 ccflags=TO_LIST(cflags),
311 define_ret=define_ret)
312 if not ret and CONFIG_SET(conf, define):
313 # sometimes conf.check() returns false, but it
314 # sets the define. Maybe a waf bug?
318 conf.DEFINE(define, 1)
321 conf.DEFINE(define, 0)
327 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
328 always=False, define=None, headers=None):
329 '''check for a structure member'''
331 define = 'HAVE_%s' % member.upper()
332 return CHECK_CODE(conf,
333 '%s s; void *_x; _x=(void *)&s.%s' % (structname, member),
340 msg="Checking for member %s in %s" % (member, structname))
344 def CHECK_CFLAGS(conf, cflags):
345 '''check if the given cflags are accepted by the compiler
347 return conf.check(fragment='int main(void) { return 0; }',
350 msg="Checking compiler accepts %s" % cflags)
353 #################################################
354 # return True if a configuration option was found
356 def CONFIG_SET(conf, option):
357 return (option in conf.env) and (conf.env[option] != ())
358 Build.BuildContext.CONFIG_SET = CONFIG_SET
362 def CHECK_LIB(conf, libs):
363 '''check if a set of libraries exist'''
364 liblist = TO_LIST(library)
367 for lib in liblist[:]:
368 if GET_TARGET_TYPE(conf, lib):
370 if not conf.check(lib=lib, uselib_store=lib):
371 conf.ASSERT(not mandatory,
372 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
373 # if it isn't a mandatory library, then remove it from dependency lists
374 SET_TARGET_TYPE(conf, lib, 'EMPTY')
377 conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
378 conf.env['LIB_' + lib.upper()] = lib
379 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
384 ###########################################################
385 # check that the functions in 'list' are available in 'library'
386 # if they are, then make that library available as a dependency
388 # if the library is not available and mandatory==True, then
391 # If the library is not available and mandatory==False, then
392 # add the library to the list of dependencies to remove from
395 # optionally check for the functions first in libc
397 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, headers=None, link=None):
398 remaining = TO_LIST(list)
399 liblist = TO_LIST(library)
401 # check if some already found
402 for f in remaining[:]:
403 if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
406 # see if the functions are in libc
408 for f in remaining[:]:
409 if CHECK_FUNC(conf, f, link=True, headers=headers):
414 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
415 SET_TARGET_TYPE(conf, lib, 'EMPTY')
419 for lib in liblist[:]:
420 if GET_TARGET_TYPE(conf, lib):
422 if not conf.check(lib=lib, uselib_store=lib):
423 conf.ASSERT(not mandatory,
424 "Mandatory library '%s' not found for functions '%s'" % (lib, list))
425 # if it isn't a mandatory library, then remove it from dependency lists
426 SET_TARGET_TYPE(conf, lib, 'EMPTY')
429 conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
430 conf.env['LIB_' + lib.upper()] = lib
431 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
438 if not CHECK_FUNC(conf, f, lib=' '.join(liblist), headers=headers, link=link):
445 def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None):
446 '''verify that a C prototype matches the one on the current system'''
447 if not conf.CHECK_DECLS(function, headers=headers):
449 return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
453 msg='Checking C prototype for %s' % function)
457 def CHECK_LARGEFILE(conf):
458 '''see what we need for largefile support'''
459 if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
462 msg='Checking for large file support'):
464 if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
467 cflags='-D_FILE_OFFSET_BITS=64',
468 msg='Checking for -D_FILE_OFFSET_BITS=64'):
469 conf.DEFINE('_FILE_OFFSET_BITS', 64)
475 #################################################
476 # write out config.h in the right directory
478 def SAMBA_CONFIG_H(conf, path=None):
479 # we don't want to produce a config.h in places like lib/replace
480 # when we are building projects that depend on lib/replace
481 if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
484 if Options.options.developer:
485 # we add these here to ensure that -Wstrict-prototypes is not set during configure
486 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')
489 conf.write_config_header('config.h', top=True)
491 conf.write_config_header(path)
494 ##############################################################
495 # setup a configurable path
497 def CONFIG_PATH(conf, name, default):
498 if not name in conf.env:
499 if default[0] == '/':
500 conf.env[name] = default
502 conf.env[name] = conf.env['PREFIX'] + default
503 conf.define(name, conf.env[name], quote=True)
505 ##############################################################
506 # add some CFLAGS to the command line
508 def ADD_CFLAGS(conf, flags):
509 if not 'EXTRA_CFLAGS' in conf.env:
510 conf.env['EXTRA_CFLAGS'] = []
511 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
513 ##############################################################
514 # add some extra include directories to all builds
516 def ADD_EXTRA_INCLUDES(conf, includes):
517 if not 'EXTRA_INCLUDES' in conf.env:
518 conf.env['EXTRA_INCLUDES'] = []
519 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
522 ##############################################################
523 # work out the current flags. local flags are added first
524 def CURRENT_CFLAGS(bld, target, cflags):
525 if not 'EXTRA_CFLAGS' in bld.env:
528 list = bld.env['EXTRA_CFLAGS'];
529 ret = TO_LIST(cflags)
534 def CHECK_RPATH_SUPPORT(conf):
535 '''see if the system supports rpath'''
536 return conf.CHECK_CODE('int x',
537 define='HAVE_RPATH_SUPPORT',
540 msg='Checking for rpath support',
541 cflags='-Wl,-rpath=.')
544 def CHECK_CC_ENV(conf):
545 '''trim whitespaces from 'CC'.
546 The build farm sometimes puts a space at the start'''
547 if os.environ.get('CC'):
548 conf.env.CC = TO_LIST(os.environ.get('CC'))
549 if len(conf.env.CC) == 1:
550 # make for nicer logs if just a single command
551 conf.env.CC = conf.env.CC[0]
555 def SETUP_CONFIGURE_CACHE(conf, enable):
556 '''enable/disable cache of configure results'''
558 # when -C is chosen, we will use a private cache and will
559 # not look into system includes. This roughtly matches what
560 # autoconf does with -C
561 cache_path = os.path.join(conf.blddir, '.confcache')
563 Options.cache_global = os.environ['WAFCACHE'] = cache_path
565 # when -C is not chosen we will not cache configure checks
566 # We set the recursion limit low to prevent waf from spending
567 # a lot of time on the signatures of the files.
568 Options.cache_global = os.environ['WAFCACHE'] = ''
569 preproc.recursion_limit = 1
570 # in either case we don't need to scan system includes
571 preproc.go_absolute = False