Merge from gnulib.
[bpt/emacs.git] / m4 / gnulib-common.m4
1 # gnulib-common.m4 serial 33
2 dnl Copyright (C) 2007-2013 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 # gl_COMMON
8 # is expanded unconditionally through gnulib-tool magic.
9 AC_DEFUN([gl_COMMON], [
10 dnl Use AC_REQUIRE here, so that the code is expanded once only.
11 AC_REQUIRE([gl_00GNULIB])
12 AC_REQUIRE([gl_COMMON_BODY])
13 ])
14 AC_DEFUN([gl_COMMON_BODY], [
15 AH_VERBATIM([_Noreturn],
16 [/* The _Noreturn keyword of C11. */
17 #if ! (defined _Noreturn \
18 || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
19 # if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__) \
20 || 0x5110 <= __SUNPRO_C)
21 # define _Noreturn __attribute__ ((__noreturn__))
22 # elif defined _MSC_VER && 1200 <= _MSC_VER
23 # define _Noreturn __declspec (noreturn)
24 # else
25 # define _Noreturn
26 # endif
27 #endif
28 ])
29 AH_VERBATIM([isoc99_inline],
30 [/* Work around a bug in Apple GCC 4.0.1 build 5465: In C99 mode, it supports
31 the ISO C 99 semantics of 'extern inline' (unlike the GNU C semantics of
32 earlier versions), but does not display it by setting __GNUC_STDC_INLINE__.
33 __APPLE__ && __MACH__ test for Mac OS X.
34 __APPLE_CC__ tests for the Apple compiler and its version.
35 __STDC_VERSION__ tests for the C99 mode. */
36 #if defined __APPLE__ && defined __MACH__ && __APPLE_CC__ >= 5465 && !defined __cplusplus && __STDC_VERSION__ >= 199901L && !defined __GNUC_STDC_INLINE__
37 # define __GNUC_STDC_INLINE__ 1
38 #endif])
39 AH_VERBATIM([unused_parameter],
40 [/* Define as a marker that can be attached to declarations that might not
41 be used. This helps to reduce warnings, such as from
42 GCC -Wunused-parameter. */
43 #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
44 # define _GL_UNUSED __attribute__ ((__unused__))
45 #else
46 # define _GL_UNUSED
47 #endif
48 /* The name _UNUSED_PARAMETER_ is an earlier spelling, although the name
49 is a misnomer outside of parameter lists. */
50 #define _UNUSED_PARAMETER_ _GL_UNUSED
51
52 /* The __pure__ attribute was added in gcc 2.96. */
53 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
54 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
55 #else
56 # define _GL_ATTRIBUTE_PURE /* empty */
57 #endif
58
59 /* The __const__ attribute was added in gcc 2.95. */
60 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
61 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
62 #else
63 # define _GL_ATTRIBUTE_CONST /* empty */
64 #endif
65 ])
66 dnl Preparation for running test programs:
67 dnl Tell glibc to write diagnostics from -D_FORTIFY_SOURCE=2 to stderr, not
68 dnl to /dev/tty, so they can be redirected to log files. Such diagnostics
69 dnl arise e.g., in the macros gl_PRINTF_DIRECTIVE_N, gl_SNPRINTF_DIRECTIVE_N.
70 LIBC_FATAL_STDERR_=1
71 export LIBC_FATAL_STDERR_
72 ])
73
74 # gl_MODULE_INDICATOR_CONDITION
75 # expands to a C preprocessor expression that evaluates to 1 or 0, depending
76 # whether a gnulib module that has been requested shall be considered present
77 # or not.
78 m4_define([gl_MODULE_INDICATOR_CONDITION], [1])
79
80 # gl_MODULE_INDICATOR_SET_VARIABLE([modulename])
81 # sets the shell variable that indicates the presence of the given module to
82 # a C preprocessor expression that will evaluate to 1.
83 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE],
84 [
85 gl_MODULE_INDICATOR_SET_VARIABLE_AUX(
86 [GNULIB_[]m4_translit([[$1]],
87 [abcdefghijklmnopqrstuvwxyz./-],
88 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])],
89 [gl_MODULE_INDICATOR_CONDITION])
90 ])
91
92 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX([variable])
93 # modifies the shell variable to include the gl_MODULE_INDICATOR_CONDITION.
94 # The shell variable's value is a C preprocessor expression that evaluates
95 # to 0 or 1.
96 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX],
97 [
98 m4_if(m4_defn([gl_MODULE_INDICATOR_CONDITION]), [1],
99 [
100 dnl Simplify the expression VALUE || 1 to 1.
101 $1=1
102 ],
103 [gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([$1],
104 [gl_MODULE_INDICATOR_CONDITION])])
105 ])
106
107 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([variable], [condition])
108 # modifies the shell variable to include the given condition. The shell
109 # variable's value is a C preprocessor expression that evaluates to 0 or 1.
110 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR],
111 [
112 dnl Simplify the expression 1 || CONDITION to 1.
113 if test "$[]$1" != 1; then
114 dnl Simplify the expression 0 || CONDITION to CONDITION.
115 if test "$[]$1" = 0; then
116 $1=$2
117 else
118 $1="($[]$1 || $2)"
119 fi
120 fi
121 ])
122
123 # gl_MODULE_INDICATOR([modulename])
124 # defines a C macro indicating the presence of the given module
125 # in a location where it can be used.
126 # | Value | Value |
127 # | in lib/ | in tests/ |
128 # --------------------------------------------+---------+-----------+
129 # Module present among main modules: | 1 | 1 |
130 # --------------------------------------------+---------+-----------+
131 # Module present among tests-related modules: | 0 | 1 |
132 # --------------------------------------------+---------+-----------+
133 # Module not present at all: | 0 | 0 |
134 # --------------------------------------------+---------+-----------+
135 AC_DEFUN([gl_MODULE_INDICATOR],
136 [
137 AC_DEFINE_UNQUOTED([GNULIB_]m4_translit([[$1]],
138 [abcdefghijklmnopqrstuvwxyz./-],
139 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]),
140 [gl_MODULE_INDICATOR_CONDITION],
141 [Define to a C preprocessor expression that evaluates to 1 or 0,
142 depending whether the gnulib module $1 shall be considered present.])
143 ])
144
145 # gl_MODULE_INDICATOR_FOR_TESTS([modulename])
146 # defines a C macro indicating the presence of the given module
147 # in lib or tests. This is useful to determine whether the module
148 # should be tested.
149 # | Value | Value |
150 # | in lib/ | in tests/ |
151 # --------------------------------------------+---------+-----------+
152 # Module present among main modules: | 1 | 1 |
153 # --------------------------------------------+---------+-----------+
154 # Module present among tests-related modules: | 1 | 1 |
155 # --------------------------------------------+---------+-----------+
156 # Module not present at all: | 0 | 0 |
157 # --------------------------------------------+---------+-----------+
158 AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS],
159 [
160 AC_DEFINE([GNULIB_TEST_]m4_translit([[$1]],
161 [abcdefghijklmnopqrstuvwxyz./-],
162 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]), [1],
163 [Define to 1 when the gnulib module $1 should be tested.])
164 ])
165
166 # gl_ASSERT_NO_GNULIB_POSIXCHECK
167 # asserts that there will never be a need to #define GNULIB_POSIXCHECK.
168 # and thereby enables an optimization of configure and config.h.
169 # Used by Emacs.
170 AC_DEFUN([gl_ASSERT_NO_GNULIB_POSIXCHECK],
171 [
172 dnl Override gl_WARN_ON_USE_PREPARE.
173 dnl But hide this definition from 'aclocal'.
174 AC_DEFUN([gl_W][ARN_ON_USE_PREPARE], [])
175 ])
176
177 # gl_ASSERT_NO_GNULIB_TESTS
178 # asserts that there will be no gnulib tests in the scope of the configure.ac
179 # and thereby enables an optimization of config.h.
180 # Used by Emacs.
181 AC_DEFUN([gl_ASSERT_NO_GNULIB_TESTS],
182 [
183 dnl Override gl_MODULE_INDICATOR_FOR_TESTS.
184 AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS], [])
185 ])
186
187 # Test whether <features.h> exists.
188 # Set HAVE_FEATURES_H.
189 AC_DEFUN([gl_FEATURES_H],
190 [
191 AC_CHECK_HEADERS_ONCE([features.h])
192 if test $ac_cv_header_features_h = yes; then
193 HAVE_FEATURES_H=1
194 else
195 HAVE_FEATURES_H=0
196 fi
197 AC_SUBST([HAVE_FEATURES_H])
198 ])
199
200 # m4_foreach_w
201 # is a backport of autoconf-2.59c's m4_foreach_w.
202 # Remove this macro when we can assume autoconf >= 2.60.
203 m4_ifndef([m4_foreach_w],
204 [m4_define([m4_foreach_w],
205 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])])
206
207 # AS_VAR_IF(VAR, VALUE, [IF-MATCH], [IF-NOT-MATCH])
208 # ----------------------------------------------------
209 # Backport of autoconf-2.63b's macro.
210 # Remove this macro when we can assume autoconf >= 2.64.
211 m4_ifndef([AS_VAR_IF],
212 [m4_define([AS_VAR_IF],
213 [AS_IF([test x"AS_VAR_GET([$1])" = x""$2], [$3], [$4])])])
214
215 # gl_PROG_CC_C99
216 # Modifies the value of the shell variable CC in an attempt to make $CC
217 # understand ISO C99 source code.
218 # This is like AC_PROG_CC_C99, except that
219 # - AC_PROG_CC_C99 did not exist in Autoconf versions < 2.60,
220 # - AC_PROG_CC_C99 does not mix well with AC_PROG_CC_STDC
221 # <http://lists.gnu.org/archive/html/bug-gnulib/2011-09/msg00367.html>,
222 # but many more packages use AC_PROG_CC_STDC than AC_PROG_CC_C99
223 # <http://lists.gnu.org/archive/html/bug-gnulib/2011-09/msg00441.html>.
224 # Remaining problems:
225 # - When AC_PROG_CC_STDC is invoked twice, it adds the C99 enabling options
226 # to CC twice
227 # <http://lists.gnu.org/archive/html/bug-gnulib/2011-09/msg00431.html>.
228 # - AC_PROG_CC_STDC is likely to change now that C11 is an ISO standard.
229 AC_DEFUN([gl_PROG_CC_C99],
230 [
231 dnl Change that version number to the minimum Autoconf version that supports
232 dnl mixing AC_PROG_CC_C99 calls with AC_PROG_CC_STDC calls.
233 m4_version_prereq([9.0],
234 [AC_REQUIRE([AC_PROG_CC_C99])],
235 [AC_REQUIRE([AC_PROG_CC_STDC])])
236 ])
237
238 # gl_PROG_AR_RANLIB
239 # Determines the values for AR, ARFLAGS, RANLIB that fit with the compiler.
240 # The user can set the variables AR, ARFLAGS, RANLIB if he wants to override
241 # the values.
242 AC_DEFUN([gl_PROG_AR_RANLIB],
243 [
244 dnl Minix 3 comes with two toolchains: The Amsterdam Compiler Kit compiler
245 dnl as "cc", and GCC as "gcc". They have different object file formats and
246 dnl library formats. In particular, the GNU binutils programs ar, ranlib
247 dnl produce libraries that work only with gcc, not with cc.
248 AC_REQUIRE([AC_PROG_CC])
249 AC_CACHE_CHECK([for Minix Amsterdam compiler], [gl_cv_c_amsterdam_compiler],
250 [
251 AC_EGREP_CPP([Amsterdam],
252 [
253 #ifdef __ACK__
254 Amsterdam
255 #endif
256 ],
257 [gl_cv_c_amsterdam_compiler=yes],
258 [gl_cv_c_amsterdam_compiler=no])
259 ])
260 if test -z "$AR"; then
261 if test $gl_cv_c_amsterdam_compiler = yes; then
262 AR='cc -c.a'
263 if test -z "$ARFLAGS"; then
264 ARFLAGS='-o'
265 fi
266 else
267 dnl Use the Automake-documented default values for AR and ARFLAGS,
268 dnl but prefer ${host}-ar over ar (useful for cross-compiling).
269 AC_CHECK_TOOL([AR], [ar], [ar])
270 if test -z "$ARFLAGS"; then
271 ARFLAGS='cru'
272 fi
273 fi
274 else
275 if test -z "$ARFLAGS"; then
276 ARFLAGS='cru'
277 fi
278 fi
279 AC_SUBST([AR])
280 AC_SUBST([ARFLAGS])
281 if test -z "$RANLIB"; then
282 if test $gl_cv_c_amsterdam_compiler = yes; then
283 RANLIB=':'
284 else
285 dnl Use the ranlib program if it is available.
286 AC_PROG_RANLIB
287 fi
288 fi
289 AC_SUBST([RANLIB])
290 ])
291
292 # AC_PROG_MKDIR_P
293 # is a backport of autoconf-2.60's AC_PROG_MKDIR_P, with a fix
294 # for interoperability with automake-1.9.6 from autoconf-2.62.
295 # Remove this macro when we can assume autoconf >= 2.62 or
296 # autoconf >= 2.60 && automake >= 1.10.
297 # AC_AUTOCONF_VERSION was introduced in 2.62, so use that as the witness.
298 m4_ifndef([AC_AUTOCONF_VERSION],[
299 m4_ifdef([AC_PROG_MKDIR_P], [
300 dnl For automake-1.9.6 && autoconf < 2.62: Ensure MKDIR_P is AC_SUBSTed.
301 m4_define([AC_PROG_MKDIR_P],
302 m4_defn([AC_PROG_MKDIR_P])[
303 AC_SUBST([MKDIR_P])])], [
304 dnl For autoconf < 2.60: Backport of AC_PROG_MKDIR_P.
305 AC_DEFUN_ONCE([AC_PROG_MKDIR_P],
306 [AC_REQUIRE([AM_PROG_MKDIR_P])dnl defined by automake
307 MKDIR_P='$(mkdir_p)'
308 AC_SUBST([MKDIR_P])])])
309 ])
310
311 # AC_C_RESTRICT
312 # This definition overrides the AC_C_RESTRICT macro from autoconf 2.60..2.61,
313 # so that mixed use of GNU C and GNU C++ and mixed use of Sun C and Sun C++
314 # works.
315 # This definition can be removed once autoconf >= 2.62 can be assumed.
316 # AC_AUTOCONF_VERSION was introduced in 2.62, so use that as the witness.
317 m4_ifndef([AC_AUTOCONF_VERSION],[
318 AC_DEFUN([AC_C_RESTRICT],
319 [AC_CACHE_CHECK([for C/C++ restrict keyword], [ac_cv_c_restrict],
320 [ac_cv_c_restrict=no
321 # The order here caters to the fact that C++ does not require restrict.
322 for ac_kw in __restrict __restrict__ _Restrict restrict; do
323 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
324 [[typedef int * int_ptr;
325 int foo (int_ptr $ac_kw ip) {
326 return ip[0];
327 }]],
328 [[int s[1];
329 int * $ac_kw t = s;
330 t[0] = 0;
331 return foo(t)]])],
332 [ac_cv_c_restrict=$ac_kw])
333 test "$ac_cv_c_restrict" != no && break
334 done
335 ])
336 AH_VERBATIM([restrict],
337 [/* Define to the equivalent of the C99 'restrict' keyword, or to
338 nothing if this is not supported. Do not define if restrict is
339 supported directly. */
340 #undef restrict
341 /* Work around a bug in Sun C++: it does not support _Restrict, even
342 though the corresponding Sun C compiler does, which causes
343 "#define restrict _Restrict" in the previous line. Perhaps some future
344 version of Sun C++ will work with _Restrict; if so, it'll probably
345 define __RESTRICT, just as Sun C does. */
346 #if defined __SUNPRO_CC && !defined __RESTRICT
347 # define _Restrict
348 #endif])
349 case $ac_cv_c_restrict in
350 restrict) ;;
351 no) AC_DEFINE([restrict], []) ;;
352 *) AC_DEFINE_UNQUOTED([restrict], [$ac_cv_c_restrict]) ;;
353 esac
354 ])
355 ])
356
357 # gl_BIGENDIAN
358 # is like AC_C_BIGENDIAN, except that it can be AC_REQUIREd.
359 # Note that AC_REQUIRE([AC_C_BIGENDIAN]) does not work reliably because some
360 # macros invoke AC_C_BIGENDIAN with arguments.
361 AC_DEFUN([gl_BIGENDIAN],
362 [
363 AC_C_BIGENDIAN
364 ])
365
366 # gl_CACHE_VAL_SILENT(cache-id, command-to-set-it)
367 # is like AC_CACHE_VAL(cache-id, command-to-set-it), except that it does not
368 # output a spurious "(cached)" mark in the midst of other configure output.
369 # This macro should be used instead of AC_CACHE_VAL when it is not surrounded
370 # by an AC_MSG_CHECKING/AC_MSG_RESULT pair.
371 AC_DEFUN([gl_CACHE_VAL_SILENT],
372 [
373 saved_as_echo_n="$as_echo_n"
374 as_echo_n=':'
375 AC_CACHE_VAL([$1], [$2])
376 as_echo_n="$saved_as_echo_n"
377 ])