build: move gettimeofday check to libreplace
[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, checklink=False):
124     '''check for a function'''
125     if checklink:
126         return CHECK_CODE(conf, '%s()' % f, execute=False, define='HAVE_%s' % f.upper())
127     return conf.check(function_name=f, header_name=conf.env.hlist)
128
129
130 @conf
131 def CHECK_FUNCS(conf, list, checklink=False):
132     '''check for a list of functions'''
133     ret = True
134     for f in to_list(list):
135         if not CHECK_FUNC(conf, f, checklink):
136             ret = False
137     return ret
138
139
140 @conf
141 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
142     '''check the size of a type'''
143     hdrs=''
144     if headers is not None:
145         hlist = to_list(headers)
146     else:
147         hlist = conf.env.hlist
148     for h in hlist:
149         hdrs += '#include <%s>\n' % h
150     for v in to_list(vars):
151         if define is None:
152             define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
153         else:
154             define_name = define
155         conf.check(fragment=
156                    '''
157                   %s
158                   int main(void) {
159                     printf("%%u\\n", (unsigned)sizeof(%s));
160                     return 0;
161                   }
162                   ''' % (hdrs, v),
163                    execute=1,
164                    define_ret=True,
165                    define_name=define_name,
166                    quote=False,
167                    msg="Checking size of %s" % v)
168
169
170 @conf
171 def CHECK_CODE(conf, code, define,
172                always=False, execute=False, addmain=True,
173                headers=None, msg=None):
174     '''check if some code compiles and/or runs'''
175     hdrs=''
176     if headers is not None:
177         hlist = to_list(headers)
178     else:
179         hlist = conf.env.hlist
180     for h in hlist:
181         hdrs += '#include <%s>\n' % h
182
183     if execute:
184         execute = 1
185     else:
186         execute = 0
187
188     if addmain:
189         fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
190     else:
191         fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
192
193     conf.write_config_header('__confdefs.h', top=True)
194
195     if msg is None:
196         msg="Checking for %s" % define
197
198     if conf.check(fragment=fragment,
199                   execute=execute,
200                   ccflags='-I%s' % conf.curdir,
201                   includes='# . ../default',
202                   msg=msg):
203         conf.DEFINE(define, 1)
204         return True
205     elif always:
206         conf.DEFINE(define, 0)
207         return False
208
209
210 @conf
211 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
212                            always=False, define=None, headers=None):
213     '''check for a structure member'''
214     hdrs=''
215     if headers is not None:
216         hlist = to_list(headers)
217     else:
218         hlist = conf.env.hlist
219     for h in hlist:
220         hdrs += '#include <%s>\n' % h
221     if define is None:
222         define = 'HAVE_%s' % member.upper()
223     if conf.check(fragment=
224                   '''
225                   %s
226                   int main(void) {
227                     %s s;
228                     void *_x; _x=(void *)&s.%s;
229                     return 0;
230                   }
231                   ''' % (hdrs, structname, member),
232                   execute=0,
233                   msg="Checking for member %s in %s" % (member, structname)):
234         conf.DEFINE(define, 1)
235         return True
236     elif always:
237         conf.DEFINE(define, 0)
238         return False
239
240
241 #################################################
242 # return True if a configuration option was found
243 @conf
244 def CONFIG_SET(conf, option):
245     return (option in conf.env) and (conf.env[option] != ())
246 Build.BuildContext.CONFIG_SET = CONFIG_SET
247
248
249 ###########################################################
250 # check that the functions in 'list' are available in 'library'
251 # if they are, then make that library available as a dependency
252 #
253 # if the library is not available and mandatory==True, then
254 # raise an error.
255 #
256 # If the library is not available and mandatory==False, then
257 # add the library to the list of dependencies to remove from
258 # build rules
259 #
260 # optionally check for the functions first in libc
261 @conf
262 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
263     # first see if the functions are in libc
264     if checklibc:
265         remaining = []
266         for f in to_list(list):
267             if not CHECK_FUNC(conf, f):
268                 remaining.append(f)
269     else:
270         remaining = to_list(list)
271
272     if remaining == []:
273         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
274         return True
275
276     if not conf.check(lib=library, uselib_store=library):
277         conf.ASSERT(not mandatory,
278                     "Mandatory library '%s' not found for functions '%s'" % (library, list))
279         # if it isn't a mandatory library, then remove it from dependency lists
280         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
281         return False
282
283     ret = True
284     for f in remaining:
285         if not conf.check(function_name=f, lib=library, header_name=conf.env.hlist):
286             ret = False
287     conf.env['LIB_' + library.upper()] = library
288     LOCAL_CACHE_SET(conf, 'TARGET_TYPE', library, 'SYSLIB')
289     return ret
290
291
292 #################################################
293 # write out config.h in the right directory
294 @conf
295 def SAMBA_CONFIG_H(conf, path=None):
296     if os.path.normpath(conf.curdir) != os.path.normpath(os.environ.get('PWD')):
297         return
298     if path is None:
299         conf.write_config_header('config.h', top=True)
300     else:
301         conf.write_config_header(path)
302
303
304 ##############################################################
305 # setup a configurable path
306 @conf
307 def CONFIG_PATH(conf, name, default):
308     if not name in conf.env:
309         conf.env[name] = conf.env['PREFIX'] + default
310     conf.define(name, conf.env[name], quote=True)
311
312 ##############################################################
313 # add some CFLAGS to the command line
314 @conf
315 def ADD_CFLAGS(conf, flags):
316     if not 'EXTRA_CFLAGS' in conf.env:
317         conf.env['EXTRA_CFLAGS'] = []
318     conf.env['EXTRA_CFLAGS'].extend(to_list(flags))
319
320 ##############################################################
321 # add some extra include directories to all builds
322 @conf
323 def ADD_EXTRA_INCLUDES(conf, includes):
324     if not 'EXTRA_INCLUDES' in conf.env:
325         conf.env['EXTRA_INCLUDES'] = []
326     conf.env['EXTRA_INCLUDES'].extend(to_list(includes))
327
328
329 ##############################################################
330 # work out the current flags. local flags are added first
331 def CURRENT_CFLAGS(bld, cflags):
332     if not 'EXTRA_CFLAGS' in bld.env:
333         list = []
334     else:
335         list = bld.env['EXTRA_CFLAGS'];
336     ret = to_list(cflags)
337     ret.extend(list)
338     return ret