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