0d716e0abe03310b98a0e74be1f46089aa44072f
[sfrench/samba-autobuild/.git] / lib / replace / libreplace.m4
1 AC_DEFUN_ONCE(AC_LIBREPLACE_LOCATION_CHECKS,
2 [
3 echo "LIBREPLACE_LOCATION_CHECKS: START"
4
5 dnl find the libreplace sources. This is meant to work both for 
6 dnl libreplace standalone builds, and builds of packages using libreplace
7 libreplacedir=""
8 libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
9 for d in $libreplacepaths; do
10         if test -f "$d/replace.c"; then
11                 libreplacedir="$d"              
12                 AC_SUBST(libreplacedir)
13                 break;
14         fi
15 done
16 if test x"$libreplacedir" = "x"; then
17         AC_MSG_ERROR([cannot find libreplace in $libreplacepaths])
18 fi
19 LIBREPLACEOBJ="$libreplacedir/replace.o"
20 AC_SUBST(LIBREPLACEOBJ)
21
22 AC_CANONICAL_BUILD
23 AC_CANONICAL_HOST
24 AC_CANONICAL_TARGET
25
26 echo "LIBREPLACE_LOCATION_CHECKS: END"
27 ]) dnl end AC_LIBREPLACE_LOCATION_CHECKS
28
29
30 AC_DEFUN_ONCE(AC_LIBREPLACE_BROKEN_CHECKS,
31 [
32 echo "LIBREPLACE_BROKEN_CHECKS: START"
33
34 dnl find the libreplace sources. This is meant to work both for 
35 dnl libreplace standalone builds, and builds of packages using libreplace
36 libreplacedir=""
37 libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
38 for d in $libreplacepaths; do
39         if test -f "$d/replace.c"; then
40                 libreplacedir="$d"              
41                 AC_SUBST(libreplacedir)
42                 break;
43         fi
44 done
45 if test x"$libreplacedir" = "x"; then
46         AC_MSG_ERROR([cannot find libreplace in $libreplacepaths])
47 fi
48
49 LIBREPLACEOBJ="$libreplacedir/replace.o"
50 AC_SUBST(LIBREPLACEOBJ)
51
52 LIBREPLACEOBJ="${LIBREPLACEOBJ} $libreplacedir/snprintf.o"
53
54 AC_TYPE_UID_T
55 AC_TYPE_MODE_T
56 AC_TYPE_OFF_T
57 AC_TYPE_SIZE_T
58 AC_TYPE_PID_T
59 AC_STRUCT_ST_RDEV
60 AC_CHECK_TYPE(ino_t,unsigned)
61 AC_CHECK_TYPE(loff_t,off_t)
62 AC_CHECK_TYPE(offset_t,loff_t)
63
64 AC_FUNC_MEMCMP
65
66 AC_CHECK_FUNCS([pipe strftime srandom random srand rand usleep setbuffer lstat getpgrp utime utimes])
67
68 AC_CHECK_HEADERS(stdbool.h stdint.h sys/select.h)
69 AC_CHECK_HEADERS(setjmp.h utime.h)
70
71 LIBREPLACE_PROVIDE_HEADER([stdint.h])
72 LIBREPLACE_PROVIDE_HEADER([stdbool.h])
73
74 AC_CHECK_TYPE(bool, 
75 [AC_DEFINE(HAVE_BOOL, 1, [Whether the bool type is available])],,
76 [
77 AC_INCLUDES_DEFAULT
78 #ifdef HAVE_STDBOOL_H
79 #include <stdbool.h>
80 #endif]
81 )
82
83 AC_CHECK_TYPE(_Bool, 
84 [AC_DEFINE(HAVE__Bool, 1, [Whether the _Bool type is available])],,
85 [
86 AC_INCLUDES_DEFAULT
87 #ifdef HAVE_STDBOOL_H
88 #include <stdbool.h>
89 #endif]
90 )
91
92 AC_CACHE_CHECK([for working mmap],libreplace_cv_HAVE_MMAP,[
93 AC_TRY_RUN([#include "$libreplacedir/test/shared_mmap.c"],
94            libreplace_cv_HAVE_MMAP=yes,libreplace_cv_HAVE_MMAP=no,libreplace_cv_HAVE_MMAP=cross)])
95 if test x"$libreplace_cv_HAVE_MMAP" = x"yes"; then
96     AC_DEFINE(HAVE_MMAP,1,[Whether mmap works])
97 fi
98
99
100 AC_CHECK_HEADERS(sys/syslog.h syslog.h)
101 AC_CHECK_HEADERS(sys/time.h time.h)
102 AC_CHECK_HEADERS(stdarg.h vararg.h)
103 AC_CHECK_HEADERS(sys/mount.h mntent.h)
104 AC_CHECK_HEADERS(stropts.h)
105 AC_CHECK_HEADERS(unix.h)
106
107 AC_CHECK_FUNCS(seteuid setresuid setegid setresgid chroot bzero strerror strerror_r)
108 AC_CHECK_FUNCS(vsyslog setlinebuf mktime ftruncate chsize rename)
109 AC_CHECK_FUNCS(waitpid wait4 strlcpy strlcat initgroups memmove strdup)
110 AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2 dprintf vdprintf)
111 AC_CHECK_FUNCS(isatty chown lchown link readlink symlink realpath)
112 AC_CHECK_FUNCS(fdatasync,,[
113         # if we didn't find it, look in librt (Solaris hides it there...)
114         AC_CHECK_LIB(rt, fdatasync,
115                 [libreplace_cv_HAVE_FDATASYNC_IN_LIBRT=yes
116                 AC_DEFINE(HAVE_FDATASYNC, 1, Define to 1 if there is support for fdatasync)])
117 ])
118 AC_CHECK_FUNCS(clock_gettime,libreplace_cv_have_clock_gettime=yes,[
119         AC_CHECK_LIB(rt, clock_gettime,
120                 [libreplace_cv_HAVE_CLOCK_GETTIME_IN_LIBRT=yes
121                 libreplace_cv_have_clock_gettime=yes
122                 AC_DEFINE(HAVE_CLOCK_GETTIME, 1, Define to 1 if there is support for clock_gettime)])
123 ])
124 AC_CHECK_FUNCS(get_current_dir_name)
125 AC_HAVE_DECL(setresuid, [#include <unistd.h>])
126 AC_HAVE_DECL(setresgid, [#include <unistd.h>])
127 AC_HAVE_DECL(errno, [#include <errno.h>])
128
129 AC_CACHE_CHECK([for secure mkstemp],libreplace_cv_HAVE_SECURE_MKSTEMP,[
130 AC_TRY_RUN([#include <stdlib.h>
131 #include <sys/types.h>
132 #include <sys/stat.h>
133 #include <unistd.h>
134 main() { 
135   struct stat st;
136   char tpl[20]="/tmp/test.XXXXXX"; 
137   int fd = mkstemp(tpl); 
138   if (fd == -1) exit(1);
139   unlink(tpl);
140   if (fstat(fd, &st) != 0) exit(1);
141   if ((st.st_mode & 0777) != 0600) exit(1);
142   exit(0);
143 }],
144 libreplace_cv_HAVE_SECURE_MKSTEMP=yes,
145 libreplace_cv_HAVE_SECURE_MKSTEMP=no,
146 libreplace_cv_HAVE_SECURE_MKSTEMP=cross)])
147 if test x"$libreplace_cv_HAVE_SECURE_MKSTEMP" = x"yes"; then
148     AC_DEFINE(HAVE_SECURE_MKSTEMP,1,[Whether mkstemp is secure])
149 fi
150
151 dnl Provided by snprintf.c:
152 AC_CHECK_HEADERS(stdio.h strings.h)
153 AC_CHECK_DECLS([snprintf, vsnprintf, asprintf, vasprintf])
154 AC_CHECK_FUNCS(snprintf vsnprintf asprintf vasprintf)
155
156 AC_CACHE_CHECK([for C99 vsnprintf],libreplace_cv_HAVE_C99_VSNPRINTF,[
157 AC_TRY_RUN([
158 #include <sys/types.h>
159 #include <stdio.h>
160 #include <stdarg.h>
161 #include <stdlib.h>
162 void foo(const char *format, ...) { 
163        va_list ap;
164        int len;
165        char buf[20];
166        long long l = 1234567890;
167        l *= 100;
168
169        va_start(ap, format);
170        len = vsnprintf(buf, 0, format, ap);
171        va_end(ap);
172        if (len != 5) exit(1);
173
174        va_start(ap, format);
175        len = vsnprintf(0, 0, format, ap);
176        va_end(ap);
177        if (len != 5) exit(2);
178
179        if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(3);
180
181        if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(4);
182        if (snprintf(buf, 20, "%zu", 123456789) != 9 || strcmp(buf, "123456789") != 0) exit(5);
183        if (snprintf(buf, 20, "%2\$d %1\$d", 3, 4) != 3 || strcmp(buf, "4 3") != 0) exit(6);
184        if (snprintf(buf, 20, "%s", 0) < 3) exit(7);
185
186        exit(0);
187 }
188 main() { foo("hello"); }
189 ],
190 libreplace_cv_HAVE_C99_VSNPRINTF=yes,libreplace_cv_HAVE_C99_VSNPRINTF=no,libreplace_cv_HAVE_C99_VSNPRINTF=cross)])
191 if test x"$libreplace_cv_HAVE_C99_VSNPRINTF" = x"yes"; then
192     AC_DEFINE(HAVE_C99_VSNPRINTF,1,[Whether there is a C99 compliant vsnprintf])
193 fi
194
195
196 dnl VA_COPY
197 AC_CACHE_CHECK([for va_copy],libreplace_cv_HAVE_VA_COPY,[
198 AC_TRY_LINK([#include <stdarg.h>
199 va_list ap1,ap2;], [va_copy(ap1,ap2);],
200 libreplace_cv_HAVE_VA_COPY=yes,libreplace_cv_HAVE_VA_COPY=no)])
201 if test x"$libreplace_cv_HAVE_VA_COPY" = x"yes"; then
202     AC_DEFINE(HAVE_VA_COPY,1,[Whether va_copy() is available])
203 fi
204
205 if test x"$libreplace_cv_HAVE_VA_COPY" != x"yes"; then
206 AC_CACHE_CHECK([for __va_copy],libreplace_cv_HAVE___VA_COPY,[
207 AC_TRY_LINK([#include <stdarg.h>
208 va_list ap1,ap2;], [__va_copy(ap1,ap2);],
209 libreplace_cv_HAVE___VA_COPY=yes,libreplace_cv_HAVE___VA_COPY=no)])
210 if test x"$libreplace_cv_HAVE___VA_COPY" = x"yes"; then
211     AC_DEFINE(HAVE___VA_COPY,1,[Whether __va_copy() is available])
212 fi
213 fi
214
215 dnl __FUNCTION__ macro
216 AC_CACHE_CHECK([for __FUNCTION__ macro],libreplace_cv_HAVE_FUNCTION_MACRO,[
217 AC_TRY_COMPILE([#include <stdio.h>], [printf("%s\n", __FUNCTION__);],
218 libreplace_cv_HAVE_FUNCTION_MACRO=yes,libreplace_cv_HAVE_FUNCTION_MACRO=no)])
219 if test x"$libreplace_cv_HAVE_FUNCTION_MACRO" = x"yes"; then
220     AC_DEFINE(HAVE_FUNCTION_MACRO,1,[Whether there is a __FUNCTION__ macro])
221 else
222     dnl __func__ macro
223     AC_CACHE_CHECK([for __func__ macro],libreplace_cv_HAVE_func_MACRO,[
224     AC_TRY_COMPILE([#include <stdio.h>], [printf("%s\n", __func__);],
225     libreplace_cv_HAVE_func_MACRO=yes,libreplace_cv_HAVE_func_MACRO=no)])
226     if test x"$libreplace_cv_HAVE_func_MACRO" = x"yes"; then
227        AC_DEFINE(HAVE_func_MACRO,1,[Whether there is a __func__ macro])
228     fi
229 fi
230
231 AC_CHECK_HEADERS([sys/param.h limits.h])
232
233 AC_CHECK_TYPE(comparison_fn_t, 
234 [AC_DEFINE(HAVE_COMPARISON_FN_T, 1,[Whether or not we have comparison_fn_t])])
235
236 AC_HAVE_DECL(setenv, [#include <stdlib.h>])
237 AC_CHECK_FUNCS(setenv unsetenv)
238 AC_HAVE_DECL(environ, [#include <unistd.h>])
239
240 AC_CHECK_FUNCS(strnlen)
241 AC_CHECK_FUNCS(strtoull __strtoull strtouq strtoll __strtoll strtoq)
242
243 AC_CHECK_FUNCS(memmem)
244
245 # this test disabled as we don't actually need __VA_ARGS__ yet
246 AC_TRY_CPP([
247 #define eprintf(...) fprintf(stderr, __VA_ARGS__)
248 eprintf("bla", "bar");
249 ], AC_DEFINE(HAVE__VA_ARGS__MACRO, 1, [Whether the __VA_ARGS__ macro is available]))
250
251
252 AC_CACHE_CHECK([for sig_atomic_t type],libreplace_cv_sig_atomic_t, [
253     AC_TRY_COMPILE([
254 #include <sys/types.h>
255 #if STDC_HEADERS
256 #include <stdlib.h>
257 #include <stddef.h>
258 #endif
259 #include <signal.h>],[sig_atomic_t i = 0],
260         libreplace_cv_sig_atomic_t=yes,libreplace_cv_sig_atomic_t=no)])
261 if test x"$libreplace_cv_sig_atomic_t" = x"yes"; then
262    AC_DEFINE(HAVE_SIG_ATOMIC_T_TYPE,1,[Whether we have the atomic_t variable type])
263 fi
264
265
266 AC_CACHE_CHECK([for O_DIRECT flag to open(2)],libreplace_cv_HAVE_OPEN_O_DIRECT,[
267 AC_TRY_COMPILE([
268 #include <unistd.h>
269 #ifdef HAVE_FCNTL_H
270 #include <fcntl.h>
271 #endif],
272 [int fd = open("/dev/null", O_DIRECT);],
273 libreplace_cv_HAVE_OPEN_O_DIRECT=yes,libreplace_cv_HAVE_OPEN_O_DIRECT=no)])
274 if test x"$libreplace_cv_HAVE_OPEN_O_DIRECT" = x"yes"; then
275     AC_DEFINE(HAVE_OPEN_O_DIRECT,1,[Whether the open(2) accepts O_DIRECT])
276 fi
277
278
279 dnl Check if the C compiler understands volatile (it should, being ANSI).
280 AC_CACHE_CHECK([that the C compiler understands volatile],libreplace_cv_volatile, [
281         AC_TRY_COMPILE([#include <sys/types.h>],[volatile int i = 0],
282                 libreplace_cv_volatile=yes,libreplace_cv_volatile=no)])
283 if test x"$libreplace_cv_volatile" = x"yes"; then
284         AC_DEFINE(HAVE_VOLATILE, 1, [Whether the C compiler understands volatile])
285 fi
286
287 m4_include(system/config.m4)
288
289 m4_include(dlfcn.m4)
290 m4_include(getpass.m4)
291 m4_include(strptime.m4)
292 m4_include(win32.m4)
293 m4_include(timegm.m4)
294 m4_include(repdir.m4)
295 m4_include(crypt.m4)
296
297 if test x$libreplace_cv_have_clock_gettime = xyes ; then
298         SMB_CHECK_CLOCK_ID(CLOCK_MONOTONIC)
299         SMB_CHECK_CLOCK_ID(CLOCK_PROCESS_CPUTIME_ID)
300         SMB_CHECK_CLOCK_ID(CLOCK_REALTIME)
301 fi
302
303 AC_CHECK_FUNCS([printf memset memcpy],,[AC_MSG_ERROR([Required function not found])])
304
305 echo "LIBREPLACE_BROKEN_CHECKS: END"
306 ]) dnl end AC_LIBREPLACE_BROKEN_CHECKS
307
308 AC_DEFUN_ONCE(AC__LIBREPLACE_ALL_CHECKS_START,
309 [
310 #LIBREPLACE_ALL_CHECKS: START"
311 ])
312 AC_DEFUN_ONCE(AC__LIBREPLACE_ALL_CHECKS_END,
313 [
314 #LIBREPLACE_ALL_CHECKS: END"
315 ])
316 m4_define(AC_LIBREPLACE_ALL_CHECKS,
317 [
318 AC__LIBREPLACE_ALL_CHECKS_START
319 AC_LIBREPLACE_LOCATION_CHECKS
320 AC_LIBREPLACE_CC_CHECKS
321 AC_LIBREPLACE_BROKEN_CHECKS
322 AC__LIBREPLACE_ALL_CHECKS_END
323 CFLAGS="$CFLAGS -I$libreplacedir"
324 ])
325
326 m4_include(libreplace_cc.m4)
327 m4_include(libreplace_ld.m4)
328 m4_include(libreplace_network.m4)
329 m4_include(libreplace_macros.m4)
330
331
332 dnl SMB_CHECK_CLOCK_ID(clockid)
333 dnl Test whether the specified clock_gettime clock ID is available. If it
334 dnl is, we define HAVE_clockid
335 AC_DEFUN([SMB_CHECK_CLOCK_ID],
336 [
337     AC_MSG_CHECKING(for $1)
338     AC_TRY_LINK([
339 #if TIME_WITH_SYS_TIME
340 # include <sys/time.h>
341 # include <time.h>
342 #else
343 # if HAVE_SYS_TIME_H
344 #  include <sys/time.h>
345 # else
346 #  include <time.h>
347 # endif
348 #endif
349     ],
350     [
351 clockid_t clk = $1;
352     ],
353     [
354         AC_MSG_RESULT(yes)
355         AC_DEFINE(HAVE_$1, 1,
356             [Whether the clock_gettime clock ID $1 is available])
357     ],
358     [
359         AC_MSG_RESULT(no)
360     ])
361 ])
362 m4_ifndef([AC_USE_SYSTEM_EXTENSIONS],[m4_include(autoconf-2.60.m4)])