jpayne@68: # iconv.m4 serial 27 jpayne@68: dnl Copyright (C) 2000-2002, 2007-2014, 2016-2024 Free Software Foundation, jpayne@68: dnl Inc. jpayne@68: dnl This file is free software; the Free Software Foundation jpayne@68: dnl gives unlimited permission to copy and/or distribute it, jpayne@68: dnl with or without modifications, as long as this notice is preserved. jpayne@68: jpayne@68: dnl From Bruno Haible. jpayne@68: jpayne@68: AC_PREREQ([2.64]) jpayne@68: jpayne@68: dnl Note: AM_ICONV is documented in the GNU gettext manual jpayne@68: dnl . jpayne@68: dnl Don't make changes that are incompatible with that documentation! jpayne@68: jpayne@68: AC_DEFUN([AM_ICONV_LINKFLAGS_BODY], jpayne@68: [ jpayne@68: dnl Prerequisites of AC_LIB_LINKFLAGS_BODY. jpayne@68: AC_REQUIRE([AC_LIB_PREPARE_PREFIX]) jpayne@68: AC_REQUIRE([AC_LIB_RPATH]) jpayne@68: jpayne@68: dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV jpayne@68: dnl accordingly. jpayne@68: AC_LIB_LINKFLAGS_BODY([iconv]) jpayne@68: ]) jpayne@68: jpayne@68: AC_DEFUN([AM_ICONV_LINK], jpayne@68: [ jpayne@68: dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and jpayne@68: dnl those with the standalone portable GNU libiconv installed). jpayne@68: AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles jpayne@68: jpayne@68: dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV jpayne@68: dnl accordingly. jpayne@68: AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY]) jpayne@68: jpayne@68: dnl Add $INCICONV to CPPFLAGS before performing the following checks, jpayne@68: dnl because if the user has installed libiconv and not disabled its use jpayne@68: dnl via --without-libiconv-prefix, he wants to use it. The first jpayne@68: dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed. jpayne@68: gl_saved_CPPFLAGS="$CPPFLAGS" jpayne@68: AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV]) jpayne@68: jpayne@68: AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [ jpayne@68: am_cv_func_iconv="no, consider installing GNU libiconv" jpayne@68: am_cv_lib_iconv=no jpayne@68: AC_LINK_IFELSE( jpayne@68: [AC_LANG_PROGRAM( jpayne@68: [[ jpayne@68: #include jpayne@68: #include jpayne@68: ]], jpayne@68: [[iconv_t cd = iconv_open("",""); jpayne@68: iconv(cd,NULL,NULL,NULL,NULL); jpayne@68: iconv_close(cd);]])], jpayne@68: [am_cv_func_iconv=yes]) jpayne@68: if test "$am_cv_func_iconv" != yes; then jpayne@68: gl_saved_LIBS="$LIBS" jpayne@68: LIBS="$LIBS $LIBICONV" jpayne@68: AC_LINK_IFELSE( jpayne@68: [AC_LANG_PROGRAM( jpayne@68: [[ jpayne@68: #include jpayne@68: #include jpayne@68: ]], jpayne@68: [[iconv_t cd = iconv_open("",""); jpayne@68: iconv(cd,NULL,NULL,NULL,NULL); jpayne@68: iconv_close(cd);]])], jpayne@68: [am_cv_lib_iconv=yes] jpayne@68: [am_cv_func_iconv=yes]) jpayne@68: LIBS="$gl_saved_LIBS" jpayne@68: fi jpayne@68: ]) jpayne@68: if test "$am_cv_func_iconv" = yes; then jpayne@68: AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [ jpayne@68: dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11, jpayne@68: dnl Solaris 10. jpayne@68: gl_saved_LIBS="$LIBS" jpayne@68: if test $am_cv_lib_iconv = yes; then jpayne@68: LIBS="$LIBS $LIBICONV" jpayne@68: fi jpayne@68: am_cv_func_iconv_works=no jpayne@68: for ac_iconv_const in '' 'const'; do jpayne@68: AC_RUN_IFELSE( jpayne@68: [AC_LANG_PROGRAM( jpayne@68: [[ jpayne@68: #include jpayne@68: #include jpayne@68: jpayne@68: #ifndef ICONV_CONST jpayne@68: # define ICONV_CONST $ac_iconv_const jpayne@68: #endif jpayne@68: ]], jpayne@68: [[int result = 0; jpayne@68: /* Test against AIX 5.1...7.2 bug: Failures are not distinguishable from jpayne@68: successful returns. This is even documented in jpayne@68: */ jpayne@68: { jpayne@68: iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8"); jpayne@68: if (cd_utf8_to_88591 != (iconv_t)(-1)) jpayne@68: { jpayne@68: static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */ jpayne@68: char buf[10]; jpayne@68: ICONV_CONST char *inptr = input; jpayne@68: size_t inbytesleft = strlen (input); jpayne@68: char *outptr = buf; jpayne@68: size_t outbytesleft = sizeof (buf); jpayne@68: size_t res = iconv (cd_utf8_to_88591, jpayne@68: &inptr, &inbytesleft, jpayne@68: &outptr, &outbytesleft); jpayne@68: if (res == 0) jpayne@68: result |= 1; jpayne@68: iconv_close (cd_utf8_to_88591); jpayne@68: } jpayne@68: } jpayne@68: /* Test against Solaris 10 bug: Failures are not distinguishable from jpayne@68: successful returns. */ jpayne@68: { jpayne@68: iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646"); jpayne@68: if (cd_ascii_to_88591 != (iconv_t)(-1)) jpayne@68: { jpayne@68: static ICONV_CONST char input[] = "\263"; jpayne@68: char buf[10]; jpayne@68: ICONV_CONST char *inptr = input; jpayne@68: size_t inbytesleft = strlen (input); jpayne@68: char *outptr = buf; jpayne@68: size_t outbytesleft = sizeof (buf); jpayne@68: size_t res = iconv (cd_ascii_to_88591, jpayne@68: &inptr, &inbytesleft, jpayne@68: &outptr, &outbytesleft); jpayne@68: if (res == 0) jpayne@68: result |= 2; jpayne@68: iconv_close (cd_ascii_to_88591); jpayne@68: } jpayne@68: } jpayne@68: /* Test against AIX 6.1..7.1 bug: Buffer overrun. */ jpayne@68: { jpayne@68: iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1"); jpayne@68: if (cd_88591_to_utf8 != (iconv_t)(-1)) jpayne@68: { jpayne@68: static ICONV_CONST char input[] = "\304"; jpayne@68: static char buf[2] = { (char)0xDE, (char)0xAD }; jpayne@68: ICONV_CONST char *inptr = input; jpayne@68: size_t inbytesleft = 1; jpayne@68: char *outptr = buf; jpayne@68: size_t outbytesleft = 1; jpayne@68: size_t res = iconv (cd_88591_to_utf8, jpayne@68: &inptr, &inbytesleft, jpayne@68: &outptr, &outbytesleft); jpayne@68: if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD) jpayne@68: result |= 4; jpayne@68: iconv_close (cd_88591_to_utf8); jpayne@68: } jpayne@68: } jpayne@68: #if 0 /* This bug could be worked around by the caller. */ jpayne@68: /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */ jpayne@68: { jpayne@68: iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591"); jpayne@68: if (cd_88591_to_utf8 != (iconv_t)(-1)) jpayne@68: { jpayne@68: static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337"; jpayne@68: char buf[50]; jpayne@68: ICONV_CONST char *inptr = input; jpayne@68: size_t inbytesleft = strlen (input); jpayne@68: char *outptr = buf; jpayne@68: size_t outbytesleft = sizeof (buf); jpayne@68: size_t res = iconv (cd_88591_to_utf8, jpayne@68: &inptr, &inbytesleft, jpayne@68: &outptr, &outbytesleft); jpayne@68: if ((int)res > 0) jpayne@68: result |= 8; jpayne@68: iconv_close (cd_88591_to_utf8); jpayne@68: } jpayne@68: } jpayne@68: #endif jpayne@68: /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is jpayne@68: provided. */ jpayne@68: { jpayne@68: /* Try standardized names. */ jpayne@68: iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP"); jpayne@68: /* Try IRIX, OSF/1 names. */ jpayne@68: iconv_t cd2 = iconv_open ("UTF-8", "eucJP"); jpayne@68: /* Try AIX names. */ jpayne@68: iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP"); jpayne@68: /* Try HP-UX names. */ jpayne@68: iconv_t cd4 = iconv_open ("utf8", "eucJP"); jpayne@68: if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1) jpayne@68: && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1)) jpayne@68: result |= 16; jpayne@68: if (cd1 != (iconv_t)(-1)) jpayne@68: iconv_close (cd1); jpayne@68: if (cd2 != (iconv_t)(-1)) jpayne@68: iconv_close (cd2); jpayne@68: if (cd3 != (iconv_t)(-1)) jpayne@68: iconv_close (cd3); jpayne@68: if (cd4 != (iconv_t)(-1)) jpayne@68: iconv_close (cd4); jpayne@68: } jpayne@68: return result; jpayne@68: ]])], jpayne@68: [am_cv_func_iconv_works=yes], , jpayne@68: [case "$host_os" in jpayne@68: aix* | hpux*) am_cv_func_iconv_works="guessing no" ;; jpayne@68: *) am_cv_func_iconv_works="guessing yes" ;; jpayne@68: esac]) jpayne@68: test "$am_cv_func_iconv_works" = no || break jpayne@68: done jpayne@68: LIBS="$gl_saved_LIBS" jpayne@68: ]) jpayne@68: case "$am_cv_func_iconv_works" in jpayne@68: *no) am_func_iconv=no am_cv_lib_iconv=no ;; jpayne@68: *) am_func_iconv=yes ;; jpayne@68: esac jpayne@68: else jpayne@68: am_func_iconv=no am_cv_lib_iconv=no jpayne@68: fi jpayne@68: if test "$am_func_iconv" = yes; then jpayne@68: AC_DEFINE([HAVE_ICONV], [1], jpayne@68: [Define if you have the iconv() function and it works.]) jpayne@68: fi jpayne@68: if test "$am_cv_lib_iconv" = yes; then jpayne@68: AC_MSG_CHECKING([how to link with libiconv]) jpayne@68: AC_MSG_RESULT([$LIBICONV]) jpayne@68: else jpayne@68: dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV jpayne@68: dnl either. jpayne@68: CPPFLAGS="$gl_saved_CPPFLAGS" jpayne@68: LIBICONV= jpayne@68: LTLIBICONV= jpayne@68: fi jpayne@68: AC_SUBST([LIBICONV]) jpayne@68: AC_SUBST([LTLIBICONV]) jpayne@68: ]) jpayne@68: jpayne@68: dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like jpayne@68: dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required". jpayne@68: AC_DEFUN_ONCE([AM_ICONV], jpayne@68: [ jpayne@68: AM_ICONV_LINK jpayne@68: if test "$am_cv_func_iconv" = yes; then jpayne@68: AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature], jpayne@68: [gl_cv_iconv_nonconst], jpayne@68: [AC_COMPILE_IFELSE( jpayne@68: [AC_LANG_PROGRAM( jpayne@68: [[ jpayne@68: #include jpayne@68: #include jpayne@68: extern jpayne@68: #ifdef __cplusplus jpayne@68: "C" jpayne@68: #endif jpayne@68: size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft); jpayne@68: ]], jpayne@68: [[]])], jpayne@68: [gl_cv_iconv_nonconst=yes], jpayne@68: [gl_cv_iconv_nonconst=no]) jpayne@68: ]) jpayne@68: else jpayne@68: dnl When compiling GNU libiconv on a system that does not have iconv yet, jpayne@68: dnl pick the POSIX compliant declaration without 'const'. jpayne@68: gl_cv_iconv_nonconst=yes jpayne@68: fi jpayne@68: if test $gl_cv_iconv_nonconst = yes; then jpayne@68: iconv_arg1="" jpayne@68: else jpayne@68: iconv_arg1="const" jpayne@68: fi jpayne@68: AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1], jpayne@68: [Define as const if the declaration of iconv() needs const.]) jpayne@68: dnl Also substitute ICONV_CONST in the gnulib generated . jpayne@68: m4_ifdef([gl_ICONV_H_DEFAULTS], jpayne@68: [AC_REQUIRE([gl_ICONV_H_DEFAULTS]) jpayne@68: if test $gl_cv_iconv_nonconst != yes; then jpayne@68: ICONV_CONST="const" jpayne@68: fi jpayne@68: ]) jpayne@68: jpayne@68: dnl A summary result, for those packages which want to print a summary at the jpayne@68: dnl end of the configuration. jpayne@68: if test "$am_func_iconv" = yes; then jpayne@68: if test -n "$LIBICONV"; then jpayne@68: am_cv_func_iconv_summary='yes, in libiconv' jpayne@68: else jpayne@68: am_cv_func_iconv_summary='yes, in libc' jpayne@68: fi jpayne@68: else jpayne@68: if test "$am_cv_func_iconv" = yes; then jpayne@68: am_cv_func_iconv_summary='not working, consider installing GNU libiconv' jpayne@68: else jpayne@68: am_cv_func_iconv_summary='no, consider installing GNU libiconv' jpayne@68: fi jpayne@68: fi jpayne@68: ])