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