1 # a waf tool to add autoconf-like macros to the configure section
3 import Build, os, Options, preproc, Logs
5 from Configure import conf
6 from samba_utils import *
9 missing_headers = set()
11 ####################################################
12 # some autoconf like helpers, to make the transition
13 # to waf a bit easier for those used to autoconf
18 def DEFINE(conf, d, v, add_to_cflags=False, quote=False):
19 '''define a config option'''
20 conf.define(d, v, quote=quote)
22 conf.env.append_value('CCDEFINES', d + '=' + str(v))
24 def hlist_to_string(conf, headers=None):
25 '''convert a headers list to a set of #include lines'''
27 hlist = conf.env.hlist
30 hlist.extend(TO_LIST(headers))
32 hdrs += '#include <%s>\n' % h
37 def COMPOUND_START(conf, msg):
38 '''start a compound test'''
39 def null_check_message_1(self,*k,**kw):
41 def null_check_message_2(self,*k,**kw):
44 v = getattr(conf.env, 'in_compound', [])
45 if v != [] and v != 0:
46 conf.env.in_compound = v + 1
48 conf.check_message_1(msg)
49 conf.saved_check_message_1 = conf.check_message_1
50 conf.check_message_1 = null_check_message_1
51 conf.saved_check_message_2 = conf.check_message_2
52 conf.check_message_2 = null_check_message_2
53 conf.env.in_compound = 1
57 def COMPOUND_END(conf, result):
58 '''start a compound test'''
59 conf.env.in_compound -= 1
60 if conf.env.in_compound != 0:
62 conf.check_message_1 = conf.saved_check_message_1
63 conf.check_message_2 = conf.saved_check_message_2
64 p = conf.check_message_2
68 p('not found', 'YELLOW')
75 '''using the nolink type in conf.check() allows us to avoid
76 the link stage of a test, thus speeding it up for tests
77 that where linking is not needed'''
81 def CHECK_HEADER(conf, h, add_headers=False, lib=None):
82 '''check for a header'''
83 if h in missing_headers:
85 d = h.upper().replace('/', '_')
86 d = d.replace('.', '_')
88 if CONFIG_SET(conf, d):
90 if not h in conf.env.hlist:
91 conf.env.hlist.append(h)
94 (ccflags, ldflags) = library_flags(conf, lib)
96 hdrs = hlist_to_string(conf, headers=h)
97 ret = conf.check(fragment='%s\nint main(void) { return 0; }' % hdrs,
101 msg="Checking for header %s" % h)
103 missing_headers.add(h)
107 if add_headers and not h in conf.env.hlist:
108 conf.env.hlist.append(h)
113 def CHECK_HEADERS(conf, headers, add_headers=False, together=False, lib=None):
114 '''check for a list of headers
116 when together==True, then the headers accumulate within this test.
117 This is useful for interdependent headers
120 if not add_headers and together:
121 saved_hlist = conf.env.hlist[:]
122 set_add_headers = True
124 set_add_headers = add_headers
125 for hdr in TO_LIST(headers):
126 if not CHECK_HEADER(conf, hdr, set_add_headers, lib=lib):
128 if not add_headers and together:
129 conf.env.hlist = saved_hlist
133 def header_list(conf, headers=None, lib=None):
134 '''form a list of headers which exist, as a string'''
136 if headers is not None:
137 for h in TO_LIST(headers):
138 if CHECK_HEADER(conf, h, add_headers=False, lib=lib):
140 return hlist_to_string(conf, headers=hlist)
144 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None, lib=None, msg=None):
145 '''check for a single type'''
147 define = 'HAVE_' + t.upper().replace(' ', '_')
149 msg='Checking for %s' % t
150 ret = CHECK_CODE(conf, '%s _x' % t,
158 if not ret and alternate:
159 conf.DEFINE(t, alternate)
164 def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None, lib=None):
165 '''check for a list of types'''
167 for t in TO_LIST(list):
168 if not CHECK_TYPE(conf, t, headers=headers,
169 define=define, alternate=alternate, lib=lib):
175 def CHECK_TYPE_IN(conf, t, headers=None, alternate=None, define=None):
176 '''check for a single type with a header'''
177 return CHECK_TYPE(conf, t, headers=headers, alternate=alternate, define=define)
181 def CHECK_VARIABLE(conf, v, define=None, always=False,
182 headers=None, msg=None, lib=None):
183 '''check for a variable declaration (or define)'''
185 define = 'HAVE_%s' % v.upper()
188 msg="Checking for variable %s" % v
190 return CHECK_CODE(conf,
191 # we need to make sure the compiler doesn't
195 void *_x; _x=(void *)&%s; return (int)_x;
210 def CHECK_DECLS(conf, vars, reverse=False, headers=None, always=False):
211 '''check a list of variable declarations, using the HAVE_DECL_xxx form
214 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
217 for v in TO_LIST(vars):
219 define='HAVE_DECL_%s' % v.upper()
221 define='HAVE_%s_DECL' % v.upper()
222 if not CHECK_VARIABLE(conf, v,
225 msg='Checking for declaration of %s' % v,
231 def CHECK_FUNC(conf, f, link=True, lib=None, headers=None):
232 '''check for a function'''
233 define='HAVE_%s' % f.upper()
237 conf.COMPOUND_START('Checking for %s' % f)
239 if link is None or link == True:
240 ret = CHECK_CODE(conf,
241 # this is based on the autoconf strategy
243 #define %s __fake__%s
250 #if defined __stub_%s || defined __stub___%s
251 #error "bad glibc stub"
254 int main() { return %s(); }
255 ''' % (f, f, f, f, f, f, f),
264 msg='Checking for %s' % f)
267 ret = CHECK_CODE(conf,
268 # it might be a macro
269 # we need to make sure the compiler doesn't
271 'void *__x = (void *)%s; return (int)__x' % f,
280 msg='Checking for macro %s' % f)
282 if not ret and (link is None or link == False):
283 ret = CHECK_VARIABLE(conf, f,
286 msg='Checking for declaration of %s' % f)
287 conf.COMPOUND_END(ret)
292 def CHECK_FUNCS(conf, list, link=True, lib=None, headers=None):
293 '''check for a list of functions'''
295 for f in TO_LIST(list):
296 if not CHECK_FUNC(conf, f, link=link, lib=lib, headers=headers):
302 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
303 '''check the size of a type'''
305 for v in TO_LIST(vars):
308 v_define = 'SIZEOF_%s' % v.upper().replace(' ', '_')
309 if not CHECK_CODE(conf,
310 'printf("%%u", (unsigned)sizeof(%s))' % v,
317 msg="Checking size of %s" % v):
324 def CHECK_CODE(conf, code, define,
325 always=False, execute=False, addmain=True,
326 add_headers=True, mandatory=False,
327 headers=None, msg=None, cflags='', includes='# .',
328 local_include=True, lib=None, link=True,
329 define_ret=False, quote=False,
331 '''check if some code compiles and/or runs'''
333 if CONFIG_SET(conf, define):
336 if headers is not None:
337 CHECK_HEADERS(conf, headers=headers, lib=lib)
340 hdrs = header_list(conf, headers=headers, lib=lib)
349 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
351 fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
353 conf.write_config_header('__confdefs.h', top=True)
356 msg="Checking for %s" % define
358 # include the directory containing __confdefs.h
359 cflags += ' -I../../default'
362 cflags += ' -I%s' % conf.curdir
369 uselib = TO_LIST(lib)
371 (ccflags, ldflags) = library_flags(conf, uselib)
373 cflags = TO_LIST(cflags)
374 cflags.extend(ccflags)
377 exec_args = conf.SAMBA_CROSS_ARGS(msg=msg)
381 conf.COMPOUND_START(msg)
383 ret = conf.check(fragment=fragment,
385 define_name = define,
386 mandatory = mandatory,
395 define_ret=define_ret)
396 if not ret and CONFIG_SET(conf, define):
397 # sometimes conf.check() returns false, but it
398 # sets the define. Maybe a waf bug?
402 conf.DEFINE(define, 1)
403 conf.COMPOUND_END(True)
405 conf.COMPOUND_END(conf.env[define])
408 conf.DEFINE(define, 0)
409 conf.COMPOUND_END(False)
415 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
416 always=False, define=None, headers=None):
417 '''check for a structure member'''
419 define = 'HAVE_%s' % member.upper()
420 return CHECK_CODE(conf,
421 '%s s; void *_x; _x=(void *)&s.%s' % (structname, member),
428 msg="Checking for member %s in %s" % (member, structname))
432 def CHECK_CFLAGS(conf, cflags):
433 '''check if the given cflags are accepted by the compiler
435 return conf.check(fragment='int main(void) { return 0; }\n',
439 msg="Checking compiler accepts %s" % cflags)
443 def CONFIG_SET(conf, option):
444 '''return True if a configuration option was found'''
445 return (option in conf.env) and (conf.env[option] != ())
446 Build.BuildContext.CONFIG_SET = CONFIG_SET
449 def library_flags(conf, libs):
450 '''work out flags from pkg_config'''
453 for lib in TO_LIST(libs):
455 inc_path = getattr(conf.env, 'CPPPATH_%s' % lib.upper(), [])
456 lib_path = getattr(conf.env, 'LIBPATH_%s' % lib.upper(), [])
458 ccflags.append('-I%s' % i)
460 ldflags.append('-L%s' % l)
461 return (ccflags, ldflags)
465 def CHECK_LIB(conf, libs, mandatory=False, empty_decl=True):
466 '''check if a set of libraries exist'''
468 liblist = TO_LIST(libs)
470 for lib in liblist[:]:
471 if GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
474 (ccflags, ldflags) = library_flags(conf, lib)
476 if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
478 Logs.error("Mandatory library '%s' not found for functions '%s'" % (lib, list))
481 # if it isn't a mandatory library, then remove it from dependency lists
482 SET_TARGET_TYPE(conf, lib, 'EMPTY')
485 conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
486 conf.env['LIB_' + lib.upper()] = lib
487 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
494 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False,
495 headers=None, link=True, empty_decl=True):
497 check that the functions in 'list' are available in 'library'
498 if they are, then make that library available as a dependency
500 if the library is not available and mandatory==True, then
503 If the library is not available and mandatory==False, then
504 add the library to the list of dependencies to remove from
507 optionally check for the functions first in libc
509 remaining = TO_LIST(list)
510 liblist = TO_LIST(library)
512 # check if some already found
513 for f in remaining[:]:
514 if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
517 # see if the functions are in libc
519 for f in remaining[:]:
520 if CHECK_FUNC(conf, f, link=True, headers=headers):
525 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB' and empty_decl:
526 SET_TARGET_TYPE(conf, lib, 'EMPTY')
529 conf.CHECK_LIB(liblist, empty_decl=empty_decl)
530 for lib in liblist[:]:
531 if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
533 Logs.error("Mandatory library '%s' not found for functions '%s'" % (lib, list))
535 # if it isn't a mandatory library, then remove it from dependency lists
541 if not CHECK_FUNC(conf, f, lib=' '.join(liblist), headers=headers, link=link):
548 def IN_LAUNCH_DIR(conf):
549 '''return True if this rule is being run from the launch directory'''
550 return os.path.realpath(conf.curdir) == os.path.realpath(Options.launch_dir)
551 Options.Handler.IN_LAUNCH_DIR = IN_LAUNCH_DIR
555 def SAMBA_CONFIG_H(conf, path=None):
556 '''write out config.h in the right directory'''
557 # we don't want to produce a config.h in places like lib/replace
558 # when we are building projects that depend on lib/replace
559 if not IN_LAUNCH_DIR(conf):
562 if Options.options.developer:
563 # we add these here to ensure that -Wstrict-prototypes is not set during configure
564 conf.ADD_CFLAGS('-Wall -g -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Werror-implicit-function-declaration -Wformat=2 -Wno-format-y2k',
567 if Options.options.picky_developer:
568 conf.ADD_CFLAGS('-Werror', testflags=True)
570 if Options.options.fatal_errors:
571 conf.ADD_CFLAGS('-Wfatal-errors', testflags=True)
573 if Options.options.pedantic:
574 conf.ADD_CFLAGS('-W', testflags=True)
577 conf.write_config_header('config.h', top=True)
579 conf.write_config_header(path)
580 conf.SAMBA_CROSS_CHECK_COMPLETE()
584 def CONFIG_PATH(conf, name, default):
585 '''setup a configurable path'''
586 if not name in conf.env:
587 if default[0] == '/':
588 conf.env[name] = default
590 conf.env[name] = conf.env['PREFIX'] + default
593 def ADD_CFLAGS(conf, flags, testflags=False):
594 '''add some CFLAGS to the command line
595 optionally set testflags to ensure all the flags work
599 for f in flags.split():
600 if CHECK_CFLAGS(conf, f):
603 if not 'EXTRA_CFLAGS' in conf.env:
604 conf.env['EXTRA_CFLAGS'] = []
605 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
610 def ADD_EXTRA_INCLUDES(conf, includes):
611 '''add some extra include directories to all builds'''
612 if not 'EXTRA_INCLUDES' in conf.env:
613 conf.env['EXTRA_INCLUDES'] = []
614 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
618 def CURRENT_CFLAGS(bld, target, cflags, hide_symbols=False):
619 '''work out the current flags. local flags are added first'''
620 if not 'EXTRA_CFLAGS' in bld.env:
623 list = bld.env['EXTRA_CFLAGS'];
624 ret = TO_LIST(cflags)
626 if hide_symbols and bld.env.HAVE_VISIBILITY_ATTR:
627 ret.append('-fvisibility=hidden')
632 def CHECK_CC_ENV(conf):
633 """trim whitespaces from 'CC'.
634 The build farm sometimes puts a space at the start"""
635 if os.environ.get('CC'):
636 conf.env.CC = TO_LIST(os.environ.get('CC'))
637 if len(conf.env.CC) == 1:
638 # make for nicer logs if just a single command
639 conf.env.CC = conf.env.CC[0]
643 def SETUP_CONFIGURE_CACHE(conf, enable):
644 '''enable/disable cache of configure results'''
646 # when -C is chosen, we will use a private cache and will
647 # not look into system includes. This roughtly matches what
648 # autoconf does with -C
649 cache_path = os.path.join(conf.blddir, '.confcache')
651 Options.cache_global = os.environ['WAFCACHE'] = cache_path
653 # when -C is not chosen we will not cache configure checks
654 # We set the recursion limit low to prevent waf from spending
655 # a lot of time on the signatures of the files.
656 Options.cache_global = os.environ['WAFCACHE'] = ''
657 preproc.recursion_limit = 1
658 # in either case we don't need to scan system includes
659 preproc.go_absolute = False