build: Reduce build systems to just top level waf and autoconf
[amitay/samba.git] / buildtools / wafsamba / samba_autoconf.py
index 9d0229e1479ae5553889dcf0734b6c43af2598b4..909e836fdb045581592815d230487a44affb45a7 100644 (file)
@@ -1,6 +1,6 @@
 # a waf tool to add autoconf-like macros to the configure section
 
-import Build, os, Options, preproc, Logs
+import Build, os, sys, Options, preproc, Logs
 import string
 from Configure import conf
 from samba_utils import *
@@ -80,10 +80,11 @@ def nolink(self):
 
 def CHECK_HEADER(conf, h, add_headers=False, lib=None):
     '''check for a header'''
-    if h in missing_headers:
+    if h in missing_headers and lib is None:
         return False
     d = h.upper().replace('/', '_')
     d = d.replace('.', '_')
+    d = d.replace('-', '_')
     d = 'HAVE_%s' % d
     if CONFIG_SET(conf, d):
         if add_headers:
@@ -188,9 +189,11 @@ def CHECK_VARIABLE(conf, v, define=None, always=False,
         msg="Checking for variable %s" % v
 
     return CHECK_CODE(conf,
+                      # we need to make sure the compiler doesn't
+                      # optimize it out...
                       '''
                       #ifndef %s
-                      void *_x; _x=(void *)&%s;
+                      void *_x; _x=(void *)&%s; return (int)_x;
                       #endif
                       return 0
                       ''' % (v, v),
@@ -264,7 +267,9 @@ def CHECK_FUNC(conf, f, link=True, lib=None, headers=None):
         if not ret:
             ret = CHECK_CODE(conf,
                              # it might be a macro
-                             'void *__x = (void *)%s' % f,
+                             # we need to make sure the compiler doesn't
+                             # optimize it out...
+                             'void *__x = (void *)%s; return (int)__x' % f,
                              execute=False,
                              link=True,
                              addmain=True,
@@ -303,7 +308,7 @@ def CHECK_SIZEOF(conf, vars, headers=None, define=None):
         if v_define is None:
             v_define = 'SIZEOF_%s' % v.upper().replace(' ', '_')
         if not CHECK_CODE(conf,
-                          'printf("%%u\\n", (unsigned)sizeof(%s))' % v,
+                          'printf("%%u", (unsigned)sizeof(%s))' % v,
                           define=v_define,
                           execute=True,
                           define_ret=True,
@@ -322,7 +327,8 @@ def CHECK_CODE(conf, code, define,
                add_headers=True, mandatory=False,
                headers=None, msg=None, cflags='', includes='# .',
                local_include=True, lib=None, link=True,
-               define_ret=False, quote=False):
+               define_ret=False, quote=False,
+               on_target=True):
     '''check if some code compiles and/or runs'''
 
     if CONFIG_SET(conf, define):
@@ -340,21 +346,20 @@ def CHECK_CODE(conf, code, define,
     else:
         execute = 0
 
+    defs = conf.get_config_header()
+
     if addmain:
-        fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }\n' % (hdrs, code)
+        fragment='%s\n%s\n int main(void) { %s; return 0; }\n' % (defs, hdrs, code)
     else:
-        fragment='#include "__confdefs.h"\n%s\n%s\n' % (hdrs, code)
-
-    conf.write_config_header('__confdefs.h', top=True)
+        fragment='%s\n%s\n%s\n' % (defs, hdrs, code)
 
     if msg is None:
         msg="Checking for %s" % define
 
-    # include the directory containing __confdefs.h
-    cflags += ' -I../../default'
+    cflags = TO_LIST(cflags)
 
     if local_include:
-        cflags += ' -I%s' % conf.curdir
+        cflags.append('-I%s' % conf.curdir)
 
     if not link:
         type='nolink'
@@ -365,10 +370,14 @@ def CHECK_CODE(conf, code, define,
 
     (ccflags, ldflags) = library_flags(conf, uselib)
 
-    cflags = TO_LIST(cflags)
     cflags.extend(ccflags)
 
-    exec_args = conf.SAMBA_CROSS_ARGS()
+    if on_target:
+        exec_args = conf.SAMBA_CROSS_ARGS(msg=msg)
+    else:
+        exec_args = []
+
+    conf.COMPOUND_START(msg)
 
     ret = conf.check(fragment=fragment,
                      execute=execute,
@@ -390,9 +399,13 @@ def CHECK_CODE(conf, code, define,
     if ret:
         if not define_ret:
             conf.DEFINE(define, 1)
+            conf.COMPOUND_END(True)
+        else:
+            conf.COMPOUND_END(conf.env[define])
         return True
     if always:
         conf.DEFINE(define, 0)
+    conf.COMPOUND_END(False)
     return False
 
 
@@ -424,53 +437,94 @@ def CHECK_CFLAGS(conf, cflags):
                       ccflags=cflags,
                       msg="Checking compiler accepts %s" % cflags)
 
+@conf
+def CHECK_LDFLAGS(conf, ldflags):
+    '''check if the given ldflags are accepted by the linker
+    '''
+    return conf.check(fragment='int main(void) { return 0; }\n',
+                      execute=0,
+                      ldflags=ldflags,
+                      msg="Checking linker accepts %s" % ldflags)
+
+
+@conf
+def CONFIG_GET(conf, option):
+    '''return True if a configuration option was found'''
+    if (option in conf.env):
+        return conf.env[option]
+    else:
+        return None
 
 @conf
 def CONFIG_SET(conf, option):
     '''return True if a configuration option was found'''
     return (option in conf.env) and (conf.env[option] != ())
 Build.BuildContext.CONFIG_SET = CONFIG_SET
+Build.BuildContext.CONFIG_GET = CONFIG_GET
 
 
-def library_flags(conf, libs):
+def library_flags(self, 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)
+        inc_path = getattr(self.env, 'CPPPATH_%s' % lib.upper(), [])
+        lib_path = getattr(self.env, 'LIBPATH_%s' % lib.upper(), [])
+        ccflags.extend(['-I%s' % i for i in inc_path])
+        ldflags.extend(['-L%s' % l for l in lib_path])
+        extra_ccflags = TO_LIST(getattr(self.env, 'CCFLAGS_%s' % lib.upper(), []))
+        extra_ldflags = TO_LIST(getattr(self.env, 'LDFLAGS_%s' % lib.upper(), []))
+        ccflags.extend(extra_ccflags)
+        ldflags.extend(extra_ldflags)
+    if 'EXTRA_LDFLAGS' in self.env:
+        ldflags.extend(self.env['EXTRA_LDFLAGS'])
+
+    ccflags = unique_list(ccflags)
+    ldflags = unique_list(ldflags)
     return (ccflags, ldflags)
 
 
 @conf
-def CHECK_LIB(conf, libs, mandatory=False, empty_decl=True):
-    '''check if a set of libraries exist'''
+def CHECK_LIB(conf, libs, mandatory=False, empty_decl=True, set_target=True, shlib=False):
+    '''check if a set of libraries exist as system libraries
+
+    returns the sublist of libs that do exist as a syslib or []
+    '''
 
+    fragment= '''
+int foo()
+{
+    int v = 2;
+    return v*2;
+}
+'''
+    ret = []
     liblist  = TO_LIST(libs)
-    ret = True
     for lib in liblist[:]:
         if GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
+            ret.append(lib)
             continue
 
         (ccflags, ldflags) = library_flags(conf, lib)
+        if shlib:
+            res = conf.check(features='cc cshlib', fragment=fragment, lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags)
+        else:
+            res = conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags)
 
-        if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
+        if not res:
             if mandatory:
                 Logs.error("Mandatory library '%s' not found for functions '%s'" % (lib, list))
                 sys.exit(1)
             if empty_decl:
                 # if it isn't a mandatory library, then remove it from dependency lists
-                SET_TARGET_TYPE(conf, lib, 'EMPTY')
-            ret = False
+                if set_target:
+                    SET_TARGET_TYPE(conf, lib, 'EMPTY')
         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 set_target:
+                conf.SET_TARGET_TYPE(lib, 'SYSLIB')
+            ret.append(lib)
 
     return ret
 
@@ -478,7 +532,7 @@ def CHECK_LIB(conf, libs, mandatory=False, empty_decl=True):
 
 @conf
 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False,
-                   headers=None, link=True, empty_decl=True):
+                   headers=None, link=True, empty_decl=True, set_target=True):
     """
     check that the functions in 'list' are available in 'library'
     if they are, then make that library available as a dependency
@@ -512,19 +566,15 @@ def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False,
                 SET_TARGET_TYPE(conf, lib, 'EMPTY')
         return True
 
-    conf.CHECK_LIB(liblist, empty_decl=empty_decl)
+    checklist = conf.CHECK_LIB(liblist, empty_decl=empty_decl, set_target=set_target)
     for lib in liblist[:]:
-        if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
-            if mandatory:
-                Logs.error("Mandatory library '%s' not found for functions '%s'" % (lib, list))
-                sys.exit(1)
-            # if it isn't a mandatory library, then remove it from dependency lists
-            liblist.remove(lib)
-            continue
+        if not lib in checklist and mandatory:
+            Logs.error("Mandatory library '%s' not found for functions '%s'" % (lib, list))
+            sys.exit(1)
 
     ret = True
     for f in remaining:
-        if not CHECK_FUNC(conf, f, lib=' '.join(liblist), headers=headers, link=link):
+        if not CHECK_FUNC(conf, f, lib=' '.join(checklist), headers=headers, link=link):
             ret = False
 
     return ret
@@ -547,8 +597,10 @@ 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 -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 -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Werror-implicit-function-declaration -Wformat=2 -Wno-format-y2k -Wmissing-prototypes -fno-common',
                         testflags=True)
+        conf.ADD_CFLAGS('-Wcast-qual', testflags=True)
+        conf.env.DEVELOPER_MODE = True
 
     if Options.options.picky_developer:
         conf.ADD_CFLAGS('-Werror', testflags=True)
@@ -563,6 +615,7 @@ def SAMBA_CONFIG_H(conf, path=None):
         conf.write_config_header('config.h', top=True)
     else:
         conf.write_config_header(path)
+    conf.SAMBA_CROSS_CHECK_COMPLETE()
 
 
 @conf
@@ -589,6 +642,23 @@ def ADD_CFLAGS(conf, flags, testflags=False):
         conf.env['EXTRA_CFLAGS'] = []
     conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
 
+@conf
+def ADD_LDFLAGS(conf, flags, testflags=False):
+    '''add some LDFLAGS to the command line
+       optionally set testflags to ensure all the flags work
+
+       this will return the flags that are added, if any
+    '''
+    if testflags:
+        ok_flags=[]
+        for f in flags.split():
+            if CHECK_LDFLAGS(conf, f):
+                ok_flags.append(f)
+        flags = ok_flags
+    if not 'EXTRA_LDFLAGS' in conf.env:
+        conf.env['EXTRA_LDFLAGS'] = []
+    conf.env['EXTRA_LDFLAGS'].extend(TO_LIST(flags))
+    return flags
 
 
 @conf
@@ -600,7 +670,7 @@ def ADD_EXTRA_INCLUDES(conf, includes):
 
 
 
-def CURRENT_CFLAGS(bld, target, cflags):
+def CURRENT_CFLAGS(bld, target, cflags, hide_symbols=False):
     '''work out the current flags. local flags are added first'''
     if not 'EXTRA_CFLAGS' in bld.env:
         list = []
@@ -608,6 +678,8 @@ def CURRENT_CFLAGS(bld, target, cflags):
         list = bld.env['EXTRA_CFLAGS'];
     ret = TO_LIST(cflags)
     ret.extend(list)
+    if hide_symbols and bld.env.HAVE_VISIBILITY_ATTR:
+        ret.append('-fvisibility=hidden')
     return ret