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
, execute
=False, define
=define
)
144 return conf
.check_cc(function_name
=f
, header_name
=hlist
)
148 def CHECK_FUNCS(conf
, list, checklink
=False, header
=''):
149 '''check for a list of functions'''
151 for f
in TO_LIST(list):
152 if not CHECK_FUNC(conf
, f
, checklink
=checklink
, header
=header
):
158 def CHECK_SIZEOF(conf
, vars, headers
=None, define
=None):
159 '''check the size of a type'''
161 if headers
is not None:
162 hlist
= TO_LIST(headers
)
164 hlist
= conf
.env
.hlist
166 hdrs
+= '#include <%s>\n' % h
167 for v
in TO_LIST(vars):
169 define_name
= 'SIZEOF_%s' % string
.replace(v
.upper(), ' ', '_')
176 printf("%%u\\n", (unsigned)sizeof(%s));
182 define_name
=define_name
,
184 msg
="Checking size of %s" % v
)
188 def CHECK_CODE(conf
, code
, define
,
189 always
=False, execute
=False, addmain
=True, mandatory
=False,
190 headers
=None, msg
=None, cflags
='', includes
='# .',
192 '''check if some code compiles and/or runs'''
194 if headers
is not None:
195 hlist
= TO_LIST(headers
)
197 hlist
= conf
.env
.hlist
199 hdrs
+= '#include <%s>\n' % h
207 fragment
='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs
, code
)
209 fragment
='#include "__confdefs.h"\n%s\n%s' % (hdrs
, code
)
211 conf
.write_config_header('__confdefs.h', top
=True)
214 msg
="Checking for %s" % define
216 # include the directory containing __confdefs.h
217 cflags
+= ' -I../../default'
220 cflags
+= ' -I%s' % conf
.curdir
222 if conf
.check(fragment
=fragment
,
224 define_name
= define
,
225 mandatory
= mandatory
,
226 ccflags
=TO_LIST(cflags
),
229 conf
.DEFINE(define
, 1)
232 conf
.DEFINE(define
, 0)
238 def CHECK_STRUCTURE_MEMBER(conf
, structname
, member
,
239 always
=False, define
=None, headers
=None):
240 '''check for a structure member'''
242 if headers
is not None:
243 hlist
= TO_LIST(headers
)
245 hlist
= conf
.env
.hlist
247 hdrs
+= '#include <%s>\n' % h
249 define
= 'HAVE_%s' % member
.upper()
250 if conf
.check(fragment
=
255 void *_x; _x=(void *)&s.%s;
258 ''' % (hdrs
, structname
, member
),
260 msg
="Checking for member %s in %s" % (member
, structname
)):
261 conf
.DEFINE(define
, 1)
264 conf
.DEFINE(define
, 0)
269 def CHECK_CFLAGS(conf
, cflags
, variable
):
270 '''check if the given cflags are accepted by the compiler'''
271 if conf
.check(fragment
='int main(void) { return 0; }',
274 msg
="Checking compiler accepts %s" % cflags
):
275 conf
.env
[variable
] = cflags
280 #################################################
281 # return True if a configuration option was found
283 def CONFIG_SET(conf
, option
):
284 return (option
in conf
.env
) and (conf
.env
[option
] != ())
285 Build
.BuildContext
.CONFIG_SET
= CONFIG_SET
288 ###########################################################
289 # check that the functions in 'list' are available in 'library'
290 # if they are, then make that library available as a dependency
292 # if the library is not available and mandatory==True, then
295 # If the library is not available and mandatory==False, then
296 # add the library to the list of dependencies to remove from
299 # optionally check for the functions first in libc
301 def CHECK_FUNCS_IN(conf
, list, library
, mandatory
=False, checklibc
=False, header
=''):
302 remaining
= TO_LIST(list)
303 liblist
= TO_LIST(library
)
305 hlist
= conf
.env
.hlist
[:]
306 for h
in TO_LIST(header
):
307 if CHECK_HEADER(conf
, h
, add_headers
=False):
310 # check if some already found
311 for f
in remaining
[:]:
312 if CONFIG_SET(conf
, 'HAVE_%s' % f
.upper()):
315 # see if the functions are in libc
317 for f
in remaining
[:]:
318 if CHECK_FUNC(conf
, f
, checklink
=True, header
=header
):
323 if GET_TARGET_TYPE(conf
, lib
) != 'SYSLIB':
324 SET_TARGET_TYPE(conf
, lib
, 'EMPTY')
328 for lib
in liblist
[:]:
329 if GET_TARGET_TYPE(conf
, lib
):
331 if not conf
.check(lib
=lib
, uselib_store
=lib
):
332 conf
.ASSERT(not mandatory
,
333 "Mandatory library '%s' not found for functions '%s'" % (lib
, list))
334 # if it isn't a mandatory library, then remove it from dependency lists
335 SET_TARGET_TYPE(conf
, lib
, 'EMPTY')
338 conf
.define('HAVE_LIB%s' % string
.replace(lib
.upper(),'-','_'), 1)
345 if not conf
.check_cc(function_name
=f
, lib
=liblist
, header_name
=hlist
):
352 if GET_TARGET_TYPE(conf
, lib
):
354 conf
.env
['LIB_' + lib
.upper()] = lib
355 LOCAL_CACHE_SET(conf
, 'TARGET_TYPE', lib
, 'SYSLIB')
360 #################################################
361 # write out config.h in the right directory
363 def SAMBA_CONFIG_H(conf
, path
=None):
364 # we don't want to produce a config.h in places like lib/replace
365 # when we are building projects that depend on lib/replace
366 if os
.path
.realpath(conf
.curdir
) != os
.path
.realpath(Options
.launch_dir
):
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
)