2 dnl Copyright (C) 2000-2002, 2007-2014, 2016-2019 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.
10 AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
12 dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
13 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
14 AC_REQUIRE([AC_LIB_RPATH])
16 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
18 AC_LIB_LINKFLAGS_BODY([iconv])
21 AC_DEFUN([AM_ICONV_LINK],
23 dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
24 dnl those with the standalone portable GNU libiconv installed).
25 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
27 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
29 AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
31 dnl Add $INCICONV to CPPFLAGS before performing the following checks,
32 dnl so that if libiconv is installed, it will be used (unless disabled
33 dnl via --without-libiconv-prefix). The first AC_LINK_IFELSE will
34 dnl then fail, the second AC_LINK_IFELSE will succeed.
35 am_save_CPPFLAGS="$CPPFLAGS"
36 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
38 AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
39 am_cv_func_iconv="no, consider installing GNU libiconv"
47 [[iconv_t cd = iconv_open("","");
48 iconv(cd,NULL,NULL,NULL,NULL);
50 [am_cv_func_iconv=yes])
51 if test "$am_cv_func_iconv" != yes; then
53 LIBS="$LIBS $LIBICONV"
60 [[iconv_t cd = iconv_open("","");
61 iconv(cd,NULL,NULL,NULL,NULL);
64 [am_cv_func_iconv=yes])
68 if test "$am_cv_func_iconv" = yes; then
69 AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
70 dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
73 if test $am_cv_lib_iconv = yes; then
74 LIBS="$LIBS $LIBICONV"
76 am_cv_func_iconv_works=no
77 for ac_iconv_const in '' 'const'; do
85 # define ICONV_CONST $ac_iconv_const
89 /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
92 iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
93 if (cd_utf8_to_88591 != (iconv_t)(-1))
95 static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
97 ICONV_CONST char *inptr = input;
98 size_t inbytesleft = strlen (input);
100 size_t outbytesleft = sizeof (buf);
101 size_t res = iconv (cd_utf8_to_88591,
102 &inptr, &inbytesleft,
103 &outptr, &outbytesleft);
106 iconv_close (cd_utf8_to_88591);
109 /* Test against Solaris 10 bug: Failures are not distinguishable from
110 successful returns. */
112 iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
113 if (cd_ascii_to_88591 != (iconv_t)(-1))
115 static ICONV_CONST char input[] = "\263";
117 ICONV_CONST char *inptr = input;
118 size_t inbytesleft = strlen (input);
120 size_t outbytesleft = sizeof (buf);
121 size_t res = iconv (cd_ascii_to_88591,
122 &inptr, &inbytesleft,
123 &outptr, &outbytesleft);
126 iconv_close (cd_ascii_to_88591);
129 /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
131 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
132 if (cd_88591_to_utf8 != (iconv_t)(-1))
134 static ICONV_CONST char input[] = "\304";
135 static char buf[2] = { (char)0xDE, (char)0xAD };
136 ICONV_CONST char *inptr = input;
137 size_t inbytesleft = 1;
139 size_t outbytesleft = 1;
140 size_t res = iconv (cd_88591_to_utf8,
141 &inptr, &inbytesleft,
142 &outptr, &outbytesleft);
143 if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
145 iconv_close (cd_88591_to_utf8);
148 #if 0 /* This bug could be worked around by the caller. */
149 /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */
151 iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
152 if (cd_88591_to_utf8 != (iconv_t)(-1))
154 static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
156 ICONV_CONST char *inptr = input;
157 size_t inbytesleft = strlen (input);
159 size_t outbytesleft = sizeof (buf);
160 size_t res = iconv (cd_88591_to_utf8,
161 &inptr, &inbytesleft,
162 &outptr, &outbytesleft);
165 iconv_close (cd_88591_to_utf8);
169 /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
172 /* Try standardized names. */
173 iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
174 /* Try IRIX, OSF/1 names. */
175 iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
177 iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
178 /* Try HP-UX names. */
179 iconv_t cd4 = iconv_open ("utf8", "eucJP");
180 if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
181 && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
183 if (cd1 != (iconv_t)(-1))
185 if (cd2 != (iconv_t)(-1))
187 if (cd3 != (iconv_t)(-1))
189 if (cd4 != (iconv_t)(-1))
194 [am_cv_func_iconv_works=yes], ,
196 aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
197 *) am_cv_func_iconv_works="guessing yes" ;;
199 test "$am_cv_func_iconv_works" = no || break
203 case "$am_cv_func_iconv_works" in
204 *no) am_func_iconv=no am_cv_lib_iconv=no ;;
205 *) am_func_iconv=yes ;;
208 am_func_iconv=no am_cv_lib_iconv=no
210 if test "$am_func_iconv" = yes; then
211 AC_DEFINE([HAVE_ICONV], [1],
212 [Define if you have the iconv() function and it works.])
214 if test "$am_cv_lib_iconv" = yes; then
215 AC_MSG_CHECKING([how to link with libiconv])
216 AC_MSG_RESULT([$LIBICONV])
218 dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
220 CPPFLAGS="$am_save_CPPFLAGS"
225 AC_SUBST([LTLIBICONV])
228 dnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
229 dnl avoid warnings like
230 dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
231 dnl This is tricky because of the way 'aclocal' is implemented:
232 dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
233 dnl Otherwise aclocal's initial scan pass would miss the macro definition.
234 dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
235 dnl Otherwise aclocal would emit many "Use of uninitialized value $1"
237 m4_define([gl_iconv_AC_DEFUN],
238 m4_version_prereq([2.64],
241 [m4_ifdef([gl_00GNULIB],
246 gl_iconv_AC_DEFUN([AM_ICONV],
249 if test "$am_cv_func_iconv" = yes; then
250 AC_MSG_CHECKING([for iconv declaration])
251 AC_CACHE_VAL([am_cv_proto_iconv], [
261 #if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
262 size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
268 [am_cv_proto_iconv_arg1=""],
269 [am_cv_proto_iconv_arg1="const"])
270 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);"])
271 am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
272 AC_MSG_RESULT([$am_cv_proto_iconv])
274 dnl When compiling GNU libiconv on a system that does not have iconv yet,
275 dnl pick the POSIX compliant declaration without 'const'.
276 am_cv_proto_iconv_arg1=""
278 AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
279 [Define as const if the declaration of iconv() needs const.])
280 dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
281 m4_ifdef([gl_ICONV_H_DEFAULTS],
282 [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
283 if test -n "$am_cv_proto_iconv_arg1"; then