2 dnl Copyright (C) 2000-2002, 2007-2014, 2016-2023 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...7.2 bug: Failures are not distinguishable from
96 successful returns. This is even documented in
97 <https://www.ibm.com/support/knowledgecenter/ssw_aix_72/i_bostechref/iconv.html> */
99 iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
100 if (cd_utf8_to_88591 != (iconv_t)(-1))
102 static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
104 ICONV_CONST char *inptr = input;
105 size_t inbytesleft = strlen (input);
107 size_t outbytesleft = sizeof (buf);
108 size_t res = iconv (cd_utf8_to_88591,
109 &inptr, &inbytesleft,
110 &outptr, &outbytesleft);
113 iconv_close (cd_utf8_to_88591);
116 /* Test against Solaris 10 bug: Failures are not distinguishable from
117 successful returns. */
119 iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
120 if (cd_ascii_to_88591 != (iconv_t)(-1))
122 static ICONV_CONST char input[] = "\263";
124 ICONV_CONST char *inptr = input;
125 size_t inbytesleft = strlen (input);
127 size_t outbytesleft = sizeof (buf);
128 size_t res = iconv (cd_ascii_to_88591,
129 &inptr, &inbytesleft,
130 &outptr, &outbytesleft);
133 iconv_close (cd_ascii_to_88591);
136 /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
138 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
139 if (cd_88591_to_utf8 != (iconv_t)(-1))
141 static ICONV_CONST char input[] = "\304";
142 static char buf[2] = { (char)0xDE, (char)0xAD };
143 ICONV_CONST char *inptr = input;
144 size_t inbytesleft = 1;
146 size_t outbytesleft = 1;
147 size_t res = iconv (cd_88591_to_utf8,
148 &inptr, &inbytesleft,
149 &outptr, &outbytesleft);
150 if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
152 iconv_close (cd_88591_to_utf8);
155 #if 0 /* This bug could be worked around by the caller. */
156 /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */
158 iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
159 if (cd_88591_to_utf8 != (iconv_t)(-1))
161 static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
163 ICONV_CONST char *inptr = input;
164 size_t inbytesleft = strlen (input);
166 size_t outbytesleft = sizeof (buf);
167 size_t res = iconv (cd_88591_to_utf8,
168 &inptr, &inbytesleft,
169 &outptr, &outbytesleft);
172 iconv_close (cd_88591_to_utf8);
176 /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
179 /* Try standardized names. */
180 iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
181 /* Try IRIX, OSF/1 names. */
182 iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
184 iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
185 /* Try HP-UX names. */
186 iconv_t cd4 = iconv_open ("utf8", "eucJP");
187 if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
188 && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
190 if (cd1 != (iconv_t)(-1))
192 if (cd2 != (iconv_t)(-1))
194 if (cd3 != (iconv_t)(-1))
196 if (cd4 != (iconv_t)(-1))
201 [am_cv_func_iconv_works=yes], ,
203 aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
204 *) am_cv_func_iconv_works="guessing yes" ;;
206 test "$am_cv_func_iconv_works" = no || break
210 case "$am_cv_func_iconv_works" in
211 *no) am_func_iconv=no am_cv_lib_iconv=no ;;
212 *) am_func_iconv=yes ;;
215 am_func_iconv=no am_cv_lib_iconv=no
217 if test "$am_func_iconv" = yes; then
218 AC_DEFINE([HAVE_ICONV], [1],
219 [Define if you have the iconv() function and it works.])
221 if test "$am_cv_lib_iconv" = yes; then
222 AC_MSG_CHECKING([how to link with libiconv])
223 AC_MSG_RESULT([$LIBICONV])
225 dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
227 CPPFLAGS="$am_save_CPPFLAGS"
232 AC_SUBST([LTLIBICONV])
235 dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like
236 dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
237 AC_DEFUN_ONCE([AM_ICONV],
240 if test "$am_cv_func_iconv" = yes; then
241 AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature],
242 [gl_cv_iconv_nonconst],
252 size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
255 [gl_cv_iconv_nonconst=yes],
256 [gl_cv_iconv_nonconst=no])
259 dnl When compiling GNU libiconv on a system that does not have iconv yet,
260 dnl pick the POSIX compliant declaration without 'const'.
261 gl_cv_iconv_nonconst=yes
263 if test $gl_cv_iconv_nonconst = yes; then
268 AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1],
269 [Define as const if the declaration of iconv() needs const.])
270 dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
271 m4_ifdef([gl_ICONV_H_DEFAULTS],
272 [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
273 if test $gl_cv_iconv_nonconst != yes; then
278 dnl A summary result, for those packages which want to print a summary at the
279 dnl end of the configuration.
280 if test "$am_func_iconv" = yes; then
281 if test -n "$LIBICONV"; then
282 am_cv_func_iconv_summary='yes, in libiconv'
284 am_cv_func_iconv_summary='yes, in libc'
287 if test "$am_cv_func_iconv" = yes; then
288 am_cv_func_iconv_summary='not working, consider installing GNU libiconv'
290 am_cv_func_iconv_summary='no, consider installing GNU libiconv'