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