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 if conf
.check(header_name
=h
) and add_headers
:
25 conf
.env
.hlist
.append(h
)
31 def CHECK_HEADERS(conf
, list, add_headers
=True):
32 '''check for a list of headers'''
34 for hdr
in TO_LIST(list):
35 if not CHECK_HEADER(conf
, hdr
, add_headers
):
41 def CHECK_TYPES(conf
, list):
42 '''check for a list of types'''
45 for t
in TO_LIST(list):
46 if not conf
.check(type_name
=t
, header_name
=conf
.env
.hlist
):
52 def CHECK_TYPE_IN(conf
, t
, hdr
, define
=None):
53 '''check for a type in a specific header'''
54 if conf
.check(header_name
=hdr
):
56 ret
= conf
.check(type_name
=t
, header_name
=hdr
)
58 ret
= conf
.check(type_name
=t
, header_name
=hdr
, define_name
=define
)
64 def CHECK_TYPE(conf
, t
, alternate
=None, headers
=None, define
=None):
65 '''check for a type with an alternate'''
67 headers
= conf
.env
.hlist
68 if define
is not None:
69 ret
= conf
.check(type_name
=t
, header_name
=headers
, define_name
=define
)
71 ret
= conf
.check(type_name
=t
, header_name
=headers
)
72 if not ret
and alternate
is not None:
73 conf
.DEFINE(t
, alternate
)
78 def CHECK_VARIABLE(conf
, v
, define
=None, always
=False, headers
=None):
79 '''check for a variable declaration (or define)'''
81 if headers
is not None:
82 hlist
= TO_LIST(headers
)
84 hlist
= conf
.env
.hlist
86 hdrs
+= '#include <%s>\n' % h
88 define
= 'HAVE_%s' % v
.upper()
89 if conf
.check(fragment
=
94 void *_x; _x=(void *)&%s;
100 msg
="Checking for variable %s" % v
):
101 conf
.DEFINE(define
, 1)
104 conf
.DEFINE(define
, 0)
108 def CHECK_DECLS(conf
, vars, reverse
=False, headers
=None):
109 '''check a list of variable declarations, using the HAVE_DECL_xxx form
112 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
115 for v
in TO_LIST(vars):
117 define
='HAVE_DECL_%s' % v
.upper()
119 define
='HAVE_%s_DECL' % v
.upper()
120 if not CHECK_VARIABLE(conf
, v
, define
=define
, headers
=headers
):
126 def CHECK_FUNC(conf
, f
, checklink
=False):
127 '''check for a function'''
129 return CHECK_CODE(conf
, '%s()' % f
, execute
=False, define
='HAVE_%s' % f
.upper())
130 return conf
.check(function_name
=f
, header_name
=conf
.env
.hlist
)
134 def CHECK_FUNCS(conf
, list, checklink
=False):
135 '''check for a list of functions'''
137 for f
in TO_LIST(list):
138 if not CHECK_FUNC(conf
, f
, checklink
):
144 def CHECK_SIZEOF(conf
, vars, headers
=None, define
=None):
145 '''check the size of a type'''
147 if headers
is not None:
148 hlist
= TO_LIST(headers
)
150 hlist
= conf
.env
.hlist
152 hdrs
+= '#include <%s>\n' % h
153 for v
in TO_LIST(vars):
155 define_name
= 'SIZEOF_%s' % string
.replace(v
.upper(), ' ', '_')
162 printf("%%u\\n", (unsigned)sizeof(%s));
168 define_name
=define_name
,
170 msg
="Checking size of %s" % v
)
174 def CHECK_CODE(conf
, code
, define
,
175 always
=False, execute
=False, addmain
=True, mandatory
=False,
176 headers
=None, msg
=None, cflags
='', includes
='# .',
178 '''check if some code compiles and/or runs'''
180 if headers
is not None:
181 hlist
= TO_LIST(headers
)
183 hlist
= conf
.env
.hlist
185 hdrs
+= '#include <%s>\n' % h
193 fragment
='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs
, code
)
195 fragment
='#include "__confdefs.h"\n%s\n%s' % (hdrs
, code
)
197 conf
.write_config_header('__confdefs.h', top
=True)
200 msg
="Checking for %s" % define
202 # include the directory containing __confdefs.h
203 cflags
+= ' -I../../default'
206 cflags
+= ' -I%s' % conf
.curdir
208 if conf
.check(fragment
=fragment
,
210 define_name
= define
,
211 mandatory
= mandatory
,
212 ccflags
=TO_LIST(cflags
),
215 conf
.DEFINE(define
, 1)
218 conf
.DEFINE(define
, 0)
224 def CHECK_STRUCTURE_MEMBER(conf
, structname
, member
,
225 always
=False, define
=None, headers
=None):
226 '''check for a structure member'''
228 if headers
is not None:
229 hlist
= TO_LIST(headers
)
231 hlist
= conf
.env
.hlist
233 hdrs
+= '#include <%s>\n' % h
235 define
= 'HAVE_%s' % member
.upper()
236 if conf
.check(fragment
=
241 void *_x; _x=(void *)&s.%s;
244 ''' % (hdrs
, structname
, member
),
246 msg
="Checking for member %s in %s" % (member
, structname
)):
247 conf
.DEFINE(define
, 1)
250 conf
.DEFINE(define
, 0)
255 def CHECK_CFLAGS(conf
, cflags
, variable
):
256 '''check if the given cflags are accepted by the compiler'''
257 if conf
.check(fragment
='int main(void) { return 0; }',
260 msg
="Checking compiler accepts %s" % cflags
):
261 conf
.env
[variable
] = cflags
266 #################################################
267 # return True if a configuration option was found
269 def CONFIG_SET(conf
, option
):
270 return (option
in conf
.env
) and (conf
.env
[option
] != ())
271 Build
.BuildContext
.CONFIG_SET
= CONFIG_SET
274 ###########################################################
275 # check that the functions in 'list' are available in 'library'
276 # if they are, then make that library available as a dependency
278 # if the library is not available and mandatory==True, then
281 # If the library is not available and mandatory==False, then
282 # add the library to the list of dependencies to remove from
285 # optionally check for the functions first in libc
287 def CHECK_FUNCS_IN(conf
, list, library
, mandatory
=False, checklibc
=False):
288 # first see if the functions are in libc
291 for f
in TO_LIST(list):
292 if not CHECK_FUNC(conf
, f
):
295 remaining
= TO_LIST(list)
298 SET_TARGET_TYPE(conf
, library
, 'EMPTY')
301 if not conf
.check(lib
=library
, uselib_store
=library
):
302 conf
.ASSERT(not mandatory
,
303 "Mandatory library '%s' not found for functions '%s'" % (library
, list))
304 # if it isn't a mandatory library, then remove it from dependency lists
305 SET_TARGET_TYPE(conf
, library
, 'EMPTY')
308 conf
.define('HAVE_LIB%s' % string
.replace(library
.upper(),'-','_'), 1)
312 if not conf
.check(function_name
=f
, lib
=library
, header_name
=conf
.env
.hlist
):
314 conf
.env
['LIB_' + library
.upper()] = library
315 LOCAL_CACHE_SET(conf
, 'TARGET_TYPE', library
, 'SYSLIB')
319 #################################################
320 # write out config.h in the right directory
322 def SAMBA_CONFIG_H(conf
, path
=None):
323 # we don't want to produce a config.h in places like lib/replace
324 # when we are building projects that depend on lib/replace
325 if os
.path
.realpath(conf
.curdir
) != os
.path
.realpath(Options
.launch_dir
):
328 conf
.write_config_header('config.h', top
=True)
330 conf
.write_config_header(path
)
333 ##############################################################
334 # setup a configurable path
336 def CONFIG_PATH(conf
, name
, default
):
337 if not name
in conf
.env
:
338 if default
[0] == '/':
339 conf
.env
[name
] = default
341 conf
.env
[name
] = conf
.env
['PREFIX'] + default
342 conf
.define(name
, conf
.env
[name
], quote
=True)
344 ##############################################################
345 # add some CFLAGS to the command line
347 def ADD_CFLAGS(conf
, flags
):
348 if not 'EXTRA_CFLAGS' in conf
.env
:
349 conf
.env
['EXTRA_CFLAGS'] = []
350 conf
.env
['EXTRA_CFLAGS'].extend(TO_LIST(flags
))
352 ##############################################################
353 # add some extra include directories to all builds
355 def ADD_EXTRA_INCLUDES(conf
, includes
):
356 if not 'EXTRA_INCLUDES' in conf
.env
:
357 conf
.env
['EXTRA_INCLUDES'] = []
358 conf
.env
['EXTRA_INCLUDES'].extend(TO_LIST(includes
))
361 ##############################################################
362 # work out the current flags. local flags are added first
363 def CURRENT_CFLAGS(bld
, target
, cflags
):
364 if not 'EXTRA_CFLAGS' in bld
.env
:
367 list = bld
.env
['EXTRA_CFLAGS'];
368 ret
= TO_LIST(cflags
)