build: added quote option to conf.DEFINE()
[nivanova/samba-autobuild/.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, preproc
4 import string
5 from Configure import conf
6 from samba_utils import *
7
8 missing_headers = set()
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, quote=False):
18     '''define a config option'''
19     conf.define(d, v, quote=quote)
20     if add_to_cflags:
21         conf.env.append_value('CCDEFINES', d + '=' + str(v))
22
23 def hlist_to_string(conf, headers=None):
24     '''convert a headers list to a set of #include lines'''
25     hdrs=''
26     hlist = conf.env.hlist
27     if headers:
28         hlist = hlist[:]
29         hlist.extend(TO_LIST(headers))
30     for h in hlist:
31         hdrs += '#include <%s>\n' % h
32     return hdrs
33
34
35 @feature('nolink')
36 def nolink(self):
37     '''using the nolink type in conf.check() allows us to avoid
38        the link stage of a test, thus speeding it up for tests
39        that where linking is not needed'''
40     pass
41
42
43 def CHECK_HEADER(conf, h, add_headers=False, lib=None):
44     '''check for a header'''
45     if h in missing_headers:
46         return False
47     d = h.upper().replace('/', '_')
48     d = d.replace('.', '_')
49     d = 'HAVE_%s' % d
50     if CONFIG_SET(conf, d):
51         if add_headers:
52             if not h in conf.env.hlist:
53                 conf.env.hlist.append(h)
54         return True
55
56     (ccflags, ldflags) = library_flags(conf, lib)
57
58     hdrs = hlist_to_string(conf, headers=h)
59     ret = conf.check(fragment='%s\nint main(void) { return 0; }' % hdrs,
60                      type='nolink',
61                      execute=0,
62                      ccflags=ccflags,
63                      msg="Checking for header %s" % h)
64     if not ret:
65         missing_headers.add(h)
66         return False
67
68     conf.DEFINE(d, 1)
69     if add_headers and not h in conf.env.hlist:
70         conf.env.hlist.append(h)
71     return ret
72
73
74 @conf
75 def CHECK_HEADERS(conf, headers, add_headers=False, together=False, lib=None):
76     '''check for a list of headers
77
78     when together==True, then the headers accumulate within this test.
79     This is useful for interdependent headers
80     '''
81     ret = True
82     if not add_headers and together:
83         saved_hlist = conf.env.hlist[:]
84         set_add_headers = True
85     else:
86         set_add_headers = add_headers
87     for hdr in TO_LIST(headers):
88         if not CHECK_HEADER(conf, hdr, set_add_headers, lib=lib):
89             ret = False
90     if not add_headers and together:
91         conf.env.hlist = saved_hlist
92     return ret
93
94
95 def header_list(conf, headers=None, lib=None):
96     '''form a list of headers which exist, as a string'''
97     hlist=[]
98     if headers is not None:
99         for h in TO_LIST(headers):
100             if CHECK_HEADER(conf, h, add_headers=False, lib=lib):
101                 hlist.append(h)
102     return hlist_to_string(conf, headers=hlist)
103
104
105 @conf
106 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None, lib=None, msg=None):
107     '''check for a single type'''
108     if define is None:
109         define = 'HAVE_' + t.upper().replace(' ', '_')
110     if msg is None:
111         msg='Checking for %s' % t
112     ret = CHECK_CODE(conf, '%s _x' % t,
113                      define,
114                      execute=False,
115                      headers=headers,
116                      local_include=False,
117                      msg=msg,
118                      lib=lib,
119                      link=False)
120     if not ret and alternate:
121         conf.DEFINE(t, alternate)
122     return ret
123
124
125 @conf
126 def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None, lib=None):
127     '''check for a list of types'''
128     ret = True
129     for t in TO_LIST(list):
130         if not CHECK_TYPE(conf, t, headers=headers,
131                           define=define, alternate=alternate, lib=lib):
132             ret = False
133     return ret
134
135
136 @conf
137 def CHECK_TYPE_IN(conf, t, headers=None, alternate=None, define=None):
138     '''check for a single type with a header'''
139     return CHECK_TYPE(conf, t, headers=headers, alternate=alternate, define=define)
140
141
142 @conf
143 def CHECK_VARIABLE(conf, v, define=None, always=False,
144                    headers=None, msg=None, lib=None):
145     '''check for a variable declaration (or define)'''
146     if define is None:
147         define = 'HAVE_%s' % v.upper()
148
149     if msg is None:
150         msg="Checking for variable %s" % v
151
152     return CHECK_CODE(conf,
153                       '''
154                       #ifndef %s
155                       void *_x; _x=(void *)&%s;
156                       #endif
157                       return 0
158                       ''' % (v, v),
159                       execute=False,
160                       link=False,
161                       msg=msg,
162                       local_include=False,
163                       lib=lib,
164                       headers=headers,
165                       define=define,
166                       always=always)
167
168
169 @conf
170 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
171     '''check a list of variable declarations, using the HAVE_DECL_xxx form
172        of define
173
174        When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
175        '''
176     ret = True
177     for v in TO_LIST(vars):
178         if not reverse:
179             define='HAVE_DECL_%s' % v.upper()
180         else:
181             define='HAVE_%s_DECL' % v.upper()
182         if not CHECK_VARIABLE(conf, v,
183                               define=define,
184                               headers=headers,
185                               msg='Checking for declaration of %s' % v):
186             ret = False
187     return ret
188
189
190 def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
191     '''check for a function'''
192     define='HAVE_%s' % f.upper()
193
194     # there are two ways to find a function. The first is
195     # to see if there is a declaration of the function, the
196     # 2nd is to try and link a program that calls the function
197     # unfortunately both strategies have problems.
198     # the 'check the declaration' approach works fine as long
199     # as the function has a declaraion in a header. If there is
200     # no header declaration we can get a false negative.
201     # The link method works fine as long as the compiler
202     # doesn't have a builtin for the function, which could cause
203     # a false negative due to mismatched parameters
204     # so to be sure, we need to try both
205     ret = False
206
207     if link is None or link == True:
208         ret = CHECK_CODE(conf,
209                          'int main(void) { extern void %s(void); %s(); return 0; }' % (f, f),
210                          execute=False,
211                          link=True,
212                          addmain=False,
213                          add_headers=False,
214                          define=define,
215                          local_include=False,
216                          lib=lib,
217                          headers=headers,
218                          msg='Checking for %s' % f)
219
220     if not ret and (link is None or link == False):
221         ret = CHECK_VARIABLE(conf, f,
222                              define=define,
223                              headers=headers,
224                              msg='Checking for declaration of %s' % f)
225     return ret
226
227
228 @conf
229 def CHECK_FUNCS(conf, list, link=None, lib=None, headers=None):
230     '''check for a list of functions'''
231     ret = True
232     for f in TO_LIST(list):
233         if not CHECK_FUNC(conf, f, link=link, lib=lib, headers=headers):
234             ret = False
235     return ret
236
237
238 @conf
239 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
240     '''check the size of a type'''
241     ret = True
242     for v in TO_LIST(vars):
243         v_define = define
244         if v_define is None:
245             v_define = 'SIZEOF_%s' % v.upper().replace(' ', '_')
246         if not CHECK_CODE(conf,
247                           'printf("%%u\\n", (unsigned)sizeof(%s))' % v,
248                           define=v_define,
249                           execute=True,
250                           define_ret=True,
251                           quote=False,
252                           headers=headers,
253                           local_include=False,
254                           msg="Checking size of %s" % v):
255             ret = False
256     return ret
257
258
259
260 @conf
261 def CHECK_CODE(conf, code, define,
262                always=False, execute=False, addmain=True,
263                add_headers=True, mandatory=False,
264                headers=None, msg=None, cflags='', includes='# .',
265                local_include=True, lib=None, link=True,
266                define_ret=False, quote=False):
267     '''check if some code compiles and/or runs'''
268
269     if CONFIG_SET(conf, define):
270         return True
271
272     if headers is not None:
273         CHECK_HEADERS(conf, headers=headers, lib=lib)
274
275     if add_headers:
276         hdrs = header_list(conf, headers=headers, lib=lib)
277     else:
278         hdrs = ''
279     if execute:
280         execute = 1
281     else:
282         execute = 0
283
284     if addmain:
285         fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
286     else:
287         fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
288
289     conf.write_config_header('__confdefs.h', top=True)
290
291     if msg is None:
292         msg="Checking for %s" % define
293
294     # include the directory containing __confdefs.h
295     cflags += ' -I../../default'
296
297     if local_include:
298         cflags += ' -I%s' % conf.curdir
299
300     if not link:
301         type='nolink'
302     else:
303         type='cprogram'
304
305     uselib = TO_LIST(lib)
306
307     (ccflags, ldflags) = library_flags(conf, uselib)
308
309     cflags = TO_LIST(cflags)
310     cflags.extend(ccflags)
311
312     ret = conf.check(fragment=fragment,
313                      execute=execute,
314                      define_name = define,
315                      mandatory = mandatory,
316                      ccflags=cflags,
317                      ldflags=ldflags,
318                      includes=includes,
319                      uselib=uselib,
320                      type=type,
321                      msg=msg,
322                      quote=quote,
323                      define_ret=define_ret)
324     if not ret and CONFIG_SET(conf, define):
325         # sometimes conf.check() returns false, but it
326         # sets the define. Maybe a waf bug?
327         ret = True
328     if ret:
329         if not define_ret:
330             conf.DEFINE(define, 1)
331         return True
332     if always:
333         conf.DEFINE(define, 0)
334     return False
335
336
337
338 @conf
339 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
340                            always=False, define=None, headers=None):
341     '''check for a structure member'''
342     if define is None:
343         define = 'HAVE_%s' % member.upper()
344     return CHECK_CODE(conf,
345                       '%s s; void *_x; _x=(void *)&s.%s' % (structname, member),
346                       define,
347                       execute=False,
348                       link=False,
349                       always=always,
350                       headers=headers,
351                       local_include=False,
352                       msg="Checking for member %s in %s" % (member, structname))
353
354
355 @conf
356 def CHECK_CFLAGS(conf, cflags):
357     '''check if the given cflags are accepted by the compiler
358     '''
359     return conf.check(fragment='int main(void) { return 0; }\n',
360                       execute=0,
361                       type='nolink',
362                       ccflags=cflags,
363                       msg="Checking compiler accepts %s" % cflags)
364
365
366 #################################################
367 # return True if a configuration option was found
368 @conf
369 def CONFIG_SET(conf, option):
370     return (option in conf.env) and (conf.env[option] != ())
371 Build.BuildContext.CONFIG_SET = CONFIG_SET
372
373
374 def library_flags(conf, libs):
375     '''work out flags from pkg_config'''
376     ccflags = []
377     ldflags = []
378     for lib in TO_LIST(libs):
379         inc_path = None
380         inc_path = getattr(conf.env, 'CPPPATH_%s' % lib.upper(), [])
381         lib_path = getattr(conf.env, 'LIBPATH_%s' % lib.upper(), [])
382         for i in inc_path:
383             ccflags.append('-I%s' % i)
384         for l in lib_path:
385             ldflags.append('-L%s' % l)
386     return (ccflags, ldflags)
387
388
389 @conf
390 def CHECK_LIB(conf, libs, mandatory=False):
391     '''check if a set of libraries exist'''
392
393     liblist  = TO_LIST(libs)
394     ret = True
395     for lib in liblist[:]:
396         if GET_TARGET_TYPE(conf, lib):
397             continue
398
399         (ccflags, ldflags) = library_flags(conf, lib)
400
401         if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
402             conf.ASSERT(not mandatory,
403                         "Mandatory library '%s' not found for functions '%s'" % (lib, list))
404             # if it isn't a mandatory library, then remove it from dependency lists
405             SET_TARGET_TYPE(conf, lib, 'EMPTY')
406             ret = False
407         else:
408             conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
409             conf.env['LIB_' + lib.upper()] = lib
410             LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
411
412     return ret
413
414
415 ###########################################################
416 # check that the functions in 'list' are available in 'library'
417 # if they are, then make that library available as a dependency
418 #
419 # if the library is not available and mandatory==True, then
420 # raise an error.
421 #
422 # If the library is not available and mandatory==False, then
423 # add the library to the list of dependencies to remove from
424 # build rules
425 #
426 # optionally check for the functions first in libc
427 @conf
428 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, headers=None, link=None):
429     remaining = TO_LIST(list)
430     liblist   = TO_LIST(library)
431
432     # check if some already found
433     for f in remaining[:]:
434         if CONFIG_SET(conf, 'HAVE_%s' % f.upper()):
435             remaining.remove(f)
436
437     # see if the functions are in libc
438     if checklibc:
439         for f in remaining[:]:
440             if CHECK_FUNC(conf, f, link=True, headers=headers):
441                 remaining.remove(f)
442
443     if remaining == []:
444         for lib in liblist:
445             if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
446                 SET_TARGET_TYPE(conf, lib, 'EMPTY')
447         return True
448
449     conf.CHECK_LIB(liblist)
450     for lib in liblist[:]:
451         if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
452             conf.ASSERT(not mandatory,
453                         "Mandatory library '%s' not found for functions '%s'" % (lib, list))
454             # if it isn't a mandatory library, then remove it from dependency lists
455             liblist.remove(lib)
456             continue
457
458     ret = True
459     for f in remaining:
460         if not CHECK_FUNC(conf, f, lib=' '.join(liblist), headers=headers, link=link):
461             ret = False
462
463     return ret
464
465
466 #################################################
467 # write out config.h in the right directory
468 @conf
469 def SAMBA_CONFIG_H(conf, path=None):
470     # we don't want to produce a config.h in places like lib/replace
471     # when we are building projects that depend on lib/replace
472     if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
473         return
474
475     if Options.options.developer:
476         # we add these here to ensure that -Wstrict-prototypes is not set during configure
477         conf.ADD_CFLAGS('-Wall -g -Wfatal-errors -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Werror-implicit-function-declaration -Wformat=2 -Wno-format-y2k',
478                         testflags=True)
479
480     if Options.options.pedantic:
481         conf.ADD_CFLAGS('-W', testflags=True)
482
483     if path is None:
484         conf.write_config_header('config.h', top=True)
485     else:
486         conf.write_config_header(path)
487
488
489 ##############################################################
490 # setup a configurable path
491 @conf
492 def CONFIG_PATH(conf, name, default):
493     if not name in conf.env:
494         if default[0] == '/':
495             conf.env[name] = default
496         else:
497             conf.env[name] = conf.env['PREFIX'] + default
498     conf.define(name, conf.env[name], quote=True)
499
500
501 @conf
502 def ADD_CFLAGS(conf, flags, testflags=False):
503     '''add some CFLAGS to the command line
504        optionally set testflags to ensure all the flags work
505     '''
506     if testflags:
507         ok_flags=[]
508         for f in flags.split():
509             if CHECK_CFLAGS(conf, f):
510                 ok_flags.append(f)
511         flags = ok_flags
512     if not 'EXTRA_CFLAGS' in conf.env:
513         conf.env['EXTRA_CFLAGS'] = []
514     conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
515
516
517 ##############################################################
518 # add some extra include directories to all builds
519 @conf
520 def ADD_EXTRA_INCLUDES(conf, includes):
521     if not 'EXTRA_INCLUDES' in conf.env:
522         conf.env['EXTRA_INCLUDES'] = []
523     conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
524
525
526 ##############################################################
527 # work out the current flags. local flags are added first
528 def CURRENT_CFLAGS(bld, target, cflags):
529     if not 'EXTRA_CFLAGS' in bld.env:
530         list = []
531     else:
532         list = bld.env['EXTRA_CFLAGS'];
533     ret = TO_LIST(cflags)
534     ret.extend(list)
535     return ret
536
537
538 @conf
539 def CHECK_CC_ENV(conf):
540     '''trim whitespaces from 'CC'.
541     The build farm sometimes puts a space at the start'''
542     if os.environ.get('CC'):
543         conf.env.CC = TO_LIST(os.environ.get('CC'))
544         if len(conf.env.CC) == 1:
545             # make for nicer logs if just a single command
546             conf.env.CC = conf.env.CC[0]
547
548
549 @conf
550 def SETUP_CONFIGURE_CACHE(conf, enable):
551     '''enable/disable cache of configure results'''
552     if enable:
553         # when -C is chosen, we will use a private cache and will
554         # not look into system includes. This roughtly matches what
555         # autoconf does with -C
556         cache_path = os.path.join(conf.blddir, '.confcache')
557         mkdir_p(cache_path)
558         Options.cache_global = os.environ['WAFCACHE'] = cache_path
559     else:
560         # when -C is not chosen we will not cache configure checks
561         # We set the recursion limit low to prevent waf from spending
562         # a lot of time on the signatures of the files.
563         Options.cache_global = os.environ['WAFCACHE'] = ''
564         preproc.recursion_limit = 1
565     # in either case we don't need to scan system includes
566     preproc.go_absolute = False