buildtools/wafsamba: Decode output of cmd_output (which is bytes)
[samba.git] / buildtools / wafsamba / samba_conftests.py
index 4811614ab73701497a89909e34185dd39375e737..c0b9ae4929649692c9870edf28e8e0aa94fca113 100644 (file)
@@ -1,9 +1,75 @@
 # a set of config tests that use the samba_autoconf functions
 # to test for commonly needed configuration options
 
-import os, Build, shutil, Utils, re
-from Configure import conf
-from samba_utils import *
+import os, shutil, re
+from waflib import Build, Configure, Utils, Options, Logs, Errors
+from waflib.Configure import conf
+from samba_utils import TO_LIST, ADD_LD_LIBRARY_PATH
+
+
+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'''
+    Options.OptionsContext.parser = self
+    match = kw.get('match', [])
+    if match:
+        del kw['match']
+    opt = self.parser.add_option(*k, **kw)
+    opt.match = match
+    return opt
+Options.OptionsContext.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.derive()
+
+    # match the configuration test with specific 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.OptionsContext.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.start_msg(kw['msg'])
+    ret = None
+    try:
+        ret = self.run_c_code(*k, **kw)
+    except Configure.ConfigurationError as e:
+        self.end_msg(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.end_msg(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'):
@@ -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
 
 
@@ -71,7 +163,7 @@ def find_config_dir(conf):
     '''find a directory to run tests in'''
     k = 0
     while k < 10000:
-        dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
+        dir = os.path.join(conf.bldnode.abspath(), '.conf_check_%d' % k)
         try:
             shutil.rmtree(dir)
         except OSError:
@@ -105,16 +197,56 @@ int foo(int v) {
     return v * 2;
 }
 '''
-    return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
+    return conf.check(features='c cshlib',vnum="1",fragment=snip,msg=msg, mandatory=False)
 
 @conf
-def CHECK_SHLIB_W_PYTHON(conf, msg):
-    '''check if we need -undefined dynamic_lookup'''
+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)
+
+    Utils.writef(os.path.join(subdir, 'liblc1.c'), '#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
+
+    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='c 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'''
+
+    dir = find_config_dir(conf)
     snip = '''
 #include <Python.h>
 #include <crt_externs.h>
@@ -127,13 +259,13 @@ int foo(int v) {
     ldb_module = PyImport_ImportModule("ldb");
     return v * 2;
 }'''
-    return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
+    return conf.check(features='c cshlib',uselib='PYEMBED',fragment=snip,msg=msg, mandatory=False)
 
 # this one is quite complex, and should probably be broken up
 # 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:
@@ -154,13 +286,10 @@ def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
 
     os.makedirs(subdir)
 
-    dest = open(os.path.join(subdir, 'lib1.c'), 'w')
-    dest.write('int lib_func(void) { return 42; }\n')
-    dest.close()
-
-    dest = open(os.path.join(dir, 'main.c'), 'w')
-    dest.write('int main(void) {return !(lib_func() == 42);}\n')
-    dest.close()
+    Utils.writef(os.path.join(subdir, 'lib1.c'), 'int lib_func(void) { return 42; }\n')
+    Utils.writef(os.path.join(dir, 'main.c'),
+                 'int lib_func(void);\n'
+                 'int main(void) {return !(lib_func() == 42);}\n')
 
     bld = Build.BuildContext()
     bld.log = conf.log
@@ -171,12 +300,18 @@ def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
 
     bld.rescan(bld.srcnode)
 
-    bld(features='cc cshlib',
+    ldflags = []
+    if version_script:
+        ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
+        Utils.writef(os.path.join(dir,'vscript'), 'TEST_1.0A2 { global: *; };\n')
+
+    bld(features='c cshlib',
         source='libdir/lib1.c',
         target='libdir/lib1',
+        ldflags=ldflags,
         name='lib1')
 
-    o = bld(features='cc cprogram',
+    o = bld(features='c cprogram',
             source='main.c',
             target='prog1',
             uselib_local='lib1')
@@ -203,7 +338,8 @@ def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
 
     # we need to run the program, try to get its result
     args = conf.SAMBA_CROSS_ARGS(msg=msg)
-    proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
+    proc = Utils.subprocess.Popen([lastprog] + args,
+            stdout=Utils.subprocess.PIPE, stderr=Utils.subprocess.PIPE)
     (out, err) = proc.communicate()
     w = conf.log.write
     w(str(out))
@@ -230,7 +366,7 @@ def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
         else:
             msg = "perl manpage generation"
 
-    conf.check_message_1(msg)
+    conf.start_msg(msg)
 
     dir = find_config_dir(conf)
 
@@ -238,41 +374,37 @@ def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
     if not os.path.exists(bdir):
         os.makedirs(bdir)
 
-    dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
-    dest.write("""
+    Utils.writef(os.path.join(bdir, 'Makefile.PL'), """
 use ExtUtils::MakeMaker;
 WriteMakefile(
-    'NAME'     => 'WafTest',
+    'NAME'    => 'WafTest',
     'EXE_FILES' => [ 'WafTest' ]
 );
 """)
-    dest.close()
     back = os.path.abspath('.')
     os.chdir(bdir)
-    proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
-                             stdout=Utils.pproc.PIPE,
-                             stderr=Utils.pproc.PIPE)
+    proc = Utils.subprocess.Popen(['perl', 'Makefile.PL'],
+                             stdout=Utils.subprocess.PIPE,
+                             stderr=Utils.subprocess.PIPE)
     (out, err) = proc.communicate()
     os.chdir(back)
 
     ret = (proc.returncode == 0)
     if not ret:
-        conf.check_message_2('not found', color='YELLOW')
+        conf.end_msg('not found', color='YELLOW')
         return
 
     if section:
-        f = open(os.path.join(bdir,'Makefile'), 'r')
-        man = f.read()
-        f.close()
+        man = Utils.readf(os.path.join(bdir,'Makefile'))
         m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
         if not m:
-            conf.check_message_2('not found', color='YELLOW')
+            conf.end_msg('not found', color='YELLOW')
             return
         ext = m.group(1)
-        conf.check_message_2(ext)
+        conf.end_msg(ext)
         return ext
 
-    conf.check_message_2('ok')
+    conf.end_msg('ok')
     return True
 
 
@@ -286,7 +418,7 @@ def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=Fals
     if on_target:
         cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
     try:
-        ret = Utils.cmd_output(cmd)
+        ret = Utils.cmd_output(cmd).decode('utf8')
     except:
         conf.COMPOUND_END(False)
         return False
@@ -308,6 +440,7 @@ def CHECK_UNAME(conf):
     ret = True
     for v in "sysname machine release version".split():
         if not conf.CHECK_CODE('''
+                               int printf(const char *format, ...);
                                struct utsname n;
                                if (uname(&n) == -1) return -1;
                                printf("%%s", n.%s);
@@ -355,7 +488,40 @@ def CHECK_XSLTPROC_MANPAGES(conf):
         return False
 
     s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
-    conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
+    conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.get_flat('XSLTPROC'), s),
                              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")
+
+#
+# Determine the standard libpath for the used compiler,
+# so we can later use that to filter out these standard
+# library paths when some tools like cups-config or
+# python-config report standard lib paths with their
+# ldflags (-L...)
+#
+@conf
+def CHECK_STANDARD_LIBPATH(conf):
+    # at least gcc and clang support this:
+    try:
+        cmd = conf.env.CC + ['-print-search-dirs']
+        out = Utils.cmd_output(cmd).decode('utf8').split('\n')
+    except ValueError:
+        # option not supported by compiler - use a standard list of directories
+        dirlist = [ '/usr/lib', '/usr/lib64' ]
+    except:
+        raise Errors.WafError('Unexpected error running "%s"' % (cmd))
+    else:
+        dirlist = []
+        for line in out:
+            line = line.strip()
+            if line.startswith("libraries: ="):
+                dirliststr = line[len("libraries: ="):]
+                dirlist = [ os.path.normpath(x) for x in dirliststr.split(':') ]
+                break
+
+    conf.env.STANDARD_LIBPATH = dirlist
+