Move waf into third_party/.
[vlendec/samba-autobuild/.git] / lib / replace / wscript
1 #!/usr/bin/env python
2
3 APPNAME = 'libreplace'
4 VERSION = '1.2.1'
5
6 blddir = 'bin'
7
8 import sys, os
9
10 # find the buildtools directory
11 srcdir = '.'
12 while not os.path.exists(srcdir+'/buildtools') and len(srcdir.split('/')) < 5:
13     srcdir = srcdir + '/..'
14 sys.path.insert(0, srcdir + '/buildtools/wafsamba')
15
16 import wafsamba, samba_dist
17 import Options
18
19 samba_dist.DIST_DIRS('lib/replace buildtools:buildtools third_party/waf:third_party/waf')
20
21 def set_options(opt):
22     opt.BUILTIN_DEFAULT('NONE')
23     opt.PRIVATE_EXTENSION_DEFAULT('')
24     opt.RECURSE('buildtools/wafsamba')
25
26 @wafsamba.runonce
27 def configure(conf):
28     conf.RECURSE('buildtools/wafsamba')
29
30     conf.env.standalone_replace = conf.IN_LAUNCH_DIR()
31
32     conf.DEFINE('HAVE_LIBREPLACE', 1)
33     conf.DEFINE('LIBREPLACE_NETWORK_CHECKS', 1)
34
35     conf.CHECK_HEADERS('linux/types.h crypt.h locale.h acl/libacl.h compat.h')
36     conf.CHECK_HEADERS('acl/libacl.h attr/xattr.h compat.h ctype.h dustat.h')
37     conf.CHECK_HEADERS('fcntl.h fnmatch.h glob.h history.h krb5.h langinfo.h')
38     conf.CHECK_HEADERS('libaio.h locale.h ndir.h pwd.h')
39     conf.CHECK_HEADERS('shadow.h sys/acl.h')
40     conf.CHECK_HEADERS('sys/attributes.h attr/attributes.h sys/capability.h sys/dir.h sys/epoll.h')
41     conf.CHECK_HEADERS('port.h')
42     conf.CHECK_HEADERS('sys/fcntl.h sys/filio.h sys/filsys.h sys/fs/s5param.h sys/fs/vx/quota.h')
43     conf.CHECK_HEADERS('sys/id.h sys/ioctl.h sys/ipc.h sys/mman.h sys/mode.h sys/ndir.h sys/priv.h')
44     conf.CHECK_HEADERS('sys/resource.h sys/security.h sys/shm.h sys/statfs.h sys/statvfs.h sys/termio.h')
45     conf.CHECK_HEADERS('sys/vfs.h sys/xattr.h termio.h termios.h sys/file.h')
46     conf.CHECK_HEADERS('sys/ucontext.h sys/wait.h sys/stat.h malloc.h grp.h')
47     conf.CHECK_HEADERS('sys/select.h setjmp.h utime.h sys/syslog.h syslog.h')
48     conf.CHECK_HEADERS('stdarg.h vararg.h sys/mount.h mntent.h')
49     conf.CHECK_HEADERS('stropts.h unix.h string.h strings.h sys/param.h limits.h')
50     conf.CHECK_HEADERS('''sys/socket.h netinet/in.h netdb.h arpa/inet.h netinet/in_systm.h
51                           netinet/ip.h netinet/tcp.h netinet/in_ip.h
52                           sys/sockio.h sys/un.h''', together=True)
53     conf.CHECK_HEADERS('sys/uio.h ifaddrs.h direct.h dirent.h')
54     conf.CHECK_HEADERS('windows.h winsock2.h ws2tcpip.h')
55     conf.CHECK_HEADERS('errno.h')
56     conf.CHECK_HEADERS('getopt.h iconv.h')
57     conf.CHECK_HEADERS('memory.h nss.h sasl/sasl.h')
58
59     conf.CHECK_FUNCS_IN('inotify_init', 'inotify', checklibc=True,
60                         headers='sys/inotify.h')
61
62     conf.CHECK_HEADERS('security/pam_appl.h zlib.h asm/unistd.h')
63     conf.CHECK_HEADERS('aio.h sys/unistd.h rpc/rpc.h rpc/nettype.h alloca.h float.h')
64
65     conf.CHECK_HEADERS('rpcsvc/nis.h rpcsvc/ypclnt.h sys/sysctl.h')
66     conf.CHECK_HEADERS('sys/fileio.h sys/filesys.h sys/dustat.h sys/sysmacros.h')
67     conf.CHECK_HEADERS('xfs/libxfs.h netgroup.h')
68
69     conf.CHECK_CODE('', headers='rpc/rpc.h rpcsvc/yp_prot.h', define='HAVE_RPCSVC_YP_PROT_H')
70
71     conf.CHECK_HEADERS('valgrind.h valgrind/valgrind.h valgrind/memcheck.h')
72     conf.CHECK_HEADERS('nss_common.h nsswitch.h ns_api.h')
73     conf.CHECK_HEADERS('sys/extattr.h sys/ea.h sys/proplist.h sys/cdefs.h')
74     conf.CHECK_HEADERS('utmp.h utmpx.h lastlog.h malloc.h')
75     conf.CHECK_HEADERS('syscall.h sys/syscall.h inttypes.h')
76     conf.CHECK_HEADERS('sys/atomic.h')
77
78     # Check for process set name support
79     conf.CHECK_CODE('''
80                     #include <sys/prctl.h>
81                     int main(void) {
82                         prctl(0);
83                         return 0;
84                     }
85                     ''',
86                     'HAVE_PRCTL',
87                     headers='sys/prctl.h',
88                     msg='Checking for prctl syscall')
89
90     conf.CHECK_CODE('''
91                     #include <unistd.h>
92                     #ifdef HAVE_FCNTL_H
93                     #include <fcntl.h>
94                     #endif
95                     int main(void) { int fd = open("/dev/null", O_DIRECT); }
96                     ''',
97                     define='HAVE_OPEN_O_DIRECT',
98                     addmain=False,
99                     msg='Checking for O_DIRECT flag to open(2)')
100
101     conf.CHECK_TYPES('"long long" intptr_t uintptr_t ptrdiff_t comparison_fn_t')
102     conf.CHECK_TYPE('_Bool', define='HAVE__Bool')
103     conf.CHECK_TYPE('bool', define='HAVE_BOOL')
104
105     conf.CHECK_TYPE('int8_t', 'char')
106     conf.CHECK_TYPE('uint8_t', 'unsigned char')
107     conf.CHECK_TYPE('int16_t', 'short')
108     conf.CHECK_TYPE('uint16_t', 'unsigned short')
109     conf.CHECK_TYPE('int32_t', 'int')
110     conf.CHECK_TYPE('uint32_t', 'unsigned')
111     conf.CHECK_TYPE('int64_t', 'long long')
112     conf.CHECK_TYPE('uint64_t', 'unsigned long long')
113     conf.CHECK_TYPE('size_t', 'unsigned int')
114     conf.CHECK_TYPE('ssize_t', 'int')
115     conf.CHECK_TYPE('ino_t', 'unsigned')
116     conf.CHECK_TYPE('loff_t', 'off_t')
117     conf.CHECK_TYPE('offset_t', 'loff_t')
118     conf.CHECK_TYPE('volatile int', define='HAVE_VOLATILE')
119     conf.CHECK_TYPE('uint_t', 'unsigned int')
120     conf.CHECK_TYPE('blksize_t', 'long', headers='sys/types.h sys/stat.h unistd.h')
121     conf.CHECK_TYPE('blkcnt_t', 'long', headers='sys/types.h sys/stat.h unistd.h')
122
123     conf.CHECK_SIZEOF('bool char int "long long" long short size_t ssize_t')
124     conf.CHECK_SIZEOF('int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t')
125     conf.CHECK_SIZEOF('void*', define='SIZEOF_VOID_P')
126     conf.CHECK_SIZEOF('off_t dev_t ino_t time_t')
127
128     conf.CHECK_TYPES('socklen_t', headers='sys/socket.h')
129     conf.CHECK_TYPE_IN('struct ifaddrs', 'ifaddrs.h')
130     conf.CHECK_TYPE_IN('struct addrinfo', 'netdb.h')
131     conf.CHECK_TYPE_IN('struct sockaddr', 'sys/socket.h')
132     conf.CHECK_CODE('struct sockaddr_in6 x', define='HAVE_STRUCT_SOCKADDR_IN6',
133                     headers='sys/socket.h netdb.h netinet/in.h')
134     conf.CHECK_TYPE_IN('struct sockaddr_storage', 'sys/socket.h')
135     conf.CHECK_TYPE_IN('sa_family_t', 'sys/socket.h')
136
137     conf.CHECK_TYPE_IN('sig_atomic_t', 'signal.h', define='HAVE_SIG_ATOMIC_T_TYPE')
138
139     conf.CHECK_FUNCS_IN('''inet_ntoa inet_aton inet_ntop inet_pton connect gethostbyname
140                            getaddrinfo getnameinfo freeaddrinfo gai_strerror socketpair''',
141                         'socket nsl', checklibc=True,
142                         headers='sys/socket.h netinet/in.h arpa/inet.h netdb.h')
143
144     # Some old Linux systems have broken header files and
145     # miss the IPV6_V6ONLY define in netinet/in.h,
146     # but have it in linux/in6.h.
147     # We can't include both files so we just check if the value
148     # if defined and do the replacement in system/network.h
149     if not conf.CHECK_VARIABLE('IPV6_V6ONLY',
150                                headers='sys/socket.h netdb.h netinet/in.h'):
151         conf.CHECK_CODE('''
152                         #include <linux/in6.h>
153                         #if (IPV6_V6ONLY != 26)
154                         #error no IPV6_V6ONLY support on linux
155                         #endif
156                         int main(void) { return IPV6_V6ONLY; }
157                         ''',
158                         define='HAVE_LINUX_IPV6_V6ONLY_26',
159                         addmain=False,
160                         msg='Checking for IPV6_V6ONLY in linux/in6.h',
161                         local_include=False)
162
163     conf.CHECK_CODE('''
164                        struct sockaddr_storage sa_store;
165                        struct addrinfo *ai = NULL;
166                        struct in6_addr in6addr;
167                        int idx = if_nametoindex("iface1");
168                        int s = socket(AF_INET6, SOCK_STREAM, 0);
169                        int ret = getaddrinfo(NULL, NULL, NULL, &ai);
170                        if (ret != 0) {
171                            const char *es = gai_strerror(ret);
172                        }
173                        freeaddrinfo(ai);
174                        {
175                           int val = 1;
176                           #ifdef HAVE_LINUX_IPV6_V6ONLY_26
177                           #define IPV6_V6ONLY 26
178                           #endif
179                           ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
180                                            (const void *)&val, sizeof(val));
181                        }
182                        ''',
183                     define='HAVE_IPV6',
184                     lib='nsl socket',
185                     headers='sys/socket.h netdb.h netinet/in.h')
186
187     if conf.CONFIG_SET('HAVE_SYS_UCONTEXT_H') and conf.CONFIG_SET('HAVE_SIGNAL_H'):
188         conf.CHECK_CODE('''
189                        ucontext_t uc;
190                        sigaddset(&uc.uc_sigmask, SIGUSR1);
191                        ''',
192                        'HAVE_UCONTEXT_T',
193                        msg="Checking whether we have ucontext_t",
194                        headers='signal.h sys/ucontext.h')
195
196     # Check for atomic builtins. */
197     conf.CHECK_CODE('''
198                     int main(void) {
199                         int i;
200                         (void)__sync_fetch_and_add(&i, 1);
201                         return 0;
202                     }
203                     ''',
204                     'HAVE___SYNC_FETCH_AND_ADD',
205                     msg='Checking for __sync_fetch_and_add compiler builtin')
206
207     conf.CHECK_CODE('''
208                     #include <stdint.h>
209                     #include <sys/atomic.h>
210                     int main(void) {
211                         int32_t i;
212                         atomic_add_32(&i, 1);
213                         return 0;
214                     }
215                     ''',
216                     'HAVE_ATOMIC_ADD_32',
217                     headers='stdint.h sys/atomic.h',
218                     msg='Checking for atomic_add_32 compiler builtin')
219
220     # these may be builtins, so we need the link=False strategy
221     conf.CHECK_FUNCS('strdup memmem printf memset memcpy memmove strcpy strncpy bzero', link=False)
222
223     # See https://bugzilla.samba.org/show_bug.cgi?id=1097
224     #
225     # Ported in from autoconf where it was added with this commit:
226     # commit 804cfb20a067b4b687089dc72a8271b3abf20f31
227     # Author: Simo Sorce <idra@samba.org>
228     # Date:   Wed Aug 25 14:24:16 2004 +0000
229     #     r2070: Let's try to overload srnlen and strndup for AIX where they are natly broken.
230
231     host_os = sys.platform
232     if host_os.rfind('aix') > -1:
233         conf.DEFINE('BROKEN_STRNLEN', 1)
234         conf.DEFINE('BROKEN_STRNDUP', 1)
235
236     conf.CHECK_FUNCS('shl_load shl_unload shl_findsym')
237     conf.CHECK_FUNCS('pipe strftime srandom random srand rand usleep setbuffer')
238     conf.CHECK_FUNCS('lstat getpgrp utime utimes setuid seteuid setreuid setresuid setgid setegid')
239     conf.CHECK_FUNCS('setregid setresgid chroot strerror vsyslog setlinebuf mktime')
240     conf.CHECK_FUNCS('ftruncate chsize rename waitpid wait4')
241     conf.CHECK_FUNCS('initgroups pread pwrite strndup strcasestr')
242     conf.CHECK_FUNCS('strtok_r mkdtemp dup2 dprintf vdprintf isatty chown lchown')
243     conf.CHECK_FUNCS('link readlink symlink realpath snprintf vsnprintf')
244     conf.CHECK_FUNCS('asprintf vasprintf setenv unsetenv strnlen strtoull __strtoull')
245     conf.CHECK_FUNCS('strtouq strtoll __strtoll strtoq memalign posix_memalign')
246     conf.CHECK_FUNCS('prctl')
247
248     # libbsd on some platforms provides strlcpy and strlcat
249     if not conf.CHECK_FUNCS('strlcpy strlcat'):
250         conf.CHECK_FUNCS_IN('strlcpy strlcat', 'bsd', headers='bsd/string.h',
251                 checklibc=True)
252     if not conf.CHECK_FUNCS('getpeereid'):
253         conf.CHECK_FUNCS_IN('getpeereid', 'bsd', headers='sys/types.h bsd/unistd.h')
254     if not conf.CHECK_FUNCS_IN('setproctitle', 'setproctitle', headers='setproctitle.h'):
255         conf.CHECK_FUNCS_IN('setproctitle', 'bsd', headers='sys/types.h bsd/unistd.h')
256
257     conf.CHECK_CODE('''
258                 struct ucred cred;
259                 socklen_t cred_len;
260                 int ret = getsockopt(0, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len);''',
261                 'HAVE_PEERCRED',
262                 msg="Checking whether we can use SO_PEERCRED to get socket credentials",
263                 headers='sys/types.h sys/socket.h')
264
265     #Some OS (ie. freebsd) return EINVAL if the convertion could not be done, it's not what we expect
266     #Let's detect those cases
267     if conf.CONFIG_SET('HAVE_STRTOLL'):
268         conf.CHECK_CODE('''
269                         long long nb = strtoll("Text", NULL, 0);
270                         if (errno == EINVAL) {
271                             return 0;
272                         } else {
273                             return 1;
274                         }
275                         ''',
276                         msg="Checking correct behavior of strtoll",
277                         headers = 'errno.h',
278                         execute = True,
279                         define = 'HAVE_BSD_STRTOLL',
280                         )
281     conf.CHECK_FUNCS('if_nametoindex strerror_r')
282     conf.CHECK_FUNCS('getdirentries getdents syslog')
283     conf.CHECK_FUNCS('gai_strerror get_current_dir_name')
284     conf.CHECK_FUNCS('timegm getifaddrs freeifaddrs mmap setgroups syscall setsid')
285     conf.CHECK_FUNCS('getgrent_r getgrgid_r getgrnam_r getgrouplist getpagesize')
286     conf.CHECK_FUNCS('getpwent_r getpwnam_r getpwuid_r epoll_create')
287     conf.CHECK_FUNCS('port_create')
288
289     conf.SET_TARGET_TYPE('attr', 'EMPTY')
290
291     xattr_headers='sys/attributes.h attr/xattr.h sys/xattr.h'
292
293     conf.CHECK_FUNCS_IN('''
294 fgetxattr flistea flistxattr
295 fremovexattr fsetxattr getxattr
296 listxattr removexattr setxattr
297 ''', 'attr', checklibc=True, headers=xattr_headers)
298
299     # We need to check for linux xattrs first, as we do not wish to link to -lattr
300     # (the XFS compat API) on Linux systems with the native xattr API
301     if not conf.CONFIG_SET('HAVE_GETXATTR'):
302         conf.CHECK_FUNCS_IN('''
303 attr_get attr_getf attr_list attr_listf attropen attr_remove
304 attr_removef attr_set attr_setf extattr_delete_fd extattr_delete_file
305 extattr_get_fd extattr_get_file extattr_list_fd extattr_list_file
306 extattr_set_fd extattr_set_file fgetea
307 fremoveea fsetea getea listea
308 removeea setea
309 ''', 'attr', checklibc=True, headers=xattr_headers)
310
311     if (conf.CONFIG_SET('HAVE_ATTR_LISTF') or
312         conf.CONFIG_SET('HAVE_EXTATTR_LIST_FD') or
313         conf.CONFIG_SET('HAVE_FLISTEA') or
314         conf.CONFIG_SET('HAVE_FLISTXATTR')):
315             conf.DEFINE('HAVE_XATTR_SUPPORT', 1)
316
317     # Darwin has extra options to xattr-family functions
318     conf.CHECK_CODE('getxattr(NULL, NULL, NULL, 0, 0, 0)',
319                     headers=xattr_headers, local_include=False,
320                     define='XATTR_ADDITIONAL_OPTIONS',
321                     msg="Checking whether xattr interface takes additional options")
322
323     conf.CHECK_FUNCS_IN('dlopen dlsym dlerror dlclose', 'dl',
324                         checklibc=True, headers='dlfcn.h dl.h')
325
326     conf.CHECK_C_PROTOTYPE('dlopen', 'void *dlopen(const char* filename, unsigned int flags)',
327                            define='DLOPEN_TAKES_UNSIGNED_FLAGS', headers='dlfcn.h dl.h')
328
329     if conf.CHECK_FUNCS_IN('fdatasync', 'rt', checklibc=True):
330         # some systems are missing the declaration
331         conf.CHECK_DECLS('fdatasync')
332
333     if conf.CHECK_FUNCS_IN('clock_gettime', 'rt', checklibc=True):
334         for c in ['CLOCK_MONOTONIC', 'CLOCK_PROCESS_CPUTIME_ID', 'CLOCK_REALTIME']:
335             conf.CHECK_CODE('''
336                 #if TIME_WITH_SYS_TIME
337                 # include <sys/time.h>
338                 # include <time.h>
339                 #else
340                 # if HAVE_SYS_TIME_H
341                 #  include <sys/time.h>
342                 # else
343                 #  include <time.h>
344                 # endif
345                 #endif
346                 clockid_t clk = %s''' % c,
347                 'HAVE_%s' % c,
348                 msg='Checking whether the clock_gettime clock ID %s is available' % c)
349
350     conf.CHECK_TYPE('struct timespec', headers='sys/time.h time.h')
351
352     # these headers need to be tested as a group on freebsd
353     conf.CHECK_HEADERS(headers='sys/socket.h net/if.h', together=True)
354     conf.CHECK_HEADERS(headers='netinet/in.h arpa/nameser.h resolv.h', together=True)
355     conf.CHECK_FUNCS_IN('res_search', 'resolv', checklibc=True,
356                         headers='netinet/in.h arpa/nameser.h resolv.h')
357
358
359     # try to find libintl (if --without-gettext is not given)
360     conf.env.intl_libs=''
361     if not Options.options.disable_gettext:
362         # any extra path given to look at?
363         if not Options.options.gettext_location == 'None':
364            conf.env['CFLAGS'].extend(["-I%s" % Options.options.gettext_location]);
365            conf.env['LDFLAGS'].extend(["-L%s" % Options.options.gettext_location]);
366         else:
367            conf.env['CFLAGS'].extend(["-I/usr/local"]);
368            conf.env['LDFLAGS'].extend(["-L/usr/local"]);
369         conf.CHECK_HEADERS('libintl.h')
370         conf.CHECK_LIB('intl')
371         conf.CHECK_DECLS('dgettext gettext bindtextdomain textdomain bind_textdomain_codeset', headers="libintl.h")
372         # *textdomain functions are not strictly necessary
373         conf.CHECK_FUNCS_IN('bindtextdomain textdomain bind_textdomain_codeset',
374                             '', checklibc=True, headers='libintl.h')
375         # gettext and dgettext must exist
376         # on some systems (the ones with glibc, those are in libc)
377         if conf.CHECK_FUNCS_IN('dgettext gettext', '', checklibc=True, headers='libintl.h'):
378             # save for dependency definitions
379             conf.env.intl_libs=''
380         # others (e.g. FreeBSD) have seperate libintl
381         elif conf.CHECK_FUNCS_IN('dgettext gettext', 'intl', checklibc=False, headers='libintl.h'):
382             # save for dependency definitions
383             conf.env.intl_libs='intl'
384             # recheck with libintl
385             conf.CHECK_FUNCS_IN('bindtextdomain textdomain bind_textdomain_codeset',
386                             'intl', checklibc=False, headers='libintl.h')
387         else:
388             # Some hosts need lib iconv for linking with lib intl
389             # So we try with flags just in case it helps.
390             oldflags = list(conf.env['EXTRA_LDFLAGS']);
391             conf.env['EXTRA_LDFLAGS'].extend(["-liconv"])
392             conf.CHECK_FUNCS_IN('dgettext gettext bindtextdomain textdomain bind_textdomain_codeset',
393                                 'intl', checklibc=False, headers='libintl.h')
394             conf.env['EXTRA_LDFLAGS'] = oldflags
395             if conf.env['HAVE_GETTEXT'] and conf.env['HAVE_DGETTEXT']:
396                 # save for dependency definitions
397                 conf.env.intl_libs='iconv intl'
398
399     # did we find both prototypes and a library to link against?
400     # if not, unset the detected values (see Bug #9911)
401     if not (conf.env['HAVE_GETTEXT'] and conf.env['HAVE_DECL_GETTEXT']):
402        conf.undefine('HAVE_GETTEXT')
403        conf.undefine('HAVE_DECL_GETTEXT')
404     if not (conf.env['HAVE_DGETTEXT'] and conf.env['HAVE_DECL_DGETTEXT']):
405        conf.undefine('HAVE_DGETTEXT')
406        conf.undefine('HAVE_DECL_DGETTEXT')
407
408     # did the user insist on gettext (--with-gettext)?
409     if Options.options.gettext_location != 'None' and (not conf.env['HAVE_GETTEXT'] or not conf.env['HAVE_DGETTEXT']):
410         conf.fatal('library gettext not found at specified location')
411
412     conf.CHECK_FUNCS_IN('pthread_create', 'pthread', checklibc=True, headers='pthread.h')
413
414     PTHREAD_CFLAGS='error'
415     PTHREAD_LDFLAGS='error'
416
417     if PTHREAD_LDFLAGS == 'error':
418         if conf.CHECK_FUNCS_IN('pthread_attr_init', 'pthread'):
419             PTHREAD_CFLAGS='-D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS'
420             PTHREAD_LDFLAGS='-lpthread'
421     if PTHREAD_LDFLAGS == 'error':
422         if conf.CHECK_FUNCS_IN('pthread_attr_init', 'pthreads'):
423             PTHREAD_CFLAGS='-D_THREAD_SAFE'
424             PTHREAD_LDFLAGS='-lpthreads'
425     if PTHREAD_LDFLAGS == 'error':
426         if conf.CHECK_FUNCS_IN('pthread_attr_init', 'c_r'):
427             PTHREAD_CFLAGS='-D_THREAD_SAFE -pthread'
428             PTHREAD_LDFLAGS='-pthread'
429     if PTHREAD_LDFLAGS == 'error':
430         if conf.CHECK_FUNCS('pthread_attr_init'):
431             PTHREAD_CFLAGS='-D_REENTRANT'
432             PTHREAD_LDFLAGS='-lpthread'
433     # especially for HP-UX, where the CHECK_FUNC macro fails to test for
434     # pthread_attr_init. On pthread_mutex_lock it works there...
435     if PTHREAD_LDFLAGS == 'error':
436         if conf.CHECK_FUNCS_IN('pthread_mutex_lock', 'pthread'):
437             PTHREAD_CFLAGS='-D_REENTRANT'
438             PTHREAD_LDFLAGS='-lpthread'
439
440     if PTHREAD_CFLAGS != 'error' and PTHREAD_LDFLAGS != 'error':
441         if conf.CONFIG_SET('replace_add_global_pthread'):
442             conf.ADD_CFLAGS(PTHREAD_CFLAGS)
443             conf.ADD_LDFLAGS(PTHREAD_LDFLAGS)
444         conf.CHECK_HEADERS('pthread.h')
445         conf.DEFINE('HAVE_PTHREAD', '1')
446
447     if conf.CONFIG_SET('HAVE_PTHREAD'):
448
449         conf.CHECK_DECLS('pthread_mutexattr_setrobust', headers='pthread.h')
450         if not conf.CONFIG_SET('HAVE_DECL_PTHREAD_MUTEXATTR_SETROBUST'):
451             conf.CHECK_DECLS('pthread_mutexattr_setrobust_np',
452                              headers='pthread.h')
453
454         conf.CHECK_FUNCS_IN('pthread_mutexattr_setrobust', 'pthread',
455                             checklibc=True, headers='pthread.h')
456         if not conf.CONFIG_SET('HAVE_PTHREAD_MUTEXATTR_SETROBUST'):
457             conf.CHECK_FUNCS_IN('pthread_mutexattr_setrobust_np', 'pthread',
458                                 checklibc=True, headers='pthread.h')
459
460         conf.CHECK_DECLS('pthread_mutex_consistent', headers='pthread.h')
461         if not conf.CONFIG_SET('HAVE_DECL_PTHREAD_MUTEX_CONSISTENT'):
462             conf.CHECK_DECLS('pthread_mutex_consistent_np',
463                              headers='pthread.h')
464
465         conf.CHECK_FUNCS_IN('pthread_mutex_consistent', 'pthread',
466                             checklibc=True, headers='pthread.h')
467         if not conf.CONFIG_SET('HAVE_PTHREAD_MUTEX_CONSISTENT'):
468             conf.CHECK_FUNCS_IN('pthread_mutex_consistent_np', 'pthread',
469                                 checklibc=True, headers='pthread.h')
470
471         if ((conf.CONFIG_SET('HAVE_PTHREAD_MUTEXATTR_SETROBUST') or
472              conf.CONFIG_SET('HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP')) and
473             (conf.CONFIG_SET('HAVE_PTHREAD_MUTEX_CONSISTENT') or
474              conf.CONFIG_SET('HAVE_PTHREAD_MUTEX_CONSISTENT_NP'))):
475             conf.DEFINE('HAVE_ROBUST_MUTEXES', 1)
476
477     conf.CHECK_FUNCS_IN('crypt', 'crypt', checklibc=True)
478
479     conf.CHECK_VARIABLE('rl_event_hook', define='HAVE_DECL_RL_EVENT_HOOK', always=True,
480                         headers='readline.h readline/readline.h readline/history.h')
481
482     conf.CHECK_DECLS('snprintf vsnprintf asprintf vasprintf')
483
484     conf.CHECK_DECLS('errno', headers='errno.h', reverse=True)
485     conf.CHECK_DECLS('EWOULDBLOCK', headers='errno.h')
486     conf.CHECK_DECLS('environ getgrent_r getpwent_r', reverse=True, headers='pwd.h grp.h')
487     conf.CHECK_DECLS('pread pwrite setenv setresgid setresuid', reverse=True)
488
489     if conf.CONFIG_SET('HAVE_EPOLL_CREATE') and conf.CONFIG_SET('HAVE_SYS_EPOLL_H'):
490         conf.DEFINE('HAVE_EPOLL', 1)
491
492     if conf.CONFIG_SET('HAVE_PORT_CREATE') and conf.CONFIG_SET('HAVE_PORT_H'):
493         conf.DEFINE('HAVE_SOLARIS_PORTS', 1)
494
495     conf.CHECK_HEADERS('poll.h')
496     conf.CHECK_FUNCS('poll')
497
498     conf.CHECK_FUNCS('strptime')
499     conf.CHECK_DECLS('strptime', headers='time.h')
500     conf.CHECK_CODE('''#define LIBREPLACE_CONFIGURE_TEST_STRPTIME
501                        #include "test/strptime.c"''',
502                        define='HAVE_WORKING_STRPTIME',
503                        execute=True,
504                        addmain=False,
505                        msg='Checking for working strptime')
506
507     conf.CHECK_CODE('gettimeofday(NULL, NULL)', 'HAVE_GETTIMEOFDAY_TZ', execute=False)
508
509     conf.CHECK_CODE('#include "test/snprintf.c"',
510                     define="HAVE_C99_VSNPRINTF",
511                     execute=True,
512                     addmain=False,
513                     msg="Checking for C99 vsnprintf")
514
515     conf.CHECK_CODE('#include "test/shared_mmap.c"',
516                     addmain=False, add_headers=False, execute=True,
517                     define='HAVE_SHARED_MMAP',
518                     msg="Checking for HAVE_SHARED_MMAP")
519
520     conf.CHECK_CODE('#include "test/shared_mremap.c"',
521                     addmain=False, add_headers=False, execute=True,
522                     define='HAVE_MREMAP',
523                     msg="Checking for HAVE_MREMAP")
524
525     # OpenBSD (and I've heard HPUX) doesn't sync between mmap and write.
526     # FIXME: Anything other than a 0 or 1 exit code should abort configure!
527     conf.CHECK_CODE('#include "test/incoherent_mmap.c"',
528                     addmain=False, add_headers=False, execute=True,
529                     define='HAVE_INCOHERENT_MMAP',
530                     msg="Checking for HAVE_INCOHERENT_MMAP")
531
532     conf.SAMBA_BUILD_ENV()
533
534     conf.CHECK_CODE('''
535                     typedef struct {unsigned x;} FOOBAR;
536                     #define X_FOOBAR(x) ((FOOBAR) { x })
537                     #define FOO_ONE X_FOOBAR(1)
538                     FOOBAR f = FOO_ONE;
539                     static const struct {
540                         FOOBAR y;
541                     } f2[] = {
542                         {FOO_ONE}
543                     };
544                     static const FOOBAR f3[] = {FOO_ONE};
545                     ''',
546                     define='HAVE_IMMEDIATE_STRUCTURES')
547
548     conf.CHECK_CODE('mkdir("foo",0777)', define='HAVE_MKDIR_MODE', headers='sys/stat.h')
549
550     conf.CHECK_STRUCTURE_MEMBER('struct stat', 'st_mtim.tv_nsec', define='HAVE_STAT_TV_NSEC',
551                                 headers='sys/stat.h')
552     # we need the st_rdev test under two names
553     conf.CHECK_STRUCTURE_MEMBER('struct stat', 'st_rdev',
554                                 define='HAVE_STRUCT_STAT_ST_RDEV',
555                                 headers='sys/stat.h')
556     conf.CHECK_STRUCTURE_MEMBER('struct stat', 'st_rdev', define='HAVE_ST_RDEV',
557                                 headers='sys/stat.h')
558     conf.CHECK_STRUCTURE_MEMBER('struct sockaddr_storage', 'ss_family',
559                                 headers='sys/socket.h netinet/in.h')
560     conf.CHECK_STRUCTURE_MEMBER('struct sockaddr_storage', '__ss_family',
561                                 headers='sys/socket.h netinet/in.h')
562
563
564     if conf.CHECK_STRUCTURE_MEMBER('struct sockaddr', 'sa_len',
565                                    headers='sys/socket.h netinet/in.h',
566                                    define='HAVE_SOCKADDR_SA_LEN'):
567         # the old build system produced both defines
568         conf.DEFINE('HAVE_STRUCT_SOCKADDR_SA_LEN', 1)
569
570     conf.CHECK_STRUCTURE_MEMBER('struct sockaddr_in', 'sin_len',
571                                 headers='sys/socket.h netinet/in.h',
572                                 define='HAVE_SOCK_SIN_LEN')
573
574     conf.CHECK_CODE('struct sockaddr_un sunaddr; sunaddr.sun_family = AF_UNIX;',
575                     define='HAVE_UNIXSOCKET', headers='sys/socket.h sys/un.h')
576
577
578     conf.CHECK_CODE('''
579                     struct stat st;
580                     char tpl[20]="/tmp/test.XXXXXX";
581                     char tpl2[20]="/tmp/test.XXXXXX";
582                     int fd = mkstemp(tpl);
583                     int fd2 = mkstemp(tpl2);
584                     if (fd == -1) {
585                           if (fd2 != -1) {
586                                   unlink(tpl2);
587                           }
588                           exit(1);
589                     }
590                     if (fd2 == -1) exit(1);
591                     unlink(tpl);
592                     unlink(tpl2);
593                     if (fstat(fd, &st) != 0) exit(1);
594                     if ((st.st_mode & 0777) != 0600) exit(1);
595                     if (strcmp(tpl, "/tmp/test.XXXXXX") == 0) {
596                           exit(1);
597                     }
598                     if (strcmp(tpl, tpl2) == 0) {
599                           exit(1);
600                     }
601                     exit(0);
602                     ''',
603                     define='HAVE_SECURE_MKSTEMP',
604                     execute=True,
605                     mandatory=True) # lets see if we get a mandatory failure for this one
606
607     # look for a method of finding the list of network interfaces
608     for method in ['HAVE_IFACE_GETIFADDRS', 'HAVE_IFACE_AIX', 'HAVE_IFACE_IFCONF', 'HAVE_IFACE_IFREQ']:
609         if conf.CHECK_CODE('''
610                            #define %s 1
611                            #define NO_CONFIG_H 1
612                            #define AUTOCONF_TEST 1
613                            #include "replace.c"
614                            #include "inet_ntop.c"
615                            #include "snprintf.c"
616                            #include "getifaddrs.c"
617                            #define getifaddrs_test main
618                            #include "test/getifaddrs.c"
619                            ''' % method,
620                            method,
621                            lib='nsl socket',
622                            addmain=False,
623                            execute=True):
624             break
625
626     conf.RECURSE('system')
627     conf.SAMBA_CONFIG_H()
628
629
630 REPLACEMENT_FUNCTIONS = {
631     'replace.c': ['ftruncate', 'strlcpy', 'strlcat', 'mktime', 'initgroups',
632                   'memmove', 'strdup', 'setlinebuf', 'vsyslog', 'strnlen',
633                   'strndup', 'waitpid', 'seteuid', 'setegid', 'chroot',
634                   'mkstemp', 'mkdtemp', 'pread', 'pwrite', 'strcasestr',
635                   'strtok_r', 'strtoll', 'strtoull', 'setenv', 'unsetenv',
636                   'utime', 'utimes', 'dup2', 'chown', 'link', 'readlink',
637                   'symlink', 'lchown', 'realpath', 'memmem', 'vdprintf',
638                   'dprintf', 'get_current_dir_name',
639                   'strerror_r', 'clock_gettime'],
640     'timegm.c': ['timegm'],
641     # Note: C99_VSNPRINTF is not a function, but a special condition
642     # for replacement
643     'snprintf.c': ['C99_VSNPRINTF', 'snprintf', 'vsnprintf', 'asprintf', 'vasprintf'],
644     # Note: WORKING_STRPTIME is not a function, but a special condition
645     # for replacement
646     'strptime.c': ['WORKING_STRPTIME', 'strptime'],
647     }
648
649
650 def build(bld):
651     bld.RECURSE('buildtools/wafsamba')
652
653     REPLACE_HOSTCC_SOURCE = ''
654
655     for filename, functions in REPLACEMENT_FUNCTIONS.iteritems():
656         for function in functions:
657             if not bld.CONFIG_SET('HAVE_%s' % function.upper()):
658                 REPLACE_HOSTCC_SOURCE += ' %s' % filename
659                 break
660
661     extra_libs = ''
662     if bld.CONFIG_SET('HAVE_LIBBSD'): extra_libs += ' bsd'
663
664     bld.SAMBA_SUBSYSTEM('LIBREPLACE_HOSTCC',
665         REPLACE_HOSTCC_SOURCE,
666         use_hostcc=True,
667         use_global_deps=False,
668         cflags='-D_SAMBA_HOSTCC_',
669         group='compiler_libraries',
670         deps = extra_libs
671     )
672
673     REPLACE_SOURCE = REPLACE_HOSTCC_SOURCE
674     REPLACE_SOURCE += ' cwrap.c'
675
676     if not bld.CONFIG_SET('HAVE_CRYPT'):         REPLACE_SOURCE += ' crypt.c'
677     if not bld.CONFIG_SET('HAVE_DLOPEN'):        REPLACE_SOURCE += ' dlfcn.c'
678     if not bld.CONFIG_SET('HAVE_POLL'):          REPLACE_SOURCE += ' poll.c'
679
680     if not bld.CONFIG_SET('HAVE_SOCKETPAIR'):    REPLACE_SOURCE += ' socketpair.c'
681     if not bld.CONFIG_SET('HAVE_CONNECT'):       REPLACE_SOURCE += ' socket.c'
682     if not bld.CONFIG_SET('HAVE_GETIFADDRS'):    REPLACE_SOURCE += ' getifaddrs.c'
683     if not bld.CONFIG_SET('HAVE_GETADDRINFO'):   REPLACE_SOURCE += ' getaddrinfo.c'
684     if not bld.CONFIG_SET('HAVE_INET_NTOA'):     REPLACE_SOURCE += ' inet_ntoa.c'
685     if not bld.CONFIG_SET('HAVE_INET_ATON'):     REPLACE_SOURCE += ' inet_aton.c'
686     if not bld.CONFIG_SET('HAVE_INET_NTOP'):     REPLACE_SOURCE += ' inet_ntop.c'
687     if not bld.CONFIG_SET('HAVE_INET_PTON'):     REPLACE_SOURCE += ' inet_pton.c'
688     if not bld.CONFIG_SET('HAVE_GETXATTR') or bld.CONFIG_SET('XATTR_ADDITIONAL_OPTIONS'):
689                                                  REPLACE_SOURCE += ' xattr.c'
690
691     bld.SAMBA_LIBRARY('replace',
692                       source=REPLACE_SOURCE,
693                       group='base_libraries',
694                       # FIXME: Ideally symbols should be hidden here so they 
695                       # don't appear in the global namespace when Samba 
696                       # libraries are loaded, but this doesn't appear to work 
697                       # at the moment:
698                       # hide_symbols=bld.BUILTIN_LIBRARY('replace'),
699                       private_library=True,
700                       deps='crypt dl nsl socket rt attr' + extra_libs)
701
702     bld.SAMBA_SUBSYSTEM('replace-test',
703                       source='''test/testsuite.c test/strptime.c
704                       test/os2_delete.c test/getifaddrs.c''',
705                       deps='replace')
706
707     if bld.env.standalone_replace:
708         bld.SAMBA_BINARY('replace_testsuite',
709                          source='test/main.c',
710                          deps='replace replace-test',
711                          install=False)
712
713     # build replacements for stdint.h and stdbool.h if needed
714     bld.SAMBA_GENERATOR('replace_stdint_h',
715                         rule='cp ${SRC} ${TGT}',
716                         source='hdr_replace.h',
717                         target='stdint.h',
718                         enabled = not bld.CONFIG_SET('HAVE_STDINT_H'))
719     bld.SAMBA_GENERATOR('replace_stdbool_h',
720                         rule='cp ${SRC} ${TGT}',
721                         source='hdr_replace.h',
722                         target='stdbool.h',
723                         enabled = not bld.CONFIG_SET('HAVE_STDBOOL_H'))
724
725     bld.SAMBA_SUBSYSTEM('samba_intl', source='', use_global_deps=False,deps=bld.env.intl_libs)
726
727 def dist():
728     '''makes a tarball for distribution'''
729     samba_dist.dist()