build: cope with perl not being in /usr/bin/perl
[samba.git] / buildtools / wafsamba / samba_autoconf.py
index ff2fa012c44e24b98d5a2b07f8943b367d4feb1a..dd7abe2864c810a4fb8e8bcd157437e8995d64c9 100644 (file)
@@ -1,9 +1,10 @@
 # a waf tool to add autoconf-like macros to the configure section
 
-import Build, os, Options, preproc
+import Build, os, Options, preproc, Logs
 import string
 from Configure import conf
 from samba_utils import *
+import samba_cross
 
 missing_headers = set()
 
@@ -32,6 +33,43 @@ def hlist_to_string(conf, headers=None):
     return hdrs
 
 
+@conf
+def COMPOUND_START(conf, msg):
+    '''start a compound test'''
+    def null_check_message_1(self,*k,**kw):
+        return
+    def null_check_message_2(self,*k,**kw):
+        return
+
+    v = getattr(conf.env, 'in_compound', [])
+    if v != [] and v != 0:
+        conf.env.in_compound = v + 1
+        return
+    conf.check_message_1(msg)
+    conf.saved_check_message_1 = conf.check_message_1
+    conf.check_message_1 = null_check_message_1
+    conf.saved_check_message_2 = conf.check_message_2
+    conf.check_message_2 = null_check_message_2
+    conf.env.in_compound = 1
+
+
+@conf
+def COMPOUND_END(conf, result):
+    '''start a compound test'''
+    conf.env.in_compound -= 1
+    if conf.env.in_compound != 0:
+        return
+    conf.check_message_1 = conf.saved_check_message_1
+    conf.check_message_2 = conf.saved_check_message_2
+    p = conf.check_message_2
+    if result == True:
+        p('ok ')
+    elif result == False:
+        p('not found', 'YELLOW')
+    else:
+        p(result)
+
+
 @feature('nolink')
 def nolink(self):
     '''using the nolink type in conf.check() allows us to avoid
@@ -150,9 +188,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),
@@ -167,7 +207,7 @@ def CHECK_VARIABLE(conf, v, define=None, always=False,
 
 
 @conf
-def CHECK_DECLS(conf, vars, reverse=False, headers=None):
+def CHECK_DECLS(conf, vars, reverse=False, headers=None, always=False):
     '''check a list of variable declarations, using the HAVE_DECL_xxx form
        of define
 
@@ -182,31 +222,37 @@ def CHECK_DECLS(conf, vars, reverse=False, headers=None):
         if not CHECK_VARIABLE(conf, v,
                               define=define,
                               headers=headers,
-                              msg='Checking for declaration of %s' % v):
+                              msg='Checking for declaration of %s' % v,
+                              always=always):
             ret = False
     return ret
 
 
-def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
+def CHECK_FUNC(conf, f, link=True, lib=None, headers=None):
     '''check for a function'''
     define='HAVE_%s' % f.upper()
 
-    # there are two ways to find a function. The first is
-    # to see if there is a declaration of the function, the
-    # 2nd is to try and link a program that calls the function
-    # unfortunately both strategies have problems.
-    # the 'check the declaration' approach works fine as long
-    # as the function has a declaraion in a header. If there is
-    # no header declaration we can get a false negative.
-    # The link method works fine as long as the compiler
-    # doesn't have a builtin for the function, which could cause
-    # a false negative due to mismatched parameters
-    # so to be sure, we need to try both
     ret = False
 
+    conf.COMPOUND_START('Checking for %s' % f)
+
     if link is None or link == True:
         ret = CHECK_CODE(conf,
-                         'int main(void) { extern void %s(void); %s(); return 0; }' % (f, f),
+                         # this is based on the autoconf strategy
+                         '''
+                         #define %s __fake__%s
+                         #ifdef HAVE_LIMITS_H
+                         # include <limits.h>
+                         #else
+                         # include <assert.h>
+                         #endif
+                         #undef %s
+                         #if defined __stub_%s || defined __stub___%s
+                         #error "bad glibc stub"
+                         #endif
+                         extern char %s();
+                         int main() { return %s(); }
+                         ''' % (f, f, f, f, f, f, f),
                          execute=False,
                          link=True,
                          addmain=False,
@@ -217,16 +263,33 @@ def CHECK_FUNC(conf, f, link=None, lib=None, headers=None):
                          headers=headers,
                          msg='Checking for %s' % f)
 
+        if not ret:
+            ret = CHECK_CODE(conf,
+                             # it might be a macro
+                             # 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,
+                             add_headers=True,
+                             define=define,
+                             local_include=False,
+                             lib=lib,
+                             headers=headers,
+                             msg='Checking for macro %s' % f)
+
     if not ret and (link is None or link == False):
         ret = CHECK_VARIABLE(conf, f,
                              define=define,
                              headers=headers,
                              msg='Checking for declaration of %s' % f)
+    conf.COMPOUND_END(ret)
     return ret
 
 
 @conf
-def CHECK_FUNCS(conf, list, link=None, lib=None, headers=None):
+def CHECK_FUNCS(conf, list, link=True, lib=None, headers=None):
     '''check for a list of functions'''
     ret = True
     for f in TO_LIST(list):
@@ -309,6 +372,8 @@ def CHECK_CODE(conf, code, define,
     cflags = TO_LIST(cflags)
     cflags.extend(ccflags)
 
+    exec_args = conf.SAMBA_CROSS_ARGS()
+
     ret = conf.check(fragment=fragment,
                      execute=execute,
                      define_name = define,
@@ -320,6 +385,7 @@ def CHECK_CODE(conf, code, define,
                      type=type,
                      msg=msg,
                      quote=quote,
+                     exec_args=exec_args,
                      define_ret=define_ret)
     if not ret and CONFIG_SET(conf, define):
         # sometimes conf.check() returns false, but it
@@ -363,10 +429,9 @@ def CHECK_CFLAGS(conf, cflags):
                       msg="Checking compiler accepts %s" % cflags)
 
 
-#################################################
-# return True if a configuration option was found
 @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
 
@@ -387,23 +452,24 @@ def library_flags(conf, libs):
 
 
 @conf
-def CHECK_LIB(conf, libs, mandatory=False):
+def CHECK_LIB(conf, libs, mandatory=False, empty_decl=True):
     '''check if a set of libraries exist'''
 
     liblist  = TO_LIST(libs)
     ret = True
     for lib in liblist[:]:
-        if GET_TARGET_TYPE(conf, lib):
+        if GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
             continue
 
         (ccflags, ldflags) = library_flags(conf, lib)
 
         if not conf.check(lib=lib, uselib_store=lib, ccflags=ccflags, ldflags=ldflags):
             if mandatory:
-                print("Mandatory library '%s' not found for functions '%s'" % (lib, list))
+                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
-            SET_TARGET_TYPE(conf, lib, 'EMPTY')
+            if empty_decl:
+                # 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)
@@ -413,20 +479,23 @@ def CHECK_LIB(conf, libs, mandatory=False):
     return ret
 
 
-###########################################################
-# check that the functions in 'list' are available in 'library'
-# if they are, then make that library available as a dependency
-#
-# if the library is not available and mandatory==True, then
-# raise an error.
-#
-# If the library is not available and mandatory==False, then
-# add the library to the list of dependencies to remove from
-# build rules
-#
-# optionally check for the functions first in libc
+
 @conf
-def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, headers=None, link=None):
+def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False,
+                   headers=None, link=True, empty_decl=True):
+    """
+    check that the functions in 'list' are available in 'library'
+    if they are, then make that library available as a dependency
+
+    if the library is not available and mandatory==True, then
+    raise an error.
+
+    If the library is not available and mandatory==False, then
+    add the library to the list of dependencies to remove from
+    build rules
+
+    optionally check for the functions first in libc
+    """
     remaining = TO_LIST(list)
     liblist   = TO_LIST(library)
 
@@ -443,15 +512,15 @@ def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header
 
     if remaining == []:
         for lib in liblist:
-            if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
+            if GET_TARGET_TYPE(conf, lib) != 'SYSLIB' and empty_decl:
                 SET_TARGET_TYPE(conf, lib, 'EMPTY')
         return True
 
-    conf.CHECK_LIB(liblist)
+    conf.CHECK_LIB(liblist, empty_decl=empty_decl)
     for lib in liblist[:]:
         if not GET_TARGET_TYPE(conf, lib) == 'SYSLIB':
             if mandatory:
-                print("Mandatory library '%s' not found for functions '%s'" % (lib, list))
+                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)
@@ -464,16 +533,17 @@ def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False, header
 
     return ret
 
+
 @conf
 def IN_LAUNCH_DIR(conf):
     '''return True if this rule is being run from the launch directory'''
     return os.path.realpath(conf.curdir) == os.path.realpath(Options.launch_dir)
+Options.Handler.IN_LAUNCH_DIR = IN_LAUNCH_DIR
 
 
-#################################################
-# write out config.h in the right directory
 @conf
 def SAMBA_CONFIG_H(conf, path=None):
+    '''write out config.h in the right directory'''
     # we don't want to produce a config.h in places like lib/replace
     # when we are building projects that depend on lib/replace
     if not IN_LAUNCH_DIR(conf):
@@ -481,11 +551,17 @@ 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 -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.picky_developer:
+        conf.ADD_CFLAGS('-Werror', testflags=True)
+
+    if Options.options.fatal_errors:
+        conf.ADD_CFLAGS('-Wfatal-errors', testflags=True)
+
     if Options.options.pedantic:
-       conf.ADD_CFLAGS('-W', testflags=True)
+        conf.ADD_CFLAGS('-W', testflags=True)
 
     if path is None:
         conf.write_config_header('config.h', top=True)
@@ -493,17 +569,14 @@ def SAMBA_CONFIG_H(conf, path=None):
         conf.write_config_header(path)
 
 
-##############################################################
-# setup a configurable path
 @conf
 def CONFIG_PATH(conf, name, default):
+    '''setup a configurable path'''
     if not name in conf.env:
         if default[0] == '/':
             conf.env[name] = default
         else:
             conf.env[name] = conf.env['PREFIX'] + default
-    conf.define(name, conf.env[name], quote=True)
-
 
 @conf
 def ADD_CFLAGS(conf, flags, testflags=False):
@@ -521,31 +594,33 @@ def ADD_CFLAGS(conf, flags, testflags=False):
     conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
 
 
-##############################################################
-# add some extra include directories to all builds
+
 @conf
 def ADD_EXTRA_INCLUDES(conf, includes):
+    '''add some extra include directories to all builds'''
     if not 'EXTRA_INCLUDES' in conf.env:
         conf.env['EXTRA_INCLUDES'] = []
     conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
 
 
-##############################################################
-# work out the current flags. local flags are added first
-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 = []
     else:
         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
 
 
 @conf
 def CHECK_CC_ENV(conf):
-    '''trim whitespaces from 'CC'.
-    The build farm sometimes puts a space at the start'''
+    """trim whitespaces from 'CC'.
+    The build farm sometimes puts a space at the start"""
     if os.environ.get('CC'):
         conf.env.CC = TO_LIST(os.environ.get('CC'))
         if len(conf.env.CC) == 1: