blob: 00057953e33c61cae9d7d807e7aa1a05dc20928a [file] [log] [blame]
Patrick Williams7784c422022-11-17 07:29:11 -06001# iconv.m4 serial 24
2dnl Copyright (C) 2000-2002, 2007-2014, 2016-2022 Free Software Foundation,
Andrew Geissler82c905d2020-04-13 13:39:40 -05003dnl Inc.
4dnl This file is free software; the Free Software Foundation
5dnl gives unlimited permission to copy and/or distribute it,
6dnl with or without modifications, as long as this notice is preserved.
7
8dnl From Bruno Haible.
9
Patrick Williams7784c422022-11-17 07:29:11 -060010AC_PREREQ([2.64])
11
12dnl Note: AM_ICONV is documented in the GNU gettext manual
13dnl <https://www.gnu.org/software/gettext/manual/html_node/AM_005fICONV.html>.
14dnl Don't make changes that are incompatible with that documentation!
15
Andrew Geissler82c905d2020-04-13 13:39:40 -050016AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
17[
18 dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
19 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
20 AC_REQUIRE([AC_LIB_RPATH])
21
22 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
23 dnl accordingly.
24 AC_LIB_LINKFLAGS_BODY([iconv])
25])
26
27AC_DEFUN([AM_ICONV_LINK],
28[
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
32
33 dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
34 dnl accordingly.
35 AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
36
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])
43
44 AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
45 am_cv_func_iconv="no, consider installing GNU libiconv"
46 am_cv_lib_iconv=no
47 AC_LINK_IFELSE(
48 [AC_LANG_PROGRAM(
49 [[
50#include <stdlib.h>
51#include <iconv.h>
52 ]],
53 [[iconv_t cd = iconv_open("","");
54 iconv(cd,NULL,NULL,NULL,NULL);
55 iconv_close(cd);]])],
56 [am_cv_func_iconv=yes])
57 if test "$am_cv_func_iconv" != yes; then
58 am_save_LIBS="$LIBS"
59 LIBS="$LIBS $LIBICONV"
60 AC_LINK_IFELSE(
61 [AC_LANG_PROGRAM(
62 [[
63#include <stdlib.h>
64#include <iconv.h>
65 ]],
66 [[iconv_t cd = iconv_open("","");
67 iconv(cd,NULL,NULL,NULL,NULL);
68 iconv_close(cd);]])],
69 [am_cv_lib_iconv=yes]
70 [am_cv_func_iconv=yes])
71 LIBS="$am_save_LIBS"
72 fi
73 ])
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,
77 dnl Solaris 10.
78 am_save_LIBS="$LIBS"
79 if test $am_cv_lib_iconv = yes; then
80 LIBS="$LIBS $LIBICONV"
81 fi
82 am_cv_func_iconv_works=no
83 for ac_iconv_const in '' 'const'; do
84 AC_RUN_IFELSE(
85 [AC_LANG_PROGRAM(
86 [[
87#include <iconv.h>
88#include <string.h>
89
90#ifndef ICONV_CONST
91# define ICONV_CONST $ac_iconv_const
92#endif
93 ]],
94 [[int result = 0;
Patrick Williams7784c422022-11-17 07:29:11 -060095 /* 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> */
Andrew Geissler82c905d2020-04-13 13:39:40 -050098 {
99 iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
100 if (cd_utf8_to_88591 != (iconv_t)(-1))
101 {
102 static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
103 char buf[10];
104 ICONV_CONST char *inptr = input;
105 size_t inbytesleft = strlen (input);
106 char *outptr = buf;
107 size_t outbytesleft = sizeof (buf);
108 size_t res = iconv (cd_utf8_to_88591,
109 &inptr, &inbytesleft,
110 &outptr, &outbytesleft);
111 if (res == 0)
112 result |= 1;
113 iconv_close (cd_utf8_to_88591);
114 }
115 }
116 /* Test against Solaris 10 bug: Failures are not distinguishable from
117 successful returns. */
118 {
119 iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
120 if (cd_ascii_to_88591 != (iconv_t)(-1))
121 {
122 static ICONV_CONST char input[] = "\263";
123 char buf[10];
124 ICONV_CONST char *inptr = input;
125 size_t inbytesleft = strlen (input);
126 char *outptr = buf;
127 size_t outbytesleft = sizeof (buf);
128 size_t res = iconv (cd_ascii_to_88591,
129 &inptr, &inbytesleft,
130 &outptr, &outbytesleft);
131 if (res == 0)
132 result |= 2;
133 iconv_close (cd_ascii_to_88591);
134 }
135 }
136 /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
137 {
138 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
139 if (cd_88591_to_utf8 != (iconv_t)(-1))
140 {
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;
145 char *outptr = buf;
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)
151 result |= 4;
152 iconv_close (cd_88591_to_utf8);
153 }
154 }
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. */
157 {
158 iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
159 if (cd_88591_to_utf8 != (iconv_t)(-1))
160 {
161 static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
162 char buf[50];
163 ICONV_CONST char *inptr = input;
164 size_t inbytesleft = strlen (input);
165 char *outptr = buf;
166 size_t outbytesleft = sizeof (buf);
167 size_t res = iconv (cd_88591_to_utf8,
168 &inptr, &inbytesleft,
169 &outptr, &outbytesleft);
170 if ((int)res > 0)
171 result |= 8;
172 iconv_close (cd_88591_to_utf8);
173 }
174 }
175#endif
176 /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
177 provided. */
178 {
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");
183 /* Try AIX names. */
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))
189 result |= 16;
190 if (cd1 != (iconv_t)(-1))
191 iconv_close (cd1);
192 if (cd2 != (iconv_t)(-1))
193 iconv_close (cd2);
194 if (cd3 != (iconv_t)(-1))
195 iconv_close (cd3);
196 if (cd4 != (iconv_t)(-1))
197 iconv_close (cd4);
198 }
199 return result;
200]])],
201 [am_cv_func_iconv_works=yes], ,
202 [case "$host_os" in
203 aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
204 *) am_cv_func_iconv_works="guessing yes" ;;
205 esac])
206 test "$am_cv_func_iconv_works" = no || break
207 done
208 LIBS="$am_save_LIBS"
209 ])
210 case "$am_cv_func_iconv_works" in
211 *no) am_func_iconv=no am_cv_lib_iconv=no ;;
212 *) am_func_iconv=yes ;;
213 esac
214 else
215 am_func_iconv=no am_cv_lib_iconv=no
216 fi
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.])
220 fi
221 if test "$am_cv_lib_iconv" = yes; then
222 AC_MSG_CHECKING([how to link with libiconv])
223 AC_MSG_RESULT([$LIBICONV])
224 else
225 dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
226 dnl either.
227 CPPFLAGS="$am_save_CPPFLAGS"
228 LIBICONV=
229 LTLIBICONV=
230 fi
231 AC_SUBST([LIBICONV])
232 AC_SUBST([LTLIBICONV])
233])
234
Patrick Williams7784c422022-11-17 07:29:11 -0600235dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like
Andrew Geissler82c905d2020-04-13 13:39:40 -0500236dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
237dnl This is tricky because of the way 'aclocal' is implemented:
238dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
239dnl Otherwise aclocal's initial scan pass would miss the macro definition.
240dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
241dnl Otherwise aclocal would emit many "Use of uninitialized value $1"
242dnl warnings.
Patrick Williams7784c422022-11-17 07:29:11 -0600243AC_DEFUN_ONCE([AM_ICONV],
Andrew Geissler82c905d2020-04-13 13:39:40 -0500244[
245 AM_ICONV_LINK
246 if test "$am_cv_func_iconv" = yes; then
Patrick Williams7784c422022-11-17 07:29:11 -0600247 AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature],
248 [gl_cv_iconv_nonconst],
249 [AC_COMPILE_IFELSE(
250 [AC_LANG_PROGRAM(
251 [[
Andrew Geissler82c905d2020-04-13 13:39:40 -0500252#include <stdlib.h>
253#include <iconv.h>
254extern
255#ifdef __cplusplus
256"C"
257#endif
Andrew Geissler82c905d2020-04-13 13:39:40 -0500258size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
Patrick Williams7784c422022-11-17 07:29:11 -0600259 ]],
260 [[]])],
261 [gl_cv_iconv_nonconst=yes],
262 [gl_cv_iconv_nonconst=no])
263 ])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500264 else
265 dnl When compiling GNU libiconv on a system that does not have iconv yet,
266 dnl pick the POSIX compliant declaration without 'const'.
Patrick Williams7784c422022-11-17 07:29:11 -0600267 gl_cv_iconv_nonconst=yes
Andrew Geissler82c905d2020-04-13 13:39:40 -0500268 fi
Patrick Williams7784c422022-11-17 07:29:11 -0600269 if test $gl_cv_iconv_nonconst = yes; then
270 iconv_arg1=""
271 else
272 iconv_arg1="const"
273 fi
274 AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1],
Andrew Geissler82c905d2020-04-13 13:39:40 -0500275 [Define as const if the declaration of iconv() needs const.])
276 dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
277 m4_ifdef([gl_ICONV_H_DEFAULTS],
278 [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
Patrick Williams7784c422022-11-17 07:29:11 -0600279 if test $gl_cv_iconv_nonconst != yes; then
Andrew Geissler82c905d2020-04-13 13:39:40 -0500280 ICONV_CONST="const"
281 fi
282 ])
283])