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