xref: /Universal-ctags/m4/gnulib-common.m4 (revision 820c1a8d46849a90376d8eb15b319ac05439f656)
1# gnulib-common.m4 serial 66
2dnl Copyright (C) 2007-2021 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_PREREQ([2.62])
8
9# gl_COMMON
10# is expanded unconditionally through gnulib-tool magic.
11AC_DEFUN([gl_COMMON], [
12  dnl Use AC_REQUIRE here, so that the code is expanded once only.
13  AC_REQUIRE([gl_00GNULIB])
14  AC_REQUIRE([gl_COMMON_BODY])
15  AC_REQUIRE([gl_ZZGNULIB])
16])
17AC_DEFUN([gl_COMMON_BODY], [
18  AH_VERBATIM([_GL_GNUC_PREREQ],
19[/* True if the compiler says it groks GNU C version MAJOR.MINOR.  */
20#if defined __GNUC__ && defined __GNUC_MINOR__
21# define _GL_GNUC_PREREQ(major, minor) \
22    ((major) < __GNUC__ + ((minor) <= __GNUC_MINOR__))
23#else
24# define _GL_GNUC_PREREQ(major, minor) 0
25#endif
26])
27  AH_VERBATIM([_Noreturn],
28[/* The _Noreturn keyword of C11.  */
29#ifndef _Noreturn
30# if (defined __cplusplus \
31      && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
32          || (defined _MSC_VER && 1900 <= _MSC_VER)) \
33      && 0)
34    /* [[noreturn]] is not practically usable, because with it the syntax
35         extern _Noreturn void func (...);
36       would not be valid; such a declaration would only be valid with 'extern'
37       and '_Noreturn' swapped, or without the 'extern' keyword.  However, some
38       AIX system header files and several gnulib header files use precisely
39       this syntax with 'extern'.  */
40#  define _Noreturn [[noreturn]]
41# elif ((!defined __cplusplus || defined __clang__) \
42        && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
43            || (!defined __STRICT_ANSI__ \
44                && (_GL_GNUC_PREREQ (4, 7) \
45                    || (defined __apple_build_version__ \
46                        ? 6000000 <= __apple_build_version__ \
47                        : 3 < __clang_major__ + (5 <= __clang_minor__))))))
48   /* _Noreturn works as-is.  */
49# elif _GL_GNUC_PREREQ (2, 8) || defined __clang__ || 0x5110 <= __SUNPRO_C
50#  define _Noreturn __attribute__ ((__noreturn__))
51# elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
52#  define _Noreturn __declspec (noreturn)
53# else
54#  define _Noreturn
55# endif
56#endif
57])
58  AH_VERBATIM([isoc99_inline],
59[/* Work around a bug in Apple GCC 4.0.1 build 5465: In C99 mode, it supports
60   the ISO C 99 semantics of 'extern inline' (unlike the GNU C semantics of
61   earlier versions), but does not display it by setting __GNUC_STDC_INLINE__.
62   __APPLE__ && __MACH__ test for Mac OS X.
63   __APPLE_CC__ tests for the Apple compiler and its version.
64   __STDC_VERSION__ tests for the C99 mode.  */
65#if defined __APPLE__ && defined __MACH__ && __APPLE_CC__ >= 5465 && !defined __cplusplus && __STDC_VERSION__ >= 199901L && !defined __GNUC_STDC_INLINE__
66# define __GNUC_STDC_INLINE__ 1
67#endif])
68  AH_VERBATIM([attribute],
69[/* Attributes.  */
70#if (defined __has_attribute \
71     && (!defined __clang_minor__ \
72         || 3 < __clang_major__ + (5 <= __clang_minor__)))
73# define _GL_HAS_ATTRIBUTE(attr) __has_attribute (__##attr##__)
74#else
75# define _GL_HAS_ATTRIBUTE(attr) _GL_ATTR_##attr
76# define _GL_ATTR_alloc_size _GL_GNUC_PREREQ (4, 3)
77# define _GL_ATTR_always_inline _GL_GNUC_PREREQ (3, 2)
78# define _GL_ATTR_artificial _GL_GNUC_PREREQ (4, 3)
79# define _GL_ATTR_cold _GL_GNUC_PREREQ (4, 3)
80# define _GL_ATTR_const _GL_GNUC_PREREQ (2, 95)
81# define _GL_ATTR_deprecated _GL_GNUC_PREREQ (3, 1)
82# define _GL_ATTR_diagnose_if 0
83# define _GL_ATTR_error _GL_GNUC_PREREQ (4, 3)
84# define _GL_ATTR_externally_visible _GL_GNUC_PREREQ (4, 1)
85# define _GL_ATTR_fallthrough _GL_GNUC_PREREQ (7, 0)
86# define _GL_ATTR_format _GL_GNUC_PREREQ (2, 7)
87# define _GL_ATTR_leaf _GL_GNUC_PREREQ (4, 6)
88# ifdef _ICC
89#  define _GL_ATTR_may_alias 0
90# else
91#  define _GL_ATTR_may_alias _GL_GNUC_PREREQ (3, 3)
92# endif
93# define _GL_ATTR_malloc _GL_GNUC_PREREQ (3, 0)
94# define _GL_ATTR_noinline _GL_GNUC_PREREQ (3, 1)
95# define _GL_ATTR_nonnull _GL_GNUC_PREREQ (3, 3)
96# define _GL_ATTR_nonstring _GL_GNUC_PREREQ (8, 0)
97# define _GL_ATTR_nothrow _GL_GNUC_PREREQ (3, 3)
98# define _GL_ATTR_packed _GL_GNUC_PREREQ (2, 7)
99# define _GL_ATTR_pure _GL_GNUC_PREREQ (2, 96)
100# define _GL_ATTR_returns_nonnull _GL_GNUC_PREREQ (4, 9)
101# define _GL_ATTR_sentinel _GL_GNUC_PREREQ (4, 0)
102# define _GL_ATTR_unused _GL_GNUC_PREREQ (2, 7)
103# define _GL_ATTR_warn_unused_result _GL_GNUC_PREREQ (3, 4)
104#endif
105
106]dnl There is no _GL_ATTRIBUTE_ALIGNED; use stdalign's _Alignas instead.
107[
108#if _GL_HAS_ATTRIBUTE (alloc_size)
109# define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
110#else
111# define _GL_ATTRIBUTE_ALLOC_SIZE(args)
112#endif
113
114#if _GL_HAS_ATTRIBUTE (always_inline)
115# define _GL_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
116#else
117# define _GL_ATTRIBUTE_ALWAYS_INLINE
118#endif
119
120#if _GL_HAS_ATTRIBUTE (artificial)
121# define _GL_ATTRIBUTE_ARTIFICIAL __attribute__ ((__artificial__))
122#else
123# define _GL_ATTRIBUTE_ARTIFICIAL
124#endif
125
126/* Avoid __attribute__ ((cold)) on MinGW; see thread starting at
127   <https://lists.gnu.org/r/emacs-devel/2019-04/msg01152.html>.
128   Also, Oracle Studio 12.6 requires 'cold' not '__cold__'.  */
129#if _GL_HAS_ATTRIBUTE (cold) && !defined __MINGW32__
130# ifndef __SUNPRO_C
131#  define _GL_ATTRIBUTE_COLD __attribute__ ((__cold__))
132# else
133#  define _GL_ATTRIBUTE_COLD __attribute__ ((cold))
134# endif
135#else
136# define _GL_ATTRIBUTE_COLD
137#endif
138
139#if _GL_HAS_ATTRIBUTE (const)
140# define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
141#else
142# define _GL_ATTRIBUTE_CONST
143#endif
144
145#if 201710L < __STDC_VERSION__
146# define _GL_ATTRIBUTE_DEPRECATED [[__deprecated__]]
147#elif _GL_HAS_ATTRIBUTE (deprecated)
148# define _GL_ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
149#else
150# define _GL_ATTRIBUTE_DEPRECATED
151#endif
152
153#if _GL_HAS_ATTRIBUTE (error)
154# define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__error__ (msg)))
155# define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__warning__ (msg)))
156#elif _GL_HAS_ATTRIBUTE (diagnose_if)
157# define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__diagnose_if__ (1, msg, "error")))
158# define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__diagnose_if__ (1, msg, "warning")))
159#else
160# define _GL_ATTRIBUTE_ERROR(msg)
161# define _GL_ATTRIBUTE_WARNING(msg)
162#endif
163
164#if _GL_HAS_ATTRIBUTE (externally_visible)
165# define _GL_ATTRIBUTE_EXTERNALLY_VISIBLE __attribute__ ((externally_visible))
166#else
167# define _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
168#endif
169
170/* FALLTHROUGH is special, because it always expands to something.  */
171#if 201710L < __STDC_VERSION__
172# define _GL_ATTRIBUTE_FALLTHROUGH [[__fallthrough__]]
173#elif _GL_HAS_ATTRIBUTE (fallthrough)
174# define _GL_ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))
175#else
176# define _GL_ATTRIBUTE_FALLTHROUGH ((void) 0)
177#endif
178
179#if _GL_HAS_ATTRIBUTE (format)
180# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
181#else
182# define _GL_ATTRIBUTE_FORMAT(spec)
183#endif
184
185#if _GL_HAS_ATTRIBUTE (leaf)
186# define _GL_ATTRIBUTE_LEAF __attribute__ ((__leaf__))
187#else
188# define _GL_ATTRIBUTE_LEAF
189#endif
190
191/* Oracle Studio 12.6 mishandles may_alias despite __has_attribute OK.  */
192#if _GL_HAS_ATTRIBUTE (may_alias) && !defined __SUNPRO_C
193# define _GL_ATTRIBUTE_MAY_ALIAS __attribute__ ((__may_alias__))
194#else
195# define _GL_ATTRIBUTE_MAY_ALIAS
196#endif
197
198#if 201710L < __STDC_VERSION__
199# define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
200#elif _GL_HAS_ATTRIBUTE (unused)
201# define _GL_ATTRIBUTE_MAYBE_UNUSED __attribute__ ((__unused__))
202#else
203# define _GL_ATTRIBUTE_MAYBE_UNUSED
204#endif
205/* Earlier spellings of this macro.  */
206#define _GL_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED
207#define _UNUSED_PARAMETER_ _GL_ATTRIBUTE_MAYBE_UNUSED
208
209#if _GL_HAS_ATTRIBUTE (malloc)
210# define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
211#else
212# define _GL_ATTRIBUTE_MALLOC
213#endif
214
215#if 201710L < __STDC_VERSION__
216# define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
217#elif _GL_HAS_ATTRIBUTE (warn_unused_result)
218# define _GL_ATTRIBUTE_NODISCARD __attribute__ ((__warn_unused_result__))
219#else
220# define _GL_ATTRIBUTE_NODISCARD
221#endif
222
223#if _GL_HAS_ATTRIBUTE (noinline)
224# define _GL_ATTRIBUTE_NOINLINE __attribute__ ((__noinline__))
225#else
226# define _GL_ATTRIBUTE_NOINLINE
227#endif
228
229#if _GL_HAS_ATTRIBUTE (nonnull)
230# define _GL_ATTRIBUTE_NONNULL(args) __attribute__ ((__nonnull__ args))
231#else
232# define _GL_ATTRIBUTE_NONNULL(args)
233#endif
234
235#if _GL_HAS_ATTRIBUTE (nonstring)
236# define _GL_ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__))
237#else
238# define _GL_ATTRIBUTE_NONSTRING
239#endif
240
241/* There is no _GL_ATTRIBUTE_NORETURN; use _Noreturn instead.  */
242
243#if _GL_HAS_ATTRIBUTE (nothrow) && !defined __cplusplus
244# define _GL_ATTRIBUTE_NOTHROW __attribute__ ((__nothrow__))
245#else
246# define _GL_ATTRIBUTE_NOTHROW
247#endif
248
249#if _GL_HAS_ATTRIBUTE (packed)
250# define _GL_ATTRIBUTE_PACKED __attribute__ ((__packed__))
251#else
252# define _GL_ATTRIBUTE_PACKED
253#endif
254
255#if _GL_HAS_ATTRIBUTE (pure)
256# define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
257#else
258# define _GL_ATTRIBUTE_PURE
259#endif
260
261#if _GL_HAS_ATTRIBUTE (returns_nonnull)
262# define _GL_ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
263#else
264# define _GL_ATTRIBUTE_RETURNS_NONNULL
265#endif
266
267#if _GL_HAS_ATTRIBUTE (sentinel)
268# define _GL_ATTRIBUTE_SENTINEL(pos) __attribute__ ((__sentinel__ pos))
269#else
270# define _GL_ATTRIBUTE_SENTINEL(pos)
271#endif
272
273]dnl There is no _GL_ATTRIBUTE_VISIBILITY; see m4/visibility.m4 instead.
274[
275/* To support C++ as well as C, use _GL_UNUSED_LABEL with trailing ';'.  */
276#if !defined __cplusplus || _GL_GNUC_PREREQ (4, 5)
277# define _GL_UNUSED_LABEL _GL_ATTRIBUTE_MAYBE_UNUSED
278#else
279# define _GL_UNUSED_LABEL
280#endif
281])
282  AH_VERBATIM([async_safe],
283[/* The _GL_ASYNC_SAFE marker should be attached to functions that are
284   signal handlers (for signals other than SIGABRT, SIGPIPE) or can be
285   invoked from such signal handlers.  Such functions have some restrictions:
286     * All functions that it calls should be marked _GL_ASYNC_SAFE as well,
287       or should be listed as async-signal-safe in POSIX
288       <https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04>
289       section 2.4.3.  Note that malloc(), sprintf(), and fwrite(), in
290       particular, are NOT async-signal-safe.
291     * All memory locations (variables and struct fields) that these functions
292       access must be marked 'volatile'.  This holds for both read and write
293       accesses.  Otherwise the compiler might optimize away stores to and
294       reads from such locations that occur in the program, depending on its
295       data flow analysis.  For example, when the program contains a loop
296       that is intended to inspect a variable set from within a signal handler
297           while (!signal_occurred)
298             ;
299       the compiler is allowed to transform this into an endless loop if the
300       variable 'signal_occurred' is not declared 'volatile'.
301   Additionally, recall that:
302     * A signal handler should not modify errno (except if it is a handler
303       for a fatal signal and ends by raising the same signal again, thus
304       provoking the termination of the process).  If it invokes a function
305       that may clobber errno, it needs to save and restore the value of
306       errno.  */
307#define _GL_ASYNC_SAFE
308])
309  AH_VERBATIM([micro_optimizations],
310[/* _GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2, where
311   n1 and n2 are expressions without side effects, that evaluate to real
312   numbers (excluding NaN).
313   It returns
314     1  if n1 > n2
315     0  if n1 == n2
316     -1 if n1 < n2
317   The naïve code   (n1 > n2 ? 1 : n1 < n2 ? -1 : 0)  produces a conditional
318   jump with nearly all GCC versions up to GCC 10.
319   This variant     (n1 < n2 ? -1 : n1 > n2)  produces a conditional with many
320   GCC versions up to GCC 9.
321   The better code  (n1 > n2) - (n1 < n2)  from Hacker's Delight § 2-9
322   avoids conditional jumps in all GCC versions >= 3.4.  */
323#define _GL_CMP(n1, n2) (((n1) > (n2)) - ((n1) < (n2)))
324])
325  dnl Hint which direction to take regarding cross-compilation guesses:
326  dnl When a user installs a program on a platform they are not intimately
327  dnl familiar with, --enable-cross-guesses=conservative is the appropriate
328  dnl choice.  It implements the "If we don't know, assume the worst" principle.
329  dnl However, when an operating system developer (on a platform which is not
330  dnl yet known to gnulib) builds packages for their platform, they want to
331  dnl expose, not hide, possible platform bugs; in this case,
332  dnl --enable-cross-guesses=risky is the appropriate choice.
333  dnl Sets the variables
334  dnl gl_cross_guess_normal    (to be used when 'yes' is good and 'no' is bad),
335  dnl gl_cross_guess_inverted  (to be used when 'no' is good and 'yes' is bad).
336  AC_ARG_ENABLE([cross-guesses],
337    [AS_HELP_STRING([--enable-cross-guesses={conservative|risky}],
338       [specify policy for cross-compilation guesses])],
339    [if test "x$enableval" != xconservative && test "x$enableval" != xrisky; then
340       AC_MSG_WARN([invalid argument supplied to --enable-cross-guesses])
341       enableval=conservative
342     fi
343     gl_cross_guesses="$enableval"],
344    [gl_cross_guesses=conservative])
345  if test $gl_cross_guesses = risky; then
346    gl_cross_guess_normal="guessing yes"
347    gl_cross_guess_inverted="guessing no"
348  else
349    gl_cross_guess_normal="guessing no"
350    gl_cross_guess_inverted="guessing yes"
351  fi
352  dnl Preparation for running test programs:
353  dnl Tell glibc to write diagnostics from -D_FORTIFY_SOURCE=2 to stderr, not
354  dnl to /dev/tty, so they can be redirected to log files.  Such diagnostics
355  dnl arise e.g., in the macros gl_PRINTF_DIRECTIVE_N, gl_SNPRINTF_DIRECTIVE_N.
356  LIBC_FATAL_STDERR_=1
357  export LIBC_FATAL_STDERR_
358])
359
360# gl_MODULE_INDICATOR_INIT_VARIABLE([variablename])
361# gl_MODULE_INDICATOR_INIT_VARIABLE([variablename], [initialvalue])
362# initializes the shell variable that indicates the presence of the given module
363# as a C preprocessor expression.
364AC_DEFUN([gl_MODULE_INDICATOR_INIT_VARIABLE],
365[
366  GL_MODULE_INDICATOR_PREFIX[]_[$1]=m4_if([$2], , [0], [$2])
367  AC_SUBST(GL_MODULE_INDICATOR_PREFIX[]_[$1])
368])
369
370# gl_MODULE_INDICATOR_CONDITION
371# expands to a C preprocessor expression that evaluates to 1 or 0, depending
372# whether a gnulib module that has been requested shall be considered present
373# or not.
374m4_define([gl_MODULE_INDICATOR_CONDITION], [1])
375
376# gl_MODULE_INDICATOR_SET_VARIABLE([modulename])
377# sets the shell variable that indicates the presence of the given module to
378# a C preprocessor expression that will evaluate to 1.
379AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE],
380[
381  gl_MODULE_INDICATOR_SET_VARIABLE_AUX(
382    [GL_MODULE_INDICATOR_PREFIX[]_GNULIB_[]m4_translit([[$1]],
383                                                       [abcdefghijklmnopqrstuvwxyz./-],
384                                                       [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])],
385    [gl_MODULE_INDICATOR_CONDITION])
386])
387
388# gl_MODULE_INDICATOR_SET_VARIABLE_AUX([variable])
389# modifies the shell variable to include the gl_MODULE_INDICATOR_CONDITION.
390# The shell variable's value is a C preprocessor expression that evaluates
391# to 0 or 1.
392AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX],
393[
394  m4_if(m4_defn([gl_MODULE_INDICATOR_CONDITION]), [1],
395    [
396     dnl Simplify the expression VALUE || 1 to 1.
397     $1=1
398    ],
399    [gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([$1],
400                                             [gl_MODULE_INDICATOR_CONDITION])])
401])
402
403# gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([variable], [condition])
404# modifies the shell variable to include the given condition.  The shell
405# variable's value is a C preprocessor expression that evaluates to 0 or 1.
406AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR],
407[
408  dnl Simplify the expression 1 || CONDITION to 1.
409  if test "$[]$1" != 1; then
410    dnl Simplify the expression 0 || CONDITION to CONDITION.
411    if test "$[]$1" = 0; then
412      $1=$2
413    else
414      $1="($[]$1 || $2)"
415    fi
416  fi
417])
418
419# gl_MODULE_INDICATOR([modulename])
420# defines a C macro indicating the presence of the given module
421# in a location where it can be used.
422#                                             |  Value  |   Value   |
423#                                             | in lib/ | in tests/ |
424# --------------------------------------------+---------+-----------+
425# Module present among main modules:          |    1    |     1     |
426# --------------------------------------------+---------+-----------+
427# Module present among tests-related modules: |    0    |     1     |
428# --------------------------------------------+---------+-----------+
429# Module not present at all:                  |    0    |     0     |
430# --------------------------------------------+---------+-----------+
431AC_DEFUN([gl_MODULE_INDICATOR],
432[
433  AC_DEFINE_UNQUOTED([GNULIB_]m4_translit([[$1]],
434      [abcdefghijklmnopqrstuvwxyz./-],
435      [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]),
436    [gl_MODULE_INDICATOR_CONDITION],
437    [Define to a C preprocessor expression that evaluates to 1 or 0,
438     depending whether the gnulib module $1 shall be considered present.])
439])
440
441# gl_MODULE_INDICATOR_FOR_TESTS([modulename])
442# defines a C macro indicating the presence of the given module
443# in lib or tests. This is useful to determine whether the module
444# should be tested.
445#                                             |  Value  |   Value   |
446#                                             | in lib/ | in tests/ |
447# --------------------------------------------+---------+-----------+
448# Module present among main modules:          |    1    |     1     |
449# --------------------------------------------+---------+-----------+
450# Module present among tests-related modules: |    1    |     1     |
451# --------------------------------------------+---------+-----------+
452# Module not present at all:                  |    0    |     0     |
453# --------------------------------------------+---------+-----------+
454AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS],
455[
456  AC_DEFINE([GNULIB_TEST_]m4_translit([[$1]],
457      [abcdefghijklmnopqrstuvwxyz./-],
458      [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]), [1],
459    [Define to 1 when the gnulib module $1 should be tested.])
460])
461
462# gl_ASSERT_NO_GNULIB_POSIXCHECK
463# asserts that there will never be a need to #define GNULIB_POSIXCHECK.
464# and thereby enables an optimization of configure and config.h.
465# Used by Emacs.
466AC_DEFUN([gl_ASSERT_NO_GNULIB_POSIXCHECK],
467[
468  dnl Override gl_WARN_ON_USE_PREPARE.
469  dnl But hide this definition from 'aclocal'.
470  AC_DEFUN([gl_W][ARN_ON_USE_PREPARE], [])
471])
472
473# gl_ASSERT_NO_GNULIB_TESTS
474# asserts that there will be no gnulib tests in the scope of the configure.ac
475# and thereby enables an optimization of config.h.
476# Used by Emacs.
477AC_DEFUN([gl_ASSERT_NO_GNULIB_TESTS],
478[
479  dnl Override gl_MODULE_INDICATOR_FOR_TESTS.
480  AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS], [])
481])
482
483# Test whether <features.h> exists.
484# Set HAVE_FEATURES_H.
485AC_DEFUN([gl_FEATURES_H],
486[
487  AC_CHECK_HEADERS_ONCE([features.h])
488  if test $ac_cv_header_features_h = yes; then
489    HAVE_FEATURES_H=1
490  else
491    HAVE_FEATURES_H=0
492  fi
493  AC_SUBST([HAVE_FEATURES_H])
494])
495
496# gl_PROG_CC_C99
497# Modifies the value of the shell variable CC in an attempt to make $CC
498# understand ISO C99 source code.
499AC_DEFUN([gl_PROG_CC_C99],
500[
501  dnl Just use AC_PROG_CC_C99.
502  dnl When AC_PROG_CC_C99 and AC_PROG_CC_STDC are used together, the substituted
503  dnl value of CC will contain the C99 enabling options twice. But this is only
504  dnl a cosmetic problem.
505  dnl With Autoconf >= 2.70, use AC_PROG_CC since it implies AC_PROG_CC_C99;
506  dnl this avoids a "warning: The macro `AC_PROG_CC_C99' is obsolete."
507  m4_version_prereq([2.70],
508    [AC_REQUIRE([AC_PROG_CC])],
509    [AC_REQUIRE([AC_PROG_CC_C99])])
510])
511
512# gl_PROG_AR_RANLIB
513# Determines the values for AR, ARFLAGS, RANLIB that fit with the compiler.
514# The user can set the variables AR, ARFLAGS, RANLIB if he wants to override
515# the values.
516AC_DEFUN([gl_PROG_AR_RANLIB],
517[
518  dnl Minix 3 comes with two toolchains: The Amsterdam Compiler Kit compiler
519  dnl as "cc", and GCC as "gcc". They have different object file formats and
520  dnl library formats. In particular, the GNU binutils programs ar and ranlib
521  dnl produce libraries that work only with gcc, not with cc.
522  AC_REQUIRE([AC_PROG_CC])
523  dnl The '][' hides this use from 'aclocal'.
524  AC_BEFORE([$0], [A][M_PROG_AR])
525  AC_CACHE_CHECK([for Minix Amsterdam compiler], [gl_cv_c_amsterdam_compiler],
526    [
527      AC_EGREP_CPP([Amsterdam],
528        [
529#ifdef __ACK__
530Amsterdam
531#endif
532        ],
533        [gl_cv_c_amsterdam_compiler=yes],
534        [gl_cv_c_amsterdam_compiler=no])
535    ])
536
537  dnl Don't compete with AM_PROG_AR's decision about AR/ARFLAGS if we are not
538  dnl building with __ACK__.
539  if test $gl_cv_c_amsterdam_compiler = yes; then
540    if test -z "$AR"; then
541      AR='cc -c.a'
542    fi
543    if test -z "$ARFLAGS"; then
544      ARFLAGS='-o'
545    fi
546  else
547    dnl AM_PROG_AR was added in automake v1.11.2.  AM_PROG_AR does not AC_SUBST
548    dnl ARFLAGS variable (it is filed into Makefile.in directly by automake
549    dnl script on-demand, if not specified by ./configure of course).
550    dnl Don't AC_REQUIRE the AM_PROG_AR otherwise the code for __ACK__ above
551    dnl will be ignored.  Also, pay attention to call AM_PROG_AR in else block
552    dnl because AM_PROG_AR is written so it could re-set AR variable even for
553    dnl __ACK__.  It may seem like its easier to avoid calling the macro here,
554    dnl but we need to AC_SUBST both AR/ARFLAGS (thus those must have some good
555    dnl default value and automake should usually know them).
556    dnl
557    dnl The '][' hides this use from 'aclocal'.
558    m4_ifdef([A][M_PROG_AR], [A][M_PROG_AR], [:])
559  fi
560
561  dnl In case the code above has not helped with setting AR/ARFLAGS, use
562  dnl Automake-documented default values for AR and ARFLAGS, but prefer
563  dnl ${host}-ar over ar (useful for cross-compiling).
564  AC_CHECK_TOOL([AR], [ar], [ar])
565  if test -z "$ARFLAGS"; then
566    ARFLAGS='cr'
567  fi
568
569  AC_SUBST([AR])
570  AC_SUBST([ARFLAGS])
571  if test -z "$RANLIB"; then
572    if test $gl_cv_c_amsterdam_compiler = yes; then
573      RANLIB=':'
574    else
575      dnl Use the ranlib program if it is available.
576      AC_PROG_RANLIB
577    fi
578  fi
579  AC_SUBST([RANLIB])
580])
581
582# AC_C_RESTRICT
583# This definition is copied from post-2.70 Autoconf and overrides the
584# AC_C_RESTRICT macro from autoconf 2.60..2.70.
585m4_version_prereq([2.70.1], [], [
586AC_DEFUN([AC_C_RESTRICT],
587[AC_CACHE_CHECK([for C/C++ restrict keyword], [ac_cv_c_restrict],
588  [ac_cv_c_restrict=no
589   # Put '__restrict__' first, to avoid problems with glibc and non-GCC; see:
590   # https://lists.gnu.org/archive/html/bug-autoconf/2016-02/msg00006.html
591   # Put 'restrict' last, because C++ lacks it.
592   for ac_kw in __restrict__ __restrict _Restrict restrict; do
593     AC_COMPILE_IFELSE(
594      [AC_LANG_PROGRAM(
595         [[typedef int *int_ptr;
596           int foo (int_ptr $ac_kw ip) { return ip[0]; }
597           int bar (int [$ac_kw]); /* Catch GCC bug 14050.  */
598           int bar (int ip[$ac_kw]) { return ip[0]; }
599         ]],
600         [[int s[1];
601           int *$ac_kw t = s;
602           t[0] = 0;
603           return foo (t) + bar (t);
604         ]])],
605      [ac_cv_c_restrict=$ac_kw])
606     test "$ac_cv_c_restrict" != no && break
607   done
608  ])
609 AH_VERBATIM([restrict],
610[/* Define to the equivalent of the C99 'restrict' keyword, or to
611   nothing if this is not supported.  Do not define if restrict is
612   supported only directly.  */
613#undef restrict
614/* Work around a bug in older versions of Sun C++, which did not
615   #define __restrict__ or support _Restrict or __restrict__
616   even though the corresponding Sun C compiler ended up with
617   "#define restrict _Restrict" or "#define restrict __restrict__"
618   in the previous line.  This workaround can be removed once
619   we assume Oracle Developer Studio 12.5 (2016) or later.  */
620#if defined __SUNPRO_CC && !defined __RESTRICT && !defined __restrict__
621# define _Restrict
622# define __restrict__
623#endif])
624 case $ac_cv_c_restrict in
625   restrict) ;;
626   no) AC_DEFINE([restrict], []) ;;
627   *)  AC_DEFINE_UNQUOTED([restrict], [$ac_cv_c_restrict]) ;;
628 esac
629])# AC_C_RESTRICT
630])
631
632# gl_BIGENDIAN
633# is like AC_C_BIGENDIAN, except that it can be AC_REQUIREd.
634# Note that AC_REQUIRE([AC_C_BIGENDIAN]) does not work reliably because some
635# macros invoke AC_C_BIGENDIAN with arguments.
636AC_DEFUN([gl_BIGENDIAN],
637[
638  AC_C_BIGENDIAN
639])
640
641# A temporary file descriptor.
642# Must be less than 10, because dash 0.5.8 does not support redirections
643# with multi-digit file descriptors.
644m4_define([GL_TMP_FD], 9)
645
646# gl_SILENT(command)
647# executes command, but without the normal configure output.
648# This is useful when you want to invoke AC_CACHE_CHECK (or AC_CHECK_FUNC etc.)
649# inside another AC_CACHE_CHECK.
650AC_DEFUN([gl_SILENT],
651[
652  exec GL_TMP_FD>&AS_MESSAGE_FD AS_MESSAGE_FD>/dev/null
653  $1
654  exec AS_MESSAGE_FD>&GL_TMP_FD GL_TMP_FD>&-
655])
656
657# gl_CACHE_VAL_SILENT(cache-id, command-to-set-it)
658# is like AC_CACHE_VAL(cache-id, command-to-set-it), except that it does not
659# output a spurious "(cached)" mark in the midst of other configure output.
660# This macro should be used instead of AC_CACHE_VAL when it is not surrounded
661# by an AC_MSG_CHECKING/AC_MSG_RESULT pair.
662AC_DEFUN([gl_CACHE_VAL_SILENT],
663[
664  gl_SILENT([
665    AC_CACHE_VAL([$1], [$2])
666  ])
667])
668
669# gl_CC_ALLOW_WARNINGS
670# sets and substitutes a variable GL_CFLAG_ALLOW_WARNINGS, to a $(CC) option
671# that reverts a preceding '-Werror' option, if available.
672# This is expected to be '-Wno-error' on gcc, clang (except clang/MSVC), xlclang
673# and empty otherwise.
674AC_DEFUN([gl_CC_ALLOW_WARNINGS],
675[
676  AC_REQUIRE([AC_PROG_CC])
677  AC_CACHE_CHECK([for C compiler option to allow warnings],
678    [gl_cv_cc_wallow],
679    [rm -f conftest*
680     echo 'int dummy;' > conftest.c
681     AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -c conftest.c 2>conftest1.err]) >/dev/null
682     AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -Wno-error -c conftest.c 2>conftest2.err]) >/dev/null
683     dnl Test the number of error output lines, because AIX xlc accepts the
684     dnl option '-Wno-error', just to produce a warning
685     dnl "Option -Wno-error was incorrectly specified. The option will be ignored."
686     dnl afterwards.
687     if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
688       gl_cv_cc_wallow='-Wno-error'
689     else
690       gl_cv_cc_wallow=none
691     fi
692     rm -f conftest*
693    ])
694  case "$gl_cv_cc_wallow" in
695    none) GL_CFLAG_ALLOW_WARNINGS='' ;;
696    *)    GL_CFLAG_ALLOW_WARNINGS="$gl_cv_cc_wallow" ;;
697  esac
698  AC_SUBST([GL_CFLAG_ALLOW_WARNINGS])
699])
700
701# gl_CXX_ALLOW_WARNINGS
702# sets and substitutes a variable GL_CXXFLAG_ALLOW_WARNINGS, to a $(CC) option
703# that reverts a preceding '-Werror' option, if available.
704AC_DEFUN([gl_CXX_ALLOW_WARNINGS],
705[
706  dnl Requires AC_PROG_CXX or gl_PROG_ANSI_CXX.
707  if test -n "$CXX" && test "$CXX" != no; then
708    AC_CACHE_CHECK([for C++ compiler option to allow warnings],
709      [gl_cv_cxx_wallow],
710      [rm -f conftest*
711       echo 'int dummy;' > conftest.cc
712       AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -c conftest.cc 2>conftest1.err]) >/dev/null
713       AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -Wno-error -c conftest.cc 2>conftest2.err]) >/dev/null
714       dnl Test the number of error output lines, because AIX xlC accepts the
715       dnl option '-Wno-error', just to produce a warning
716       dnl "Option -Wno-error was incorrectly specified. The option will be ignored."
717       dnl afterwards.
718       if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
719         gl_cv_cxx_wallow='-Wno-error'
720       else
721         gl_cv_cxx_wallow=none
722       fi
723       rm -f conftest*
724      ])
725    case "$gl_cv_cxx_wallow" in
726      none) GL_CXXFLAG_ALLOW_WARNINGS='' ;;
727      *)    GL_CXXFLAG_ALLOW_WARNINGS="$gl_cv_cxx_wallow" ;;
728    esac
729  else
730    GL_CXXFLAG_ALLOW_WARNINGS=''
731  fi
732  AC_SUBST([GL_CXXFLAG_ALLOW_WARNINGS])
733])
734
735dnl Expands to some code for use in .c programs that, on native Windows, defines
736dnl the Microsoft deprecated alias function names to the underscore-prefixed
737dnl actual function names. With this macro, these function names are available
738dnl without linking with '-loldnames' and without generating warnings.
739dnl Usage: Use it after all system header files are included.
740dnl          #include <...>
741dnl          #include <...>
742dnl          ]GL_MDA_DEFINES[
743dnl          ...
744AC_DEFUN([GL_MDA_DEFINES],[
745AC_REQUIRE([_GL_MDA_DEFINES])
746[$gl_mda_defines]
747])
748AC_DEFUN([_GL_MDA_DEFINES],
749[gl_mda_defines='
750#if defined _WIN32 && !defined __CYGWIN__
751#define access    _access
752#define chdir     _chdir
753#define chmod     _chmod
754#define close     _close
755#define creat     _creat
756#define dup       _dup
757#define dup2      _dup2
758#define ecvt      _ecvt
759#define execl     _execl
760#define execle    _execle
761#define execlp    _execlp
762#define execv     _execv
763#define execve    _execve
764#define execvp    _execvp
765#define execvpe   _execvpe
766#define fcloseall _fcloseall
767#define fcvt      _fcvt
768#define fdopen    _fdopen
769#define fileno    _fileno
770#define gcvt      _gcvt
771#define getcwd    _getcwd
772#define getpid    _getpid
773#define getw      _getw
774#define isatty    _isatty
775#define j0        _j0
776#define j1        _j1
777#define jn        _jn
778#define lfind     _lfind
779#define lsearch   _lsearch
780#define lseek     _lseek
781#define memccpy   _memccpy
782#define mkdir     _mkdir
783#define mktemp    _mktemp
784#define open      _open
785#define putenv    _putenv
786#define putw      _putw
787#define read      _read
788#define rmdir     _rmdir
789#define strdup    _strdup
790#define swab      _swab
791#define tempnam   _tempnam
792#define tzset     _tzset
793#define umask     _umask
794#define unlink    _unlink
795#define utime     _utime
796#define wcsdup    _wcsdup
797#define write     _write
798#define y0        _y0
799#define y1        _y1
800#define yn        _yn
801#endif
802'
803])
804