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