xref: /freebsd/sys/contrib/openzfs/config/iconv.m4 (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1# iconv.m4 serial 21
2dnl Copyright (C) 2000-2002, 2007-2014, 2016-2019 Free Software Foundation,
3dnl 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
10AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
11[
12  dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
13  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
14  AC_REQUIRE([AC_LIB_RPATH])
15
16  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
17  dnl accordingly.
18  AC_LIB_LINKFLAGS_BODY([iconv])
19])
20
21AC_DEFUN([AM_ICONV_LINK],
22[
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
26
27  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
28  dnl accordingly.
29  AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
30
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])
37
38  AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
39    am_cv_func_iconv="no, consider installing GNU libiconv"
40    am_cv_lib_iconv=no
41    AC_LINK_IFELSE(
42      [AC_LANG_PROGRAM(
43         [[
44#include <stdlib.h>
45#include <iconv.h>
46         ]],
47         [[iconv_t cd = iconv_open("","");
48           iconv(cd,NULL,NULL,NULL,NULL);
49           iconv_close(cd);]])],
50      [am_cv_func_iconv=yes])
51    if test "$am_cv_func_iconv" != yes; then
52      am_save_LIBS="$LIBS"
53      LIBS="$LIBS $LIBICONV"
54      AC_LINK_IFELSE(
55        [AC_LANG_PROGRAM(
56           [[
57#include <stdlib.h>
58#include <iconv.h>
59           ]],
60           [[iconv_t cd = iconv_open("","");
61             iconv(cd,NULL,NULL,NULL,NULL);
62             iconv_close(cd);]])],
63        [am_cv_lib_iconv=yes]
64        [am_cv_func_iconv=yes])
65      LIBS="$am_save_LIBS"
66    fi
67  ])
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,
71      dnl Solaris 10.
72      am_save_LIBS="$LIBS"
73      if test $am_cv_lib_iconv = yes; then
74        LIBS="$LIBS $LIBICONV"
75      fi
76      am_cv_func_iconv_works=no
77      for ac_iconv_const in '' 'const'; do
78        AC_RUN_IFELSE(
79          [AC_LANG_PROGRAM(
80             [[
81#include <iconv.h>
82#include <string.h>
83
84#ifndef ICONV_CONST
85# define ICONV_CONST $ac_iconv_const
86#endif
87             ]],
88             [[int result = 0;
89  /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
90     returns.  */
91  {
92    iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
93    if (cd_utf8_to_88591 != (iconv_t)(-1))
94      {
95        static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
96        char buf[10];
97        ICONV_CONST char *inptr = input;
98        size_t inbytesleft = strlen (input);
99        char *outptr = buf;
100        size_t outbytesleft = sizeof (buf);
101        size_t res = iconv (cd_utf8_to_88591,
102                            &inptr, &inbytesleft,
103                            &outptr, &outbytesleft);
104        if (res == 0)
105          result |= 1;
106        iconv_close (cd_utf8_to_88591);
107      }
108  }
109  /* Test against Solaris 10 bug: Failures are not distinguishable from
110     successful returns.  */
111  {
112    iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
113    if (cd_ascii_to_88591 != (iconv_t)(-1))
114      {
115        static ICONV_CONST char input[] = "\263";
116        char buf[10];
117        ICONV_CONST char *inptr = input;
118        size_t inbytesleft = strlen (input);
119        char *outptr = buf;
120        size_t outbytesleft = sizeof (buf);
121        size_t res = iconv (cd_ascii_to_88591,
122                            &inptr, &inbytesleft,
123                            &outptr, &outbytesleft);
124        if (res == 0)
125          result |= 2;
126        iconv_close (cd_ascii_to_88591);
127      }
128  }
129  /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
130  {
131    iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
132    if (cd_88591_to_utf8 != (iconv_t)(-1))
133      {
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;
138        char *outptr = buf;
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)
144          result |= 4;
145        iconv_close (cd_88591_to_utf8);
146      }
147  }
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.  */
150  {
151    iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
152    if (cd_88591_to_utf8 != (iconv_t)(-1))
153      {
154        static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
155        char buf[50];
156        ICONV_CONST char *inptr = input;
157        size_t inbytesleft = strlen (input);
158        char *outptr = buf;
159        size_t outbytesleft = sizeof (buf);
160        size_t res = iconv (cd_88591_to_utf8,
161                            &inptr, &inbytesleft,
162                            &outptr, &outbytesleft);
163        if ((int)res > 0)
164          result |= 8;
165        iconv_close (cd_88591_to_utf8);
166      }
167  }
168#endif
169  /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
170     provided.  */
171  {
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");
176    /* Try AIX names.  */
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))
182      result |= 16;
183    if (cd1 != (iconv_t)(-1))
184      iconv_close (cd1);
185    if (cd2 != (iconv_t)(-1))
186      iconv_close (cd2);
187    if (cd3 != (iconv_t)(-1))
188      iconv_close (cd3);
189    if (cd4 != (iconv_t)(-1))
190      iconv_close (cd4);
191  }
192  return result;
193]])],
194          [am_cv_func_iconv_works=yes], ,
195          [case "$host_os" in
196             aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
197             *)            am_cv_func_iconv_works="guessing yes" ;;
198           esac])
199        test "$am_cv_func_iconv_works" = no || break
200      done
201      LIBS="$am_save_LIBS"
202    ])
203    case "$am_cv_func_iconv_works" in
204      *no) am_func_iconv=no am_cv_lib_iconv=no ;;
205      *)   am_func_iconv=yes ;;
206    esac
207  else
208    am_func_iconv=no am_cv_lib_iconv=no
209  fi
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.])
213  fi
214  if test "$am_cv_lib_iconv" = yes; then
215    AC_MSG_CHECKING([how to link with libiconv])
216    AC_MSG_RESULT([$LIBICONV])
217  else
218    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
219    dnl either.
220    CPPFLAGS="$am_save_CPPFLAGS"
221    LIBICONV=
222    LTLIBICONV=
223  fi
224  AC_SUBST([LIBICONV])
225  AC_SUBST([LTLIBICONV])
226])
227
228dnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
229dnl avoid warnings like
230dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
231dnl This is tricky because of the way 'aclocal' is implemented:
232dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
233dnl   Otherwise aclocal's initial scan pass would miss the macro definition.
234dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
235dnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
236dnl   warnings.
237m4_define([gl_iconv_AC_DEFUN],
238  m4_version_prereq([2.64],
239    [[AC_DEFUN_ONCE(
240        [$1], [$2])]],
241    [m4_ifdef([gl_00GNULIB],
242       [[AC_DEFUN_ONCE(
243           [$1], [$2])]],
244       [[AC_DEFUN(
245           [$1], [$2])]])]))
246gl_iconv_AC_DEFUN([AM_ICONV],
247[
248  AM_ICONV_LINK
249  if test "$am_cv_func_iconv" = yes; then
250    AC_MSG_CHECKING([for iconv declaration])
251    AC_CACHE_VAL([am_cv_proto_iconv], [
252      AC_COMPILE_IFELSE(
253        [AC_LANG_PROGRAM(
254           [[
255#include <stdlib.h>
256#include <iconv.h>
257extern
258#ifdef __cplusplus
259"C"
260#endif
261#if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
262size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
263#else
264size_t iconv();
265#endif
266           ]],
267           [[]])],
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])
273  else
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=""
277  fi
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
284       ICONV_CONST="const"
285     fi
286    ])
287])
288