Detect broken AIX iconv() function.
[pspp] / m4 / iconv.m4
1 # iconv.m4 serial AM5 (gettext-0.16.2)
2 dnl Copyright (C) 2000-2002, 2007 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Bruno Haible.
8
9 AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
10 [
11   dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
12   AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
13   AC_REQUIRE([AC_LIB_RPATH])
14
15   dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
16   dnl accordingly.
17   AC_LIB_LINKFLAGS_BODY([iconv])
18 ])
19
20 AC_DEFUN([AM_ICONV_LINK],
21 [
22   dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
23   dnl those with the standalone portable GNU libiconv installed).
24   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
25
26   dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
27   dnl accordingly.
28   AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
29
30   dnl Add $INCICONV to CPPFLAGS before performing the following checks,
31   dnl because if the user has installed libiconv and not disabled its use
32   dnl via --without-libiconv-prefix, he wants to use it. The first
33   dnl AC_TRY_LINK will then fail, the second AC_TRY_LINK will succeed.
34   am_save_CPPFLAGS="$CPPFLAGS"
35   AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
36
37   AC_CACHE_CHECK([for iconv], am_cv_func_iconv, [
38     am_cv_func_iconv="no, consider installing GNU libiconv"
39     am_cv_lib_iconv=no
40     AC_TRY_LINK([#include <stdlib.h>
41 #include <iconv.h>],
42       [iconv_t cd = iconv_open("","");
43        iconv(cd,NULL,NULL,NULL,NULL);
44        iconv_close(cd);],
45       am_cv_func_iconv=yes)
46     if test "$am_cv_func_iconv" != yes; then
47       am_save_LIBS="$LIBS"
48       LIBS="$LIBS $LIBICONV"
49       AC_TRY_LINK([#include <stdlib.h>
50 #include <iconv.h>],
51         [iconv_t cd = iconv_open("","");
52          iconv(cd,NULL,NULL,NULL,NULL);
53          iconv_close(cd);],
54         am_cv_lib_iconv=yes
55         am_cv_func_iconv=yes)
56       LIBS="$am_save_LIBS"
57     fi
58   ])
59   if test "$am_cv_func_iconv" = yes; then
60     AC_CACHE_CHECK([for working iconv], am_cv_func_iconv_works, [
61       dnl This tests against a bug in AIX 5.1: failures are not distinguishable
62       dnl from successul returns.
63       am_save_LIBS="$LIBS"
64       if test $am_cv_lib_iconv = yes; then
65         LIBS="$LIBS $LIBICONV"
66       fi
67       AC_TRY_RUN([
68 int main ()
69 {
70   static const char input[] = "\342\202\254"; /* EURO SIGN */
71   iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
72   if (cd_utf8_to_88591 != (iconv_t)(-1))
73     {
74       char buf[10];
75       const char *inptr = input;
76       size_t inbytesleft = strlen (input);
77       char *outptr = buf;
78       size_t outbytesleft = sizeof (buf);
79       size_t res = iconv (cd_utf8_to_88591,
80                           (char **) &inptr, &inbytesleft,
81                           &outptr, &outbytesleft);
82       return res == 0;
83     }
84   else
85     return 0;
86 }], [am_cv_func_iconv_works=yes], [am_cv_func_iconv_works=no],
87         [case "$host_os" in
88            aix*) am_cv_func_iconv_works="guessing no" ;;
89            *)    am_cv_func_iconv_works="guessing yes" ;;
90          esac])
91       LIBS="$am_save_LIBS"
92     ])
93     case "$am_cv_func_iconv_works" in
94       *no) am_func_iconv=no am_cv_lib_iconv=no ;;
95       *)   am_func_iconv=yes ;;
96     esac
97   else
98     am_func_iconv=no am_cv_lib_iconv=no
99   fi
100   if test "$am_func_iconv" = yes; then
101     AC_DEFINE(HAVE_ICONV, 1,
102       [Define if you have the iconv() function and it works.])
103   fi
104   if test "$am_cv_lib_iconv" = yes; then
105     AC_MSG_CHECKING([how to link with libiconv])
106     AC_MSG_RESULT([$LIBICONV])
107   else
108     dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
109     dnl either.
110     CPPFLAGS="$am_save_CPPFLAGS"
111     LIBICONV=
112     LTLIBICONV=
113   fi
114   AC_SUBST(LIBICONV)
115   AC_SUBST(LTLIBICONV)
116 ])
117
118 AC_DEFUN([AM_ICONV],
119 [
120   AM_ICONV_LINK
121   if test "$am_cv_func_iconv" = yes; then
122     AC_MSG_CHECKING([for iconv declaration])
123     AC_CACHE_VAL(am_cv_proto_iconv, [
124       AC_TRY_COMPILE([
125 #include <stdlib.h>
126 #include <iconv.h>
127 extern
128 #ifdef __cplusplus
129 "C"
130 #endif
131 #if defined(__STDC__) || defined(__cplusplus)
132 size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
133 #else
134 size_t iconv();
135 #endif
136 ], [], am_cv_proto_iconv_arg1="", am_cv_proto_iconv_arg1="const")
137       am_cv_proto_iconv="extern size_t iconv (iconv_t cd, $am_cv_proto_iconv_arg1 char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);"])
138     am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
139     AC_MSG_RESULT([$]{ac_t:-
140          }[$]am_cv_proto_iconv)
141     AC_DEFINE_UNQUOTED(ICONV_CONST, $am_cv_proto_iconv_arg1,
142       [Define as const if the declaration of iconv() needs const.])
143   fi
144 ])