ldb: bump to version 1.1.19
[obnox/samba/samba-obnox.git] / ctdb / wscript
index 4920f9e438534fb6a415374405f86d549b6ffbed..1400685404cfee6e0a32f29c03bbea93e0dd9cc3 100755 (executable)
@@ -12,14 +12,14 @@ while not os.path.exists(srcdir+'/buildtools') and len(srcdir.split('/')) < 5:
     srcdir = srcdir + '/..'
 sys.path.insert(0, srcdir + '/buildtools/wafsamba')
 
-import wafsamba, samba_dist, Options, Logs
+import wafsamba, samba_dist, Options, Logs, Utils
 import samba_utils, samba_version
 
 env = samba_utils.LOAD_ENVIRONMENT()
 if os.path.isfile('./VERSION'):
-    vdir='.'
+    vdir = '.'
 elif os.path.isfile('../VERSION'):
-    vdir='..'
+    vdir = '..'
 else:
     Logs.error("VERSION file not found")
 
@@ -29,13 +29,21 @@ VERSION = version.STRING.replace('-', '.')
 Options.default_prefix = '/usr/local'
 
 samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc
-                        lib/tevent:lib/tevent lib/tdb:lib/tdb lib/popt:lib/popt
+                        lib/tevent:lib/tevent lib/tdb:lib/tdb
                         lib/socket_wrapper:lib/socket_wrapper
+                        third_party/popt:third_party/popt
+                        lib/util:lib/util lib/tdb_wrap:lib/tdb_wrap
+                        lib/ccan:lib/ccan libcli/util:libcli/util
                         buildtools:buildtools''')
 
+
 def set_options(opt):
     opt.PRIVATE_EXTENSION_DEFAULT('ctdb')
+
     opt.RECURSE('lib/replace')
+
+    opt.RECURSE('lib/util')
+
     opt.RECURSE('lib/talloc')
     opt.RECURSE('lib/tevent')
     opt.RECURSE('lib/tdb')
@@ -51,8 +59,9 @@ def set_options(opt):
                    help=("Path to log directory"),
                    action="store", dest='ctdb_logdir', default=None)
     opt.add_option('--with-socketpath',
-                  help=("path to CTDB daemon socket"),
-                  action="store_true", dest='ctdb_sockpath', default=False)
+                   help=("path to CTDB daemon socket"),
+                   action="store_true", dest='ctdb_sockpath', default=False)
+
 
 def configure(conf):
 
@@ -61,15 +70,59 @@ def configure(conf):
 
     # No need to build python bindings for talloc/tevent/tdb
     if conf.IN_LAUNCH_DIR():
+        conf.env.standalone_ctdb = True
         Options.options.disable_python = True
 
     conf.RECURSE('lib/replace')
-    conf.RECURSE('lib/popt')
+
+    if conf.env.standalone_ctdb:
+        conf.SAMBA_CHECK_PERL(mandatory=True)
+
+        conf.SAMBA_CHECK_PYTHON(mandatory=True, version=(2,5,0))
+        conf.SAMBA_CHECK_PYTHON_HEADERS(mandatory=True)
+
+    if conf.CHECK_FOR_THIRD_PARTY():
+        conf.RECURSE('third_party/popt')
+    else:
+        if not conf.CHECK_POPT():
+            raise Utils.WafError('popt development packages have not been found\nIf third_party is installed, check that it is in the proper place.')
+        else:
+            conf.define('USING_SYSTEM_POPT', 1)
+
+    conf.RECURSE('lib/util')
+
     conf.RECURSE('lib/talloc')
     conf.RECURSE('lib/tevent')
     conf.RECURSE('lib/tdb')
     conf.RECURSE('lib/socket_wrapper')
 
+    conf.CHECK_HEADERS('sched.h')
+    conf.CHECK_HEADERS('procinfo.h')
+    if sys.platform.startswith('aix') and not conf.CHECK_FUNCS('thread_setsched'):
+        Logs.error('Need thread_setsched() on AIX')
+        sys.exit(1)
+    elif not conf.CHECK_FUNCS('sched_setscheduler'):
+        Logs.error('Need sched_setscheduler()')
+        sys.exit(1)
+    conf.CHECK_FUNCS('mlockall')
+
+    if not conf.CHECK_VARIABLE('ETIME', headers='errno.h'):
+        conf.DEFINE('ETIME', 'ETIMEDOUT')
+
+    if sys.platform.startswith('linux'):
+        conf.SET_TARGET_TYPE('pcap', 'EMPTY')
+    else:
+        if not conf.CHECK_HEADERS('pcap.h'):
+            Logs.error('Need libpcap')
+            sys.exit(1)
+        if not conf.CHECK_FUNCS_IN('pcap_open_live', 'pcap', headers='pcap.h'):
+            Logs.error('Need libpcap')
+            sys.exit(1)
+
+    if not conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo',
+                               checklibc=True, headers='execinfo.h'):
+        Logs.error('backtrace support not available')
+
     have_pmda = False
     if Options.options.ctdb_pmda:
         pmda_support = True
@@ -115,10 +168,6 @@ def configure(conf):
     conf.env.CTDB_VARDIR = os.path.join(conf.env.LOCALSTATEDIR, 'lib/ctdb')
     conf.env.CTDB_RUNDIR = os.path.join(conf.env.LOCALSTATEDIR, 'run/ctdb')
 
-    machine = os.uname()[4]
-    if machine in ['x86_64', 'ppc64', 'powerpc64']:
-        conf.env.LIBDIR = conf.env.LIBDIR + '64'
-
     if Options.options.ctdb_logdir:
         conf.env.CTDB_LOGDIR = Options.options.ctdb_logdir
     else:
@@ -130,49 +179,95 @@ def configure(conf):
         conf.env.CTDB_SOCKPATH = os.path.join(conf.env.CTDB_RUNDIR,
                                               'ctdbd.socket')
 
-    conf.ADD_CFLAGS('''-DBINDIR=\"%s\" -DETCDIR=\"%s\" -DVARDIR=\"%s\"
-                       -DLOGDIR=\"%s\" -DSOCKPATH=\"%s\"
-                       -DCTDB_VARDIR=\"%s\"''' % (
+    conf.ADD_CFLAGS('''-DCTDB_HELPER_BINDIR=\"%s\"
+                       -DLOGDIR=\"%s\"
+                       -DSOCKPATH=\"%s\"
+                       -DCTDB_ETCDIR=\"%s\"
+                       -DCTDB_VARDIR=\"%s\"
+                       -DCTDB_RUNDIR=\"%s\"''' % (
                     conf.env.CTDB_BINDIR,
-                    conf.env.CTDB_ETCDIR,
-                    conf.env.CTDB_VARDIR,
                     conf.env.CTDB_LOGDIR,
                     conf.env.CTDB_SOCKPATH,
-                    conf.env.CTDB_VARDIR))
+                    conf.env.CTDB_ETCDIR,
+                    conf.env.CTDB_VARDIR,
+                    conf.env.CTDB_RUNDIR))
 
     conf.env.CTDB_TEST_DATADIR = os.path.join(conf.env.EXEC_PREFIX,
                                               'share/ctdb-tests')
     conf.env.CTDB_TEST_LIBDIR = os.path.join(conf.env.LIBDIR, 'ctdb-tests')
 
-    conf.DEFINE('HAVE_CONFIG_H', 1, add_to_cflags=True)
-    conf.SAMBA_CONFIG_H()
+    # Allow unified compilation and separate compilation of utilities
+    # to find includes
+    if not conf.env.standalone_ctdb:
+        conf.ADD_EXTRA_INCLUDES('#ctdb/include')
+    else:
+        if srcdir == '.':
+            # Building from tarball
+            conf.ADD_EXTRA_INCLUDES('#include')
+            conf.ADD_EXTRA_INCLUDES('#include/internal')
+        else:
+            # Building standalone CTDB from within Samba tree
+            conf.ADD_EXTRA_INCLUDES('#ctdb/include')
+            conf.ADD_EXTRA_INCLUDES('#ctdb/include/internal')
+            conf.ADD_EXTRA_INCLUDES('#ctdb')
+        conf.ADD_EXTRA_INCLUDES('#lib #lib/replace')
+
+        conf.SET_TARGET_TYPE('systemd-daemon', 'EMPTY')
+
+        del(conf.env.defines['PYTHONDIR'])
+        del(conf.env.defines['PYTHONARCHDIR'])
+
+        conf.DEFINE('HAVE_CONFIG_H', 1, add_to_cflags=True)
+        conf.DEFINE('SAMBA_UTIL_CORE_ONLY', 1, add_to_cflags=True)
+        conf.SAMBA_CONFIG_H()
+
 
 def build(bld):
+    if bld.env.standalone_ctdb:
+        # enable building of public headers in the build tree
+        bld.env.build_public_headers = 'include/public'
+
+    if bld.env.standalone_ctdb:
+        ctdb_mkversion = '../packaging/mkversion.sh'
+    else:
+        ctdb_mkversion = '../ctdb/packaging/mkversion.sh'
+    t = bld.SAMBA_GENERATOR('ctdb-version-header',
+                            target='include/ctdb_version.h',
+                            rule='%s ${TGT} %s' % (ctdb_mkversion, VERSION),
+                            dep_vars=['VERSION'])
+    t.env.VERSION = VERSION
+
+    if bld.env.standalone_ctdb:
+        version_h = samba_utils.os_path_relpath(os.path.join(Options.launch_dir,
+                                                             "version.h"),
+                                                bld.curdir)
+        t.bld.SAMBA_GENERATOR('ctdb-samba-version-header',
+                              target=version_h,
+                              rule='printf "#include \\"ctdb_version.h\\" \\n#define SAMBA_VERSION_STRING CTDB_VERSION_STRING\\n" > ${TGT}',
+                              dep_vars=['VERSION'])
+        t.env.VERSION = VERSION
+
     bld.RECURSE('lib/replace')
-    bld.RECURSE('lib/popt')
+    if bld.CHECK_FOR_THIRD_PARTY():
+        bld.RECURSE('third_party/popt')
+
+    bld.RECURSE('lib/tdb_wrap')
+    bld.RECURSE('lib/util')
+
     bld.RECURSE('lib/talloc')
     bld.RECURSE('lib/tevent')
     bld.RECURSE('lib/tdb')
     bld.RECURSE('lib/socket_wrapper')
 
-    bld.SAMBA_GENERATOR('ctdb-version-header',
-                        target='include/ctdb_version.h',
-                        rule='../packaging/mkversion.sh ${TGT} %s' % (VERSION),
-                        always=True)
-
-    bld.SAMBA_SUBSYSTEM('ctdb-util',
-                        source=bld.SUBDIR('lib/util',
-                                          'util.c util_file.c util_time.c'),
-                        includes='include include/internal',
-                        deps='replace talloc tevent tdb')
-
-    bld.SAMBA_SUBSYSTEM('ctdb-util-misc',
-                        source=bld.SUBDIR('lib/util',
-                                          '''db_wrap.c debug.c fault.c
-                                             idtree.c signal.c strlist.c
-                                             substitute.c'''),
-                        includes='include include/internal lib/util',
-                        deps='replace talloc tevent tdb')
+    if bld.env.standalone_ctdb:
+        # In a combined build is implemented, CTDB will wanted to
+        # build against samba-util rather than samba-util-core.
+        # Similarly, other Samba subsystems expect samba-util.  So,
+        # for a standalone build, just define a fake samba-util
+        # subsystem that pulls in samba-util-core.
+        bld.SAMBA_SUBSYSTEM('samba-util',
+                            source='',
+                            deps='samba-util-core')
 
     bld.SAMBA_SUBSYSTEM('ctdb-tcp',
                         source=bld.SUBDIR('tcp',
@@ -195,8 +290,8 @@ def build(bld):
                                           '''ctdb_io.c ctdb_util.c ctdb_ltdb.c
                                              ctdb_message.c cmdline.c rb_tree.c
                                              system_common.c ctdb_fork.c'''),
-                        includes='include include/internal common . lib/util',
-                        deps='replace popt talloc tevent tdb')
+                        includes='include include/internal common .',
+                        deps='replace popt talloc tevent tdb popt')
 
     bld.SAMBA_SUBSYSTEM('ctdb-common-util',
                         source=bld.SUBDIR('common',
@@ -204,11 +299,11 @@ def build(bld):
                         includes='include include/internal',
                         deps='replace tevent tdb')
 
-    if sys.platform == 'linux2':
+    if sys.platform.startswith('linux'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_linux.c')
-    elif sys.platform == 'aix':
+    elif sys.platform.startswith('aix'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_aix.c')
-    elif sys.platform == 'freebsd':
+    elif sys.platform.startswith('freebsd'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_freebsd.c')
     elif sys.platform == 'kfreebsd':
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_kfreebsd.c')
@@ -220,13 +315,14 @@ def build(bld):
     bld.SAMBA_SUBSYSTEM('ctdb-system',
                         source=CTDB_SYSTEM_SRC,
                         includes='include include/internal',
-                        deps='replace talloc tevent tdb')
+                        deps='replace talloc tevent tdb pcap')
 
     bld.SAMBA_SUBSYSTEM('ctdb-client',
                         source=bld.SUBDIR('client', 'ctdb_client.c'),
-                        includes='include include/internal lib/util',
+                        includes='include include/internal',
                         public_headers='include/ctdb_client.h',
-                        deps='replace popt talloc tevent tdb')
+                        deps='''replace popt talloc tevent tdb
+                                samba-util tdb-wrap''')
 
     bld.SAMBA_SUBSYSTEM('ctdb-server',
                         source='server/ctdbd.c ' +
@@ -239,50 +335,55 @@ def build(bld):
                                              ctdb_traverse.c eventscript.c
                                              ctdb_takeover.c ctdb_serverids.c
                                              ctdb_persistent.c ctdb_keepalive.c
-                                             ctdb_logging.c ctdb_uptime.c
+                                             ctdb_logging.c
+                                             ctdb_logging_syslog.c
+                                             ctdb_logging_file.c
+                                             ctdb_uptime.c
                                              ctdb_vacuum.c ctdb_banning.c
                                              ctdb_statistics.c
                                              ctdb_update_record.c
                                              ctdb_lock.c'''),
-                        includes='include include/internal lib/util',
-                        public_headers='''include/ctdb.h
+                        includes='include include/internal',
+                        public_headers='''include/ctdb_version.h
+                                          include/ctdb.h
                                           include/ctdb_private.h
                                           include/ctdb_protocol.h
                                           include/ctdb_typesafe_cb.h''',
                         deps='replace popt talloc tevent tdb')
 
-    bld.SAMBA_BINARY('bin/ctdbd',
+    bld.SAMBA_BINARY('ctdbd',
                      source='',
                      deps='''ctdb-server ctdb-client ctdb-common
-                             ctdb-common-util ctdb-system ctdb-tcp
-                             ctdb-util ctdb-util-misc''' +
+                             ctdb-common-util ctdb-system ctdb-tcp''' +
                           ib_deps,
                      install_path='${SBINDIR}',
                      manpages='doc/ctdbd.1')
 
-    bld.SAMBA_BINARY('bin/ctdb',
+    bld.SAMBA_BINARY('ctdb',
                      source='tools/ctdb.c tools/ctdb_vacuum.c',
                      deps='''ctdb-client ctdb-common ctdb-common-util
-                             ctdb-system ctdb-util ctdb-util-misc''',
+                             ctdb-system''',
+                     includes='include include/internal',
                      install_path='${BINDIR}',
                      manpages='doc/ctdb.1')
 
-    bld.SAMBA_BINARY('bin/ltdbtool',
+    bld.SAMBA_BINARY('ltdbtool',
                      source='tools/ltdbtool.c',
                      includes='include',
                      deps='tdb',
                      install_path='${BINDIR}',
                      manpages='doc/ltdbtool.1')
 
-    bld.SAMBA_BINARY('bin/ctdb_lock_helper',
+    bld.SAMBA_BINARY('ctdb_lock_helper',
                      source='server/ctdb_lock_helper.c',
-                     deps='ctdb-util ctdb-util-misc ctdb-common-util talloc tdb',
+                     deps='samba-util ctdb-common-util talloc tdb',
+                     includes='include include/internal',
                      install_path='${BINDIR}')
 
-    bld.SAMBA_BINARY('bin/ctdb_event_helper',
+    bld.SAMBA_BINARY('ctdb_event_helper',
                      source='server/ctdb_event_helper.c',
                      includes='include include/internal',
-                     deps='ctdb-util ctdb-util-misc ctdb-common-util replace tdb',
+                     deps='samba-util ctdb-common-util replace tdb',
                      install_path='${BINDIR}')
 
     bld.SAMBA_GENERATOR('ctdb-smnotify-h',
@@ -290,35 +391,37 @@ def build(bld):
                         target='utils/smnotify/smnotify.h',
                         rule='rpcgen -h ${SRC} > ${TGT}')
 
+    xdr_buf_hack = 'sed -e "s@^\([ \t]*register int32_t \*buf\);@\\1 = buf;@"'
+
     bld.SAMBA_GENERATOR('ctdb-smnotify-x',
                         source='utils/smnotify/smnotify.x',
                         target='utils/smnotify/gen_xdr.c',
-                        rule='rpcgen -c ${SRC} > ${TGT}')
+                        rule='rpcgen -c ${SRC} | ' + xdr_buf_hack + ' > ${TGT}')
 
     bld.SAMBA_GENERATOR('ctdb-smnotify-c',
                         source='utils/smnotify/smnotify.x',
                         target='utils/smnotify/gen_smnotify.c',
                         rule='rpcgen -l ${SRC} > ${TGT}')
 
-    bld.SAMBA_BINARY('bin/smnotify',
+    bld.SAMBA_BINARY('smnotify',
                      source=bld.SUBDIR('utils/smnotify',
                                        'smnotify.c gen_smnotify.c gen_xdr.c'),
                      deps='ctdb-smnotify-h ctdb-smnotify-c ctdb-smnotify-x popt',
                      includes='utils utils/smnotify',
                      install_path='${BINDIR}')
 
-    bld.SAMBA_BINARY('bin/ping_pong',
+    bld.SAMBA_BINARY('ping_pong',
                      source='utils/ping_pong/ping_pong.c',
                      deps='',
                      install_path='${BINDIR}',
                      manpages='doc/ping_pong.1')
 
     if bld.env.HAVE_PMDA:
-        bld.SAMBA_BINARY('bin/pmdactdb',
+        bld.SAMBA_BINARY('pmdactdb',
                          source='utils/pmda/pmda_ctdb.c',
                          includes='include include/internal',
                          deps='''ctdb-client ctdb-common ctdb-system
-                                 ctdb-util-misc ctdb-util pcp_pmda pcp''',
+                                 pcp_pmda pcp''',
                          install_path='${CTDB_PMDADIR}')
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/Install',
                           destname='Install')
@@ -333,8 +436,10 @@ def build(bld):
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/README',
                           destname='README')
 
-    bld.MANPAGES('''doc/onnode.1 doc/ctdbd_wrapper.1 doc/ctdbd.conf.5
-                    doc/ctdb.7 doc/ctdb-tunables.7''', True)
+    if 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
+        bld.MANPAGES('''doc/onnode.1 doc/ctdbd_wrapper.1 doc/ctdbd.conf.5
+                        doc/ctdb.7 doc/ctdb-statistics.7 doc/ctdb-tunables.7''',
+                     True)
 
     bld.INSTALL_FILES('${BINDIR}', 'tools/onnode',
                       destname='onnode', chmod=0755)
@@ -350,21 +455,26 @@ def build(bld):
                 continue
             mode = os.lstat(fl).st_mode & 0777
             if arg['trim_path']:
-                    fl = samba_utils.os_path_relpath(fl, arg['trim_path'])
+                fl = samba_utils.os_path_relpath(fl, arg['trim_path'])
             arg['file_list'].append([fl, mode])
 
     def SUBDIR_MODE(path, trim_path=None):
-         pd = {'trim_path': trim_path, 'file_list': []}
-         os.path.walk(path, SUBDIR_MODE_callback, pd)
-         return pd['file_list']
+        pd = {'trim_path': trim_path, 'file_list': []}
+        os.path.walk(path, SUBDIR_MODE_callback, pd)
+        return pd['file_list']
 
     etc_subdirs = [
         'events.d',
         'nfs-rpc-checks.d'
     ]
 
+    if bld.env.standalone_ctdb:
+        configdir = 'config'
+    else:
+        configdir = 'ctdb/config'
+
     for t in etc_subdirs:
-        files = SUBDIR_MODE('config/%s' % t, trim_path='config')
+        files = SUBDIR_MODE('%s/%s' % (configdir, t), trim_path=configdir)
         for fmode in files:
             bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % fmode[0],
                               destname=fmode[0], chmod=fmode[1])
@@ -382,7 +492,7 @@ def build(bld):
     ]
 
     for t in etc_scripts:
-        bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/' + t,
+        bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % t,
                           destname=t, chmod=0755)
 
     bld.INSTALL_FILES('${SYSCONFDIR}/sudoers.d', 'config/ctdb.sudoers',
@@ -396,10 +506,12 @@ def build(bld):
     bld.install_dir(bld.env.CTDB_VARDIR)
 
     sed_expr = 's/@PACKAGE_VERSION@/%s/g' % VERSION
-    bld.SAMBA_GENERATOR('ctdb-pc',
-                        source='ctdb.pc.in',
-                        target='ctdb.pc',
-                        rule='sed -e "%s" ${SRC} > ${TGT}' % sed_expr)
+    t = bld.SAMBA_GENERATOR('ctdb-pc',
+                            source='ctdb.pc.in',
+                            target='ctdb.pc',
+                            rule='sed -e "%s" ${SRC} > ${TGT}' % sed_expr,
+                            dep_vars=['VERSION'])
+    t.env.VERSION = VERSION
     bld.INSTALL_FILES('${LIBDIR}/pkgconfig', 'ctdb.pc')
 
     # Test binaries
@@ -422,41 +534,44 @@ def build(bld):
         'ctdb_lock_tdb'
     ]
 
-    for t in ctdb_tests:
-         target = 'bin/' + t
-         src = 'tests/src/' + t + '.c'
+    for target in ctdb_tests:
+        src = 'tests/src/' + target + '.c'
 
-         bld.SAMBA_BINARY(target,
-                          source=src,
-                          deps='''ctdb-client ctdb-common ctdb-common-util
-                                  ctdb-system ctdb-util ctdb-util-misc''',
-                          install_path='${CTDB_TEST_LIBDIR}')
+        bld.SAMBA_BINARY(target,
+                         source=src,
+                         includes='include include/internal',
+                         deps='''ctdb-client ctdb-common ctdb-common-util
+                                 ctdb-system''',
+                         install_path='${CTDB_TEST_LIBDIR}')
 
-    bld.SAMBA_BINARY('bin/ctdb_takeover_tests',
+    bld.SAMBA_BINARY('ctdb_takeover_tests',
                      source='tests/src/ctdb_takeover_tests.c',
-                     deps='replace popt tdb tevent talloc ctdb-system' +
+                     deps='''replace popt tdb tevent talloc ctdb-system
+                             samba-util tdb-wrap''' +
                           ib_deps,
-                     includes='../include ../include/internal lib/util',
+                     includes='include include/internal',
                      install_path='${CTDB_TEST_LIBDIR}')
 
-    bld.SAMBA_BINARY('bin/ctdb_functest',
+    bld.SAMBA_BINARY('ctdb_functest',
                      source='tests/src/ctdb_functest.c',
-                     deps='replace tdb tevent talloc popt ctdb-system',
-                     includes='../include ../include/internal lib/util',
+                     deps='''replace tdb tevent talloc popt ctdb-system
+                             samba-util tdb-wrap''',
+                     includes='include include/internal',
                      install_path='${CTDB_TEST_LIBDIR}')
 
-    bld.SAMBA_BINARY('bin/ctdb_stubtest',
+    bld.SAMBA_BINARY('ctdb_stubtest',
                      source='tests/src/ctdb_test.c',
-                     deps='replace tdb tevent talloc popt ctdb-system',
-                     includes='../include ../include/internal lib/util',
+                     deps='''replace tdb tevent talloc popt ctdb-system
+                             samba-util tdb-wrap''',
+                     includes='include include/internal',
                      install_path='${CTDB_TEST_LIBDIR}')
 
     if bld.env.HAVE_INFINIBAND:
-        bld.SAMBA_BINARY('bin/ibwrapper_test',
+        bld.SAMBA_BINARY('ibwrapper_test',
                          source='ib/ibwrapper_test.c',
                          includes='include include/internal',
                          deps='''replace talloc ctdb-client ctdb-common
-                                 ctdb-system ctdb-util-misc ctdb-util''' +
+                                 ctdb-system''' +
                               ib_deps,
                          install_path='${CTDB_TEST_LIBDIR}')
 
@@ -465,7 +580,6 @@ def build(bld):
         'events.d',
         'eventscripts',
         'onnode',
-        'scripts',
         'simple',
         'takeover',
         'tool'
@@ -477,16 +591,30 @@ def build(bld):
             bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR, 'tests/%s' % fmode[0],
                               destname=fmode[0], chmod=fmode[1])
 
+    # Install tests/scripts directory without test_wrap
+    test_scripts = [
+        'common.sh',
+        'integration.bash',
+        'unit.sh'
+    ]
+
+    for t in test_scripts:
+        bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR,
+                          os.path.join('tests/scripts', t),
+                          destname=os.path.join('scripts', t))
+
     sed_expr = 's@^TEST_SCRIPTS_DIR=.*@&\\nexport TEST_BIN_DIR=\"%s\"@' % (
                bld.env.CTDB_TEST_LIBDIR)
     bld.SAMBA_GENERATOR('ctdb-test-wrap',
                         source='tests/scripts/test_wrap',
                         target='test_wrap',
                         rule='sed -e "%s" ${SRC} > ${TGT}' % sed_expr)
+    bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR+"/scripts", 'test_wrap',
+                      destname='test_wrap', chmod=0755)
 
     sed_expr1 = 's@^test_dir=.*@test_dir=%s\\nexport TEST_BIN_DIR=\"%s\"@' % (
-                bld.env.CTDB_DATA_DIR, bld.env.CTDB_LIBDIR)
-    sed_expr2 = 's@^\(export CTDB_TESTS_ARE_INSTALLED\)=false@\1=true@'
+                bld.env.CTDB_TEST_DATADIR, bld.env.CTDB_TEST_LIBDIR)
+    sed_expr2 = 's@^\(export CTDB_TESTS_ARE_INSTALLED\)=false@\\1=true@'
     bld.SAMBA_GENERATOR('ctdb-test-runner',
                         source='tests/run_tests.sh',
                         target='ctdb_run_tests.sh',
@@ -512,30 +640,50 @@ def build(bld):
 
 def testonly(ctx):
     cmd = 'tests/run_tests.sh -V tests/var'
-    samba_utils.RUN_COMMAND(cmd)
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('tests exited with exit status %d' % ret)
+        sys.exit(ret)
+
 
 def test(ctx):
     import Scripting
     Scripting.commands.append('build')
     Scripting.commands.append('testonly')
 
+
 def autotest(ctx):
-    cmd = 'tests/run_tests.sh -e -S -C'
-    samba_utils.RUN_COMMAND(cmd)
+    cmd = 'LD_PRELOAD=bin/shared/libsocket-wrapper.so tests/run_tests.sh -e -S -C'
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('autotest exited with exit status %d' % ret)
+        sys.exit(ret)
 
-def dist():
 
+def show_version(ctx):
+    print VERSION
+
+
+def dist():
     samba_dist.DIST_FILES('VERSION:VERSION', extend=True)
 
     t = 'include/ctdb_version.h'
-    out = os.system('packaging/mkversion.sh %s %s' % (t, VERSION))
+    cmd = 'packaging/mkversion.sh %s %s' % (t, VERSION)
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('Command "%s" failed with exit status %d' % (cmd, ret))
+        sys.exit(ret)
     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
 
     t = 'ctdb.spec'
     sed_expr1 = 's/@VERSION@/%s/g' % VERSION
     sed_expr2 = 's/@RELEASE@/%s/g' % '1'
-    os.system('sed -e "%s" -e "%s" packaging/RPM/ctdb.spec.in > %s' %
-              (sed_expr1, sed_expr2, t))
+    cmd = 'sed -e "%s" -e "%s" packaging/RPM/ctdb.spec.in > %s' % (
+        sed_expr1, sed_expr2, t)
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('Command "%s" failed with exit status %d' % (cmd, ret))
+        sys.exit(ret)
     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
 
     manpages = [
@@ -544,11 +692,16 @@ def dist():
         'ctdbd.1',
         'ctdbd.conf.5',
         'ctdbd_wrapper.1',
+        'ctdb-statistics.7',
         'ctdb-tunables.7',
         'ltdbtool.1'
     ]
 
-    os.system('make -C doc')
+    cmd = 'make -C doc'
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('Command "%s" failed with exit status %d' % (cmd, ret))
+        sys.exit(ret)
     for t in manpages:
         samba_dist.DIST_FILES('ctdb/doc/%s:doc/%s' % (t, t), extend=True)
         samba_dist.DIST_FILES('ctdb/doc/%s.html:doc/%s.html' % (t, t),
@@ -556,10 +709,28 @@ def dist():
 
     samba_dist.dist()
 
+
+def rpmonly(ctx):
+    cmd = 'rpmbuild -ta --clean --rmsource ctdb-%s.tar.gz' % VERSION
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('rpmbuild exited with exit status %d' % ret)
+        sys.exit(ret)
+
+
+def rpm(ctx):
+    import Scripting
+    Scripting.commands.append('dist')
+    Scripting.commands.append('rpmonly')
+
+
 def ctags(ctx):
     "build 'tags' file using ctags"
     import Utils
     source_root = os.path.dirname(Utils.g_module.root_path)
-    cmd = 'ctags $(find %s/.. -name "*.[ch]" | grep -v "*_proto\.h")' % source_root
+    cmd = 'ctags $(find %s -name "*.[ch]")' % source_root
     print("Running: %s" % cmd)
-    os.system(cmd)
+    ret = samba_utils.RUN_COMMAND(cmd)
+    if ret != 0:
+        print('ctags failed with exit status %d' % ret)
+        sys.exit(ret)