wafsamba: fix ordering problems with lib-provided and internal RPATHs
[obnox/samba/samba-obnox.git] / buildtools / wafsamba / samba_conftests.py
index 4811614ab73701497a89909e34185dd39375e737..f94b0b7ca8ce628dcec3cdc725d59b05ae5a0c3b 100644 (file)
@@ -1,10 +1,76 @@
 # a set of config tests that use the samba_autoconf functions
 # to test for commonly needed configuration options
 
-import os, Build, shutil, Utils, re
+import os, shutil, re
+import Build, Configure, Utils
 from Configure import conf
+import config_c
 from samba_utils import *
 
+
+def add_option(self, *k, **kw):
+    '''syntax help: provide the "match" attribute to opt.add_option() so that folders can be added to specific config tests'''
+    match = kw.get('match', [])
+    if match:
+        del kw['match']
+    opt = self.parser.add_option(*k, **kw)
+    opt.match = match
+    return opt
+Options.Handler.add_option = add_option
+
+@conf
+def check(self, *k, **kw):
+    '''Override the waf defaults to inject --with-directory options'''
+
+    if not 'env' in kw:
+        kw['env'] = self.env.copy()
+
+    # match the configuration test with speficic options, for example:
+    # --with-libiconv -> Options.options.iconv_open -> "Checking for library iconv"
+    additional_dirs = []
+    if 'msg' in kw:
+        msg = kw['msg']
+        for x in Options.Handler.parser.parser.option_list:
+             if getattr(x, 'match', None) and msg in x.match:
+                 d = getattr(Options.options, x.dest, '')
+                 if d:
+                     additional_dirs.append(d)
+
+    # we add the additional dirs twice: once for the test data, and again if the compilation test suceeds below
+    def add_options_dir(dirs, env):
+        for x in dirs:
+             if not x in env.CPPPATH:
+                 env.CPPPATH = [os.path.join(x, 'include')] + env.CPPPATH
+             if not x in env.LIBPATH:
+                 env.LIBPATH = [os.path.join(x, 'lib')] + env.LIBPATH
+
+    add_options_dir(additional_dirs, kw['env'])
+
+    self.validate_c(kw)
+    self.check_message_1(kw['msg'])
+    ret = None
+    try:
+        ret = self.run_c_code(*k, **kw)
+    except Configure.ConfigurationError, e:
+        self.check_message_2(kw['errmsg'], 'YELLOW')
+        if 'mandatory' in kw and kw['mandatory']:
+            if Logs.verbose > 1:
+                raise
+            else:
+                self.fatal('the configuration failed (see %r)' % self.log.name)
+    else:
+        kw['success'] = ret
+        self.check_message_2(self.ret_msg(kw['okmsg'], kw))
+
+        # success! keep the CPPPATH/LIBPATH
+        add_options_dir(additional_dirs, self.env)
+
+    self.post_check(*k, **kw)
+    if not kw.get('execute', False):
+        return ret == 0
+    return ret
+
+
 @conf
 def CHECK_ICONV(conf, define='HAVE_NATIVE_ICONV'):
     '''check if the iconv library is installed
@@ -18,11 +84,29 @@ def CHECK_ICONV(conf, define='HAVE_NATIVE_ICONV'):
 @conf
 def CHECK_LARGEFILE(conf, define='HAVE_LARGEFILE'):
     '''see what we need for largefile support'''
+    getconf_cflags = conf.CHECK_COMMAND(['getconf', 'LFS_CFLAGS']);
+    if getconf_cflags is not False:
+        if (conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
+                            define='WORKING_GETCONF_LFS_CFLAGS',
+                            execute=True,
+                            cflags=getconf_cflags,
+                            msg='Checking getconf large file support flags work')):
+            conf.ADD_CFLAGS(getconf_cflags)
+            getconf_cflags_list=TO_LIST(getconf_cflags)
+            for flag in getconf_cflags_list:
+                if flag[:2] == "-D":
+                    flag_split = flag[2:].split('=')
+                    if len(flag_split) == 1:
+                        conf.DEFINE(flag_split[0], '1')
+                    else:
+                        conf.DEFINE(flag_split[0], flag_split[1])
+
     if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
                        define,
                        execute=True,
-                       msg='Checking for large file support'):
+                       msg='Checking for large file support without additional flags'):
         return True
+
     if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
                        define,
                        execute=True,
@@ -30,6 +114,14 @@ def CHECK_LARGEFILE(conf, define='HAVE_LARGEFILE'):
                        msg='Checking for -D_FILE_OFFSET_BITS=64'):
         conf.DEFINE('_FILE_OFFSET_BITS', 64)
         return True
+
+    if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
+                       define,
+                       execute=True,
+                       cflags='-D_LARGE_FILES',
+                       msg='Checking for -D_LARGE_FILES'):
+        conf.DEFINE('_LARGE_FILES', 1)
+        return True
     return False
 
 
@@ -107,6 +199,51 @@ int foo(int v) {
 '''
     return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
 
+@conf
+def CHECK_NEED_LC(conf, msg):
+    '''check if we need -lc'''
+
+    dir = find_config_dir(conf)
+
+    env = conf.env
+
+    bdir = os.path.join(dir, 'testbuild2')
+    if not os.path.exists(bdir):
+        os.makedirs(bdir)
+
+
+    subdir = os.path.join(dir, "liblctest")
+
+    os.makedirs(subdir)
+
+    dest = open(os.path.join(subdir, 'liblc1.c'), 'w')
+    dest.write('#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
+    dest.close()
+
+    bld = Build.BuildContext()
+    bld.log = conf.log
+    bld.all_envs.update(conf.all_envs)
+    bld.all_envs['default'] = env
+    bld.lst_variants = bld.all_envs.keys()
+    bld.load_dirs(dir, bdir)
+
+    bld.rescan(bld.srcnode)
+
+    bld(features='cc cshlib',
+        source='liblctest/liblc1.c',
+        ldflags=conf.env['EXTRA_LDFLAGS'],
+        target='liblc',
+        name='liblc')
+
+    try:
+        bld.compile()
+        conf.check_message(msg, '', True)
+        return True
+    except:
+        conf.check_message(msg, '', False)
+        return False
+
+
 @conf
 def CHECK_SHLIB_W_PYTHON(conf, msg):
     '''check if we need -undefined dynamic_lookup'''
@@ -133,7 +270,7 @@ int foo(int v) {
 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
 # functions that make writing complex compound tests like this much easier
 @conf
-def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
+def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
     '''see if the platform supports building libraries'''
 
     if msg is None:
@@ -171,9 +308,17 @@ def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
 
     bld.rescan(bld.srcnode)
 
+    ldflags = []
+    if version_script:
+        ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
+        dest = open(os.path.join(dir,'vscript'), 'w')
+        dest.write('TEST_1.0A2 { global: *; };\n')
+        dest.close()
+
     bld(features='cc cshlib',
         source='libdir/lib1.c',
         target='libdir/lib1',
+        ldflags=ldflags,
         name='lib1')
 
     o = bld(features='cc cprogram',
@@ -242,7 +387,7 @@ def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
     dest.write("""
 use ExtUtils::MakeMaker;
 WriteMakefile(
-    'NAME'     => 'WafTest',
+    'NAME'    => 'WafTest',
     'EXE_FILES' => [ 'WafTest' ]
 );
 """)
@@ -359,3 +504,40 @@ def CHECK_XSLTPROC_MANPAGES(conf):
                              msg='Checking for stylesheet %s' % s,
                              define='XSLTPROC_MANPAGES', on_target=False,
                              boolean=True)
+    if not conf.CONFIG_SET('XSLTPROC_MANPAGES'):
+        print "A local copy of the docbook.xsl wasn't found on your system" \
+              " consider installing package like docbook-xsl"
+
+
+waf_config_c_parse_flags = config_c.parse_flags;
+def samba_config_c_parse_flags(line, uselib, env):
+    waf_config_c_parse_flags(line, uselib, env)
+
+    try:
+        linkflags = env['LINKFLAGS_' + uselib]
+    except KeyError:
+        linkflags = []
+    for x in linkflags:
+        #
+        # NOTE on special treatment of -Wl,-R and -Wl,-rpath:
+        #
+        # It is important to not put a library provided RPATH
+        # into the LINKFLAGS but in the RPATH instead, since
+        # the provided LINKFLAGS get prepended to our own internal
+        # RPATH later, and hence can potentially lead to linking
+        # in too old versions of our internal libs.
+        #
+        if x.startswith('-Wl,-R,'):
+            rpath = x[7:]
+        elif x.startswith('-Wl,-R'):
+            rpath = x[6:]
+        elif x.startswith('-Wl,-rpath,'):
+            rpath = x[11:]
+        else:
+            continue
+
+        env.append_value('RPATH_' + uselib, rpath)
+        linkflags.remove(x)
+
+    return
+config_c.parse_flags = samba_config_c_parse_flags