1dnl A placeholder for ISO C99 <wchar.h>, for platforms that have issues.
2
3dnl Copyright (C) 2007-2021 Free Software Foundation, 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 Written by Eric Blake.
9
10# wchar_h.m4 serial 50
11
12AC_DEFUN([gl_WCHAR_H],
13[
14  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
15  AC_REQUIRE([gl_WCHAR_H_INLINE_OK])
16  dnl Prepare for creating substitute <wchar.h>.
17  dnl Check for <wchar.h> (missing in Linux uClibc when built without wide
18  dnl character support).
19  dnl <wchar.h> is always overridden, because of GNULIB_POSIXCHECK.
20  gl_CHECK_NEXT_HEADERS([wchar.h])
21  if test $ac_cv_header_wchar_h = yes; then
22    HAVE_WCHAR_H=1
23  else
24    HAVE_WCHAR_H=0
25  fi
26  AC_SUBST([HAVE_WCHAR_H])
27
28  AC_REQUIRE([gl_FEATURES_H])
29
30  AC_REQUIRE([gt_TYPE_WINT_T])
31  if test $gt_cv_c_wint_t = yes; then
32    HAVE_WINT_T=1
33  else
34    HAVE_WINT_T=0
35  fi
36  AC_SUBST([HAVE_WINT_T])
37
38  AC_REQUIRE([gl_TYPE_WINT_T_PREREQ])
39
40  dnl Check for declarations of anything we want to poison if the
41  dnl corresponding gnulib module is not in use.
42  gl_WARN_ON_USE_PREPARE([[
43      #include <wchar.h>
44    ]],
45    [btowc wctob mbsinit mbrtowc mbrlen mbsrtowcs mbsnrtowcs wcrtomb
46     wcsrtombs wcsnrtombs wcwidth
47     wmemchr wmemcmp wmemcpy wmemmove wmempcpy wmemset
48     wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat wcscmp
49     wcsncmp wcscasecmp wcsncasecmp wcscoll wcsxfrm wcsdup wcschr wcsrchr
50     wcscspn wcsspn wcspbrk wcsstr wcstok wcswidth wcsftime
51    ])
52
53  AC_REQUIRE([AC_C_RESTRICT])
54
55  AC_CHECK_DECLS([wcsdup], [], [], [[
56      #include <wchar.h>
57    ]])
58  if test $ac_cv_have_decl_wcsdup = no; then
59    HAVE_DECL_WCSDUP=0
60  fi
61])
62
63dnl Check whether <wchar.h> is usable at all.
64AC_DEFUN([gl_WCHAR_H_INLINE_OK],
65[
66  dnl Test whether <wchar.h> suffers due to the transition from '__inline' to
67  dnl 'gnu_inline'. See <https://sourceware.org/bugzilla/show_bug.cgi?id=4022>
68  dnl and <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42440>. In summary,
69  dnl glibc version 2.5 or older, together with gcc version 4.3 or newer and
70  dnl the option -std=c99 or -std=gnu99, leads to a broken <wchar.h>.
71  AC_REQUIRE([AC_CANONICAL_HOST])
72  AC_CACHE_CHECK([whether <wchar.h> uses 'inline' correctly],
73    [gl_cv_header_wchar_h_correct_inline],
74    [gl_cv_header_wchar_h_correct_inline=yes
75     case "$host_os" in
76       *-gnu* | gnu*)
77         AC_LANG_CONFTEST([
78           AC_LANG_SOURCE([[
79             #define wcstod renamed_wcstod
80             #include <wchar.h>
81             extern int zero (void);
82             int main () { return zero(); }
83           ]])])
84         dnl Do not rename the object file from conftest.$ac_objext to
85         dnl conftest1.$ac_objext, as this will cause the link to fail on
86         dnl z/OS when using the XPLINK object format (due to duplicate
87         dnl CSECT names). Instead, temporarily redefine $ac_compile so
88         dnl that the object file has the latter name from the start.
89         save_ac_compile="$ac_compile"
90         ac_compile=`echo "$save_ac_compile" | sed s/conftest/conftest1/`
91         if echo '#include "conftest.c"' >conftest1.c \
92            && AC_TRY_EVAL([ac_compile]); then
93           AC_LANG_CONFTEST([
94             AC_LANG_SOURCE([[
95               #define wcstod renamed_wcstod
96               #include <wchar.h>
97               int zero (void) { return 0; }
98             ]])])
99           dnl See note above about renaming object files.
100           ac_compile=`echo "$save_ac_compile" | sed s/conftest/conftest2/`
101           if echo '#include "conftest.c"' >conftest2.c \
102              && AC_TRY_EVAL([ac_compile]); then
103             if $CC -o conftest$ac_exeext $CFLAGS $LDFLAGS conftest1.$ac_objext conftest2.$ac_objext $LIBS >&AS_MESSAGE_LOG_FD 2>&1; then
104               :
105             else
106               gl_cv_header_wchar_h_correct_inline=no
107             fi
108           fi
109         fi
110         ac_compile="$save_ac_compile"
111         rm -f conftest[12].c conftest[12].$ac_objext conftest$ac_exeext
112         ;;
113     esac
114    ])
115  if test $gl_cv_header_wchar_h_correct_inline = no; then
116    AC_MSG_ERROR([<wchar.h> cannot be used with this compiler ($CC $CFLAGS $CPPFLAGS).
117This is a known interoperability problem of glibc <= 2.5 with gcc >= 4.3 in
118C99 mode. You have four options:
119  - Add the flag -fgnu89-inline to CC and reconfigure, or
120  - Fix your include files, using parts of
121    <https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=b037a293a48718af30d706c2e18c929d0e69a621>, or
122  - Use a gcc version older than 4.3, or
123  - Don't use the flags -std=c99 or -std=gnu99.
124Configuration aborted.])
125  fi
126])
127
128AC_DEFUN([gl_WCHAR_MODULE_INDICATOR],
129[
130  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
131  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
132  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
133  dnl Define it also as a C macro, for the benefit of the unit tests.
134  gl_MODULE_INDICATOR_FOR_TESTS([$1])
135])
136
137AC_DEFUN([gl_WCHAR_H_DEFAULTS],
138[
139  GNULIB_BTOWC=0;       AC_SUBST([GNULIB_BTOWC])
140  GNULIB_WCTOB=0;       AC_SUBST([GNULIB_WCTOB])
141  GNULIB_MBSINIT=0;     AC_SUBST([GNULIB_MBSINIT])
142  GNULIB_MBRTOWC=0;     AC_SUBST([GNULIB_MBRTOWC])
143  GNULIB_MBRLEN=0;      AC_SUBST([GNULIB_MBRLEN])
144  GNULIB_MBSRTOWCS=0;   AC_SUBST([GNULIB_MBSRTOWCS])
145  GNULIB_MBSNRTOWCS=0;  AC_SUBST([GNULIB_MBSNRTOWCS])
146  GNULIB_WCRTOMB=0;     AC_SUBST([GNULIB_WCRTOMB])
147  GNULIB_WCSRTOMBS=0;   AC_SUBST([GNULIB_WCSRTOMBS])
148  GNULIB_WCSNRTOMBS=0;  AC_SUBST([GNULIB_WCSNRTOMBS])
149  GNULIB_WCWIDTH=0;     AC_SUBST([GNULIB_WCWIDTH])
150  GNULIB_WMEMCHR=0;     AC_SUBST([GNULIB_WMEMCHR])
151  GNULIB_WMEMCMP=0;     AC_SUBST([GNULIB_WMEMCMP])
152  GNULIB_WMEMCPY=0;     AC_SUBST([GNULIB_WMEMCPY])
153  GNULIB_WMEMMOVE=0;    AC_SUBST([GNULIB_WMEMMOVE])
154  GNULIB_WMEMPCPY=0;    AC_SUBST([GNULIB_WMEMPCPY])
155  GNULIB_WMEMSET=0;     AC_SUBST([GNULIB_WMEMSET])
156  GNULIB_WCSLEN=0;      AC_SUBST([GNULIB_WCSLEN])
157  GNULIB_WCSNLEN=0;     AC_SUBST([GNULIB_WCSNLEN])
158  GNULIB_WCSCPY=0;      AC_SUBST([GNULIB_WCSCPY])
159  GNULIB_WCPCPY=0;      AC_SUBST([GNULIB_WCPCPY])
160  GNULIB_WCSNCPY=0;     AC_SUBST([GNULIB_WCSNCPY])
161  GNULIB_WCPNCPY=0;     AC_SUBST([GNULIB_WCPNCPY])
162  GNULIB_WCSCAT=0;      AC_SUBST([GNULIB_WCSCAT])
163  GNULIB_WCSNCAT=0;     AC_SUBST([GNULIB_WCSNCAT])
164  GNULIB_WCSCMP=0;      AC_SUBST([GNULIB_WCSCMP])
165  GNULIB_WCSNCMP=0;     AC_SUBST([GNULIB_WCSNCMP])
166  GNULIB_WCSCASECMP=0;  AC_SUBST([GNULIB_WCSCASECMP])
167  GNULIB_WCSNCASECMP=0; AC_SUBST([GNULIB_WCSNCASECMP])
168  GNULIB_WCSCOLL=0;     AC_SUBST([GNULIB_WCSCOLL])
169  GNULIB_WCSXFRM=0;     AC_SUBST([GNULIB_WCSXFRM])
170  GNULIB_WCSDUP=0;      AC_SUBST([GNULIB_WCSDUP])
171  GNULIB_WCSCHR=0;      AC_SUBST([GNULIB_WCSCHR])
172  GNULIB_WCSRCHR=0;     AC_SUBST([GNULIB_WCSRCHR])
173  GNULIB_WCSCSPN=0;     AC_SUBST([GNULIB_WCSCSPN])
174  GNULIB_WCSSPN=0;      AC_SUBST([GNULIB_WCSSPN])
175  GNULIB_WCSPBRK=0;     AC_SUBST([GNULIB_WCSPBRK])
176  GNULIB_WCSSTR=0;      AC_SUBST([GNULIB_WCSSTR])
177  GNULIB_WCSTOK=0;      AC_SUBST([GNULIB_WCSTOK])
178  GNULIB_WCSWIDTH=0;    AC_SUBST([GNULIB_WCSWIDTH])
179  GNULIB_WCSFTIME=0;    AC_SUBST([GNULIB_WCSFTIME])
180  dnl Support Microsoft deprecated alias function names by default.
181  GNULIB_MDA_WCSDUP=1;  AC_SUBST([GNULIB_MDA_WCSDUP])
182  dnl Assume proper GNU behavior unless another module says otherwise.
183  HAVE_BTOWC=1;         AC_SUBST([HAVE_BTOWC])
184  HAVE_MBSINIT=1;       AC_SUBST([HAVE_MBSINIT])
185  HAVE_MBRTOWC=1;       AC_SUBST([HAVE_MBRTOWC])
186  HAVE_MBRLEN=1;        AC_SUBST([HAVE_MBRLEN])
187  HAVE_MBSRTOWCS=1;     AC_SUBST([HAVE_MBSRTOWCS])
188  HAVE_MBSNRTOWCS=1;    AC_SUBST([HAVE_MBSNRTOWCS])
189  HAVE_WCRTOMB=1;       AC_SUBST([HAVE_WCRTOMB])
190  HAVE_WCSRTOMBS=1;     AC_SUBST([HAVE_WCSRTOMBS])
191  HAVE_WCSNRTOMBS=1;    AC_SUBST([HAVE_WCSNRTOMBS])
192  HAVE_WMEMCHR=1;       AC_SUBST([HAVE_WMEMCHR])
193  HAVE_WMEMCMP=1;       AC_SUBST([HAVE_WMEMCMP])
194  HAVE_WMEMCPY=1;       AC_SUBST([HAVE_WMEMCPY])
195  HAVE_WMEMMOVE=1;      AC_SUBST([HAVE_WMEMMOVE])
196  HAVE_WMEMPCPY=1;      AC_SUBST([HAVE_WMEMPCPY])
197  HAVE_WMEMSET=1;       AC_SUBST([HAVE_WMEMSET])
198  HAVE_WCSLEN=1;        AC_SUBST([HAVE_WCSLEN])
199  HAVE_WCSNLEN=1;       AC_SUBST([HAVE_WCSNLEN])
200  HAVE_WCSCPY=1;        AC_SUBST([HAVE_WCSCPY])
201  HAVE_WCPCPY=1;        AC_SUBST([HAVE_WCPCPY])
202  HAVE_WCSNCPY=1;       AC_SUBST([HAVE_WCSNCPY])
203  HAVE_WCPNCPY=1;       AC_SUBST([HAVE_WCPNCPY])
204  HAVE_WCSCAT=1;        AC_SUBST([HAVE_WCSCAT])
205  HAVE_WCSNCAT=1;       AC_SUBST([HAVE_WCSNCAT])
206  HAVE_WCSCMP=1;        AC_SUBST([HAVE_WCSCMP])
207  HAVE_WCSNCMP=1;       AC_SUBST([HAVE_WCSNCMP])
208  HAVE_WCSCASECMP=1;    AC_SUBST([HAVE_WCSCASECMP])
209  HAVE_WCSNCASECMP=1;   AC_SUBST([HAVE_WCSNCASECMP])
210  HAVE_WCSCOLL=1;       AC_SUBST([HAVE_WCSCOLL])
211  HAVE_WCSXFRM=1;       AC_SUBST([HAVE_WCSXFRM])
212  HAVE_WCSDUP=1;        AC_SUBST([HAVE_WCSDUP])
213  HAVE_WCSCHR=1;        AC_SUBST([HAVE_WCSCHR])
214  HAVE_WCSRCHR=1;       AC_SUBST([HAVE_WCSRCHR])
215  HAVE_WCSCSPN=1;       AC_SUBST([HAVE_WCSCSPN])
216  HAVE_WCSSPN=1;        AC_SUBST([HAVE_WCSSPN])
217  HAVE_WCSPBRK=1;       AC_SUBST([HAVE_WCSPBRK])
218  HAVE_WCSSTR=1;        AC_SUBST([HAVE_WCSSTR])
219  HAVE_WCSTOK=1;        AC_SUBST([HAVE_WCSTOK])
220  HAVE_WCSWIDTH=1;      AC_SUBST([HAVE_WCSWIDTH])
221  HAVE_WCSFTIME=1;      AC_SUBST([HAVE_WCSFTIME])
222  HAVE_DECL_WCTOB=1;    AC_SUBST([HAVE_DECL_WCTOB])
223  HAVE_DECL_WCSDUP=1;   AC_SUBST([HAVE_DECL_WCSDUP])
224  HAVE_DECL_WCWIDTH=1;  AC_SUBST([HAVE_DECL_WCWIDTH])
225  REPLACE_MBSTATE_T=0;  AC_SUBST([REPLACE_MBSTATE_T])
226  REPLACE_BTOWC=0;      AC_SUBST([REPLACE_BTOWC])
227  REPLACE_WCTOB=0;      AC_SUBST([REPLACE_WCTOB])
228  REPLACE_MBSINIT=0;    AC_SUBST([REPLACE_MBSINIT])
229  REPLACE_MBRTOWC=0;    AC_SUBST([REPLACE_MBRTOWC])
230  REPLACE_MBRLEN=0;     AC_SUBST([REPLACE_MBRLEN])
231  REPLACE_MBSRTOWCS=0;  AC_SUBST([REPLACE_MBSRTOWCS])
232  REPLACE_MBSNRTOWCS=0; AC_SUBST([REPLACE_MBSNRTOWCS])
233  REPLACE_WCRTOMB=0;    AC_SUBST([REPLACE_WCRTOMB])
234  REPLACE_WCSRTOMBS=0;  AC_SUBST([REPLACE_WCSRTOMBS])
235  REPLACE_WCSNRTOMBS=0; AC_SUBST([REPLACE_WCSNRTOMBS])
236  REPLACE_WCWIDTH=0;    AC_SUBST([REPLACE_WCWIDTH])
237  REPLACE_WCSWIDTH=0;   AC_SUBST([REPLACE_WCSWIDTH])
238  REPLACE_WCSFTIME=0;   AC_SUBST([REPLACE_WCSFTIME])
239  REPLACE_WCSTOK=0;     AC_SUBST([REPLACE_WCSTOK])
240])
241