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