1a32e9e42Smrg# iconv.m4 serial 19 (gettext-0.18.2)
2a32e9e42Smrgdnl Copyright (C) 2000-2002, 2007-2014, 2016 Free Software Foundation, Inc.
3a32e9e42Smrgdnl This file is free software; the Free Software Foundation
4a32e9e42Smrgdnl gives unlimited permission to copy and/or distribute it,
5a32e9e42Smrgdnl with or without modifications, as long as this notice is preserved.
6a32e9e42Smrg
7a32e9e42Smrgdnl From Bruno Haible.
8a32e9e42Smrg
9a32e9e42SmrgAC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
10a32e9e42Smrg[
11a32e9e42Smrg  dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
12a32e9e42Smrg  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
13a32e9e42Smrg  AC_REQUIRE([AC_LIB_RPATH])
14a32e9e42Smrg
15a32e9e42Smrg  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
16a32e9e42Smrg  dnl accordingly.
17a32e9e42Smrg  AC_LIB_LINKFLAGS_BODY([iconv])
18a32e9e42Smrg])
19a32e9e42Smrg
20a32e9e42SmrgAC_DEFUN([AM_ICONV_LINK],
21a32e9e42Smrg[
22a32e9e42Smrg  dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
23a32e9e42Smrg  dnl those with the standalone portable GNU libiconv installed).
24a32e9e42Smrg  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
25a32e9e42Smrg
26a32e9e42Smrg  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
27a32e9e42Smrg  dnl accordingly.
28a32e9e42Smrg  AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
29a32e9e42Smrg
30a32e9e42Smrg  dnl Add $INCICONV to CPPFLAGS before performing the following checks,
31a32e9e42Smrg  dnl because if the user has installed libiconv and not disabled its use
32a32e9e42Smrg  dnl via --without-libiconv-prefix, he wants to use it. The first
33a32e9e42Smrg  dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed.
34a32e9e42Smrg  am_save_CPPFLAGS="$CPPFLAGS"
35a32e9e42Smrg  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
36a32e9e42Smrg
37a32e9e42Smrg  AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
38a32e9e42Smrg    am_cv_func_iconv="no, consider installing GNU libiconv"
39a32e9e42Smrg    am_cv_lib_iconv=no
40a32e9e42Smrg    AC_LINK_IFELSE(
41a32e9e42Smrg      [AC_LANG_PROGRAM(
42a32e9e42Smrg         [[
43a32e9e42Smrg#include <stdlib.h>
44a32e9e42Smrg#include <iconv.h>
45a32e9e42Smrg         ]],
46a32e9e42Smrg         [[iconv_t cd = iconv_open("","");
47a32e9e42Smrg           iconv(cd,NULL,NULL,NULL,NULL);
48a32e9e42Smrg           iconv_close(cd);]])],
49a32e9e42Smrg      [am_cv_func_iconv=yes])
50a32e9e42Smrg    if test "$am_cv_func_iconv" != yes; then
51a32e9e42Smrg      am_save_LIBS="$LIBS"
52a32e9e42Smrg      LIBS="$LIBS $LIBICONV"
53a32e9e42Smrg      AC_LINK_IFELSE(
54a32e9e42Smrg        [AC_LANG_PROGRAM(
55a32e9e42Smrg           [[
56a32e9e42Smrg#include <stdlib.h>
57a32e9e42Smrg#include <iconv.h>
58a32e9e42Smrg           ]],
59a32e9e42Smrg           [[iconv_t cd = iconv_open("","");
60a32e9e42Smrg             iconv(cd,NULL,NULL,NULL,NULL);
61a32e9e42Smrg             iconv_close(cd);]])],
62a32e9e42Smrg        [am_cv_lib_iconv=yes]
63a32e9e42Smrg        [am_cv_func_iconv=yes])
64a32e9e42Smrg      LIBS="$am_save_LIBS"
65a32e9e42Smrg    fi
66a32e9e42Smrg  ])
67a32e9e42Smrg  if test "$am_cv_func_iconv" = yes; then
68a32e9e42Smrg    AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
69a32e9e42Smrg      dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
70a32e9e42Smrg      dnl Solaris 10.
71a32e9e42Smrg      am_save_LIBS="$LIBS"
72a32e9e42Smrg      if test $am_cv_lib_iconv = yes; then
73a32e9e42Smrg        LIBS="$LIBS $LIBICONV"
74a32e9e42Smrg      fi
75a32e9e42Smrg      am_cv_func_iconv_works=no
76a32e9e42Smrg      for ac_iconv_const in '' 'const'; do
77a32e9e42Smrg        AC_RUN_IFELSE(
78a32e9e42Smrg          [AC_LANG_PROGRAM(
79a32e9e42Smrg             [[
80a32e9e42Smrg#include <iconv.h>
81a32e9e42Smrg#include <string.h>
82a32e9e42Smrg
83a32e9e42Smrg#ifndef ICONV_CONST
84a32e9e42Smrg# define ICONV_CONST $ac_iconv_const
85a32e9e42Smrg#endif
86a32e9e42Smrg             ]],
87a32e9e42Smrg             [[int result = 0;
88a32e9e42Smrg  /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
89a32e9e42Smrg     returns.  */
90a32e9e42Smrg  {
91a32e9e42Smrg    iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
92a32e9e42Smrg    if (cd_utf8_to_88591 != (iconv_t)(-1))
93a32e9e42Smrg      {
94a32e9e42Smrg        static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
95a32e9e42Smrg        char buf[10];
96a32e9e42Smrg        ICONV_CONST char *inptr = input;
97a32e9e42Smrg        size_t inbytesleft = strlen (input);
98a32e9e42Smrg        char *outptr = buf;
99a32e9e42Smrg        size_t outbytesleft = sizeof (buf);
100a32e9e42Smrg        size_t res = iconv (cd_utf8_to_88591,
101a32e9e42Smrg                            &inptr, &inbytesleft,
102a32e9e42Smrg                            &outptr, &outbytesleft);
103a32e9e42Smrg        if (res == 0)
104a32e9e42Smrg          result |= 1;
105a32e9e42Smrg        iconv_close (cd_utf8_to_88591);
106a32e9e42Smrg      }
107a32e9e42Smrg  }
108a32e9e42Smrg  /* Test against Solaris 10 bug: Failures are not distinguishable from
109a32e9e42Smrg     successful returns.  */
110a32e9e42Smrg  {
111a32e9e42Smrg    iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
112a32e9e42Smrg    if (cd_ascii_to_88591 != (iconv_t)(-1))
113a32e9e42Smrg      {
114a32e9e42Smrg        static ICONV_CONST char input[] = "\263";
115a32e9e42Smrg        char buf[10];
116a32e9e42Smrg        ICONV_CONST char *inptr = input;
117a32e9e42Smrg        size_t inbytesleft = strlen (input);
118a32e9e42Smrg        char *outptr = buf;
119a32e9e42Smrg        size_t outbytesleft = sizeof (buf);
120a32e9e42Smrg        size_t res = iconv (cd_ascii_to_88591,
121a32e9e42Smrg                            &inptr, &inbytesleft,
122a32e9e42Smrg                            &outptr, &outbytesleft);
123a32e9e42Smrg        if (res == 0)
124a32e9e42Smrg          result |= 2;
125a32e9e42Smrg        iconv_close (cd_ascii_to_88591);
126a32e9e42Smrg      }
127a32e9e42Smrg  }
128a32e9e42Smrg  /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
129a32e9e42Smrg  {
130a32e9e42Smrg    iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
131a32e9e42Smrg    if (cd_88591_to_utf8 != (iconv_t)(-1))
132a32e9e42Smrg      {
133a32e9e42Smrg        static ICONV_CONST char input[] = "\304";
134a32e9e42Smrg        static char buf[2] = { (char)0xDE, (char)0xAD };
135a32e9e42Smrg        ICONV_CONST char *inptr = input;
136a32e9e42Smrg        size_t inbytesleft = 1;
137a32e9e42Smrg        char *outptr = buf;
138a32e9e42Smrg        size_t outbytesleft = 1;
139a32e9e42Smrg        size_t res = iconv (cd_88591_to_utf8,
140a32e9e42Smrg                            &inptr, &inbytesleft,
141a32e9e42Smrg                            &outptr, &outbytesleft);
142a32e9e42Smrg        if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
143a32e9e42Smrg          result |= 4;
144a32e9e42Smrg        iconv_close (cd_88591_to_utf8);
145a32e9e42Smrg      }
146a32e9e42Smrg  }
147a32e9e42Smrg#if 0 /* This bug could be worked around by the caller.  */
148a32e9e42Smrg  /* Test against HP-UX 11.11 bug: Positive return value instead of 0.  */
149a32e9e42Smrg  {
150a32e9e42Smrg    iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
151a32e9e42Smrg    if (cd_88591_to_utf8 != (iconv_t)(-1))
152a32e9e42Smrg      {
153a32e9e42Smrg        static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
154a32e9e42Smrg        char buf[50];
155a32e9e42Smrg        ICONV_CONST char *inptr = input;
156a32e9e42Smrg        size_t inbytesleft = strlen (input);
157a32e9e42Smrg        char *outptr = buf;
158a32e9e42Smrg        size_t outbytesleft = sizeof (buf);
159a32e9e42Smrg        size_t res = iconv (cd_88591_to_utf8,
160a32e9e42Smrg                            &inptr, &inbytesleft,
161a32e9e42Smrg                            &outptr, &outbytesleft);
162a32e9e42Smrg        if ((int)res > 0)
163a32e9e42Smrg          result |= 8;
164a32e9e42Smrg        iconv_close (cd_88591_to_utf8);
165a32e9e42Smrg      }
166a32e9e42Smrg  }
167a32e9e42Smrg#endif
168a32e9e42Smrg  /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
169a32e9e42Smrg     provided.  */
170a32e9e42Smrg  if (/* Try standardized names.  */
171a32e9e42Smrg      iconv_open ("UTF-8", "EUC-JP") == (iconv_t)(-1)
172a32e9e42Smrg      /* Try IRIX, OSF/1 names.  */
173a32e9e42Smrg      && iconv_open ("UTF-8", "eucJP") == (iconv_t)(-1)
174a32e9e42Smrg      /* Try AIX names.  */
175a32e9e42Smrg      && iconv_open ("UTF-8", "IBM-eucJP") == (iconv_t)(-1)
176a32e9e42Smrg      /* Try HP-UX names.  */
177a32e9e42Smrg      && iconv_open ("utf8", "eucJP") == (iconv_t)(-1))
178a32e9e42Smrg    result |= 16;
179a32e9e42Smrg  return result;
180a32e9e42Smrg]])],
181a32e9e42Smrg          [am_cv_func_iconv_works=yes], ,
182a32e9e42Smrg          [case "$host_os" in
183a32e9e42Smrg             aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
184a32e9e42Smrg             *)            am_cv_func_iconv_works="guessing yes" ;;
185a32e9e42Smrg           esac])
186a32e9e42Smrg        test "$am_cv_func_iconv_works" = no || break
187a32e9e42Smrg      done
188a32e9e42Smrg      LIBS="$am_save_LIBS"
189a32e9e42Smrg    ])
190a32e9e42Smrg    case "$am_cv_func_iconv_works" in
191a32e9e42Smrg      *no) am_func_iconv=no am_cv_lib_iconv=no ;;
192a32e9e42Smrg      *)   am_func_iconv=yes ;;
193a32e9e42Smrg    esac
194a32e9e42Smrg  else
195a32e9e42Smrg    am_func_iconv=no am_cv_lib_iconv=no
196a32e9e42Smrg  fi
197a32e9e42Smrg  if test "$am_func_iconv" = yes; then
198a32e9e42Smrg    AC_DEFINE([HAVE_ICONV], [1],
199a32e9e42Smrg      [Define if you have the iconv() function and it works.])
200a32e9e42Smrg  fi
201a32e9e42Smrg  if test "$am_cv_lib_iconv" = yes; then
202a32e9e42Smrg    AC_MSG_CHECKING([how to link with libiconv])
203a32e9e42Smrg    AC_MSG_RESULT([$LIBICONV])
204a32e9e42Smrg  else
205a32e9e42Smrg    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
206a32e9e42Smrg    dnl either.
207a32e9e42Smrg    CPPFLAGS="$am_save_CPPFLAGS"
208a32e9e42Smrg    LIBICONV=
209a32e9e42Smrg    LTLIBICONV=
210a32e9e42Smrg  fi
211a32e9e42Smrg  AC_SUBST([LIBICONV])
212a32e9e42Smrg  AC_SUBST([LTLIBICONV])
213a32e9e42Smrg])
214a32e9e42Smrg
215a32e9e42Smrgdnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
216a32e9e42Smrgdnl avoid warnings like
217a32e9e42Smrgdnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
218a32e9e42Smrgdnl This is tricky because of the way 'aclocal' is implemented:
219a32e9e42Smrgdnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
220a32e9e42Smrgdnl   Otherwise aclocal's initial scan pass would miss the macro definition.
221a32e9e42Smrgdnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
222a32e9e42Smrgdnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
223a32e9e42Smrgdnl   warnings.
224a32e9e42Smrgm4_define([gl_iconv_AC_DEFUN],
225a32e9e42Smrg  m4_version_prereq([2.64],
226a32e9e42Smrg    [[AC_DEFUN_ONCE(
227a32e9e42Smrg        [$1], [$2])]],
228a32e9e42Smrg    [m4_ifdef([gl_00GNULIB],
229a32e9e42Smrg       [[AC_DEFUN_ONCE(
230a32e9e42Smrg           [$1], [$2])]],
231a32e9e42Smrg       [[AC_DEFUN(
232a32e9e42Smrg           [$1], [$2])]])]))
233a32e9e42Smrggl_iconv_AC_DEFUN([AM_ICONV],
234a32e9e42Smrg[
235a32e9e42Smrg  AM_ICONV_LINK
236a32e9e42Smrg  if test "$am_cv_func_iconv" = yes; then
237a32e9e42Smrg    AC_MSG_CHECKING([for iconv declaration])
238a32e9e42Smrg    AC_CACHE_VAL([am_cv_proto_iconv], [
239a32e9e42Smrg      AC_COMPILE_IFELSE(
240a32e9e42Smrg        [AC_LANG_PROGRAM(
241a32e9e42Smrg           [[
242a32e9e42Smrg#include <stdlib.h>
243a32e9e42Smrg#include <iconv.h>
244a32e9e42Smrgextern
245a32e9e42Smrg#ifdef __cplusplus
246a32e9e42Smrg"C"
247a32e9e42Smrg#endif
248a32e9e42Smrg#if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
249a32e9e42Smrgsize_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
250a32e9e42Smrg#else
251a32e9e42Smrgsize_t iconv();
252a32e9e42Smrg#endif
253a32e9e42Smrg           ]],
254a32e9e42Smrg           [[]])],
255a32e9e42Smrg        [am_cv_proto_iconv_arg1=""],
256a32e9e42Smrg        [am_cv_proto_iconv_arg1="const"])
257a32e9e42Smrg      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);"])
258a32e9e42Smrg    am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
259a32e9e42Smrg    AC_MSG_RESULT([
260a32e9e42Smrg         $am_cv_proto_iconv])
261a32e9e42Smrg    AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
262a32e9e42Smrg      [Define as const if the declaration of iconv() needs const.])
263a32e9e42Smrg    dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
264a32e9e42Smrg    m4_ifdef([gl_ICONV_H_DEFAULTS],
265a32e9e42Smrg      [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
266a32e9e42Smrg       if test -n "$am_cv_proto_iconv_arg1"; then
267a32e9e42Smrg         ICONV_CONST="const"
268a32e9e42Smrg       fi
269a32e9e42Smrg      ])
270a32e9e42Smrg  fi
271a32e9e42Smrg])
272