2 dnl Copyright (C) 2000-2002, 2007-2014, 2016-2020 Free Software Foundation,
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
12 dnl Note: AM_ICONV is documented in the GNU gettext manual
13 dnl <https://www.gnu.org/software/gettext/manual/html_node/AM_005fICONV.html>.
14 dnl Don't make changes that are incompatible with that documentation!
16 AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
18 dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
19 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
20 AC_REQUIRE([AC_LIB_RPATH])
22 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
24 AC_LIB_LINKFLAGS_BODY([iconv])
27 AC_DEFUN([AM_ICONV_LINK],
29 dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
30 dnl those with the standalone portable GNU libiconv installed).
31 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
33 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
35 AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
37 dnl Add $INCICONV to CPPFLAGS before performing the following checks,
38 dnl because if the user has installed libiconv and not disabled its use
39 dnl via --without-libiconv-prefix, he wants to use it. The first
40 dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed.
41 am_save_CPPFLAGS="$CPPFLAGS"
42 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
44 AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
45 am_cv_func_iconv="no, consider installing GNU libiconv"
53 [[iconv_t cd = iconv_open("","");
54 iconv(cd,NULL,NULL,NULL,NULL);
56 [am_cv_func_iconv=yes])
57 if test "$am_cv_func_iconv" != yes; then
59 LIBS="$LIBS $LIBICONV"
66 [[iconv_t cd = iconv_open("","");
67 iconv(cd,NULL,NULL,NULL,NULL);
70 [am_cv_func_iconv=yes])
74 if test "$am_cv_func_iconv" = yes; then
75 AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
76 dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
79 if test $am_cv_lib_iconv = yes; then
80 LIBS="$LIBS $LIBICONV"
82 am_cv_func_iconv_works=no
83 for ac_iconv_const in '' 'const'; do
91 # define ICONV_CONST $ac_iconv_const
95 /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
98 iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
99 if (cd_utf8_to_88591 != (iconv_t)(-1))
101 static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
103 ICONV_CONST char *inptr = input;
104 size_t inbytesleft = strlen (input);
106 size_t outbytesleft = sizeof (buf);
107 size_t res = iconv (cd_utf8_to_88591,
108 &inptr, &inbytesleft,
109 &outptr, &outbytesleft);
112 iconv_close (cd_utf8_to_88591);
115 /* Test against Solaris 10 bug: Failures are not distinguishable from
116 successful returns. */
118 iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
119 if (cd_ascii_to_88591 != (iconv_t)(-1))
121 static ICONV_CONST char input[] = "\263";
123 ICONV_CONST char *inptr = input;
124 size_t inbytesleft = strlen (input);
126 size_t outbytesleft = sizeof (buf);
127 size_t res = iconv (cd_ascii_to_88591,
128 &inptr, &inbytesleft,
129 &outptr, &outbytesleft);
132 iconv_close (cd_ascii_to_88591);
135 /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
137 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
138 if (cd_88591_to_utf8 != (iconv_t)(-1))
140 static ICONV_CONST char input[] = "\304";
141 static char buf[2] = { (char)0xDE, (char)0xAD };
142 ICONV_CONST char *inptr = input;
143 size_t inbytesleft = 1;
145 size_t outbytesleft = 1;
146 size_t res = iconv (cd_88591_to_utf8,
147 &inptr, &inbytesleft,
148 &outptr, &outbytesleft);
149 if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
151 iconv_close (cd_88591_to_utf8);
154 #if 0 /* This bug could be worked around by the caller. */
155 /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */
157 iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
158 if (cd_88591_to_utf8 != (iconv_t)(-1))
160 static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
162 ICONV_CONST char *inptr = input;
163 size_t inbytesleft = strlen (input);
165 size_t outbytesleft = sizeof (buf);
166 size_t res = iconv (cd_88591_to_utf8,
167 &inptr, &inbytesleft,
168 &outptr, &outbytesleft);
171 iconv_close (cd_88591_to_utf8);
175 /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
178 /* Try standardized names. */
179 iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
180 /* Try IRIX, OSF/1 names. */
181 iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
183 iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
184 /* Try HP-UX names. */
185 iconv_t cd4 = iconv_open ("utf8", "eucJP");
186 if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
187 && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
189 if (cd1 != (iconv_t)(-1))
191 if (cd2 != (iconv_t)(-1))
193 if (cd3 != (iconv_t)(-1))
195 if (cd4 != (iconv_t)(-1))
200 [am_cv_func_iconv_works=yes], ,
202 aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
203 *) am_cv_func_iconv_works="guessing yes" ;;
205 test "$am_cv_func_iconv_works" = no || break
209 case "$am_cv_func_iconv_works" in
210 *no) am_func_iconv=no am_cv_lib_iconv=no ;;
211 *) am_func_iconv=yes ;;
214 am_func_iconv=no am_cv_lib_iconv=no
216 if test "$am_func_iconv" = yes; then
217 AC_DEFINE([HAVE_ICONV], [1],
218 [Define if you have the iconv() function and it works.])
220 if test "$am_cv_lib_iconv" = yes; then
221 AC_MSG_CHECKING([how to link with libiconv])
222 AC_MSG_RESULT([$LIBICONV])
224 dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
226 CPPFLAGS="$am_save_CPPFLAGS"
231 AC_SUBST([LTLIBICONV])
234 dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like
235 dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
236 dnl This is tricky because of the way 'aclocal' is implemented:
237 dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
238 dnl Otherwise aclocal's initial scan pass would miss the macro definition.
239 dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
240 dnl Otherwise aclocal would emit many "Use of uninitialized value $1"
242 AC_DEFUN_ONCE([AM_ICONV],
245 if test "$am_cv_func_iconv" = yes; then
246 AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature],
247 [gl_cv_iconv_nonconst],
257 size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
260 [gl_cv_iconv_nonconst=yes],
261 [gl_cv_iconv_nonconst=no])
264 dnl When compiling GNU libiconv on a system that does not have iconv yet,
265 dnl pick the POSIX compliant declaration without 'const'.
266 gl_cv_iconv_nonconst=yes
268 if test $gl_cv_iconv_nonconst = yes; then
273 AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1],
274 [Define as const if the declaration of iconv() needs const.])
275 dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
276 m4_ifdef([gl_ICONV_H_DEFAULTS],
277 [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
278 if test $gl_cv_iconv_nonconst != yes; then