xref: /freebsd/sys/contrib/openzfs/config/iconv.m4 (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
1*eda14cbcSMatt Macy# iconv.m4 serial 21
2*eda14cbcSMatt Macydnl Copyright (C) 2000-2002, 2007-2014, 2016-2019 Free Software Foundation,
3*eda14cbcSMatt Macydnl Inc.
4*eda14cbcSMatt Macydnl This file is free software; the Free Software Foundation
5*eda14cbcSMatt Macydnl gives unlimited permission to copy and/or distribute it,
6*eda14cbcSMatt Macydnl with or without modifications, as long as this notice is preserved.
7*eda14cbcSMatt Macy
8*eda14cbcSMatt Macydnl From Bruno Haible.
9*eda14cbcSMatt Macy
10*eda14cbcSMatt MacyAC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
11*eda14cbcSMatt Macy[
12*eda14cbcSMatt Macy  dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
13*eda14cbcSMatt Macy  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
14*eda14cbcSMatt Macy  AC_REQUIRE([AC_LIB_RPATH])
15*eda14cbcSMatt Macy
16*eda14cbcSMatt Macy  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
17*eda14cbcSMatt Macy  dnl accordingly.
18*eda14cbcSMatt Macy  AC_LIB_LINKFLAGS_BODY([iconv])
19*eda14cbcSMatt Macy])
20*eda14cbcSMatt Macy
21*eda14cbcSMatt MacyAC_DEFUN([AM_ICONV_LINK],
22*eda14cbcSMatt Macy[
23*eda14cbcSMatt Macy  dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
24*eda14cbcSMatt Macy  dnl those with the standalone portable GNU libiconv installed).
25*eda14cbcSMatt Macy  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
26*eda14cbcSMatt Macy
27*eda14cbcSMatt Macy  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
28*eda14cbcSMatt Macy  dnl accordingly.
29*eda14cbcSMatt Macy  AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
30*eda14cbcSMatt Macy
31*eda14cbcSMatt Macy  dnl Add $INCICONV to CPPFLAGS before performing the following checks,
32*eda14cbcSMatt Macy  dnl so that if libiconv is installed, it will be used (unless disabled
33*eda14cbcSMatt Macy  dnl via --without-libiconv-prefix).  The first AC_LINK_IFELSE will
34*eda14cbcSMatt Macy  dnl then fail, the second AC_LINK_IFELSE will succeed.
35*eda14cbcSMatt Macy  am_save_CPPFLAGS="$CPPFLAGS"
36*eda14cbcSMatt Macy  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
37*eda14cbcSMatt Macy
38*eda14cbcSMatt Macy  AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
39*eda14cbcSMatt Macy    am_cv_func_iconv="no, consider installing GNU libiconv"
40*eda14cbcSMatt Macy    am_cv_lib_iconv=no
41*eda14cbcSMatt Macy    AC_LINK_IFELSE(
42*eda14cbcSMatt Macy      [AC_LANG_PROGRAM(
43*eda14cbcSMatt Macy         [[
44*eda14cbcSMatt Macy#include <stdlib.h>
45*eda14cbcSMatt Macy#include <iconv.h>
46*eda14cbcSMatt Macy         ]],
47*eda14cbcSMatt Macy         [[iconv_t cd = iconv_open("","");
48*eda14cbcSMatt Macy           iconv(cd,NULL,NULL,NULL,NULL);
49*eda14cbcSMatt Macy           iconv_close(cd);]])],
50*eda14cbcSMatt Macy      [am_cv_func_iconv=yes])
51*eda14cbcSMatt Macy    if test "$am_cv_func_iconv" != yes; then
52*eda14cbcSMatt Macy      am_save_LIBS="$LIBS"
53*eda14cbcSMatt Macy      LIBS="$LIBS $LIBICONV"
54*eda14cbcSMatt Macy      AC_LINK_IFELSE(
55*eda14cbcSMatt Macy        [AC_LANG_PROGRAM(
56*eda14cbcSMatt Macy           [[
57*eda14cbcSMatt Macy#include <stdlib.h>
58*eda14cbcSMatt Macy#include <iconv.h>
59*eda14cbcSMatt Macy           ]],
60*eda14cbcSMatt Macy           [[iconv_t cd = iconv_open("","");
61*eda14cbcSMatt Macy             iconv(cd,NULL,NULL,NULL,NULL);
62*eda14cbcSMatt Macy             iconv_close(cd);]])],
63*eda14cbcSMatt Macy        [am_cv_lib_iconv=yes]
64*eda14cbcSMatt Macy        [am_cv_func_iconv=yes])
65*eda14cbcSMatt Macy      LIBS="$am_save_LIBS"
66*eda14cbcSMatt Macy    fi
67*eda14cbcSMatt Macy  ])
68*eda14cbcSMatt Macy  if test "$am_cv_func_iconv" = yes; then
69*eda14cbcSMatt Macy    AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
70*eda14cbcSMatt Macy      dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
71*eda14cbcSMatt Macy      dnl Solaris 10.
72*eda14cbcSMatt Macy      am_save_LIBS="$LIBS"
73*eda14cbcSMatt Macy      if test $am_cv_lib_iconv = yes; then
74*eda14cbcSMatt Macy        LIBS="$LIBS $LIBICONV"
75*eda14cbcSMatt Macy      fi
76*eda14cbcSMatt Macy      am_cv_func_iconv_works=no
77*eda14cbcSMatt Macy      for ac_iconv_const in '' 'const'; do
78*eda14cbcSMatt Macy        AC_RUN_IFELSE(
79*eda14cbcSMatt Macy          [AC_LANG_PROGRAM(
80*eda14cbcSMatt Macy             [[
81*eda14cbcSMatt Macy#include <iconv.h>
82*eda14cbcSMatt Macy#include <string.h>
83*eda14cbcSMatt Macy
84*eda14cbcSMatt Macy#ifndef ICONV_CONST
85*eda14cbcSMatt Macy# define ICONV_CONST $ac_iconv_const
86*eda14cbcSMatt Macy#endif
87*eda14cbcSMatt Macy             ]],
88*eda14cbcSMatt Macy             [[int result = 0;
89*eda14cbcSMatt Macy  /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
90*eda14cbcSMatt Macy     returns.  */
91*eda14cbcSMatt Macy  {
92*eda14cbcSMatt Macy    iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
93*eda14cbcSMatt Macy    if (cd_utf8_to_88591 != (iconv_t)(-1))
94*eda14cbcSMatt Macy      {
95*eda14cbcSMatt Macy        static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
96*eda14cbcSMatt Macy        char buf[10];
97*eda14cbcSMatt Macy        ICONV_CONST char *inptr = input;
98*eda14cbcSMatt Macy        size_t inbytesleft = strlen (input);
99*eda14cbcSMatt Macy        char *outptr = buf;
100*eda14cbcSMatt Macy        size_t outbytesleft = sizeof (buf);
101*eda14cbcSMatt Macy        size_t res = iconv (cd_utf8_to_88591,
102*eda14cbcSMatt Macy                            &inptr, &inbytesleft,
103*eda14cbcSMatt Macy                            &outptr, &outbytesleft);
104*eda14cbcSMatt Macy        if (res == 0)
105*eda14cbcSMatt Macy          result |= 1;
106*eda14cbcSMatt Macy        iconv_close (cd_utf8_to_88591);
107*eda14cbcSMatt Macy      }
108*eda14cbcSMatt Macy  }
109*eda14cbcSMatt Macy  /* Test against Solaris 10 bug: Failures are not distinguishable from
110*eda14cbcSMatt Macy     successful returns.  */
111*eda14cbcSMatt Macy  {
112*eda14cbcSMatt Macy    iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
113*eda14cbcSMatt Macy    if (cd_ascii_to_88591 != (iconv_t)(-1))
114*eda14cbcSMatt Macy      {
115*eda14cbcSMatt Macy        static ICONV_CONST char input[] = "\263";
116*eda14cbcSMatt Macy        char buf[10];
117*eda14cbcSMatt Macy        ICONV_CONST char *inptr = input;
118*eda14cbcSMatt Macy        size_t inbytesleft = strlen (input);
119*eda14cbcSMatt Macy        char *outptr = buf;
120*eda14cbcSMatt Macy        size_t outbytesleft = sizeof (buf);
121*eda14cbcSMatt Macy        size_t res = iconv (cd_ascii_to_88591,
122*eda14cbcSMatt Macy                            &inptr, &inbytesleft,
123*eda14cbcSMatt Macy                            &outptr, &outbytesleft);
124*eda14cbcSMatt Macy        if (res == 0)
125*eda14cbcSMatt Macy          result |= 2;
126*eda14cbcSMatt Macy        iconv_close (cd_ascii_to_88591);
127*eda14cbcSMatt Macy      }
128*eda14cbcSMatt Macy  }
129*eda14cbcSMatt Macy  /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
130*eda14cbcSMatt Macy  {
131*eda14cbcSMatt Macy    iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
132*eda14cbcSMatt Macy    if (cd_88591_to_utf8 != (iconv_t)(-1))
133*eda14cbcSMatt Macy      {
134*eda14cbcSMatt Macy        static ICONV_CONST char input[] = "\304";
135*eda14cbcSMatt Macy        static char buf[2] = { (char)0xDE, (char)0xAD };
136*eda14cbcSMatt Macy        ICONV_CONST char *inptr = input;
137*eda14cbcSMatt Macy        size_t inbytesleft = 1;
138*eda14cbcSMatt Macy        char *outptr = buf;
139*eda14cbcSMatt Macy        size_t outbytesleft = 1;
140*eda14cbcSMatt Macy        size_t res = iconv (cd_88591_to_utf8,
141*eda14cbcSMatt Macy                            &inptr, &inbytesleft,
142*eda14cbcSMatt Macy                            &outptr, &outbytesleft);
143*eda14cbcSMatt Macy        if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
144*eda14cbcSMatt Macy          result |= 4;
145*eda14cbcSMatt Macy        iconv_close (cd_88591_to_utf8);
146*eda14cbcSMatt Macy      }
147*eda14cbcSMatt Macy  }
148*eda14cbcSMatt Macy#if 0 /* This bug could be worked around by the caller.  */
149*eda14cbcSMatt Macy  /* Test against HP-UX 11.11 bug: Positive return value instead of 0.  */
150*eda14cbcSMatt Macy  {
151*eda14cbcSMatt Macy    iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
152*eda14cbcSMatt Macy    if (cd_88591_to_utf8 != (iconv_t)(-1))
153*eda14cbcSMatt Macy      {
154*eda14cbcSMatt Macy        static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
155*eda14cbcSMatt Macy        char buf[50];
156*eda14cbcSMatt Macy        ICONV_CONST char *inptr = input;
157*eda14cbcSMatt Macy        size_t inbytesleft = strlen (input);
158*eda14cbcSMatt Macy        char *outptr = buf;
159*eda14cbcSMatt Macy        size_t outbytesleft = sizeof (buf);
160*eda14cbcSMatt Macy        size_t res = iconv (cd_88591_to_utf8,
161*eda14cbcSMatt Macy                            &inptr, &inbytesleft,
162*eda14cbcSMatt Macy                            &outptr, &outbytesleft);
163*eda14cbcSMatt Macy        if ((int)res > 0)
164*eda14cbcSMatt Macy          result |= 8;
165*eda14cbcSMatt Macy        iconv_close (cd_88591_to_utf8);
166*eda14cbcSMatt Macy      }
167*eda14cbcSMatt Macy  }
168*eda14cbcSMatt Macy#endif
169*eda14cbcSMatt Macy  /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
170*eda14cbcSMatt Macy     provided.  */
171*eda14cbcSMatt Macy  {
172*eda14cbcSMatt Macy    /* Try standardized names.  */
173*eda14cbcSMatt Macy    iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
174*eda14cbcSMatt Macy    /* Try IRIX, OSF/1 names.  */
175*eda14cbcSMatt Macy    iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
176*eda14cbcSMatt Macy    /* Try AIX names.  */
177*eda14cbcSMatt Macy    iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
178*eda14cbcSMatt Macy    /* Try HP-UX names.  */
179*eda14cbcSMatt Macy    iconv_t cd4 = iconv_open ("utf8", "eucJP");
180*eda14cbcSMatt Macy    if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
181*eda14cbcSMatt Macy        && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
182*eda14cbcSMatt Macy      result |= 16;
183*eda14cbcSMatt Macy    if (cd1 != (iconv_t)(-1))
184*eda14cbcSMatt Macy      iconv_close (cd1);
185*eda14cbcSMatt Macy    if (cd2 != (iconv_t)(-1))
186*eda14cbcSMatt Macy      iconv_close (cd2);
187*eda14cbcSMatt Macy    if (cd3 != (iconv_t)(-1))
188*eda14cbcSMatt Macy      iconv_close (cd3);
189*eda14cbcSMatt Macy    if (cd4 != (iconv_t)(-1))
190*eda14cbcSMatt Macy      iconv_close (cd4);
191*eda14cbcSMatt Macy  }
192*eda14cbcSMatt Macy  return result;
193*eda14cbcSMatt Macy]])],
194*eda14cbcSMatt Macy          [am_cv_func_iconv_works=yes], ,
195*eda14cbcSMatt Macy          [case "$host_os" in
196*eda14cbcSMatt Macy             aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
197*eda14cbcSMatt Macy             *)            am_cv_func_iconv_works="guessing yes" ;;
198*eda14cbcSMatt Macy           esac])
199*eda14cbcSMatt Macy        test "$am_cv_func_iconv_works" = no || break
200*eda14cbcSMatt Macy      done
201*eda14cbcSMatt Macy      LIBS="$am_save_LIBS"
202*eda14cbcSMatt Macy    ])
203*eda14cbcSMatt Macy    case "$am_cv_func_iconv_works" in
204*eda14cbcSMatt Macy      *no) am_func_iconv=no am_cv_lib_iconv=no ;;
205*eda14cbcSMatt Macy      *)   am_func_iconv=yes ;;
206*eda14cbcSMatt Macy    esac
207*eda14cbcSMatt Macy  else
208*eda14cbcSMatt Macy    am_func_iconv=no am_cv_lib_iconv=no
209*eda14cbcSMatt Macy  fi
210*eda14cbcSMatt Macy  if test "$am_func_iconv" = yes; then
211*eda14cbcSMatt Macy    AC_DEFINE([HAVE_ICONV], [1],
212*eda14cbcSMatt Macy      [Define if you have the iconv() function and it works.])
213*eda14cbcSMatt Macy  fi
214*eda14cbcSMatt Macy  if test "$am_cv_lib_iconv" = yes; then
215*eda14cbcSMatt Macy    AC_MSG_CHECKING([how to link with libiconv])
216*eda14cbcSMatt Macy    AC_MSG_RESULT([$LIBICONV])
217*eda14cbcSMatt Macy  else
218*eda14cbcSMatt Macy    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
219*eda14cbcSMatt Macy    dnl either.
220*eda14cbcSMatt Macy    CPPFLAGS="$am_save_CPPFLAGS"
221*eda14cbcSMatt Macy    LIBICONV=
222*eda14cbcSMatt Macy    LTLIBICONV=
223*eda14cbcSMatt Macy  fi
224*eda14cbcSMatt Macy  AC_SUBST([LIBICONV])
225*eda14cbcSMatt Macy  AC_SUBST([LTLIBICONV])
226*eda14cbcSMatt Macy])
227*eda14cbcSMatt Macy
228*eda14cbcSMatt Macydnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
229*eda14cbcSMatt Macydnl avoid warnings like
230*eda14cbcSMatt Macydnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
231*eda14cbcSMatt Macydnl This is tricky because of the way 'aclocal' is implemented:
232*eda14cbcSMatt Macydnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
233*eda14cbcSMatt Macydnl   Otherwise aclocal's initial scan pass would miss the macro definition.
234*eda14cbcSMatt Macydnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
235*eda14cbcSMatt Macydnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
236*eda14cbcSMatt Macydnl   warnings.
237*eda14cbcSMatt Macym4_define([gl_iconv_AC_DEFUN],
238*eda14cbcSMatt Macy  m4_version_prereq([2.64],
239*eda14cbcSMatt Macy    [[AC_DEFUN_ONCE(
240*eda14cbcSMatt Macy        [$1], [$2])]],
241*eda14cbcSMatt Macy    [m4_ifdef([gl_00GNULIB],
242*eda14cbcSMatt Macy       [[AC_DEFUN_ONCE(
243*eda14cbcSMatt Macy           [$1], [$2])]],
244*eda14cbcSMatt Macy       [[AC_DEFUN(
245*eda14cbcSMatt Macy           [$1], [$2])]])]))
246*eda14cbcSMatt Macygl_iconv_AC_DEFUN([AM_ICONV],
247*eda14cbcSMatt Macy[
248*eda14cbcSMatt Macy  AM_ICONV_LINK
249*eda14cbcSMatt Macy  if test "$am_cv_func_iconv" = yes; then
250*eda14cbcSMatt Macy    AC_MSG_CHECKING([for iconv declaration])
251*eda14cbcSMatt Macy    AC_CACHE_VAL([am_cv_proto_iconv], [
252*eda14cbcSMatt Macy      AC_COMPILE_IFELSE(
253*eda14cbcSMatt Macy        [AC_LANG_PROGRAM(
254*eda14cbcSMatt Macy           [[
255*eda14cbcSMatt Macy#include <stdlib.h>
256*eda14cbcSMatt Macy#include <iconv.h>
257*eda14cbcSMatt Macyextern
258*eda14cbcSMatt Macy#ifdef __cplusplus
259*eda14cbcSMatt Macy"C"
260*eda14cbcSMatt Macy#endif
261*eda14cbcSMatt Macy#if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
262*eda14cbcSMatt Macysize_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
263*eda14cbcSMatt Macy#else
264*eda14cbcSMatt Macysize_t iconv();
265*eda14cbcSMatt Macy#endif
266*eda14cbcSMatt Macy           ]],
267*eda14cbcSMatt Macy           [[]])],
268*eda14cbcSMatt Macy        [am_cv_proto_iconv_arg1=""],
269*eda14cbcSMatt Macy        [am_cv_proto_iconv_arg1="const"])
270*eda14cbcSMatt Macy      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*eda14cbcSMatt Macy    am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
272*eda14cbcSMatt Macy    AC_MSG_RESULT([$am_cv_proto_iconv])
273*eda14cbcSMatt Macy  else
274*eda14cbcSMatt Macy    dnl When compiling GNU libiconv on a system that does not have iconv yet,
275*eda14cbcSMatt Macy    dnl pick the POSIX compliant declaration without 'const'.
276*eda14cbcSMatt Macy    am_cv_proto_iconv_arg1=""
277*eda14cbcSMatt Macy  fi
278*eda14cbcSMatt Macy  AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
279*eda14cbcSMatt Macy    [Define as const if the declaration of iconv() needs const.])
280*eda14cbcSMatt Macy  dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
281*eda14cbcSMatt Macy  m4_ifdef([gl_ICONV_H_DEFAULTS],
282*eda14cbcSMatt Macy    [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
283*eda14cbcSMatt Macy     if test -n "$am_cv_proto_iconv_arg1"; then
284*eda14cbcSMatt Macy       ICONV_CONST="const"
285*eda14cbcSMatt Macy     fi
286*eda14cbcSMatt Macy    ])
287*eda14cbcSMatt Macy])
288