ctdb-logging: Refactor logging code
[ambi/samba-autobuild/.git] / ctdb / wscript
1 #!/usr/bin/env python
2
3 APPNAME = 'ctdb'
4
5 blddir = 'bin'
6
7 import sys, os
8
9 # find the buildtools directory
10 srcdir = '.'
11 while not os.path.exists(srcdir+'/buildtools') and len(srcdir.split('/')) < 5:
12     srcdir = srcdir + '/..'
13 sys.path.insert(0, srcdir + '/buildtools/wafsamba')
14
15 import wafsamba, samba_dist, Options, Logs, Utils
16 import samba_utils, samba_version
17
18 env = samba_utils.LOAD_ENVIRONMENT()
19 if os.path.isfile('./VERSION'):
20     vdir = '.'
21 elif os.path.isfile('../VERSION'):
22     vdir = '..'
23 else:
24     Logs.error("VERSION file not found")
25
26 version = samba_version.samba_version_file('%s/VERSION' % vdir, vdir, env)
27 VERSION = version.STRING.replace('-', '.')
28
29 default_prefix = Options.default_prefix = '/usr/local'
30
31 samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc
32                         lib/tevent:lib/tevent lib/tdb:lib/tdb
33                         lib/socket_wrapper:lib/socket_wrapper
34                         third_party/popt:third_party/popt
35                         lib/util:lib/util lib/tdb_wrap:lib/tdb_wrap
36                         lib/ccan:lib/ccan libcli/util:libcli/util
37                         lib/async_req:lib/async_req
38                         buildtools:buildtools third_party/waf:third_party/waf''')
39
40 manpages = [
41     'ctdb.1',
42     'ctdb.7',
43     'ctdb_diagnostics.1',
44     'ctdbd.1',
45     'ctdbd.conf.5',
46     'ctdbd_wrapper.1',
47     'ctdb-statistics.7',
48     'ctdb-tunables.7',
49     'ltdbtool.1',
50     'onnode.1',
51     'ping_pong.1'
52 ]
53
54
55 def set_options(opt):
56     opt.PRIVATE_EXTENSION_DEFAULT('ctdb')
57
58     opt.RECURSE('lib/replace')
59
60     opt.RECURSE('lib/util')
61
62     opt.RECURSE('lib/talloc')
63     opt.RECURSE('lib/tevent')
64     opt.RECURSE('lib/tdb')
65
66     opt.add_option('--enable-infiniband',
67                    help=("Turn on infiniband support (default=no)"),
68                    action="store_true", dest='ctdb_infiniband', default=False)
69     opt.add_option('--enable-pmda',
70                    help=("Turn on PCP pmda support (default=no)"),
71                    action="store_true", dest='ctdb_pmda', default=False)
72
73     opt.add_option('--with-logdir',
74                    help=("Path to log directory"),
75                    action="store", dest='ctdb_logdir', default=None)
76     opt.add_option('--with-socketpath',
77                    help=("path to CTDB daemon socket"),
78                    action="store", dest='ctdb_sockpath', default=None)
79
80
81 def configure(conf):
82
83     # No need to build python bindings for talloc/tevent/tdb
84     if conf.IN_LAUNCH_DIR():
85         conf.env.standalone_ctdb = True
86         Options.options.disable_python = True
87
88     conf.RECURSE('lib/replace')
89
90     if conf.env.standalone_ctdb:
91         conf.SAMBA_CHECK_PERL(mandatory=True)
92
93         conf.SAMBA_CHECK_PYTHON(mandatory=True, version=(2,5,0))
94         conf.SAMBA_CHECK_PYTHON_HEADERS(mandatory=True)
95
96     if conf.CHECK_FOR_THIRD_PARTY():
97         conf.RECURSE('third_party/popt')
98     else:
99         if not conf.CHECK_POPT():
100             raise Utils.WafError('popt development packages have not been found\nIf third_party is installed, check that it is in the proper place.')
101         else:
102             conf.define('USING_SYSTEM_POPT', 1)
103
104     conf.RECURSE('lib/util')
105
106     conf.RECURSE('lib/talloc')
107     conf.RECURSE('lib/tevent')
108     conf.RECURSE('lib/tdb')
109     if conf.env.standalone_ctdb or conf.CONFIG_GET('ENABLE_SELFTEST'):
110         conf.RECURSE('lib/socket_wrapper')
111         conf.env.SOCKET_WRAPPER_SO_PATH = conf.CONFIG_GET('LIBSOCKET_WRAPPER_SO_PATH')
112
113     conf.CHECK_HEADERS('sched.h')
114     conf.CHECK_HEADERS('procinfo.h')
115     if sys.platform.startswith('aix') and not conf.CHECK_FUNCS('thread_setsched'):
116         Logs.error('Need thread_setsched() on AIX')
117         sys.exit(1)
118     elif not conf.CHECK_FUNCS('sched_setscheduler'):
119         Logs.error('Need sched_setscheduler()')
120         sys.exit(1)
121     conf.CHECK_FUNCS('mlockall')
122
123     if not conf.CHECK_VARIABLE('ETIME', headers='errno.h'):
124         conf.DEFINE('ETIME', 'ETIMEDOUT')
125
126     if sys.platform.startswith('linux'):
127         conf.SET_TARGET_TYPE('pcap', 'EMPTY')
128     else:
129         if not conf.CHECK_HEADERS('pcap.h'):
130             Logs.error('Need libpcap')
131             sys.exit(1)
132         if not conf.CHECK_FUNCS_IN('pcap_open_live', 'pcap', headers='pcap.h'):
133             Logs.error('Need libpcap')
134             sys.exit(1)
135
136     if not conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo',
137                                checklibc=True, headers='execinfo.h'):
138         Logs.error('backtrace support not available')
139
140     have_pmda = False
141     if Options.options.ctdb_pmda:
142         pmda_support = True
143
144         if not conf.CHECK_HEADERS('pcp/pmapi.h pcp/impl.h pcp/pmda.h',
145                                   together=True):
146             pmda_support = False
147         if not conf.CHECK_FUNCS_IN('pmProgname', 'pcp'):
148             pmda_support = False
149         if not conf.CHECK_FUNCS_IN('pmdaDaemon', 'pcp_pmda'):
150             pmda_support = False
151         if pmda_support:
152             have_pmda = True
153         else:
154             Logs.error("PMDA support not available")
155             sys.exit(1)
156     if have_pmda:
157         Logs.info('Building with PMDA support')
158         conf.define('HAVE_PMDA', 1)
159         conf.env.CTDB_PMDADIR = os.path.join(conf.env.LOCALSTATEDIR,
160                                              'lib/pcp/pmdas/ctdb')
161
162     have_infiniband = False
163     if Options.options.ctdb_infiniband:
164         ib_support = True
165
166         if not conf.CHECK_HEADERS('infiniband/verbs.h rdma/rdma_cma.h'):
167             ib_support = False
168         if not conf.CHECK_FUNCS_IN('ibv_create_qp', 'ibverbs'):
169             ib_support = False
170         if not conf.CHECK_FUNCS_IN('rdma_connect', 'rdmacm'):
171             ib_support = False
172         if ib_support:
173             have_infiniband = True
174         else:
175             Logs.error("Infiniband support not available")
176             sys.exit(1)
177     if have_infiniband:
178         Logs.info('Building with Infiniband support')
179         conf.define('HAVE_INFINIBAND', 1)
180         conf.define('USE_INFINIBAND', 1)
181
182     conf.env.CTDB_BINDIR = os.path.join(conf.env.EXEC_PREFIX, 'bin')
183     conf.env.CTDB_ETCDIR = os.path.join(conf.env.SYSCONFDIR, 'ctdb')
184     conf.env.CTDB_VARDIR = os.path.join(conf.env.LOCALSTATEDIR, 'lib/ctdb')
185     conf.env.CTDB_RUNDIR = os.path.join(conf.env.LOCALSTATEDIR, 'run/ctdb')
186     conf.env.CTDB_HELPER_BINDIR = os.path.join(conf.env.LIBEXECDIR, 'ctdb')
187
188     if Options.options.ctdb_logdir:
189         conf.env.CTDB_LOGDIR = Options.options.ctdb_logdir
190     else:
191         conf.env.CTDB_LOGDIR = os.path.join(conf.env.LOCALSTATEDIR, 'log')
192
193     if Options.options.ctdb_sockpath:
194         conf.env.CTDB_SOCKPATH = Options.options.ctdb_sockpath
195     else:
196         conf.env.CTDB_SOCKPATH = os.path.join(conf.env.CTDB_RUNDIR,
197                                               'ctdbd.socket')
198     conf.define('CTDB_SOCKET', conf.env.CTDB_SOCKPATH)
199
200     conf.ADD_CFLAGS('''-DCTDB_HELPER_BINDIR=\"%s\"
201                        -DLOGDIR=\"%s\"
202                        -DCTDB_ETCDIR=\"%s\"
203                        -DCTDB_VARDIR=\"%s\"
204                        -DCTDB_RUNDIR=\"%s\"''' % (
205                     conf.env.CTDB_HELPER_BINDIR,
206                     conf.env.CTDB_LOGDIR,
207                     conf.env.CTDB_ETCDIR,
208                     conf.env.CTDB_VARDIR,
209                     conf.env.CTDB_RUNDIR))
210
211     conf.env.CTDB_TEST_DATADIR = os.path.join(conf.env.EXEC_PREFIX,
212                                               'share/ctdb/tests')
213     conf.env.CTDB_TEST_LIBEXECDIR = os.path.join(conf.env.LIBEXECDIR, 'ctdb/tests')
214
215     # Allow unified compilation and separate compilation of utilities
216     # to find includes
217     if not conf.env.standalone_ctdb:
218         conf.ADD_EXTRA_INCLUDES('#include/public #ctdb/include #ctdb')
219     else:
220         if srcdir == '.':
221             # Building from tarball
222             conf.ADD_EXTRA_INCLUDES('#include')
223         else:
224             # Building standalone CTDB from within Samba tree
225             conf.ADD_EXTRA_INCLUDES('#ctdb/include')
226             conf.ADD_EXTRA_INCLUDES('#ctdb')
227         conf.ADD_EXTRA_INCLUDES('#lib #lib/replace')
228
229         conf.DEFINE('HAVE_CONFIG_H', 1, add_to_cflags=True)
230         conf.DEFINE('SAMBA_UTIL_CORE_ONLY', 1, add_to_cflags=True)
231         conf.SAMBA_CONFIG_H()
232
233     if 'XSLTPROC_MANPAGES' in conf.env and conf.env['XSLTPROC_MANPAGES']:
234         conf.env.ctdb_generate_manpages = True
235     else:
236         conf.env.ctdb_generate_manpages = False
237
238         Logs.info("xsltproc unavailable, checking for pre-built manpages")
239         conf.env.ctdb_prebuilt_manpages = []
240         for m in manpages:
241             if os.path.exists(os.path.join("doc", m)):
242                 Logs.info("  %s: yes" % (m))
243                 conf.env.ctdb_prebuilt_manpages.append(m)
244             else:
245                 Logs.info("  %s: no" % (m))
246
247 def build(bld):
248     if bld.env.standalone_ctdb:
249         # enable building of public headers in the build tree
250         bld.env.build_public_headers = 'include/public'
251
252     version_h = samba_utils.os_path_relpath(os.path.join(Options.launch_dir,
253                                                          "version.h"),
254                                             bld.curdir)
255
256     if bld.env.standalone_ctdb:
257         ctdb_mkversion = '../packaging/mkversion.sh'
258         t = bld.SAMBA_GENERATOR('ctdb-version-header',
259                                 target='include/ctdb_version.h',
260                                 rule='%s ${TGT} %s' % (ctdb_mkversion, VERSION),
261                                 dep_vars=['VERSION'])
262         t.env.VERSION = VERSION
263
264         t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
265                                 target=version_h,
266                                 rule='printf "#include \\"ctdb_version.h\\" \\n#define SAMBA_VERSION_STRING CTDB_VERSION_STRING\\n" > ${TGT}',
267                                 dep_vars=['VERSION'])
268         t.env.VERSION = VERSION
269     else:
270         t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
271                                 target='include/ctdb_version.h',
272                                 rule='printf "#include \\"%s\\" \\n#define CTDB_VERSION_STRING SAMBA_VERSION_STRING\\n" > ${TGT}' % version_h,
273                                 dep_vars=['VERSION'])
274         t.env.VERSION = VERSION
275
276     bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'
277
278     bld.RECURSE('lib/replace')
279     if bld.CHECK_FOR_THIRD_PARTY():
280         bld.RECURSE('third_party/popt')
281
282     bld.RECURSE('lib/tdb_wrap')
283     bld.RECURSE('lib/util')
284     bld.RECURSE('lib/async_req')
285
286     bld.RECURSE('lib/talloc')
287     bld.RECURSE('lib/tevent')
288     bld.RECURSE('lib/tdb')
289     if bld.env.standalone_ctdb or bld.CONFIG_GET('SOCKET_WRAPPER'):
290         bld.RECURSE('lib/socket_wrapper')
291
292     if bld.env.standalone_ctdb:
293         # If a combined build is implemented, CTDB will want to
294         # build against samba-util rather than samba-util-core.
295         # Similarly, other Samba subsystems expect samba-util.  So,
296         # for a standalone build, just define a fake samba-util
297         # subsystem that pulls in samba-util-core.
298         bld.SAMBA_SUBSYSTEM('samba-util',
299                             source='',
300                             deps='samba-util-core')
301
302     bld.SAMBA_SUBSYSTEM('ctdb-tcp',
303                         source=bld.SUBDIR('tcp',
304                                           'tcp_connect.c tcp_init.c tcp_io.c'),
305                         includes='include',
306                         deps='replace tdb talloc tevent')
307
308     ib_deps = ''
309     if bld.env.HAVE_INFINIBAND:
310         bld.SAMBA_SUBSYSTEM('ctdb-ib',
311                             source=bld.SUBDIR('ib',
312                                               '''ibwrapper.c ibw_ctdb.c
313                                                  ibw_ctdb_init.c'''),
314                             includes='include',
315                             deps='replace talloc tevent tdb')
316         ib_deps = ' ctdb-ib rdmacm ibverbs'
317
318     if sys.platform.startswith('linux'):
319         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_linux.c')
320     elif sys.platform.startswith('aix'):
321         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_aix.c')
322     elif sys.platform.startswith('freebsd'):
323         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_freebsd.c')
324     elif sys.platform.startswith('gnukfreebsd'):
325         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_kfreebsd.c')
326     elif sys.platform == 'gnu':
327         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_gnu.c')
328     else:
329         Logs.error("Platform %s not supported" % sys.platform)
330
331     bld.SAMBA_SUBSYSTEM('ctdb-system',
332                         source=bld.SUBDIR('common',
333                                           'system_common.c system_util.c') +
334                                CTDB_SYSTEM_SRC,
335                         includes='include',
336                         deps='replace talloc tevent tdb pcap samba-util')
337
338     bld.SAMBA_SUBSYSTEM('ctdb-common',
339                         source=bld.SUBDIR('common',
340                                           '''ctdb_io.c ctdb_util.c ctdb_ltdb.c
341                                           '''),
342                         includes='include',
343                         deps='replace popt talloc tevent tdb popt ctdb-system')
344
345     bld.SAMBA_SUBSYSTEM('ctdb-util',
346                         source=bld.SUBDIR('common',
347                                           '''db_hash.c srvid.c reqid.c
348                                              pkt_read.c pkt_write.c comm.c
349                                              logging.c rb_tree.c tunable.c
350                                              pidfile.c'''),
351                         deps='''samba-util sys_rw tevent-util
352                                 replace talloc tevent tdb''')
353
354     bld.SAMBA_SUBSYSTEM('ctdb-protocol',
355                         source=bld.SUBDIR('protocol',
356                                           '''protocol_header.c protocol_packet.c
357                                              protocol_types.c protocol_call.c
358                                              protocol_message.c
359                                              protocol_control.c
360                                              protocol_client.c
361                                              protocol_debug.c
362                                              protocol_util.c'''),
363                         includes='include',
364                         deps='replace talloc tdb')
365
366     bld.SAMBA_SUBSYSTEM('ctdb-client',
367                         source=bld.SUBDIR('client', 'ctdb_client.c'),
368                         includes='include',
369                         deps='''replace popt talloc tevent tdb
370                                 samba-util tdb-wrap ctdb-util''')
371
372     bld.SAMBA_SUBSYSTEM('ctdb-client2',
373                         source=bld.SUBDIR('client',
374                                           '''client_connect.c client_call.c
375                                              client_message.c client_control.c
376                                              client_message_sync.c
377                                              client_control_sync.c
378                                              client_db.c client_util.c
379                                           '''),
380                         includes='include',
381                         deps='replace talloc tevent tdb tdb-wrap')
382
383     bld.SAMBA_SUBSYSTEM('ctdb-ipalloc',
384                         source=bld.SUBDIR('server',
385                                           '''ipalloc_deterministic.c
386                                              ipalloc_nondeterministic.c
387                                              ipalloc_lcp2.c
388                                              ipalloc_common.c
389                                              ipalloc.c
390                                           '''),
391                         includes='include',
392                         deps='ctdb-protocol replace talloc tevent')
393
394     bld.SAMBA_SUBSYSTEM('ctdb-server',
395                         source='server/ctdbd.c ' +
396                                bld.SUBDIR('server',
397                                           '''ctdb_daemon.c ctdb_recoverd.c
398                                              ctdb_recover.c ctdb_freeze.c
399                                              ctdb_tunables.c ctdb_monitor.c
400                                              ctdb_server.c ctdb_control.c
401                                              ctdb_call.c ctdb_ltdb_server.c
402                                              ctdb_traverse.c eventscript.c
403                                              ctdb_takeover.c
404                                              ctdb_persistent.c ctdb_keepalive.c
405                                              ctdb_cluster_mutex.c
406                                              ctdb_logging.c
407                                              ctdb_logging_syslog.c
408                                              ctdb_logging_file.c
409                                              ctdb_uptime.c
410                                              ctdb_vacuum.c ctdb_banning.c
411                                              ctdb_statistics.c
412                                              ctdb_update_record.c
413                                              ctdb_lock.c ctdb_fork.c'''),
414                         includes='include',
415                         deps='''ctdb-ipalloc replace sys_rw
416                                 popt talloc tevent tdb talloc_report''')
417
418     bld.SAMBA_BINARY('ctdbd',
419                      source='',
420                      deps='''ctdb-server ctdb-client ctdb-common
421                              ctdb-system ctdb-tcp ctdb-util''' +
422                           ib_deps,
423                      install_path='${SBINDIR}',
424                      manpages='ctdbd.1')
425
426     bld.SAMBA_BINARY('ctdb',
427                      source='tools/ctdb.c',
428                      deps='''ctdb-client2 ctdb-protocol ctdb-util ctdb-system
429                              samba-util sys_rw popt''',
430                      install_path='${BINDIR}',
431                      manpages='ctdb.1')
432
433     bld.SAMBA_BINARY('ctdb_killtcp',
434                      source='tools/ctdb_killtcp.c',
435                      deps='''ctdb-protocol ctdb-util ctdb-system
436                              samba-util replace''',
437                      install_path='${CTDB_HELPER_BINDIR}')
438
439     bld.SAMBA_BINARY('ltdbtool',
440                      source='tools/ltdbtool.c',
441                      includes='include',
442                      deps='tdb',
443                      install_path='${BINDIR}',
444                      manpages='ltdbtool.1')
445
446     bld.SAMBA_BINARY('ctdb_lock_helper',
447                      source='server/ctdb_lock_helper.c',
448                      deps='samba-util sys_rw ctdb-system talloc tdb',
449                      includes='include',
450                      install_path='${CTDB_HELPER_BINDIR}')
451
452     bld.SAMBA_BINARY('ctdb_event_helper',
453                      source='server/ctdb_event_helper.c',
454                      includes='include',
455                      deps='samba-util sys_rw ctdb-system replace tdb',
456                      install_path='${CTDB_HELPER_BINDIR}')
457
458     bld.SAMBA_BINARY('ctdb_recovery_helper',
459                      source='server/ctdb_recovery_helper.c',
460                      deps='''ctdb-client2 ctdb-protocol ctdb-util
461                              samba-util sys_rw replace tdb''',
462                      install_path='${CTDB_HELPER_BINDIR}')
463
464     bld.SAMBA_BINARY('ctdb_mutex_fcntl_helper',
465                      source='server/ctdb_mutex_fcntl_helper.c',
466                      deps='sys_rw ctdb-system',
467                      includes='include',
468                      install_path='${CTDB_HELPER_BINDIR}')
469
470     bld.SAMBA_GENERATOR('ctdb-smnotify-h',
471                         source='utils/smnotify/smnotify.x',
472                         target='utils/smnotify/smnotify.h',
473                         rule='rpcgen -h ${SRC} > ${TGT}')
474
475     xdr_buf_hack = 'sed -e "s@^\([ \t]*register int32_t \*buf\);@\\1 = buf;@"'
476
477     bld.SAMBA_GENERATOR('ctdb-smnotify-x',
478                         source='utils/smnotify/smnotify.x',
479                         target='utils/smnotify/gen_xdr.c',
480                         rule='rpcgen -c ${SRC} | ' + xdr_buf_hack + ' > ${TGT}')
481
482     bld.SAMBA_GENERATOR('ctdb-smnotify-c',
483                         source='utils/smnotify/smnotify.x',
484                         target='utils/smnotify/gen_smnotify.c',
485                         rule='rpcgen -l ${SRC} > ${TGT}')
486
487     bld.SAMBA_BINARY('smnotify',
488                      source=bld.SUBDIR('utils/smnotify',
489                                        'smnotify.c gen_smnotify.c gen_xdr.c'),
490                      deps='ctdb-smnotify-h ctdb-smnotify-c ctdb-smnotify-x popt',
491                      includes='utils utils/smnotify',
492                      install_path='${CTDB_HELPER_BINDIR}')
493
494     bld.SAMBA_BINARY('ping_pong',
495                      source='utils/ping_pong/ping_pong.c',
496                      deps='',
497                      install_path='${BINDIR}',
498                      manpages='ping_pong.1')
499
500     if bld.env.HAVE_PMDA:
501         bld.SAMBA_BINARY('pmdactdb',
502                          source='utils/pmda/pmda_ctdb.c',
503                          includes='include',
504                          deps='''ctdb-client2 ctdb-protocol ctdb-util
505                                  samba-util pcp_pmda pcp''',
506                          install_path='${CTDB_PMDADIR}')
507         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/Install',
508                           destname='Install')
509         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/Remove',
510                           destname='Remove')
511         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/pmns',
512                           destname='pmns')
513         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/domain.h',
514                           destname='domain.h')
515         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/help',
516                           destname='help')
517         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/README',
518                           destname='README')
519
520     sed_expr1 = 's|/usr/local/var/lib/ctdb|%s|g'  % (bld.env.CTDB_VARDIR)
521     sed_expr2 = 's|/usr/local/etc/ctdb|%s|g'      % (bld.env.CTDB_ETCDIR)
522     sed_expr3 = 's|/usr/local/var/log|%s|g'       % (bld.env.CTDB_LOGDIR)
523     sed_expr4 = 's|/usr/local/var/run/ctdb|%s|g'  % (bld.env.CTDB_RUNDIR)
524     sed_expr5 = 's|/usr/local/sbin|%s|g'          % (bld.env.SBINDIR)
525     sed_expr6 = 's|/usr/local/libexec/ctdb|%s|g'  % (bld.env.CTDB_HELPER_BINDIR)
526     sed_expr7 = 's|/usr/local/bin|%s|g'           % (bld.env.BINDIR)
527     sed_cmdline = '-e "%s" -e "%s" -e "%s" -e "%s" -e "%s" -e "%s" -e "%s"' % \
528                        (sed_expr1, sed_expr2, sed_expr3, sed_expr4, sed_expr5,
529                         sed_expr6, sed_expr7)
530
531     for f in manpages:
532         x = '%s.xml' % (f)
533         bld.SAMBA_GENERATOR(x,
534                             source=os.path.join('doc', x),
535                             target=x,
536                             rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
537
538     if bld.env.ctdb_generate_manpages:
539         bld.MANPAGES('''onnode.1 ctdbd_wrapper.1 ctdbd.conf.5
540                         ctdb.7 ctdb-statistics.7 ctdb-tunables.7
541                         ctdb_diagnostics.1''',
542                       True)
543     else:
544         for m in bld.env.ctdb_prebuilt_manpages:
545             bld.SAMBA_GENERATOR(m,
546                                 source=os.path.join("doc", m),
547                                 target=m,
548                                 rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
549             bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m)
550
551     bld.SAMBA_GENERATOR('ctdb-onnode',
552                         source='tools/onnode',
553                         target='onnode',
554                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
555     bld.INSTALL_FILES('${BINDIR}', 'onnode',
556                       destname='onnode', chmod=0755)
557
558     bld.SAMBA_GENERATOR('ctdb-diagnostics',
559                         source='tools/ctdb_diagnostics',
560                         target='ctdb_diagnostics',
561                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
562     bld.INSTALL_FILES('${BINDIR}', 'ctdb_diagnostics',
563                       destname='ctdb_diagnostics', chmod=0755)
564
565     bld.SAMBA_GENERATOR('ctdb-natgw',
566                         source='tools/ctdb_natgw',
567                         target='ctdb_natgw',
568                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
569     bld.INSTALL_FILES('${CTDB_HELPER_BINDIR}', 'ctdb_natgw',
570                       destname='ctdb_natgw', chmod=0755)
571
572     bld.SAMBA_GENERATOR('ctdb-lvs',
573                         source='tools/ctdb_lvs',
574                         target='ctdb_lvs',
575                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
576     bld.INSTALL_FILES('${CTDB_HELPER_BINDIR}', 'ctdb_lvs',
577                       destname='ctdb_lvs', chmod=0755)
578
579     bld.SAMBA_GENERATOR('ctdbd-wrapper',
580                         source='config/ctdbd_wrapper',
581                         target='ctdbd_wrapper',
582                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
583     bld.INSTALL_FILES('${SBINDIR}', 'ctdbd_wrapper',
584                       destname='ctdbd_wrapper', chmod=0755)
585
586     def SUBDIR_MODE_callback(arg, dirname, fnames):
587         for f in fnames:
588             fl = os.path.join(dirname, f)
589             if os.path.isdir(fl) or os.path.islink(fl):
590                 continue
591             mode = os.lstat(fl).st_mode & 0777
592             if arg['trim_path']:
593                 fl = samba_utils.os_path_relpath(fl, arg['trim_path'])
594             arg['file_list'].append([fl, mode])
595
596     def SUBDIR_MODE(path, trim_path=None):
597         pd = {'trim_path': trim_path, 'file_list': []}
598         os.path.walk(path, SUBDIR_MODE_callback, pd)
599         return pd['file_list']
600
601     etc_subdirs = [
602         'events.d',
603         'nfs-checks.d'
604     ]
605
606     if bld.env.standalone_ctdb:
607         configdir = 'config'
608     else:
609         configdir = 'ctdb/config'
610
611     for t in etc_subdirs:
612         files = SUBDIR_MODE('%s/%s' % (configdir, t), trim_path=configdir)
613         for fmode in files:
614             bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % fmode[0],
615                               destname=fmode[0], chmod=fmode[1])
616
617     bld.SAMBA_GENERATOR('ctdb-functions',
618                         source='config/functions',
619                         target='functions',
620                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
621     bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'functions', destname='functions')
622
623     etc_scripts = [
624         'ctdb-crash-cleanup.sh',
625         'debug-hung-script.sh',
626         'debug_locks.sh',
627         'gcore_trace.sh',
628         'nfs-linux-kernel-callout',
629         'notify.sh',
630         'statd-callout'
631     ]
632
633     for t in etc_scripts:
634         bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % t,
635                           destname=t, chmod=0755)
636
637     bld.SAMBA_GENERATOR('ctdb-sudoers',
638                         source='config/ctdb.sudoers',
639                         target='ctdb.sudoers',
640                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
641     bld.INSTALL_FILES('${SYSCONFDIR}/sudoers.d', 'ctdb.sudoers',
642                       destname='ctdb')
643
644     bld.INSTALL_FILES('${CTDB_ETCDIR}/notify.d', 'config/notify.d.README',
645                       destname='README')
646
647     bld.install_dir(bld.env.CTDB_LOGDIR)
648     bld.install_dir(bld.env.CTDB_RUNDIR)
649     bld.install_dir(bld.env.CTDB_VARDIR)
650
651     # Unit tests
652     ctdb_unit_tests = [
653         'db_hash_test',
654         'srvid_test',
655         'pkt_read_test',
656         'pkt_write_test',
657         'comm_test',
658         'comm_server_test',
659         'comm_client_test',
660         'protocol_types_test',
661         'protocol_client_test',
662         'pidfile_test',
663     ]
664
665     for target in ctdb_unit_tests:
666         src = 'tests/src/' + target + '.c'
667
668         bld.SAMBA_BINARY(target,
669                          source=src,
670                          deps='''talloc tevent tdb tevent-util
671                                  LIBASYNC_REQ samba-util''',
672                          install_path='${CTDB_TEST_LIBEXECDIR}')
673
674     bld.SAMBA_BINARY('reqid_test',
675                      source='tests/src/reqid_test.c',
676                      deps='samba-util',
677                      install_path='${CTDB_TEST_LIBEXECDIR}')
678
679     bld.SAMBA_BINARY('rb_test',
680                      source='tests/src/rb_test.c',
681                      deps='samba-util talloc',
682                      install_path='${CTDB_TEST_LIBEXECDIR}')
683
684     bld.SAMBA_BINARY('ctdb_packet_parse',
685                      source='tests/src/ctdb_packet_parse.c',
686                      deps='talloc tevent tdb ctdb-protocol',
687                      install_path='${CTDB_TEST_LIBEXECDIR}')
688
689     bld.SAMBA_BINARY('porting_tests',
690                      source='tests/src/porting_tests.c',
691                      deps='samba-util ctdb-system popt',
692                      install_path='${CTDB_TEST_LIBEXECDIR}')
693
694
695     bld.SAMBA_SUBSYSTEM('ctdb-tests-common',
696                         source=bld.SUBDIR('tests/src',
697                                           'cluster_wait.c test_options.c'),
698                         includes='include',
699                         deps='replace popt talloc tevent tdb')
700
701     ctdb_tests = [
702         'g_lock_loop',
703         'message_ring',
704         'fetch_ring',
705         'fetch_loop',
706         'fetch_loop_key',
707         'fetch_readonly',
708         'fetch_readonly_loop',
709         'transaction_loop',
710         'update_record',
711         'update_record_persistent',
712         'lock_tdb'
713     ]
714
715     for target in ctdb_tests:
716         src = 'tests/src/' + target + '.c'
717
718         bld.SAMBA_BINARY(target,
719                          source=src,
720                          includes='include',
721                          deps='''ctdb-client2 ctdb-protocol ctdb-util
722                                  samba-util ctdb-tests-common''',
723                          install_path='${CTDB_TEST_LIBEXECDIR}')
724
725     bld.SAMBA_BINARY('ctdb_takeover_tests',
726                      source='tests/src/ctdb_takeover_tests.c',
727                      deps='''replace popt tdb tevent talloc ctdb-system
728                              samba-util tdb-wrap talloc_report
729                              ctdb-ipalloc ctdb-protocol ctdb-util''',
730                      includes='include',
731                      install_path='${CTDB_TEST_LIBEXECDIR}')
732
733     bld.SAMBA_BINARY('fake_ctdbd',
734                      source='tests/src/fake_ctdbd.c',
735                      deps='''ctdb-util ctdb-protocol ctdb-system
736                              samba-util tevent-util LIBASYNC_REQ popt''',
737                      install_path='${CTDB_TEST_LIBEXECDIR}')
738
739     if bld.env.HAVE_INFINIBAND:
740         bld.SAMBA_BINARY('ibwrapper_test',
741                          source='ib/ibwrapper_test.c',
742                          includes='include',
743                          deps='replace talloc ctdb-client ctdb-common sys_rw' +
744                               ib_deps,
745                          install_path='${CTDB_TEST_LIBEXECDIR}')
746
747     test_subdirs = [
748         'complex',
749         'cunit',
750         'events.d',
751         'eventscripts',
752         'onnode',
753         'shellcheck',
754         'simple',
755         'takeover',
756         'tool'
757     ]
758
759     for t in test_subdirs:
760         files = SUBDIR_MODE('tests/%s' % t, trim_path='tests')
761         for fmode in files:
762             bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR, 'tests/%s' % fmode[0],
763                               destname=fmode[0], chmod=fmode[1])
764
765     # Install tests/scripts directory without test_wrap
766     test_scripts = [
767         'common.sh',
768         'integration.bash',
769         'unit.sh'
770     ]
771
772     for t in test_scripts:
773         bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR,
774                           os.path.join('tests/scripts', t),
775                           destname=os.path.join('scripts', t))
776
777     sed_expr = 's@^TEST_SCRIPTS_DIR=.*@&\\nexport TEST_BIN_DIR=\"%s\"@' % (
778                bld.env.CTDB_TEST_LIBEXECDIR)
779     bld.SAMBA_GENERATOR('ctdb-test-wrap',
780                         source='tests/scripts/test_wrap',
781                         target='test_wrap',
782                         rule='sed -e "%s" ${SRC} > ${TGT}' % sed_expr)
783     bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR+"/scripts", 'test_wrap',
784                       destname='test_wrap', chmod=0755)
785
786     bld.SAMBA_GENERATOR('ctdb-test-script-install-paths',
787                         source='tests/scripts/script_install_paths.sh',
788                         target='script_install_paths.sh',
789                         rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
790     bld.INSTALL_FILES(bld.env.CTDB_TEST_DATADIR+"/scripts",
791                       'script_install_paths.sh',
792                       destname='script_install_paths.sh', chmod=0644)
793
794     sed_expr1 = 's@^test_dir=.*@test_dir=%s\\nexport TEST_BIN_DIR=\"%s\"@' % (
795                 bld.env.CTDB_TEST_DATADIR, bld.env.CTDB_TEST_LIBEXECDIR)
796     sed_expr2 = 's@^\(export CTDB_TESTS_ARE_INSTALLED\)=false@\\1=true@'
797     bld.SAMBA_GENERATOR('ctdb-test-runner',
798                         source='tests/run_tests.sh',
799                         target='ctdb_run_tests.sh',
800                         rule='sed -e "%s" -e "%s" ${SRC} > ${TGT}' % (
801                              sed_expr1, sed_expr2))
802     bld.INSTALL_FILES('${BINDIR}', 'ctdb_run_tests.sh',
803                       destname='ctdb_run_tests', chmod=0755)
804     bld.symlink_as(os.path.join(bld.env.BINDIR, 'ctdb_run_cluster_tests'),
805                    'ctdb_run_tests')
806
807     test_eventscript_links = [
808         'events.d',
809         'functions',
810         'nfs-checks.d',
811         'nfs-linux-kernel-callout',
812         'statd-callout'
813     ]
814
815     test_link_dir = os.path.join(bld.env.CTDB_TEST_DATADIR,
816                                  'eventscripts/etc-ctdb')
817     for t in test_eventscript_links:
818         bld.symlink_as(os.path.join(test_link_dir, t),
819                        os.path.join(bld.env.CTDB_ETCDIR, t))
820
821     # Tests that use onnode need to overwrite link to in-tree
822     # functions file when installed
823     bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'onnode/functions'),
824                    os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
825     bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/functions'),
826                    os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
827
828     # Need a link to nodes file because $CTDB_BASE is overridden
829     bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/nodes'),
830                    os.path.join(bld.env.CTDB_ETCDIR, 'nodes'))
831
832
833 def testonly(ctx):
834     cmd = 'tests/run_tests.sh -V tests/var'
835     ret = samba_utils.RUN_COMMAND(cmd)
836     if ret != 0:
837         print('tests exited with exit status %d' % ret)
838         sys.exit(ret)
839
840
841 def test(ctx):
842     import Scripting
843     Scripting.commands.append('build')
844     Scripting.commands.append('testonly')
845
846
847 def autotest(ctx):
848     env = samba_utils.LOAD_ENVIRONMENT()
849     ld = 'LD_PRELOAD=%s' % env.SOCKET_WRAPPER_SO_PATH
850     cmd = '%s tests/run_tests.sh -e -S -C' % ld
851     ret = samba_utils.RUN_COMMAND(cmd)
852     if ret != 0:
853         print('autotest exited with exit status %d' % ret)
854         sys.exit(ret)
855
856
857 def show_version(ctx):
858     print VERSION
859
860
861 def dist():
862     samba_dist.DIST_FILES('VERSION:VERSION', extend=True)
863
864     t = 'include/ctdb_version.h'
865     cmd = 'packaging/mkversion.sh %s %s' % (t, VERSION)
866     ret = samba_utils.RUN_COMMAND(cmd)
867     if ret != 0:
868         print('Command "%s" failed with exit status %d' % (cmd, ret))
869         sys.exit(ret)
870     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
871
872     t = 'ctdb.spec'
873     sed_expr1 = 's/@VERSION@/%s/g' % VERSION
874     sed_expr2 = 's/@RELEASE@/%s/g' % '1'
875     cmd = 'sed -e "%s" -e "%s" packaging/RPM/ctdb.spec.in > %s' % (
876         sed_expr1, sed_expr2, t)
877     ret = samba_utils.RUN_COMMAND(cmd)
878     if ret != 0:
879         print('Command "%s" failed with exit status %d' % (cmd, ret))
880         sys.exit(ret)
881     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
882
883     cmd = 'make -C doc'
884     ret = samba_utils.RUN_COMMAND(cmd)
885     if ret != 0:
886         print('Command "%s" failed with exit status %d' % (cmd, ret))
887         sys.exit(ret)
888     for t in manpages:
889         samba_dist.DIST_FILES('ctdb/doc/%s:doc/%s' % (t, t), extend=True)
890         samba_dist.DIST_FILES('ctdb/doc/%s.html:doc/%s.html' % (t, t),
891                               extend=True)
892
893     samba_dist.dist()
894
895
896 def rpmonly(ctx):
897     opts = os.getenv('RPM_OPTIONS') or ''
898     cmd = 'rpmbuild -ta --clean --rmsource %s ctdb-%s.tar.gz' % (opts, VERSION)
899     ret = samba_utils.RUN_COMMAND(cmd)
900     if ret != 0:
901         print('rpmbuild exited with exit status %d' % ret)
902         sys.exit(ret)
903
904
905 def rpm(ctx):
906     import Scripting
907     Scripting.commands.append('dist')
908     Scripting.commands.append('rpmonly')
909
910
911 def ctags(ctx):
912     "build 'tags' file using ctags"
913     import Utils
914     source_root = os.path.dirname(Utils.g_module.root_path)
915     cmd = 'ctags $(find %s -name "*.[ch]")' % source_root
916     print("Running: %s" % cmd)
917     ret = samba_utils.RUN_COMMAND(cmd)
918     if ret != 0:
919         print('ctags failed with exit status %d' % ret)
920         sys.exit(ret)