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