Change make license
[platform/upstream/make.git] / m4 / gnulib-common.m4
1 # gnulib-common.m4 serial 46
2 dnl Copyright (C) 2007-2020 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 AC_PREREQ([2.62])
8
9 # gl_COMMON
10 # is expanded unconditionally through gnulib-tool magic.
11 AC_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 ])
16 AC_DEFUN([gl_COMMON_BODY], [
17   AH_VERBATIM([_Noreturn],
18 [/* The _Noreturn keyword of C11.  */
19 #ifndef _Noreturn
20 # if (defined __cplusplus \
21       && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
22           || (defined _MSC_VER && 1900 <= _MSC_VER)) \
23       && 0)
24     /* [[noreturn]] is not practically usable, because with it the syntax
25          extern _Noreturn void func (...);
26        would not be valid; such a declaration would only be valid with 'extern'
27        and '_Noreturn' swapped, or without the 'extern' keyword.  However, some
28        AIX system header files and several gnulib header files use precisely
29        this syntax with 'extern'.  */
30 #  define _Noreturn [[noreturn]]
31 # elif ((!defined __cplusplus || defined __clang__) \
32         && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0)  \
33             || 4 < __GNUC__ + (7 <= __GNUC_MINOR__)))
34    /* _Noreturn works as-is.  */
35 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__) || 0x5110 <= __SUNPRO_C
36 #  define _Noreturn __attribute__ ((__noreturn__))
37 # elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
38 #  define _Noreturn __declspec (noreturn)
39 # else
40 #  define _Noreturn
41 # endif
42 #endif
43 ])
44   AH_VERBATIM([isoc99_inline],
45 [/* Work around a bug in Apple GCC 4.0.1 build 5465: In C99 mode, it supports
46    the ISO C 99 semantics of 'extern inline' (unlike the GNU C semantics of
47    earlier versions), but does not display it by setting __GNUC_STDC_INLINE__.
48    __APPLE__ && __MACH__ test for Mac OS X.
49    __APPLE_CC__ tests for the Apple compiler and its version.
50    __STDC_VERSION__ tests for the C99 mode.  */
51 #if defined __APPLE__ && defined __MACH__ && __APPLE_CC__ >= 5465 && !defined __cplusplus && __STDC_VERSION__ >= 199901L && !defined __GNUC_STDC_INLINE__
52 # define __GNUC_STDC_INLINE__ 1
53 #endif])
54   AH_VERBATIM([unused_parameter],
55 [/* Define as a marker that can be attached to declarations that might not
56     be used.  This helps to reduce warnings, such as from
57     GCC -Wunused-parameter.  */
58 #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
59 # define _GL_UNUSED __attribute__ ((__unused__))
60 #else
61 # define _GL_UNUSED
62 #endif
63 /* The name _UNUSED_PARAMETER_ is an earlier spelling, although the name
64    is a misnomer outside of parameter lists.  */
65 #define _UNUSED_PARAMETER_ _GL_UNUSED
66
67 /* gcc supports the "unused" attribute on possibly unused labels, and
68    g++ has since version 4.5.  Note to support C++ as well as C,
69    _GL_UNUSED_LABEL should be used with a trailing ;  */
70 #if !defined __cplusplus || __GNUC__ > 4 \
71     || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
72 # define _GL_UNUSED_LABEL _GL_UNUSED
73 #else
74 # define _GL_UNUSED_LABEL
75 #endif
76
77 /* The __pure__ attribute was added in gcc 2.96.  */
78 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
79 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
80 #else
81 # define _GL_ATTRIBUTE_PURE /* empty */
82 #endif
83
84 /* The __const__ attribute was added in gcc 2.95.  */
85 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
86 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
87 #else
88 # define _GL_ATTRIBUTE_CONST /* empty */
89 #endif
90
91 /* The __malloc__ attribute was added in gcc 3.  */
92 #if 3 <= __GNUC__
93 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
94 #else
95 # define _GL_ATTRIBUTE_MALLOC /* empty */
96 #endif
97 ])
98   AH_VERBATIM([async_safe],
99 [/* The _GL_ASYNC_SAFE marker should be attached to functions that are
100    signal handlers (for signals other than SIGABRT, SIGPIPE) or can be
101    invoked from such signal handlers.  Such functions have some restrictions:
102      * All functions that it calls should be marked _GL_ASYNC_SAFE as well,
103        or should be listed as async-signal-safe in POSIX
104        <https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04>
105        section 2.4.3.  Note that malloc(), sprintf(), and fwrite(), in
106        particular, are NOT async-signal-safe.
107      * All memory locations (variables and struct fields) that these functions
108        access must be marked 'volatile'.  This holds for both read and write
109        accesses.  Otherwise the compiler might optimize away stores to and
110        reads from such locations that occur in the program, depending on its
111        data flow analysis.  For example, when the program contains a loop
112        that is intended to inspect a variable set from within a signal handler
113            while (!signal_occurred)
114              ;
115        the compiler is allowed to transform this into an endless loop if the
116        variable 'signal_occurred' is not declared 'volatile'.
117    Additionally, recall that:
118      * A signal handler should not modify errno (except if it is a handler
119        for a fatal signal and ends by raising the same signal again, thus
120        provoking the termination of the process).  If it invokes a function
121        that may clobber errno, it needs to save and restore the value of
122        errno.  */
123 #define _GL_ASYNC_SAFE
124 ])
125   dnl Hint which direction to take regarding cross-compilation guesses:
126   dnl When a user installs a program on a platform they are not intimately
127   dnl familiar with, --enable-cross-guesses=conservative is the appropriate
128   dnl choice.  It implements the "If we don't know, assume the worst" principle.
129   dnl However, when an operating system developer (on a platform which is not
130   dnl yet known to gnulib) builds packages for their platform, they want to
131   dnl expose, not hide, possible platform bugs; in this case,
132   dnl --enable-cross-guesses=risky is the appropriate choice.
133   dnl Sets the variables
134   dnl gl_cross_guess_normal    (to be used when 'yes' is good and 'no' is bad),
135   dnl gl_cross_guess_inverted  (to be used when 'no' is good and 'yes' is bad).
136   AC_ARG_ENABLE([cross-guesses],
137     [AS_HELP_STRING([--enable-cross-guesses={conservative|risky}],
138        [specify policy for cross-compilation guesses])],
139     [if test "x$enableval" != xconservative && test "x$enableval" != xrisky; then
140        AC_MSG_WARN([invalid argument supplied to --enable-cross-guesses])
141        enableval=conservative
142      fi
143      gl_cross_guesses="$enableval"],
144     [gl_cross_guesses=conservative])
145   if test $gl_cross_guesses = risky; then
146     gl_cross_guess_normal="guessing yes"
147     gl_cross_guess_inverted="guessing no"
148   else
149     gl_cross_guess_normal="guessing no"
150     gl_cross_guess_inverted="guessing yes"
151   fi
152   dnl Preparation for running test programs:
153   dnl Tell glibc to write diagnostics from -D_FORTIFY_SOURCE=2 to stderr, not
154   dnl to /dev/tty, so they can be redirected to log files.  Such diagnostics
155   dnl arise e.g., in the macros gl_PRINTF_DIRECTIVE_N, gl_SNPRINTF_DIRECTIVE_N.
156   LIBC_FATAL_STDERR_=1
157   export LIBC_FATAL_STDERR_
158 ])
159
160 # gl_MODULE_INDICATOR_CONDITION
161 # expands to a C preprocessor expression that evaluates to 1 or 0, depending
162 # whether a gnulib module that has been requested shall be considered present
163 # or not.
164 m4_define([gl_MODULE_INDICATOR_CONDITION], [1])
165
166 # gl_MODULE_INDICATOR_SET_VARIABLE([modulename])
167 # sets the shell variable that indicates the presence of the given module to
168 # a C preprocessor expression that will evaluate to 1.
169 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE],
170 [
171   gl_MODULE_INDICATOR_SET_VARIABLE_AUX(
172     [GNULIB_[]m4_translit([[$1]],
173                           [abcdefghijklmnopqrstuvwxyz./-],
174                           [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])],
175     [gl_MODULE_INDICATOR_CONDITION])
176 ])
177
178 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX([variable])
179 # modifies the shell variable to include the gl_MODULE_INDICATOR_CONDITION.
180 # The shell variable's value is a C preprocessor expression that evaluates
181 # to 0 or 1.
182 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX],
183 [
184   m4_if(m4_defn([gl_MODULE_INDICATOR_CONDITION]), [1],
185     [
186      dnl Simplify the expression VALUE || 1 to 1.
187      $1=1
188     ],
189     [gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([$1],
190                                              [gl_MODULE_INDICATOR_CONDITION])])
191 ])
192
193 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([variable], [condition])
194 # modifies the shell variable to include the given condition.  The shell
195 # variable's value is a C preprocessor expression that evaluates to 0 or 1.
196 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR],
197 [
198   dnl Simplify the expression 1 || CONDITION to 1.
199   if test "$[]$1" != 1; then
200     dnl Simplify the expression 0 || CONDITION to CONDITION.
201     if test "$[]$1" = 0; then
202       $1=$2
203     else
204       $1="($[]$1 || $2)"
205     fi
206   fi
207 ])
208
209 # gl_MODULE_INDICATOR([modulename])
210 # defines a C macro indicating the presence of the given module
211 # in a location where it can be used.
212 #                                             |  Value  |   Value   |
213 #                                             | in lib/ | in tests/ |
214 # --------------------------------------------+---------+-----------+
215 # Module present among main modules:          |    1    |     1     |
216 # --------------------------------------------+---------+-----------+
217 # Module present among tests-related modules: |    0    |     1     |
218 # --------------------------------------------+---------+-----------+
219 # Module not present at all:                  |    0    |     0     |
220 # --------------------------------------------+---------+-----------+
221 AC_DEFUN([gl_MODULE_INDICATOR],
222 [
223   AC_DEFINE_UNQUOTED([GNULIB_]m4_translit([[$1]],
224       [abcdefghijklmnopqrstuvwxyz./-],
225       [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]),
226     [gl_MODULE_INDICATOR_CONDITION],
227     [Define to a C preprocessor expression that evaluates to 1 or 0,
228      depending whether the gnulib module $1 shall be considered present.])
229 ])
230
231 # gl_MODULE_INDICATOR_FOR_TESTS([modulename])
232 # defines a C macro indicating the presence of the given module
233 # in lib or tests. This is useful to determine whether the module
234 # should be tested.
235 #                                             |  Value  |   Value   |
236 #                                             | in lib/ | in tests/ |
237 # --------------------------------------------+---------+-----------+
238 # Module present among main modules:          |    1    |     1     |
239 # --------------------------------------------+---------+-----------+
240 # Module present among tests-related modules: |    1    |     1     |
241 # --------------------------------------------+---------+-----------+
242 # Module not present at all:                  |    0    |     0     |
243 # --------------------------------------------+---------+-----------+
244 AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS],
245 [
246   AC_DEFINE([GNULIB_TEST_]m4_translit([[$1]],
247       [abcdefghijklmnopqrstuvwxyz./-],
248       [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]), [1],
249     [Define to 1 when the gnulib module $1 should be tested.])
250 ])
251
252 # gl_ASSERT_NO_GNULIB_POSIXCHECK
253 # asserts that there will never be a need to #define GNULIB_POSIXCHECK.
254 # and thereby enables an optimization of configure and config.h.
255 # Used by Emacs.
256 AC_DEFUN([gl_ASSERT_NO_GNULIB_POSIXCHECK],
257 [
258   dnl Override gl_WARN_ON_USE_PREPARE.
259   dnl But hide this definition from 'aclocal'.
260   AC_DEFUN([gl_W][ARN_ON_USE_PREPARE], [])
261 ])
262
263 # gl_ASSERT_NO_GNULIB_TESTS
264 # asserts that there will be no gnulib tests in the scope of the configure.ac
265 # and thereby enables an optimization of config.h.
266 # Used by Emacs.
267 AC_DEFUN([gl_ASSERT_NO_GNULIB_TESTS],
268 [
269   dnl Override gl_MODULE_INDICATOR_FOR_TESTS.
270   AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS], [])
271 ])
272
273 # Test whether <features.h> exists.
274 # Set HAVE_FEATURES_H.
275 AC_DEFUN([gl_FEATURES_H],
276 [
277   AC_CHECK_HEADERS_ONCE([features.h])
278   if test $ac_cv_header_features_h = yes; then
279     HAVE_FEATURES_H=1
280   else
281     HAVE_FEATURES_H=0
282   fi
283   AC_SUBST([HAVE_FEATURES_H])
284 ])
285
286 # AS_VAR_IF(VAR, VALUE, [IF-MATCH], [IF-NOT-MATCH])
287 # ----------------------------------------------------
288 # Backport of autoconf-2.63b's macro.
289 # Remove this macro when we can assume autoconf >= 2.64.
290 m4_ifndef([AS_VAR_IF],
291 [m4_define([AS_VAR_IF],
292 [AS_IF([test x"AS_VAR_GET([$1])" = x""$2], [$3], [$4])])])
293
294 # gl_PROG_CC_C99
295 # Modifies the value of the shell variable CC in an attempt to make $CC
296 # understand ISO C99 source code.
297 # This is like AC_PROG_CC_C99, except that
298 # - AC_PROG_CC_C99 does not mix well with AC_PROG_CC_STDC
299 #   <https://lists.gnu.org/r/bug-gnulib/2011-09/msg00367.html>,
300 #   but many more packages use AC_PROG_CC_STDC than AC_PROG_CC_C99
301 #   <https://lists.gnu.org/r/bug-gnulib/2011-09/msg00441.html>.
302 # Remaining problems:
303 # - When AC_PROG_CC_STDC is invoked twice, it adds the C99 enabling options
304 #   to CC twice
305 #   <https://lists.gnu.org/r/bug-gnulib/2011-09/msg00431.html>.
306 # - AC_PROG_CC_STDC is likely to change now that C11 is an ISO standard.
307 AC_DEFUN([gl_PROG_CC_C99],
308 [
309   dnl Change that version number to the minimum Autoconf version that supports
310   dnl mixing AC_PROG_CC_C99 calls with AC_PROG_CC_STDC calls.
311   m4_version_prereq([9.0],
312     [AC_REQUIRE([AC_PROG_CC_C99])],
313     [AC_REQUIRE([AC_PROG_CC_STDC])])
314 ])
315
316 # gl_PROG_AR_RANLIB
317 # Determines the values for AR, ARFLAGS, RANLIB that fit with the compiler.
318 # The user can set the variables AR, ARFLAGS, RANLIB if he wants to override
319 # the values.
320 AC_DEFUN([gl_PROG_AR_RANLIB],
321 [
322   dnl Minix 3 comes with two toolchains: The Amsterdam Compiler Kit compiler
323   dnl as "cc", and GCC as "gcc". They have different object file formats and
324   dnl library formats. In particular, the GNU binutils programs ar and ranlib
325   dnl produce libraries that work only with gcc, not with cc.
326   AC_REQUIRE([AC_PROG_CC])
327   dnl The '][' hides this use from 'aclocal'.
328   AC_BEFORE([$0], [A][M_PROG_AR])
329   AC_CACHE_CHECK([for Minix Amsterdam compiler], [gl_cv_c_amsterdam_compiler],
330     [
331       AC_EGREP_CPP([Amsterdam],
332         [
333 #ifdef __ACK__
334 Amsterdam
335 #endif
336         ],
337         [gl_cv_c_amsterdam_compiler=yes],
338         [gl_cv_c_amsterdam_compiler=no])
339     ])
340
341   dnl Don't compete with AM_PROG_AR's decision about AR/ARFLAGS if we are not
342   dnl building with __ACK__.
343   if test $gl_cv_c_amsterdam_compiler = yes; then
344     if test -z "$AR"; then
345       AR='cc -c.a'
346     fi
347     if test -z "$ARFLAGS"; then
348       ARFLAGS='-o'
349     fi
350   else
351     dnl AM_PROG_AR was added in automake v1.11.2.  AM_PROG_AR does not AC_SUBST
352     dnl ARFLAGS variable (it is filed into Makefile.in directly by automake
353     dnl script on-demand, if not specified by ./configure of course).
354     dnl Don't AC_REQUIRE the AM_PROG_AR otherwise the code for __ACK__ above
355     dnl will be ignored.  Also, pay attention to call AM_PROG_AR in else block
356     dnl because AM_PROG_AR is written so it could re-set AR variable even for
357     dnl __ACK__.  It may seem like its easier to avoid calling the macro here,
358     dnl but we need to AC_SUBST both AR/ARFLAGS (thus those must have some good
359     dnl default value and automake should usually know them).
360     dnl
361     dnl The '][' hides this use from 'aclocal'.
362     m4_ifdef([A][M_PROG_AR], [A][M_PROG_AR], [:])
363   fi
364
365   dnl In case the code above has not helped with setting AR/ARFLAGS, use
366   dnl Automake-documented default values for AR and ARFLAGS, but prefer
367   dnl ${host}-ar over ar (useful for cross-compiling).
368   AC_CHECK_TOOL([AR], [ar], [ar])
369   if test -z "$ARFLAGS"; then
370     ARFLAGS='cr'
371   fi
372
373   AC_SUBST([AR])
374   AC_SUBST([ARFLAGS])
375   if test -z "$RANLIB"; then
376     if test $gl_cv_c_amsterdam_compiler = yes; then
377       RANLIB=':'
378     else
379       dnl Use the ranlib program if it is available.
380       AC_PROG_RANLIB
381     fi
382   fi
383   AC_SUBST([RANLIB])
384 ])
385
386 # AC_C_RESTRICT
387 # This definition is copied from post-2.69 Autoconf and overrides the
388 # AC_C_RESTRICT macro from autoconf 2.60..2.69.  It can be removed
389 # once autoconf >= 2.70 can be assumed.  It's painful to check version
390 # numbers, and in practice this macro is more up-to-date than Autoconf
391 # is, so override Autoconf unconditionally.
392 AC_DEFUN([AC_C_RESTRICT],
393 [AC_CACHE_CHECK([for C/C++ restrict keyword], [ac_cv_c_restrict],
394   [ac_cv_c_restrict=no
395    # The order here caters to the fact that C++ does not require restrict.
396    for ac_kw in __restrict __restrict__ _Restrict restrict; do
397      AC_COMPILE_IFELSE(
398       [AC_LANG_PROGRAM(
399          [[typedef int *int_ptr;
400            int foo (int_ptr $ac_kw ip) { return ip[0]; }
401            int bar (int [$ac_kw]); /* Catch GCC bug 14050.  */
402            int bar (int ip[$ac_kw]) { return ip[0]; }
403          ]],
404          [[int s[1];
405            int *$ac_kw t = s;
406            t[0] = 0;
407            return foo (t) + bar (t);
408          ]])],
409       [ac_cv_c_restrict=$ac_kw])
410      test "$ac_cv_c_restrict" != no && break
411    done
412   ])
413  AH_VERBATIM([restrict],
414 [/* Define to the equivalent of the C99 'restrict' keyword, or to
415    nothing if this is not supported.  Do not define if restrict is
416    supported directly.  */
417 #undef restrict
418 /* Work around a bug in older versions of Sun C++, which did not
419    #define __restrict__ or support _Restrict or __restrict__
420    even though the corresponding Sun C compiler ended up with
421    "#define restrict _Restrict" or "#define restrict __restrict__"
422    in the previous line.  This workaround can be removed once
423    we assume Oracle Developer Studio 12.5 (2016) or later.  */
424 #if defined __SUNPRO_CC && !defined __RESTRICT && !defined __restrict__
425 # define _Restrict
426 # define __restrict__
427 #endif])
428  case $ac_cv_c_restrict in
429    restrict) ;;
430    no) AC_DEFINE([restrict], []) ;;
431    *)  AC_DEFINE_UNQUOTED([restrict], [$ac_cv_c_restrict]) ;;
432  esac
433 ])# AC_C_RESTRICT
434
435 # gl_BIGENDIAN
436 # is like AC_C_BIGENDIAN, except that it can be AC_REQUIREd.
437 # Note that AC_REQUIRE([AC_C_BIGENDIAN]) does not work reliably because some
438 # macros invoke AC_C_BIGENDIAN with arguments.
439 AC_DEFUN([gl_BIGENDIAN],
440 [
441   AC_C_BIGENDIAN
442 ])
443
444 # gl_CACHE_VAL_SILENT(cache-id, command-to-set-it)
445 # is like AC_CACHE_VAL(cache-id, command-to-set-it), except that it does not
446 # output a spurious "(cached)" mark in the midst of other configure output.
447 # This macro should be used instead of AC_CACHE_VAL when it is not surrounded
448 # by an AC_MSG_CHECKING/AC_MSG_RESULT pair.
449 AC_DEFUN([gl_CACHE_VAL_SILENT],
450 [
451   saved_as_echo_n="$as_echo_n"
452   as_echo_n=':'
453   AC_CACHE_VAL([$1], [$2])
454   as_echo_n="$saved_as_echo_n"
455 ])
456
457 # AS_VAR_COPY was added in autoconf 2.63b
458 m4_define_default([AS_VAR_COPY],
459 [AS_LITERAL_IF([$1[]$2], [$1=$$2], [eval $1=\$$2])])