build: fixed check for pthread_create()
[abartlet/samba.git/.git] / buildtools / wafsamba / samba_autoconf.py
1 # a waf tool to add autoconf-like macros to the configure section
2
3 import Build, os, Options
4 import string
5 from Configure import conf
6 from samba_utils import *
7
8 ####################################################
9 # some autoconf like helpers, to make the transition
10 # to waf a bit easier for those used to autoconf
11 # m4 files
12
13 @runonce
14 @conf
15 def DEFINE(conf, d, v, add_to_cflags=False):
16     '''define a config option'''
17     conf.define(d, v, quote=False)
18     if add_to_cflags:
19         conf.env.append_value('CCDEFINES', d + '=' + str(v))
20
21
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):
26         if add_headers:
27             conf.env.hlist.append(h)
28             conf.env.hlist = unique_list(conf.env.hlist)
29         return True
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)
34     return ret
35
36
37 @conf
38 def CHECK_HEADERS(conf, list, add_headers=True):
39     '''check for a list of headers'''
40     ret = True
41     for hdr in TO_LIST(list):
42         if not CHECK_HEADER(conf, hdr, add_headers):
43             ret = False
44     return ret
45
46
47 @conf
48 def CHECK_TYPES(conf, list):
49     '''check for a list of types'''
50     ret = True
51     lst = TO_LIST(list)
52     for t in TO_LIST(list):
53         if not conf.check(type_name=t, header_name=conf.env.hlist):
54             ret = False
55     return ret
56
57
58 @conf
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):
62         if define is None:
63             ret = conf.check(type_name=t, header_name=hdr)
64         else:
65             ret = conf.check(type_name=t, header_name=hdr, define_name=define)
66         return ret
67     return False
68
69
70 @conf
71 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
72     '''check for a type with an alternate'''
73     if headers is None:
74         headers = conf.env.hlist
75     if define is not None:
76         ret = conf.check(type_name=t, header_name=headers, define_name=define)
77     else:
78         ret = conf.check(type_name=t, header_name=headers)
79     if not ret and alternate is not None:
80         conf.DEFINE(t, alternate)
81     return ret
82
83
84 @conf
85 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
86     '''check for a variable declaration (or define)'''
87     hdrs=''
88     if headers is not None:
89         hlist = TO_LIST(headers)
90     else:
91         hlist = conf.env.hlist
92     for h in hlist:
93         hdrs += '#include <%s>\n' % h
94     if define is None:
95         define = 'HAVE_%s' % v.upper()
96     if conf.check(fragment=
97                   '''
98                   %s
99                   int main(void) {
100                     #ifndef %s
101                     void *_x; _x=(void *)&%s;
102                     #endif
103                     return 0;
104                   }
105                   ''' % (hdrs, v, v),
106                   execute=0,
107                   msg="Checking for variable %s" % v):
108         conf.DEFINE(define, 1)
109         return True
110     elif always:
111         conf.DEFINE(define, 0)
112         return False
113
114 @conf
115 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
116     '''check a list of variable declarations, using the HAVE_DECL_xxx form
117        of define
118
119        When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
120        '''
121     ret = True
122     for v in TO_LIST(vars):
123         if not reverse:
124             define='HAVE_DECL_%s' % v.upper()
125         else:
126             define='HAVE_%s_DECL' % v.upper()
127         if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
128             ret = False
129     return ret
130
131
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):
137             hlist.append(h)
138     define='HAVE_%s' % f.upper()
139     if CONFIG_SET(conf, define):
140         return True
141     if checklink:
142         return CHECK_CODE(conf, 'void *x = (void *)%s' % f, execute=False, define=define)
143
144     return conf.check_cc(function_name=f, header_name=hlist)
145
146
147 @conf
148 def CHECK_FUNCS(conf, list, checklink=False, header=''):
149     '''check for a list of functions'''
150     ret = True
151     for f in TO_LIST(list):
152         if not CHECK_FUNC(conf, f, checklink=checklink, header=header):
153             ret = False
154     return ret
155
156
157 @conf
158 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
159     '''check the size of a type'''
160     hdrs=''
161     if headers is not None:
162         hlist = TO_LIST(headers)
163     else:
164         hlist = conf.env.hlist
165     for h in hlist:
166         hdrs += '#include <%s>\n' % h
167     for v in TO_LIST(vars):
168         if define is None:
169             define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
170         else:
171             define_name = define
172         conf.check(fragment=
173                    '''
174                   %s
175                   int main(void) {
176                     printf("%%u\\n", (unsigned)sizeof(%s));
177                     return 0;
178                   }
179                   ''' % (hdrs, v),
180                    execute=1,
181                    define_ret=True,
182                    define_name=define_name,
183                    quote=False,
184                    msg="Checking size of %s" % v)
185
186
187 @conf
188 def CHECK_CODE(conf, code, define,
189                always=False, execute=False, addmain=True, mandatory=False,
190                headers=None, msg=None, cflags='', includes='# .',
191                local_include=True):
192     '''check if some code compiles and/or runs'''
193     hdrs=''
194     if headers is not None:
195         hlist = TO_LIST(headers)
196     else:
197         hlist = conf.env.hlist
198     for h in hlist:
199         hdrs += '#include <%s>\n' % h
200
201     if execute:
202         execute = 1
203     else:
204         execute = 0
205
206     if addmain:
207         fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
208     else:
209         fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
210
211     conf.write_config_header('__confdefs.h', top=True)
212
213     if msg is None:
214         msg="Checking for %s" % define
215
216     # include the directory containing __confdefs.h
217     cflags += ' -I../../default'
218
219     if local_include:
220         cflags += ' -I%s' % conf.curdir
221
222     if conf.check(fragment=fragment,
223                   execute=execute,
224                   define_name = define,
225                   mandatory = mandatory,
226                   ccflags=TO_LIST(cflags),
227                   includes=includes,
228                   msg=msg):
229         conf.DEFINE(define, 1)
230         return True
231     if always:
232         conf.DEFINE(define, 0)
233     return False
234
235
236
237 @conf
238 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
239                            always=False, define=None, headers=None):
240     '''check for a structure member'''
241     hdrs=''
242     if headers is not None:
243         hlist = TO_LIST(headers)
244     else:
245         hlist = conf.env.hlist
246     for h in hlist:
247         hdrs += '#include <%s>\n' % h
248     if define is None:
249         define = 'HAVE_%s' % member.upper()
250     if conf.check(fragment=
251                   '''
252                   %s
253                   int main(void) {
254                     %s s;
255                     void *_x; _x=(void *)&s.%s;
256                     return 0;
257                   }
258                   ''' % (hdrs, structname, member),
259                   execute=0,
260                   msg="Checking for member %s in %s" % (member, structname)):
261         conf.DEFINE(define, 1)
262         return True
263     elif always:
264         conf.DEFINE(define, 0)
265         return False
266
267
268 @conf
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; }',
272                   execute=0,
273                   ccflags=cflags,
274                   msg="Checking compiler accepts %s" % cflags):
275         conf.env[variable] = cflags
276         return True
277     return False
278
279
280 #################################################
281 # return True if a configuration option was found
282 @conf
283 def CONFIG_SET(conf, option):
284     return (option in conf.env) and (conf.env[option] != ())
285 Build.BuildContext.CONFIG_SET = CONFIG_SET
286
287
288 ###########################################################
289 # check that the functions in 'list' are available in 'library'
290 # if they are, then make that library available as a dependency
291 #
292 # if the library is not available and mandatory==True, then
293 # raise an error.
294 #
295 # If the library is not available and mandatory==False, then
296 # add the library to the list of dependencies to remove from
297 # build rules
298 #
299 # optionally check for the functions first in libc
300 @conf
301 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header=''):
302     remaining = TO_LIST(list)
303     liblist   = TO_LIST(library)
304
305     hlist = conf.env.hlist[:]
306     for h in TO_LIST(header):
307         if CHECK_HEADER(conf, h, add_headers=False):
308             hlist.append(h)
309
310     # check if some already found
311     for f in remaining[:]:
312         if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
313             remaining.remove(f)
314
315     # see if the functions are in libc
316     if checklibc:
317         for f in remaining[:]:
318             if CHECK_FUNC(conf, f, checklink=True, header=header):
319                 remaining.remove(f)
320
321     if remaining == []:
322         for lib in liblist:
323             if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
324                 SET_TARGET_TYPE(conf, lib, 'EMPTY')
325         return True
326
327     ret = True
328     for lib in liblist[:]:
329         if GET_TARGET_TYPE(conf, lib):
330             continue
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')
336             ret = False
337         else:
338             conf.define('HAVE_LIB%s' % string.replace(lib.upper(),'-','_'), 1)
339
340     if not ret:
341         return ret
342
343     ret = True
344     for f in remaining:
345         if not conf.check_cc(function_name=f, lib=liblist, header_name=hlist):
346             ret = False
347
348     if not ret:
349         return ret
350
351     for lib in liblist:
352         if GET_TARGET_TYPE(conf, lib):
353             continue
354         conf.env['LIB_' + lib.upper()] = lib
355         LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
356
357     return ret
358
359
360 #################################################
361 # write out config.h in the right directory
362 @conf
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):
367         return
368     if path is None:
369         conf.write_config_header('config.h', top=True)
370     else:
371         conf.write_config_header(path)
372
373
374 ##############################################################
375 # setup a configurable path
376 @conf
377 def CONFIG_PATH(conf, name, default):
378     if not name in conf.env:
379         if default[0] == '/':
380             conf.env[name] = default
381         else:
382             conf.env[name] = conf.env['PREFIX'] + default
383     conf.define(name, conf.env[name], quote=True)
384
385 ##############################################################
386 # add some CFLAGS to the command line
387 @conf
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))
392
393 ##############################################################
394 # add some extra include directories to all builds
395 @conf
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))
400
401
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:
406         list = []
407     else:
408         list = bld.env['EXTRA_CFLAGS'];
409     ret = TO_LIST(cflags)
410     ret.extend(list)
411     return ret