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