build: expand CHECK_CODE() function
[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, 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):
53     '''check for a type in a specific header'''
54     if conf.check(header_name=hdr):
55         conf.check(type_name=t, header_name=hdr)
56         return True
57     return False
58
59
60 @conf
61 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
62     '''check for a type with an alternate'''
63     if headers is None:
64         headers = conf.env.hlist
65     if define is not None:
66         ret = conf.check(type_name=t, header_name=headers, define_name=define)
67     else:
68         ret = conf.check(type_name=t, header_name=headers)
69     if not ret and alternate is not None:
70         conf.DEFINE(t, alternate)
71     return ret
72
73
74 @conf
75 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
76     '''check for a variable declaration (or define)'''
77     hdrs=''
78     if headers is not None:
79         hlist = to_list(headers)
80     else:
81         hlist = conf.env.hlist
82     for h in hlist:
83         hdrs += '#include <%s>\n' % h
84     if define is None:
85         define = 'HAVE_%s' % v.upper()
86     if conf.check(fragment=
87                   '''
88                   %s
89                   int main(void) {
90                     #ifndef %s
91                     void *_x; _x=(void *)&%s;
92                     #endif
93                     return 0;
94                   }
95                   ''' % (hdrs, v, v),
96                   execute=0,
97                   msg="Checking for variable %s" % v):
98         conf.DEFINE(define, 1)
99         return True
100     elif always:
101         conf.DEFINE(define, 0)
102         return False
103
104 @conf
105 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
106     '''check a list of variable declarations, using the HAVE_DECL_xxx form
107        of define
108
109        When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
110        '''
111     ret = True
112     for v in to_list(vars):
113         if not reverse:
114             define='HAVE_DECL_%s' % v.upper()
115         else:
116             define='HAVE_%s_DECL' % v.upper()
117         if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
118             ret = False
119     return ret
120
121
122 @runonce
123 def CHECK_FUNC(conf, f):
124     '''check for a function'''
125     return conf.check(function_name=f, header_name=conf.env.hlist)
126
127
128 @conf
129 def CHECK_FUNCS(conf, list):
130     '''check for a list of functions'''
131     ret = True
132     for f in to_list(list):
133         if not CHECK_FUNC(conf, f):
134             ret = False
135     return ret
136
137
138 @conf
139 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
140     '''check the size of a type'''
141     hdrs=''
142     if headers is not None:
143         hlist = to_list(headers)
144     else:
145         hlist = conf.env.hlist
146     for h in hlist:
147         hdrs += '#include <%s>\n' % h
148     for v in to_list(vars):
149         if define is None:
150             define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
151         else:
152             define_name = define
153         conf.check(fragment=
154                    '''
155                   %s
156                   int main(void) {
157                     printf("%%u\\n", (unsigned)sizeof(%s));
158                     return 0;
159                   }
160                   ''' % (hdrs, v),
161                    execute=1,
162                    define_ret=True,
163                    define_name=define_name,
164                    quote=False,
165                    msg="Checking size of %s" % v)
166
167
168 @conf
169 def CHECK_CODE(conf, code, define,
170                always=False, execute=False, addmain=True,
171                headers=None, msg=None):
172     '''check if some code compiles and/or runs'''
173     hdrs=''
174     if headers is not None:
175         hlist = to_list(headers)
176     else:
177         hlist = conf.env.hlist
178     for h in hlist:
179         hdrs += '#include <%s>\n' % h
180
181     if execute:
182         execute = 1
183     else:
184         execute = 0
185
186     if addmain:
187         fragment='#include "confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
188     else:
189         fragment='#include "confdefs.h"\n%s\n%s' % (hdrs, code)
190
191     conf.write_config_header('confdefs.h', top=True)
192
193     if msg is None:
194         msg="Checking for %s" % define
195
196     if conf.check(fragment=fragment,
197                   execute=execute,
198                   ccflags='-I%s' % conf.curdir,
199                   includes='# . ../default',
200                   msg=msg):
201         conf.DEFINE(define, 1)
202         return True
203     elif always:
204         conf.DEFINE(define, 0)
205         return False
206
207
208 @conf
209 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
210                            always=False, define=None, headers=None):
211     '''check for a structure member'''
212     hdrs=''
213     if headers is not None:
214         hlist = to_list(headers)
215     else:
216         hlist = conf.env.hlist
217     for h in hlist:
218         hdrs += '#include <%s>\n' % h
219     if define is None:
220         define = 'HAVE_%s' % member.upper()
221     if conf.check(fragment=
222                   '''
223                   %s
224                   int main(void) {
225                     %s s;
226                     void *_x; _x=(void *)&s.%s;
227                     return 0;
228                   }
229                   ''' % (hdrs, structname, member),
230                   execute=0,
231                   msg="Checking for member %s in %s" % (member, structname)):
232         conf.DEFINE(define, 1)
233         return True
234     elif always:
235         conf.DEFINE(define, 0)
236         return False
237
238
239 #################################################
240 # return True if a configuration option was found
241 @conf
242 def CONFIG_SET(conf, option):
243     return (option in conf.env) and (conf.env[option] != ())
244 Build.BuildContext.CONFIG_SET = CONFIG_SET
245
246
247 ###########################################################
248 # check that the functions in 'list' are available in 'library'
249 # if they are, then make that library available as a dependency
250 #
251 # if the library is not available and mandatory==True, then
252 # raise an error.
253 #
254 # If the library is not available and mandatory==False, then
255 # add the library to the list of dependencies to remove from
256 # build rules
257 #
258 # optionally check for the functions first in libc
259 @conf
260 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
261     # first see if the functions are in libc
262     if checklibc:
263         remaining = []
264         for f in to_list(list):
265             if not CHECK_FUNC(conf, f):
266                 remaining.append(f)
267     else:
268         remaining = to_list(list)
269
270     if remaining == []:
271         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
272         return True
273
274     if not conf.check(lib=library, uselib_store=library):
275         conf.ASSERT(not mandatory,
276                     "Mandatory library '%s' not found for functions '%s'" % (library, list))
277         # if it isn't a mandatory library, then remove it from dependency lists
278         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
279         return False
280
281     ret = True
282     for f in remaining:
283         if not conf.check(function_name=f, lib=library, header_name=conf.env.hlist):
284             ret = False
285     conf.env['LIB_' + library.upper()] = library
286     LOCAL_CACHE_SET(conf, 'TARGET_TYPE', library, 'SYSLIB')
287     return ret
288
289
290 #################################################
291 # write out config.h in the right directory
292 @conf
293 def SAMBA_CONFIG_H(conf, path=None):
294     if os.path.normpath(conf.curdir) != os.path.normpath(os.environ.get('PWD')):
295         return
296     if path is None:
297         conf.write_config_header('config.h', top=True)
298     else:
299         conf.write_config_header(path)
300
301
302 ##############################################################
303 # setup a configurable path
304 @conf
305 def CONFIG_PATH(conf, name, default):
306     if not name in conf.env:
307         conf.env[name] = conf.env['PREFIX'] + default
308     conf.define(name, conf.env[name], quote=True)
309
310 ##############################################################
311 # add some CFLAGS to the command line
312 @conf
313 def ADD_CFLAGS(conf, flags):
314     if not 'EXTRA_CFLAGS' in conf.env:
315         conf.env['EXTRA_CFLAGS'] = []
316     conf.env['EXTRA_CFLAGS'].extend(to_list(flags))
317
318 ##############################################################
319 # add some extra include directories to all builds
320 @conf
321 def ADD_EXTRA_INCLUDES(conf, includes):
322     if not 'EXTRA_INCLUDES' in conf.env:
323         conf.env['EXTRA_INCLUDES'] = []
324     conf.env['EXTRA_INCLUDES'].extend(to_list(includes))
325
326
327 ##############################################################
328 # work out the current flags. local flags are added first
329 def CURRENT_CFLAGS(bld, cflags):
330     if not 'EXTRA_CFLAGS' in bld.env:
331         list = []
332     else:
333         list = bld.env['EXTRA_CFLAGS'];
334     ret = to_list(cflags)
335     ret.extend(list)
336     return ret