iconv.m4 8.89 KB
Newer Older
1
# iconv.m4 serial 18 (gettext-0.18.2)
2
dnl Copyright (C) 2000-2002, 2007-2014 Free Software Foundation, Inc.
3 4 5
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
6 7 8

dnl From Bruno Haible.

gbazin's avatar
 
gbazin committed
9 10 11 12 13 14 15 16 17 18 19 20
AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
[
  dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
  AC_REQUIRE([AC_LIB_RPATH])

  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  dnl accordingly.
  AC_LIB_LINKFLAGS_BODY([iconv])
])

AC_DEFUN([AM_ICONV_LINK],
21 22 23
[
  dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
  dnl those with the standalone portable GNU libiconv installed).
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
24
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
25

gbazin's avatar
 
gbazin committed
26 27 28 29 30 31 32
  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  dnl accordingly.
  AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])

  dnl Add $INCICONV to CPPFLAGS before performing the following checks,
  dnl because if the user has installed libiconv and not disabled its use
  dnl via --without-libiconv-prefix, he wants to use it. The first
33
  dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed.
gbazin's avatar
 
gbazin committed
34 35
  am_save_CPPFLAGS="$CPPFLAGS"
  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
36

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
37
  AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
38 39
    am_cv_func_iconv="no, consider installing GNU libiconv"
    am_cv_lib_iconv=no
40 41 42 43 44 45 46 47 48
    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM(
         [[
#include <stdlib.h>
#include <iconv.h>
         ]],
         [[iconv_t cd = iconv_open("","");
           iconv(cd,NULL,NULL,NULL,NULL);
           iconv_close(cd);]])],
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
49
      [am_cv_func_iconv=yes])
50 51
    if test "$am_cv_func_iconv" != yes; then
      am_save_LIBS="$LIBS"
gbazin's avatar
 
gbazin committed
52
      LIBS="$LIBS $LIBICONV"
53 54 55 56 57 58 59 60 61
      AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
           [[
#include <stdlib.h>
#include <iconv.h>
           ]],
           [[iconv_t cd = iconv_open("","");
             iconv(cd,NULL,NULL,NULL,NULL);
             iconv_close(cd);]])],
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62 63
        [am_cv_lib_iconv=yes]
        [am_cv_func_iconv=yes])
64 65 66 67
      LIBS="$am_save_LIBS"
    fi
  ])
  if test "$am_cv_func_iconv" = yes; then
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
68
    AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
69 70
      dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
      dnl Solaris 10.
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
71 72 73 74
      am_save_LIBS="$LIBS"
      if test $am_cv_lib_iconv = yes; then
        LIBS="$LIBS $LIBICONV"
      fi
75 76
      AC_RUN_IFELSE(
        [AC_LANG_SOURCE([[
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
77 78 79 80
#include <iconv.h>
#include <string.h>
int main ()
{
81
  int result = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
     returns.  */
  {
    iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
    if (cd_utf8_to_88591 != (iconv_t)(-1))
      {
        static const char input[] = "\342\202\254"; /* EURO SIGN */
        char buf[10];
        const char *inptr = input;
        size_t inbytesleft = strlen (input);
        char *outptr = buf;
        size_t outbytesleft = sizeof (buf);
        size_t res = iconv (cd_utf8_to_88591,
                            (char **) &inptr, &inbytesleft,
                            &outptr, &outbytesleft);
        if (res == 0)
98 99
          result |= 1;
        iconv_close (cd_utf8_to_88591);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
100 101
      }
  }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  /* Test against Solaris 10 bug: Failures are not distinguishable from
     successful returns.  */
  {
    iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
    if (cd_ascii_to_88591 != (iconv_t)(-1))
      {
        static const char input[] = "\263";
        char buf[10];
        const char *inptr = input;
        size_t inbytesleft = strlen (input);
        char *outptr = buf;
        size_t outbytesleft = sizeof (buf);
        size_t res = iconv (cd_ascii_to_88591,
                            (char **) &inptr, &inbytesleft,
                            &outptr, &outbytesleft);
        if (res == 0)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
          result |= 2;
        iconv_close (cd_ascii_to_88591);
      }
  }
  /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
  {
    iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
    if (cd_88591_to_utf8 != (iconv_t)(-1))
      {
        static const char input[] = "\304";
        static char buf[2] = { (char)0xDE, (char)0xAD };
        const char *inptr = input;
        size_t inbytesleft = 1;
        char *outptr = buf;
        size_t outbytesleft = 1;
        size_t res = iconv (cd_88591_to_utf8,
                            (char **) &inptr, &inbytesleft,
                            &outptr, &outbytesleft);
        if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
          result |= 4;
        iconv_close (cd_88591_to_utf8);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
139 140
      }
  }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#if 0 /* This bug could be worked around by the caller.  */
  /* Test against HP-UX 11.11 bug: Positive return value instead of 0.  */
  {
    iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
    if (cd_88591_to_utf8 != (iconv_t)(-1))
      {
        static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
        char buf[50];
        const char *inptr = input;
        size_t inbytesleft = strlen (input);
        char *outptr = buf;
        size_t outbytesleft = sizeof (buf);
        size_t res = iconv (cd_88591_to_utf8,
                            (char **) &inptr, &inbytesleft,
                            &outptr, &outbytesleft);
        if ((int)res > 0)
157 158
          result |= 8;
        iconv_close (cd_88591_to_utf8);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
159 160 161 162 163 164 165 166 167 168 169 170 171
      }
  }
#endif
  /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
     provided.  */
  if (/* Try standardized names.  */
      iconv_open ("UTF-8", "EUC-JP") == (iconv_t)(-1)
      /* Try IRIX, OSF/1 names.  */
      && iconv_open ("UTF-8", "eucJP") == (iconv_t)(-1)
      /* Try AIX names.  */
      && iconv_open ("UTF-8", "IBM-eucJP") == (iconv_t)(-1)
      /* Try HP-UX names.  */
      && iconv_open ("utf8", "eucJP") == (iconv_t)(-1))
172 173 174 175 176 177 178 179
    result |= 16;
  return result;
}]])],
        [am_cv_func_iconv_works=yes],
        [am_cv_func_iconv_works=no],
        [
changequote(,)dnl
         case "$host_os" in
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180 181
           aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
           *)            am_cv_func_iconv_works="guessing yes" ;;
182 183 184
         esac
changequote([,])dnl
        ])
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
185 186 187 188 189 190 191 192 193 194
      LIBS="$am_save_LIBS"
    ])
    case "$am_cv_func_iconv_works" in
      *no) am_func_iconv=no am_cv_lib_iconv=no ;;
      *)   am_func_iconv=yes ;;
    esac
  else
    am_func_iconv=no am_cv_lib_iconv=no
  fi
  if test "$am_func_iconv" = yes; then
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
    AC_DEFINE([HAVE_ICONV], [1],
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
196
      [Define if you have the iconv() function and it works.])
gbazin's avatar
 
gbazin committed
197 198 199 200 201 202 203 204 205 206 207
  fi
  if test "$am_cv_lib_iconv" = yes; then
    AC_MSG_CHECKING([how to link with libiconv])
    AC_MSG_RESULT([$LIBICONV])
  else
    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
    dnl either.
    CPPFLAGS="$am_save_CPPFLAGS"
    LIBICONV=
    LTLIBICONV=
  fi
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
208 209
  AC_SUBST([LIBICONV])
  AC_SUBST([LTLIBICONV])
gbazin's avatar
 
gbazin committed
210 211
])

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
212 213 214 215 216 217 218 219 220 221 222 223 224
dnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
dnl avoid warnings like
dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
dnl This is tricky because of the way 'aclocal' is implemented:
dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
dnl   Otherwise aclocal's initial scan pass would miss the macro definition.
dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
dnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
dnl   warnings.
m4_define([gl_iconv_AC_DEFUN],
  m4_version_prereq([2.64],
    [[AC_DEFUN_ONCE(
        [$1], [$2])]],
225 226 227 228 229
    [m4_ifdef([gl_00GNULIB],
       [[AC_DEFUN_ONCE(
           [$1], [$2])]],
       [[AC_DEFUN(
           [$1], [$2])]])]))
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
230
gl_iconv_AC_DEFUN([AM_ICONV],
gbazin's avatar
 
gbazin committed
231 232 233
[
  AM_ICONV_LINK
  if test "$am_cv_func_iconv" = yes; then
234
    AC_MSG_CHECKING([for iconv declaration])
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
235
    AC_CACHE_VAL([am_cv_proto_iconv], [
236 237 238
      AC_COMPILE_IFELSE(
        [AC_LANG_PROGRAM(
           [[
239 240 241 242 243 244
#include <stdlib.h>
#include <iconv.h>
extern
#ifdef __cplusplus
"C"
#endif
245
#if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
246 247 248 249
size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
#else
size_t iconv();
#endif
250 251 252 253
           ]],
           [[]])],
        [am_cv_proto_iconv_arg1=""],
        [am_cv_proto_iconv_arg1="const"])
254 255
      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);"])
    am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
256 257 258
    AC_MSG_RESULT([
         $am_cv_proto_iconv])
    AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
259
      [Define as const if the declaration of iconv() needs const.])
260 261 262 263 264 265 266
    dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
    m4_ifdef([gl_ICONV_H_DEFAULTS],
      [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
       if test -n "$am_cv_proto_iconv_arg1"; then
         ICONV_CONST="const"
       fi
      ])
267 268
  fi
])