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