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