1dnl Checks for libcfile or required headers and functions
2dnl
3dnl Version: 20180407
4
5dnl Function to detect if libcfile is available
6dnl ac_libcfile_dummy is used to prevent AC_CHECK_LIB adding unnecessary -l<library> arguments
7AC_DEFUN([AX_LIBCFILE_CHECK_LIB],
8  [dnl Check if parameters were provided
9  AS_IF(
10    [test "x$ac_cv_with_libcfile" != x && test "x$ac_cv_with_libcfile" != xno && test "x$ac_cv_with_libcfile" != xauto-detect],
11    [AS_IF(
12      [test -d "$ac_cv_with_libcfile"],
13      [CFLAGS="$CFLAGS -I${ac_cv_with_libcfile}/include"
14      LDFLAGS="$LDFLAGS -L${ac_cv_with_libcfile}/lib"],
15      [AC_MSG_WARN([no such directory: $ac_cv_with_libcfile])
16      ])
17    ])
18
19  AS_IF(
20    [test "x$ac_cv_with_libcfile" = xno],
21    [ac_cv_libcfile=no],
22    [dnl Check for a pkg-config file
23    AS_IF(
24      [test "x$cross_compiling" != "xyes" && test "x$PKGCONFIG" != "x"],
25      [PKG_CHECK_MODULES(
26        [libcfile],
27        [libcfile >= 20160409],
28        [ac_cv_libcfile=yes],
29        [ac_cv_libcfile=check])
30      ])
31
32    AS_IF(
33      [test "x$ac_cv_libcfile" = xyes && test "x$ac_cv_enable_wide_character_type" != xno],
34      [AC_CACHE_CHECK(
35       [whether libcfile/features.h defines LIBCFILE_HAVE_WIDE_CHARACTER_TYPE as 1],
36       [ac_cv_header_libcfile_features_h_have_wide_character_type],
37       [AC_LANG_PUSH(C)
38       AC_COMPILE_IFELSE(
39         [AC_LANG_PROGRAM(
40           [[#include <libcfile/features.h>]],
41           [[#if !defined( LIBCFILE_HAVE_WIDE_CHARACTER_TYPE ) || ( LIBCFILE_HAVE_WIDE_CHARACTER_TYPE != 1 )
42#error LIBCFILE_HAVE_WIDE_CHARACTER_TYPE not defined
43#endif]] )],
44         [ac_cv_header_libcfile_features_h_have_wide_character_type=yes],
45         [ac_cv_header_libcfile_features_h_have_wide_character_type=no])
46       AC_LANG_POP(C)],
47       [ac_cv_header_libcfile_features_h_have_wide_character_type=no])
48
49      AS_IF(
50        [test "x$ac_cv_header_libcfile_features_h_have_wide_character_type" = xno],
51        [ac_cv_libcfile=no])
52    ])
53
54    AS_IF(
55      [test "x$ac_cv_libcfile" = xyes],
56      [ac_cv_libcfile_CPPFLAGS="$pkg_cv_libcfile_CFLAGS"
57      ac_cv_libcfile_LIBADD="$pkg_cv_libcfile_LIBS"])
58
59    AS_IF(
60      [test "x$ac_cv_libcfile" = xcheck],
61      [dnl Check for headers
62      AC_CHECK_HEADERS([libcfile.h])
63
64      AS_IF(
65        [test "x$ac_cv_header_libcfile_h" = xno],
66        [ac_cv_libcfile=no],
67        [dnl Check for the individual functions
68        ac_cv_libcfile=yes
69
70        AC_CHECK_LIB(
71          cfile,
72          libcfile_get_version,
73          [ac_cv_libcfile_dummy=yes],
74          [ac_cv_libcfile=no])
75
76        dnl File functions
77        AC_CHECK_LIB(
78          cfile,
79          libcfile_file_initialize,
80          [ac_cv_libcfile_dummy=yes],
81          [ac_cv_libcfile=no])
82        AC_CHECK_LIB(
83          cfile,
84          libcfile_file_free,
85          [ac_cv_libcfile_dummy=yes],
86          [ac_cv_libcfile=no])
87        AC_CHECK_LIB(
88          cfile,
89          libcfile_file_open,
90          [ac_cv_libcfile_dummy=yes],
91          [ac_cv_libcfile=no])
92        AC_CHECK_LIB(
93          cfile,
94          libcfile_file_open_with_error_code,
95          [ac_cv_libcfile_dummy=yes],
96          [ac_cv_libcfile=no])
97        AC_CHECK_LIB(
98          cfile,
99          libcfile_file_close,
100          [ac_cv_libcfile_dummy=yes],
101          [ac_cv_libcfile=no])
102        AC_CHECK_LIB(
103          cfile,
104          libcfile_file_read_buffer,
105          [ac_cv_libcfile_dummy=yes],
106          [ac_cv_libcfile=no])
107        AC_CHECK_LIB(
108          cfile,
109          libcfile_file_read_buffer_with_error_code,
110          [ac_cv_libcfile_dummy=yes],
111          [ac_cv_libcfile=no])
112        AC_CHECK_LIB(
113          cfile,
114          libcfile_file_write_buffer,
115          [ac_cv_libcfile_dummy=yes],
116          [ac_cv_libcfile=no])
117        AC_CHECK_LIB(
118          cfile,
119          libcfile_file_write_buffer_with_error_code,
120          [ac_cv_libcfile_dummy=yes],
121          [ac_cv_libcfile=no])
122        AC_CHECK_LIB(
123          cfile,
124          libcfile_file_seek_offset,
125          [ac_cv_libcfile_dummy=yes],
126          [ac_cv_libcfile=no])
127        AC_CHECK_LIB(
128          cfile,
129          libcfile_file_resize,
130          [ac_cv_libcfile_dummy=yes],
131          [ac_cv_libcfile=no])
132        AC_CHECK_LIB(
133          cfile,
134          libcfile_file_is_open,
135          [ac_cv_libcfile_dummy=yes],
136          [ac_cv_libcfile=no])
137        AC_CHECK_LIB(
138          cfile,
139          libcfile_file_get_offset,
140          [ac_cv_libcfile_dummy=yes],
141          [ac_cv_libcfile=no])
142        AC_CHECK_LIB(
143          cfile,
144          libcfile_file_get_size,
145          [ac_cv_libcfile_dummy=yes],
146          [ac_cv_libcfile=no])
147        AC_CHECK_LIB(
148          cfile,
149          libcfile_file_is_device,
150          [ac_cv_libcfile_dummy=yes],
151          [ac_cv_libcfile=no])
152        AC_CHECK_LIB(
153          cfile,
154          libcfile_file_io_control_read,
155          [ac_cv_libcfile_dummy=yes],
156          [ac_cv_libcfile=no])
157        AC_CHECK_LIB(
158          cfile,
159          libcfile_file_io_control_read_with_error_code,
160          [ac_cv_libcfile_dummy=yes],
161          [ac_cv_libcfile=no])
162
163        AS_IF(
164          [test "x$ac_cv_enable_wide_character_type" != xno],
165          [AC_CHECK_LIB(
166            cfile,
167            libcfile_file_open_wide,
168            [ac_cv_libcfile_dummy=yes],
169            [ac_cv_libcfile=no])
170          AC_CHECK_LIB(
171            cfile,
172            libcfile_file_open_wide_with_error_code,
173            [ac_cv_libcfile_dummy=yes],
174            [ac_cv_libcfile=no])
175          ])
176
177        dnl Support functions
178        AC_CHECK_LIB(
179          cfile,
180          libcfile_file_exists,
181          [ac_cv_libcfile_dummy=yes],
182          [ac_cv_libcfile=no])
183        AC_CHECK_LIB(
184          cfile,
185          libcfile_file_remove,
186          [ac_cv_libcfile_dummy=yes],
187          [ac_cv_libcfile=no])
188
189        AS_IF(
190          [test "x$ac_cv_enable_wide_character_type" != xno],
191          [AC_CHECK_LIB(
192            cfile,
193            libcfile_file_exists_wide,
194            [ac_cv_libcfile_dummy=yes],
195            [ac_cv_libcfile=no])
196          AC_CHECK_LIB(
197            cfile,
198            libcfile_file_remove_wide,
199            [ac_cv_libcfile_dummy=yes],
200            [ac_cv_libcfile=no])
201          ])
202
203        ac_cv_libcfile_LIBADD="-lcfile"
204        ])
205      ])
206    ])
207
208  AS_IF(
209    [test "x$ac_cv_libcfile" = xyes],
210    [AC_DEFINE(
211      [HAVE_LIBCFILE],
212      [1],
213      [Define to 1 if you have the `cfile' library (-lcfile).])
214    ])
215
216  AS_IF(
217    [test "x$ac_cv_libcfile" = xyes],
218    [AC_SUBST(
219      [HAVE_LIBCFILE],
220      [1]) ],
221    [AC_SUBST(
222      [HAVE_LIBCFILE],
223      [0])
224    ])
225  ])
226
227dnl Function to detect if posix_fadvise is available
228AC_DEFUN([AX_LIBCFILE_CHECK_FUNC_POSIX_FADVISE],
229  [AC_CHECK_FUNCS([posix_fadvise])
230
231  AS_IF(
232    [test "x$ac_cv_func_posix_fadvise" = xyes],
233    [AC_MSG_CHECKING(
234        [whether posix_fadvise can be linked])
235
236      SAVE_CFLAGS="$CFLAGS"
237      CFLAGS="$CFLAGS -Wall -Werror"
238      AC_LANG_PUSH(C)
239
240      AC_LINK_IFELSE(
241        [AC_LANG_PROGRAM(
242          [[#include <fcntl.h>]],
243          [[#if !defined( POSIX_FADV_SEQUENTIAL )
244#define POSIX_FADV_SEQUENTIAL 2
245#endif
246posix_fadvise( 0, 0, 0, POSIX_FADV_SEQUENTIAL )]] )],
247          [ac_cv_func_posix_fadvise=yes],
248          [ac_cv_func_posix_fadvise=no])
249
250      AC_LANG_POP(C)
251      CFLAGS="$SAVE_CFLAGS"
252
253      AS_IF(
254        [test "x$ac_cv_func_posix_fadvise" = xyes],
255        [AC_MSG_RESULT(
256          [yes])
257        AC_DEFINE(
258          [HAVE_POSIX_FADVISE],
259          [1],
260          [Define to 1 if you have the posix_fadvise function.]) ],
261        [AC_MSG_RESULT(
262          [no]) ])
263    ])
264  ])
265
266dnl Function to detect if libcfile dependencies are available
267AC_DEFUN([AX_LIBCFILE_CHECK_LOCAL],
268  [dnl Headers included in libcfile/libcfile_file.c and libcfile/libcfile_support.c
269  AC_CHECK_HEADERS([errno.h stdio.h sys/stat.h])
270
271  dnl Headers included in libcfile/libcfile_file.c
272  AC_CHECK_HEADERS([cygwin/fs.h fcntl.h linux/fs.h sys/disk.h sys/disklabel.h sys/ioctl.h unistd.h])
273
274  dnl File input/output functions used in libcfile/libcfile_file.c
275  AC_CHECK_FUNCS([close fstat ftruncate ioctl lseek open read write])
276
277  AS_IF(
278    [test "x$ac_cv_func_close" != xyes],
279    [AC_MSG_FAILURE(
280      [Missing function: close],
281      [1])
282    ])
283
284  AX_LIBCFILE_CHECK_FUNC_POSIX_FADVISE
285
286  AS_IF(
287    [test "x$ac_cv_func_fstat" != xyes],
288    [AC_MSG_FAILURE(
289      [Missing function: fstat],
290      [1])
291    ])
292
293  AS_IF(
294    [test "x$ac_cv_func_ftruncate" != xyes],
295    [AC_MSG_FAILURE(
296      [Missing function: ftruncate],
297      [1])
298    ])
299
300  AS_IF(
301    [test x"$ac_cv_enable_winapi" = xno],
302    [AS_IF(
303      [test "x$ac_cv_func_ioctl" != xyes],
304      [AC_MSG_FAILURE(
305        [Missing function: ioctl],
306        [1])
307      ])
308    ])
309
310  AS_IF(
311    [test "x$ac_cv_func_lseek" != xyes],
312    [AC_MSG_FAILURE(
313      [Missing function: lseek],
314      [1])
315    ])
316
317  AS_IF(
318    [test "x$ac_cv_func_open" != xyes],
319    [AC_MSG_FAILURE(
320      [Missing function: open],
321      [1])
322    ])
323
324  AS_IF(
325    [test "x$ac_cv_func_read" != xyes],
326    [AC_MSG_FAILURE(
327      [Missing function: read],
328      [1])
329    ])
330
331  AS_IF(
332    [test "x$ac_cv_func_write" != xyes],
333    [AC_MSG_FAILURE(
334      [Missing function: write],
335      [1])
336    ])
337
338  dnl File input/output functions used in libcfile/libcfile_support.c
339  AC_CHECK_FUNCS([stat unlink])
340
341  AS_IF(
342    [test "x$ac_cv_func_stat" != xyes],
343    [AC_MSG_FAILURE(
344      [Missing function: stat],
345      [1])
346    ])
347
348  AS_IF(
349    [test "x$ac_cv_func_unlink" != xyes],
350    [AC_MSG_FAILURE(
351      [Missing function: unlink],
352      [1])
353    ])
354
355  ac_cv_libcfile_CPPFLAGS="-I../libcfile";
356  ac_cv_libcfile_LIBADD="../libcfile/libcfile.la";
357
358  ac_cv_libcfile=local
359  ])
360
361dnl Function to detect how to enable libcfile
362AC_DEFUN([AX_LIBCFILE_CHECK_ENABLE],
363  [AX_COMMON_ARG_WITH(
364    [libcfile],
365    [libcfile],
366    [search for libcfile in includedir and libdir or in the specified DIR, or no if to use local version],
367    [auto-detect],
368    [DIR])
369
370  dnl Check for a shared library version
371  AX_LIBCFILE_CHECK_LIB
372
373  dnl Check if the dependencies for the local library version
374  AS_IF(
375    [test "x$ac_cv_libcfile" != xyes],
376    [AX_LIBCFILE_CHECK_LOCAL
377
378    AC_DEFINE(
379      [HAVE_LOCAL_LIBCFILE],
380      [1],
381      [Define to 1 if the local version of libcfile is used.])
382    AC_SUBST(
383      [HAVE_LOCAL_LIBCFILE],
384      [1])
385    ])
386
387  AM_CONDITIONAL(
388    [HAVE_LOCAL_LIBCFILE],
389    [test "x$ac_cv_libcfile" = xlocal])
390  AS_IF(
391    [test "x$ac_cv_libcfile_CPPFLAGS" != "x"],
392    [AC_SUBST(
393      [LIBCFILE_CPPFLAGS],
394      [$ac_cv_libcfile_CPPFLAGS])
395    ])
396  AS_IF(
397    [test "x$ac_cv_libcfile_LIBADD" != "x"],
398    [AC_SUBST(
399      [LIBCFILE_LIBADD],
400      [$ac_cv_libcfile_LIBADD])
401    ])
402
403  AS_IF(
404    [test "x$ac_cv_libcfile" = xyes],
405    [AC_SUBST(
406      [ax_libcfile_pc_libs_private],
407      [-lcfile])
408    ])
409
410  AS_IF(
411    [test "x$ac_cv_libcfile" = xyes],
412    [AC_SUBST(
413      [ax_libcfile_spec_requires],
414      [libcfile])
415    AC_SUBST(
416      [ax_libcfile_spec_build_requires],
417      [libcfile-devel])
418    ])
419  ])
420
421