build: added quote option to conf.DEFINE()
[nivanova/samba-autobuild/.git] / buildtools / wafsamba / samba_autoconf.py
index b5da80f3a6a2a4531b22a8f5bbbd8a486b8b7f39..b2f1d82040b4c9700ca9d601f7a781051ed65f09 100644 (file)
@@ -14,9 +14,9 @@ missing_headers = set()
 
 @runonce
 @conf
-def DEFINE(conf, d, v, add_to_cflags=False):
+def DEFINE(conf, d, v, add_to_cflags=False, quote=False):
     '''define a config option'''
-    conf.define(d, v, quote=False)
+    conf.define(d, v, quote=quote)
     if add_to_cflags:
         conf.env.append_value('CCDEFINES', d + '=' + str(v))
 
@@ -40,7 +40,7 @@ def nolink(self):
     pass
 
 
-def CHECK_HEADER(conf, h, add_headers=False):
+def CHECK_HEADER(conf, h, add_headers=False, lib=None):
     '''check for a header'''
     if h in missing_headers:
         return False
@@ -53,10 +53,13 @@ def CHECK_HEADER(conf, h, add_headers=False):
                 conf.env.hlist.append(h)
         return True
 
+    (ccflags, ldflags) = library_flags(conf, lib)
+
     hdrs = hlist_to_string(conf, headers=h)
     ret = conf.check(fragment='%s\nint main(void) { return 0; }' % hdrs,
                      type='nolink',
                      execute=0,
+                     ccflags=ccflags,
                      msg="Checking for header %s" % h)
     if not ret:
         missing_headers.add(h)
@@ -69,35 +72,50 @@ def CHECK_HEADER(conf, h, add_headers=False):
 
 
 @conf
-def CHECK_HEADERS(conf, headers, add_headers=False):
-    '''check for a list of headers'''
+def CHECK_HEADERS(conf, headers, add_headers=False, together=False, lib=None):
+    '''check for a list of headers
+
+    when together==True, then the headers accumulate within this test.
+    This is useful for interdependent headers
+    '''
     ret = True
+    if not add_headers and together:
+        saved_hlist = conf.env.hlist[:]
+        set_add_headers = True
+    else:
+        set_add_headers = add_headers
     for hdr in TO_LIST(headers):
-        if not CHECK_HEADER(conf, hdr, add_headers):
+        if not CHECK_HEADER(conf, hdr, set_add_headers, lib=lib):
             ret = False
+    if not add_headers and together:
+        conf.env.hlist = saved_hlist
     return ret
 
-def header_list(conf, headers=None):
+
+def header_list(conf, headers=None, lib=None):
     '''form a list of headers which exist, as a string'''
     hlist=[]
     if headers is not None:
         for h in TO_LIST(headers):
-            if CHECK_HEADER(conf, h, add_headers=False):
+            if CHECK_HEADER(conf, h, add_headers=False, lib=lib):
                 hlist.append(h)
     return hlist_to_string(conf, headers=hlist)
 
 
 @conf
-def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
+def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None, lib=None, msg=None):
     '''check for a single type'''
     if define is None:
         define = 'HAVE_' + t.upper().replace(' ', '_')
+    if msg is None:
+        msg='Checking for %s' % t
     ret = CHECK_CODE(conf, '%s _x' % t,
                      define,
                      execute=False,
                      headers=headers,
-                     msg='Checking for %s' % t,
                      local_include=False,
+                     msg=msg,
+                     lib=lib,
                      link=False)
     if not ret and alternate:
         conf.DEFINE(t, alternate)
@@ -105,11 +123,12 @@ def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
 
 
 @conf
-def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None):
+def CHECK_TYPES(conf, list, headers=None, define=None, alternate=None, lib=None):
     '''check for a list of types'''
     ret = True
     for t in TO_LIST(list):
-        if not CHECK_TYPE(conf, t, headers=headers, define=define, alternate=alternate):
+        if not CHECK_TYPE(conf, t, headers=headers,
+                          define=define, alternate=alternate, lib=lib):
             ret = False
     return ret
 
@@ -121,7 +140,8 @@ def CHECK_TYPE_IN(conf, t, headers=None, alternate=None, define=None):
 
 
 @conf
-def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None, msg=None):
+def CHECK_VARIABLE(conf, v, define=None, always=False,
+                   headers=None, msg=None, lib=None):
     '''check for a variable declaration (or define)'''
     if define is None:
         define = 'HAVE_%s' % v.upper()
@@ -140,6 +160,7 @@ def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None, msg=None):
                       link=False,
                       msg=msg,
                       local_include=False,
+                      lib=lib,
                       headers=headers,
                       define=define,
                       always=always)
@@ -166,7 +187,7 @@ def CHECK_DECLS(conf, vars, reverse=False, headers=None):
     return ret
 
 
-def CHECK_FUNC(conf, f, link=None, lib='c', headers=None):
+def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
     '''check for a function'''
     define='HAVE_%s' % f.upper()
 
@@ -205,7 +226,7 @@ def CHECK_FUNC(conf, f, link=None, lib='c', headers=None):
 
 
 @conf
-def CHECK_FUNCS(conf, list, link=None, lib='c', headers=None):
+def CHECK_FUNCS(conf, list, link=None, lib=None, headers=None):
     '''check for a list of functions'''
     ret = True
     for f in TO_LIST(list):
@@ -241,7 +262,7 @@ def CHECK_CODE(conf, code, define,
                always=False, execute=False, addmain=True,
                add_headers=True, mandatory=False,
                headers=None, msg=None, cflags='', includes='# .',
-               local_include=True, lib='c', link=True,
+               local_include=True, lib=None, link=True,
                define_ret=False, quote=False):
     '''check if some code compiles and/or runs'''
 
@@ -249,10 +270,10 @@ def CHECK_CODE(conf, code, define,
         return True
 
     if headers is not None:
-        CHECK_HEADERS(conf, headers=headers)
+        CHECK_HEADERS(conf, headers=headers, lib=lib)
 
     if add_headers:
-        hdrs = header_list(conf, headers=headers)
+        hdrs = header_list(conf, headers=headers, lib=lib)
     else:
         hdrs = ''
     if execute:
@@ -261,9 +282,9 @@ def CHECK_CODE(conf, code, define,
         execute = 0
 
     if addmain:
-        fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
+        fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
     else:
-        fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
+        fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
 
     conf.write_config_header('__confdefs.h', top=True)
 
@@ -281,17 +302,30 @@ def CHECK_CODE(conf, code, define,
     else:
         type='cprogram'
 
-    if conf.check(fragment=fragment,
-                  execute=execute,
-                  define_name = define,
-                  mandatory = mandatory,
-                  ccflags=TO_LIST(cflags),
-                  includes=includes,
-                  lib=lib, # how do I make this conditional, so I can avoid the -lc?
-                  type=type,
-                  msg=msg,
-                  quote=quote,
-                  define_ret=define_ret):
+    uselib = TO_LIST(lib)
+
+    (ccflags, ldflags) = library_flags(conf, uselib)
+
+    cflags = TO_LIST(cflags)
+    cflags.extend(ccflags)
+
+    ret = conf.check(fragment=fragment,
+                     execute=execute,
+                     define_name = define,
+                     mandatory = mandatory,
+                     ccflags=cflags,
+                     ldflags=ldflags,
+                     includes=includes,
+                     uselib=uselib,
+                     type=type,
+                     msg=msg,
+                     quote=quote,
+                     define_ret=define_ret)
+    if not ret and CONFIG_SET(conf, define):
+        # sometimes conf.check() returns false, but it
+        # sets the define. Maybe a waf bug?
+        ret = True
+    if ret:
         if not define_ret:
             conf.DEFINE(define, 1)
         return True
@@ -322,8 +356,9 @@ def CHECK_STRUCTURE_MEMBER(conf, structname, member,
 def CHECK_CFLAGS(conf, cflags):
     '''check if the given cflags are accepted by the compiler
     '''
-    return conf.check(fragment='int main(void) { return 0; }',
+    return conf.check(fragment='int main(void) { return 0; }\n',
                       execute=0,
+                      type='nolink',
                       ccflags=cflags,
                       msg="Checking compiler accepts %s" % cflags)
 
@@ -336,16 +371,34 @@ def CONFIG_SET(conf, option):
 Build.BuildContext.CONFIG_SET = CONFIG_SET
 
 
+def library_flags(conf, libs):
+    '''work out flags from pkg_config'''
+    ccflags = []
+    ldflags = []
+    for lib in TO_LIST(libs):
+        inc_path = None
+        inc_path = getattr(conf.env, 'CPPPATH_%s' % lib.upper(), [])
+        lib_path = getattr(conf.env, 'LIBPATH_%s' % lib.upper(), [])
+        for i in inc_path:
+            ccflags.append('-I%s' % i)
+        for l in lib_path:
+            ldflags.append('-L%s' % l)
+    return (ccflags, ldflags)
+
+
 @conf
-def CHECK_LIB(conf, libs):
+def CHECK_LIB(conf, libs, mandatory=False):
     '''check if a set of libraries exist'''
-    liblist   = TO_LIST(library)
 
+    liblist  = TO_LIST(libs)
     ret = True
     for lib in liblist[:]:
         if GET_TARGET_TYPE(conf, lib):
             continue
-        if not conf.check(lib=lib, uselib_store=lib):
+
+        (ccflags, ldflags) = library_flags(conf, lib)
+
+        if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
             conf.ASSERT(not mandatory,
                         "Mandatory library '%s' not found for functions '%s'" % (lib, list))
             # if it isn't a mandatory library, then remove it from dependency lists
@@ -393,23 +446,14 @@ def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header
                 SET_TARGET_TYPE(conf, lib, 'EMPTY')
         return True
 
-    ret = True
+    conf.CHECK_LIB(liblist)
     for lib in liblist[:]:
-        if GET_TARGET_TYPE(conf, lib):
-            continue
-        if not conf.check(lib=lib, uselib_store=lib):
+        if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
             conf.ASSERT(not mandatory,
                         "Mandatory library '%s' not found for functions '%s'" % (lib, list))
             # if it isn't a mandatory library, then remove it from dependency lists
-            SET_TARGET_TYPE(conf, lib, 'EMPTY')
-            ret = False
-        else:
-            conf.define('HAVE_LIB%s' % lib.upper().replace('-','_'), 1)
-            conf.env['LIB_' + lib.upper()] = lib
-            LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
-
-    if not ret:
-        return ret
+            liblist.remove(lib)
+            continue
 
     ret = True
     for f in remaining:
@@ -419,20 +463,6 @@ def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header
     return ret
 
 
-@conf
-def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None):
-    '''verify that a C prototype matches the one on the current system'''
-    if not conf.CHECK_DECLS(function, headers=headers):
-        return False
-    return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
-                           define=define,
-                           local_include=False,
-                           headers=headers,
-                           msg='Checking C prototype for %s' % function)
-
-
-
-
 #################################################
 # write out config.h in the right directory
 @conf
@@ -444,7 +474,11 @@ def SAMBA_CONFIG_H(conf, path=None):
 
     if Options.options.developer:
         # we add these here to ensure that -Wstrict-prototypes is not set during configure
-        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')
+        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',
+                        testflags=True)
+
+    if Options.options.pedantic:
+       conf.ADD_CFLAGS('-W', testflags=True)
 
     if path is None:
         conf.write_config_header('config.h', top=True)
@@ -463,14 +497,23 @@ def CONFIG_PATH(conf, name, default):
             conf.env[name] = conf.env['PREFIX'] + default
     conf.define(name, conf.env[name], quote=True)
 
-##############################################################
-# add some CFLAGS to the command line
+
 @conf
-def ADD_CFLAGS(conf, flags):
+def ADD_CFLAGS(conf, flags, testflags=False):
+    '''add some CFLAGS to the command line
+       optionally set testflags to ensure all the flags work
+    '''
+    if testflags:
+        ok_flags=[]
+        for f in flags.split():
+            if CHECK_CFLAGS(conf, f):
+                ok_flags.append(f)
+        flags = ok_flags
     if not 'EXTRA_CFLAGS' in conf.env:
         conf.env['EXTRA_CFLAGS'] = []
     conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
 
+
 ##############################################################
 # add some extra include directories to all builds
 @conf
@@ -491,15 +534,6 @@ def CURRENT_CFLAGS(bld, target, cflags):
     ret.extend(list)
     return ret
 
-@conf
-def CHECK_RPATH_SUPPORT(conf):
-    '''see if the system supports rpath'''
-    return conf.CHECK_CODE('int x',
-                           define='HAVE_RPATH_SUPPORT',
-                           execute=True,
-                           local_include=False,
-                           msg='Checking for rpath support',
-                           cflags='-Wl,-rpath=.')
 
 @conf
 def CHECK_CC_ENV(conf):