Debian Codegraph @

lib/gnulib-m4/gnulib-common.m4

raw
1 # gnulib-common.m4
2 # serial 109
3 dnl Copyright (C) 2007-2025 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_PREREQ([2.62])
10
11 # gl_COMMON
12 # is expanded unconditionally through gnulib-tool magic.
13 AC_DEFUN([gl_COMMON], [
14 dnl Use AC_REQUIRE here, so that the code is expanded once only.
15 AC_REQUIRE([gl_00GNULIB])
16 AC_REQUIRE([gl_COMMON_BODY])
17 AC_REQUIRE([gl_ZZGNULIB])
18 ])
19 AC_DEFUN([gl_COMMON_BODY], [
20 AH_VERBATIM([0witness],
21 [/* Witness that <config.h> has been included. */
22 #define _GL_CONFIG_H_INCLUDED 1
23 ])
24 dnl Avoid warnings from gcc -Wtrailing-whitespace.
25 dnl This is a temporary workaround until Autoconf fixes it.
26 dnl Test case:
27 dnl empty1=; empty2=; AC_DEFINE_UNQUOTED([FOO], [$empty1$empty2], [...])
28 dnl should produce "#define FOO /**/", not "#define FOO ".
29 AH_TOP([#if defined __GNUC__ && __GNUC__ >= 15 && !defined __clang__
30 # pragma GCC diagnostic push
31 # pragma GCC diagnostic ignored "-Wtrailing-whitespace"
32 #endif
33 ])
34 AH_BOTTOM([#if defined __GNUC__ && __GNUC__ >= 15 && !defined __clang__
35 # pragma GCC diagnostic pop
36 #endif
37 ])
38 AH_VERBATIM([_GL_GNUC_PREREQ],
39 [/* True if the compiler says it groks GNU C version MAJOR.MINOR.
40 Except that
41 - clang groks GNU C 4.2, even on Windows, where it does not define
42 __GNUC__.
43 - The OpenMandriva-modified clang compiler pretends that it groks
44 GNU C version 13.1, but it doesn't: It does not support
45 __attribute__ ((__malloc__ (f, i))), nor does it support
46 __attribute__ ((__warning__ (message))) on a function redeclaration.
47 - Users can make clang lie as well, through the -fgnuc-version option. */
48 #if defined __GNUC__ && defined __GNUC_MINOR__ && !defined __clang__
49 # define _GL_GNUC_PREREQ(major, minor) \
50 ((major) < __GNUC__ + ((minor) <= __GNUC_MINOR__))
51 #elif defined __clang__
52 /* clang really only groks GNU C 4.2. */
53 # define _GL_GNUC_PREREQ(major, minor) \
54 ((major) < 4 + ((minor) <= 2))
55 #else
56 # define _GL_GNUC_PREREQ(major, minor) 0
57 #endif
58 ])
59 AH_VERBATIM([_Noreturn],
60 [/* The _Noreturn keyword of C11. */
61 #ifndef _Noreturn
62 # if (defined __cplusplus \
63 && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
64 || (defined _MSC_VER && 1900 <= _MSC_VER)) \
65 && 0)
66 /* [[noreturn]] is not practically usable, because with it the syntax
67 extern _Noreturn void func (...);
68 would not be valid; such a declaration would only be valid with 'extern'
69 and '_Noreturn' swapped, or without the 'extern' keyword. However, some
70 AIX system header files and several gnulib header files use precisely
71 this syntax with 'extern'. */
72 # define _Noreturn [[noreturn]]
73 # elif (defined __clang__ && __clang_major__ < 16 \
74 && defined _GL_WORK_AROUND_LLVM_BUG_59792)
75 /* Compile with -D_GL_WORK_AROUND_LLVM_BUG_59792 to work around
76 that rare LLVM bug, though you may get many false-alarm warnings. */
77 # define _Noreturn
78 # elif ((!defined __cplusplus || defined __clang__) \
79 && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
80 || (!defined __STRICT_ANSI__ \
81 && (_GL_GNUC_PREREQ (4, 7) \
82 || (defined __apple_build_version__ \
83 ? 6000000 <= __apple_build_version__ \
84 : 3 < __clang_major__ + (5 <= __clang_minor__))))))
85 /* _Noreturn works as-is. */
86 # elif _GL_GNUC_PREREQ (2, 8) || defined __clang__ || 0x5110 <= __SUNPRO_C
87 # define _Noreturn __attribute__ ((__noreturn__))
88 # elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
89 # define _Noreturn __declspec (noreturn)
90 # else
91 # define _Noreturn
92 # endif
93 #endif
94 ])
95 AH_VERBATIM([isoc99_inline],
96 [/* Work around a bug in Apple GCC 4.0.1 build 5465: In C99 mode, it supports
97 the ISO C 99 semantics of 'extern inline' (unlike the GNU C semantics of
98 earlier versions), but does not display it by setting __GNUC_STDC_INLINE__.
99 __APPLE__ && __MACH__ test for Mac OS X.
100 __APPLE_CC__ tests for the Apple compiler and its version.
101 __STDC_VERSION__ tests for the C99 mode. */
102 #if defined __APPLE__ && defined __MACH__ && __APPLE_CC__ >= 5465 && !defined __cplusplus && __STDC_VERSION__ >= 199901L && !defined __GNUC_STDC_INLINE__
103 # define __GNUC_STDC_INLINE__ 1
104 #endif])
105 AH_VERBATIM([attribute],
106 [/* Attributes. */
107 /* Define _GL_HAS_ATTRIBUTE only once, because on FreeBSD, with gcc < 5, if
108 <config.h> gets included once again after <sys/cdefs.h>, __has_attribute(x)
109 expands to 0 always, and redefining _GL_HAS_ATTRIBUTE would turn off all
110 attributes. */
111 #ifndef _GL_HAS_ATTRIBUTE
112 # if (defined __has_attribute \
113 && (!defined __clang_minor__ \
114 || (defined __apple_build_version__ \
115 ? 7000000 <= __apple_build_version__ \
116 : 5 <= __clang_major__)))
117 # define _GL_HAS_ATTRIBUTE(attr) __has_attribute (__##attr##__)
118 # else
119 # define _GL_HAS_ATTRIBUTE(attr) _GL_ATTR_##attr
120 # define _GL_ATTR_alloc_size _GL_GNUC_PREREQ (4, 3)
121 # define _GL_ATTR_always_inline _GL_GNUC_PREREQ (3, 2)
122 # define _GL_ATTR_artificial _GL_GNUC_PREREQ (4, 3)
123 # define _GL_ATTR_cold _GL_GNUC_PREREQ (4, 3)
124 # define _GL_ATTR_const _GL_GNUC_PREREQ (2, 95)
125 # define _GL_ATTR_deprecated _GL_GNUC_PREREQ (3, 1)
126 # define _GL_ATTR_diagnose_if 0
127 # define _GL_ATTR_error _GL_GNUC_PREREQ (4, 3)
128 # define _GL_ATTR_externally_visible _GL_GNUC_PREREQ (4, 1)
129 # define _GL_ATTR_fallthrough _GL_GNUC_PREREQ (7, 0)
130 # define _GL_ATTR_format _GL_GNUC_PREREQ (2, 7)
131 # define _GL_ATTR_leaf _GL_GNUC_PREREQ (4, 6)
132 # define _GL_ATTR_malloc _GL_GNUC_PREREQ (3, 0)
133 # ifdef _ICC
134 # define _GL_ATTR_may_alias 0
135 # else
136 # define _GL_ATTR_may_alias _GL_GNUC_PREREQ (3, 3)
137 # endif
138 # define _GL_ATTR_noinline _GL_GNUC_PREREQ (3, 1)
139 # define _GL_ATTR_nonnull _GL_GNUC_PREREQ (3, 3)
140 # define _GL_ATTR_nonstring _GL_GNUC_PREREQ (8, 0)
141 # define _GL_ATTR_nothrow _GL_GNUC_PREREQ (3, 3)
142 # define _GL_ATTR_packed _GL_GNUC_PREREQ (2, 7)
143 # define _GL_ATTR_pure _GL_GNUC_PREREQ (2, 96)
144 # define _GL_ATTR_reproducible 0 /* not yet supported, as of GCC 14 */
145 # define _GL_ATTR_returns_nonnull _GL_GNUC_PREREQ (4, 9)
146 # define _GL_ATTR_sentinel _GL_GNUC_PREREQ (4, 0)
147 # define _GL_ATTR_unsequenced 0 /* not yet supported, as of GCC 14 */
148 # define _GL_ATTR_unused _GL_GNUC_PREREQ (2, 7)
149 # define _GL_ATTR_warn_unused_result _GL_GNUC_PREREQ (3, 4)
150 # endif
151 #endif
152
153 /* Use __has_c_attribute if available. However, do not use with
154 pre-C23 GCC, which can issue false positives if -Wpedantic. */
155 #if (defined __has_c_attribute \
156 && ! (_GL_GNUC_PREREQ (4, 6) \
157 && (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) <= 201710))
158 # define _GL_HAVE___HAS_C_ATTRIBUTE 1
159 #else
160 # define _GL_HAVE___HAS_C_ATTRIBUTE 0
161 #endif
162
163 /* Attributes in bracket syntax [[...]] vs. attributes in __attribute__((...))
164 syntax, in function declarations. There are two problems here.
165 (Last tested with gcc/g++ 14 and clang/clang++ 18.)
166
167 1) We want that the _GL_ATTRIBUTE_* can be cumulated on the same declaration
168 in any order.
169 =========================== foo.c = foo.cc ===========================
170 __attribute__ ((__deprecated__)) [[__nodiscard__]] int bar1 (int);
171 [[__nodiscard__]] __attribute__ ((__deprecated__)) int bar2 (int);
172 ======================================================================
173 This gives a syntax error
174 - in C mode with gcc
175 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108796>, and
176 - in C++ mode with clang++ version < 16, and
177 - in C++ mode, inside extern "C" {}, still in newer clang++ versions
178 <https://github.com/llvm/llvm-project/issues/101990>.
179 */
180 /* Define if, in a function declaration, the attributes in bracket syntax
181 [[...]] must come before the attributes in __attribute__((...)) syntax.
182 If this is defined, it is best to avoid the bracket syntax, so that the
183 various _GL_ATTRIBUTE_* can be cumulated on the same declaration in any
184 order. */
185 #ifdef __cplusplus
186 # if defined __clang__
187 # define _GL_BRACKET_BEFORE_ATTRIBUTE 1
188 # endif
189 #else
190 # if defined __GNUC__ && !defined __clang__
191 # define _GL_BRACKET_BEFORE_ATTRIBUTE 1
192 # endif
193 #endif
194 /*
195 2) We want that the _GL_ATTRIBUTE_* can be placed in a declaration
196 - without 'extern', in C as well as in C++,
197 - with 'extern', in C,
198 - with 'extern "C"', in C++
199 in the same position. That is, we don't want to be forced to use a
200 macro which arranges for the attribute to come before 'extern' in
201 one case and after 'extern' in the other case, because such a macro
202 would make the source code of .h files pretty ugly.
203 =========================== foo.c = foo.cc ===========================
204 #ifdef __cplusplus
205 # define CC "C"
206 #else
207 # define CC
208 #endif
209
210 #define ND [[__nodiscard__]]
211 #define WUR __attribute__((__warn_unused_result__))
212
213 #ifdef __cplusplus
214 extern "C" {
215 #endif
216 // gcc clang g++ clang++
217
218 ND int foo (int);
219 int ND foo (int); // warn error warn error
220 int foo ND (int);
221 int foo (int) ND; // warn error warn error
222
223 WUR int foo (int);
224 int WUR foo (int);
225 int fo1 WUR (int); // error error error error
226 int foo (int) WUR;
227
228 #ifdef __cplusplus
229 }
230 #endif
231
232 // gcc clang g++ clang++
233
234 ND extern CC int foo (int); // error error
235 extern CC ND int foo (int); // error error
236 extern CC int ND foo (int); // warn error warn error
237 extern CC int foo ND (int);
238 extern CC int foo (int) ND; // warn error warn error
239
240 WUR extern CC int foo (int); // warn
241 extern CC WUR int foo (int);
242 extern CC int WUR foo (int);
243 extern CC int foo WUR (int); // error error error error
244 extern CC int foo (int) WUR;
245
246 ND EXTERN_C_FUNC int foo (int); // error error
247 EXTERN_C_FUNC ND int foo (int);
248 EXTERN_C_FUNC int ND foo (int); // warn error warn error
249 EXTERN_C_FUNC int foo ND (int);
250 EXTERN_C_FUNC int foo (int) ND; // warn error warn error
251
252 WUR EXTERN_C_FUNC int foo (int); // warn
253 EXTERN_C_FUNC WUR int foo (int);
254 EXTERN_C_FUNC int WUR foo (int);
255 EXTERN_C_FUNC int fo2 WUR (int); // error error error error
256 EXTERN_C_FUNC int foo (int) WUR;
257 ======================================================================
258 So, if we insist on using the 'extern' keyword ('extern CC' idiom):
259 * If _GL_ATTRIBUTE_* expands to bracket syntax [[...]]
260 in both C and C++, there is one available position:
261 - between the function name and the parameter list.
262 * If _GL_ATTRIBUTE_* expands to __attribute__((...)) syntax
263 in both C and C++, there are several available positions:
264 - before the return type,
265 - between return type and function name,
266 - at the end of the declaration.
267 * If _GL_ATTRIBUTE_* expands to bracket syntax [[...]] in C and to
268 __attribute__((...)) syntax in C++, there is no available position:
269 it would need to come before 'extern' in C but after 'extern "C"'
270 in C++.
271 * If _GL_ATTRIBUTE_* expands to __attribute__((...)) syntax in C and
272 to bracket syntax [[...]] in C++, there is one available position:
273 - before the return type.
274 Whereas, if we use the 'EXTERN_C_FUNC' idiom, which conditionally
275 omits the 'extern' keyword:
276 * If _GL_ATTRIBUTE_* expands to bracket syntax [[...]]
277 in both C and C++, there are two available positions:
278 - before the return type,
279 - between the function name and the parameter list.
280 * If _GL_ATTRIBUTE_* expands to __attribute__((...)) syntax
281 in both C and C++, there are several available positions:
282 - before the return type,
283 - between return type and function name,
284 - at the end of the declaration.
285 * If _GL_ATTRIBUTE_* expands to bracket syntax [[...]] in C and to
286 __attribute__((...)) syntax in C++, there is one available position:
287 - before the return type.
288 * If _GL_ATTRIBUTE_* expands to __attribute__((...)) syntax in C and
289 to bracket syntax [[...]] in C++, there is one available position:
290 - before the return type.
291 The best choice is therefore to use the 'EXTERN_C_FUNC' idiom and
292 put the attributes before the return type. This works regardless
293 to what the _GL_ATTRIBUTE_* macros expand.
294 */
295
296 /* Attributes in bracket syntax [[...]] vs. attributes in __attribute__((...))
297 syntax, in static/inline function definitions.
298
299 There are similar constraints as for function declarations. However, here,
300 we cannot omit the storage-class specifier. Therefore, the following rule
301 applies:
302 * The macros
303 _GL_ATTRIBUTE_CONST
304 _GL_ATTRIBUTE_DEPRECATED
305 _GL_ATTRIBUTE_MAYBE_UNUSED
306 _GL_ATTRIBUTE_NODISCARD
307 _GL_ATTRIBUTE_PURE
308 _GL_ATTRIBUTE_REPRODUCIBLE
309 _GL_ATTRIBUTE_UNSEQUENCED
310 which may expand to bracket syntax [[...]], must come first, before the
311 storage-class specifier.
312 * Other _GL_ATTRIBUTE_* macros, that expand to __attribute__((...)) syntax,
313 are better placed between the storage-class specifier and the return
314 type.
315 */
316
317 /* Attributes in bracket syntax [[...]] vs. attributes in __attribute__((...))
318 syntax, in variable declarations.
319
320 At which position can they be placed?
321 (Last tested with gcc/g++ 14 and clang/clang++ 18.)
322
323 =========================== foo.c = foo.cc ===========================
324 #ifdef __cplusplus
325 # define CC "C"
326 #else
327 # define CC
328 #endif
329
330 #define BD [[__deprecated__]]
331 #define AD __attribute__ ((__deprecated__))
332
333 // gcc clang g++ clang++
334
335 BD extern CC int var; // error error
336 extern CC BD int var; // error error
337 extern CC int BD var; // warn error warn error
338 extern CC int var BD;
339
340 AD extern CC int var; // warn
341 extern CC AD int var;
342 extern CC int AD var;
343 extern CC int var AD;
344
345 BD extern CC int z[]; // error error
346 extern CC BD int z[]; // error error
347 extern CC int BD z[]; // warn error warn error
348 extern CC int z1 BD [];
349 extern CC int z[] BD; // warn error error
350
351 AD extern CC int z[]; // warn
352 extern CC AD int z[];
353 extern CC int AD z[];
354 extern CC int z2 AD []; // error error error error
355 extern CC int z[] AD;
356 ======================================================================
357
358 * For non-array variables, the only good position is after the variable name,
359 that is, at the end of the declaration.
360 * For array variables, you will need to distinguish C and C++:
361 - In C, before the 'extern' keyword.
362 - In C++, between the 'extern "C"' and the variable's type.
363 */
364 ]dnl There is no _GL_ATTRIBUTE_ALIGNED; use stdalign's alignas instead.
365 [
366 /* _GL_ATTRIBUTE_ALLOC_SIZE ((N)) declares that the Nth argument of the function
367 is the size of the returned memory block.
368 _GL_ATTRIBUTE_ALLOC_SIZE ((M, N)) declares that the Mth argument multiplied
369 by the Nth argument of the function is the size of the returned memory block.
370 */
371 /* Applies to: functions, pointer to functions, function types. */
372 #ifndef _GL_ATTRIBUTE_ALLOC_SIZE
373 # if _GL_HAS_ATTRIBUTE (alloc_size)
374 # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
375 # else
376 # define _GL_ATTRIBUTE_ALLOC_SIZE(args)
377 # endif
378 #endif
379
380 /* _GL_ATTRIBUTE_ALWAYS_INLINE tells that the compiler should always inline the
381 function and report an error if it cannot do so. */
382 /* Applies to: functions. */
383 #ifndef _GL_ATTRIBUTE_ALWAYS_INLINE
384 # if _GL_HAS_ATTRIBUTE (always_inline)
385 # define _GL_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
386 # else
387 # define _GL_ATTRIBUTE_ALWAYS_INLINE
388 # endif
389 #endif
390
391 /* _GL_ATTRIBUTE_ARTIFICIAL declares that the function is not important to show
392 in stack traces when debugging. The compiler should omit the function from
393 stack traces. */
394 /* Applies to: functions. */
395 #ifndef _GL_ATTRIBUTE_ARTIFICIAL
396 # if _GL_HAS_ATTRIBUTE (artificial)
397 # define _GL_ATTRIBUTE_ARTIFICIAL __attribute__ ((__artificial__))
398 # else
399 # define _GL_ATTRIBUTE_ARTIFICIAL
400 # endif
401 #endif
402
403 /* _GL_ATTRIBUTE_COLD declares that the function is rarely executed. */
404 /* Applies to: functions. */
405 /* Avoid __attribute__ ((cold)) on MinGW; see thread starting at
406 <https://lists.gnu.org/r/emacs-devel/2019-04/msg01152.html>.
407 Also, Oracle Studio 12.6 requires 'cold' not '__cold__'. */
408 #ifndef _GL_ATTRIBUTE_COLD
409 # if _GL_HAS_ATTRIBUTE (cold) && !defined __MINGW32__
410 # ifndef __SUNPRO_C
411 # define _GL_ATTRIBUTE_COLD __attribute__ ((__cold__))
412 # else
413 # define _GL_ATTRIBUTE_COLD __attribute__ ((cold))
414 # endif
415 # else
416 # define _GL_ATTRIBUTE_COLD
417 # endif
418 #endif
419
420 /* _GL_ATTRIBUTE_CONST declares:
421 It is OK for a compiler to move calls to the function and to omit
422 calls to the function if another call has the same arguments or the
423 result is not used.
424 This attribute is safe for a function that neither depends on
425 nor affects state, and always returns exactly once -
426 e.g., does not raise an exception, call longjmp, or loop forever.
427 (This attribute is stricter than _GL_ATTRIBUTE_PURE because the
428 function cannot observe state. It is stricter than
429 _GL_ATTRIBUTE_UNSEQUENCED because the function must return exactly
430 once and cannot depend on state addressed by its arguments.) */
431 /* Applies to: functions. */
432 #ifndef _GL_ATTRIBUTE_CONST
433 # if _GL_HAS_ATTRIBUTE (const)
434 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
435 # else
436 # define _GL_ATTRIBUTE_CONST _GL_ATTRIBUTE_UNSEQUENCED
437 # endif
438 #endif
439
440 /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
441 that can be freed by passing them as the Ith argument to the
442 function F.
443 _GL_ATTRIBUTE_DEALLOC_FREE declares that the function returns pointers that
444 can be freed via 'free'; it can be used only after declaring 'free'. */
445 /* Applies to: functions. Cannot be used on inline functions. */
446 #ifndef _GL_ATTRIBUTE_DEALLOC
447 # if _GL_GNUC_PREREQ (11, 0)
448 # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
449 # else
450 # define _GL_ATTRIBUTE_DEALLOC(f, i)
451 # endif
452 #endif
453 /* If gnulib's <string.h> or <wchar.h> has already defined this macro, continue
454 to use this earlier definition, since <stdlib.h> may not have been included
455 yet. */
456 #ifndef _GL_ATTRIBUTE_DEALLOC_FREE
457 # if defined __cplusplus && defined __GNUC__ && !defined __clang__
458 /* Work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108231> */
459 # define _GL_ATTRIBUTE_DEALLOC_FREE \
460 _GL_ATTRIBUTE_DEALLOC ((void (*) (void *)) free, 1)
461 # else
462 # define _GL_ATTRIBUTE_DEALLOC_FREE \
463 _GL_ATTRIBUTE_DEALLOC (free, 1)
464 # endif
465 #endif
466
467 /* _GL_ATTRIBUTE_DEPRECATED: Declares that an entity is deprecated.
468 The compiler may warn if the entity is used. */
469 /* Applies to:
470 - function, variable,
471 - struct, union, struct/union member,
472 - enumeration, enumeration item,
473 - typedef,
474 in C++ also: namespace, class, template specialization. */
475 #ifndef _GL_ATTRIBUTE_DEPRECATED
476 # ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
477 # if _GL_HAVE___HAS_C_ATTRIBUTE
478 # if __has_c_attribute (__deprecated__)
479 # define _GL_ATTRIBUTE_DEPRECATED [[__deprecated__]]
480 # endif
481 # endif
482 # endif
483 # if !defined _GL_ATTRIBUTE_DEPRECATED && _GL_HAS_ATTRIBUTE (deprecated)
484 # define _GL_ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
485 # endif
486 # ifndef _GL_ATTRIBUTE_DEPRECATED
487 # define _GL_ATTRIBUTE_DEPRECATED
488 # endif
489 #endif
490
491 /* _GL_ATTRIBUTE_ERROR(msg) requests an error if a function is called and
492 the function call is not optimized away.
493 _GL_ATTRIBUTE_WARNING(msg) requests a warning if a function is called and
494 the function call is not optimized away. */
495 /* Applies to: functions. */
496 #if !(defined _GL_ATTRIBUTE_ERROR && defined _GL_ATTRIBUTE_WARNING)
497 # if _GL_HAS_ATTRIBUTE (error)
498 # define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__error__ (msg)))
499 # define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__warning__ (msg)))
500 # elif _GL_HAS_ATTRIBUTE (diagnose_if)
501 # define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__diagnose_if__ (1, msg, "error")))
502 # define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__diagnose_if__ (1, msg, "warning")))
503 # else
504 # define _GL_ATTRIBUTE_ERROR(msg)
505 # define _GL_ATTRIBUTE_WARNING(msg)
506 # endif
507 #endif
508
509 /* _GL_ATTRIBUTE_EXTERNALLY_VISIBLE declares that the entity should remain
510 visible to debuggers etc., even with '-fwhole-program'. */
511 /* Applies to: functions, variables. */
512 #ifndef _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
513 # if _GL_HAS_ATTRIBUTE (externally_visible)
514 # define _GL_ATTRIBUTE_EXTERNALLY_VISIBLE __attribute__ ((externally_visible))
515 # else
516 # define _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
517 # endif
518 #endif
519
520 /* _GL_ATTRIBUTE_FALLTHROUGH declares that it is not a programming mistake if
521 the control flow falls through to the immediately following 'case' or
522 'default' label. The compiler should not warn in this case. */
523 /* Applies to: Empty statement (;), inside a 'switch' statement. */
524 /* Always expands to something. */
525 #ifndef _GL_ATTRIBUTE_FALLTHROUGH
526 # if _GL_HAVE___HAS_C_ATTRIBUTE
527 # if __has_c_attribute (__fallthrough__)
528 # define _GL_ATTRIBUTE_FALLTHROUGH [[__fallthrough__]]
529 # endif
530 # endif
531 # if !defined _GL_ATTRIBUTE_FALLTHROUGH && _GL_HAS_ATTRIBUTE (fallthrough)
532 # define _GL_ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))
533 # endif
534 # ifndef _GL_ATTRIBUTE_FALLTHROUGH
535 # define _GL_ATTRIBUTE_FALLTHROUGH ((void) 0)
536 # endif
537 #endif
538
539 /* _GL_ATTRIBUTE_FORMAT ((ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK))
540 declares that the STRING-INDEXth function argument is a format string of
541 style ARCHETYPE, which is one of:
542 printf, gnu_printf
543 scanf, gnu_scanf,
544 strftime, gnu_strftime,
545 strfmon,
546 or the same thing prefixed and suffixed with '__'.
547 If FIRST-TO-CHECK is not 0, arguments starting at FIRST-TO_CHECK
548 are suitable for the format string. */
549 /* Applies to: functions. */
550 #ifndef _GL_ATTRIBUTE_FORMAT
551 # if _GL_HAS_ATTRIBUTE (format)
552 # define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
553 # else
554 # define _GL_ATTRIBUTE_FORMAT(spec)
555 # endif
556 #endif
557
558 /* _GL_ATTRIBUTE_LEAF declares that if the function is called from some other
559 compilation unit, it executes code from that unit only by return or by
560 exception handling. This declaration lets the compiler optimize that unit
561 more aggressively. */
562 /* Applies to: functions. */
563 #ifndef _GL_ATTRIBUTE_LEAF
564 # if _GL_HAS_ATTRIBUTE (leaf)
565 # define _GL_ATTRIBUTE_LEAF __attribute__ ((__leaf__))
566 # else
567 # define _GL_ATTRIBUTE_LEAF
568 # endif
569 #endif
570
571 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
572 allocated memory. */
573 /* Applies to: functions. */
574 #ifndef _GL_ATTRIBUTE_MALLOC
575 # if _GL_HAS_ATTRIBUTE (malloc)
576 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
577 # else
578 # define _GL_ATTRIBUTE_MALLOC
579 # endif
580 #endif
581
582 /* _GL_ATTRIBUTE_MAY_ALIAS declares that pointers to the type may point to the
583 same storage as pointers to other types. Thus this declaration disables
584 strict aliasing optimization. */
585 /* Applies to: types. */
586 /* Oracle Studio 12.6 mishandles may_alias despite __has_attribute OK. */
587 #ifndef _GL_ATTRIBUTE_MAY_ALIAS
588 # if _GL_HAS_ATTRIBUTE (may_alias) && !defined __SUNPRO_C
589 # define _GL_ATTRIBUTE_MAY_ALIAS __attribute__ ((__may_alias__))
590 # else
591 # define _GL_ATTRIBUTE_MAY_ALIAS
592 # endif
593 #endif
594
595 /* _GL_ATTRIBUTE_MAYBE_UNUSED declares that it is not a programming mistake if
596 the entity is not used. The compiler should not warn if the entity is not
597 used. */
598 /* Applies to:
599 - function, variable,
600 - struct, union, struct/union member,
601 - enumeration, enumeration item,
602 - typedef,
603 in C++ also: class. */
604 /* In C++ and C23, this is spelled [[__maybe_unused__]].
605 GCC's syntax is __attribute__ ((__unused__)).
606 clang supports both syntaxes. Except that with clang ≥ 6, < 10, in C++ mode,
607 __has_c_attribute (__maybe_unused__) yields true but the use of
608 [[__maybe_unused__]] nevertheless produces a warning. */
609 #ifndef _GL_ATTRIBUTE_MAYBE_UNUSED
610 # ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
611 # if defined __clang__ && defined __cplusplus
612 # if !defined __apple_build_version__ && __clang_major__ >= 10
613 # define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
614 # endif
615 # elif _GL_HAVE___HAS_C_ATTRIBUTE
616 # if __has_c_attribute (__maybe_unused__)
617 # define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
618 # endif
619 # endif
620 # endif
621 # ifndef _GL_ATTRIBUTE_MAYBE_UNUSED
622 # define _GL_ATTRIBUTE_MAYBE_UNUSED _GL_ATTRIBUTE_UNUSED
623 # endif
624 #endif
625 /* Alternative spelling of this macro, for convenience and for
626 compatibility with glibc/include/libc-symbols.h. */
627 #define _GL_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED
628 /* Earlier spellings of this macro. */
629 #define _UNUSED_PARAMETER_ _GL_ATTRIBUTE_MAYBE_UNUSED
630
631 /* _GL_ATTRIBUTE_NODISCARD declares that the caller of the function should not
632 discard the return value. The compiler may warn if the caller does not use
633 the return value, unless the caller uses something like ignore_value. */
634 /* Applies to: function, enumeration, class. */
635 #ifndef _GL_ATTRIBUTE_NODISCARD
636 # ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
637 # if defined __clang__ && defined __cplusplus
638 /* With clang up to 15.0.6 (at least), in C++ mode, [[__nodiscard__]] produces
639 a warning.
640 The 1000 below means a yet unknown threshold. When clang++ version X
641 starts supporting [[__nodiscard__]] without warning about it, you can
642 replace the 1000 with X. */
643 # if __clang_major__ >= 1000
644 # define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
645 # endif
646 # elif _GL_HAVE___HAS_C_ATTRIBUTE
647 # if __has_c_attribute (__nodiscard__)
648 # define _GL_ATTRIBUTE_NODISCARD [[__nodiscard__]]
649 # endif
650 # endif
651 # endif
652 # if !defined _GL_ATTRIBUTE_NODISCARD && _GL_HAS_ATTRIBUTE (warn_unused_result)
653 # define _GL_ATTRIBUTE_NODISCARD __attribute__ ((__warn_unused_result__))
654 # endif
655 # ifndef _GL_ATTRIBUTE_NODISCARD
656 # define _GL_ATTRIBUTE_NODISCARD
657 # endif
658 #endif
659
660 /* _GL_ATTRIBUTE_NOINLINE tells that the compiler should not inline the
661 function. */
662 /* Applies to: functions. */
663 #ifndef _GL_ATTRIBUTE_NOINLINE
664 # if _GL_HAS_ATTRIBUTE (noinline)
665 # define _GL_ATTRIBUTE_NOINLINE __attribute__ ((__noinline__))
666 # else
667 # define _GL_ATTRIBUTE_NOINLINE
668 # endif
669 #endif
670
671 /* _GL_ATTRIBUTE_NONNULL ((N1, N2,...)) declares that the arguments N1, N2,...
672 must not be NULL.
673 _GL_ATTRIBUTE_NONNULL () declares that all pointer arguments must not be
674 null. */
675 /* Applies to: functions. */
676 #ifndef _GL_ATTRIBUTE_NONNULL
677 # if _GL_HAS_ATTRIBUTE (nonnull)
678 # define _GL_ATTRIBUTE_NONNULL(args) __attribute__ ((__nonnull__ args))
679 # else
680 # define _GL_ATTRIBUTE_NONNULL(args)
681 # endif
682 #endif
683
684 /* _GL_ATTRIBUTE_NONSTRING declares that the contents of a character array is
685 not meant to be NUL-terminated. */
686 /* Applies to: struct/union members and variables that are arrays of element
687 type '[[un]signed] char'. */
688 #ifndef _GL_ATTRIBUTE_NONSTRING
689 # if _GL_HAS_ATTRIBUTE (nonstring)
690 # define _GL_ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__))
691 # else
692 # define _GL_ATTRIBUTE_NONSTRING
693 # endif
694 #endif
695
696 /* There is no _GL_ATTRIBUTE_NORETURN; use _Noreturn instead. */
697
698 /* _GL_ATTRIBUTE_NOTHROW declares that the function does not throw exceptions.
699 */
700 /* Applies to: functions. */
701 /* After a function's parameter list, this attribute must come first, before
702 other attributes. */
703 #ifndef _GL_ATTRIBUTE_NOTHROW
704 # if defined __cplusplus
705 # if _GL_GNUC_PREREQ (2, 8) || __clang_major__ >= 4
706 # if __cplusplus >= 201103L
707 # define _GL_ATTRIBUTE_NOTHROW noexcept (true)
708 # else
709 # define _GL_ATTRIBUTE_NOTHROW throw ()
710 # endif
711 # else
712 # define _GL_ATTRIBUTE_NOTHROW
713 # endif
714 # else
715 # if _GL_HAS_ATTRIBUTE (nothrow)
716 # define _GL_ATTRIBUTE_NOTHROW __attribute__ ((__nothrow__))
717 # else
718 # define _GL_ATTRIBUTE_NOTHROW
719 # endif
720 # endif
721 #endif
722
723 /* _GL_ATTRIBUTE_PACKED declares:
724 For struct members: The member has the smallest possible alignment.
725 For struct, union, class: All members have the smallest possible alignment,
726 minimizing the memory required. */
727 /* Applies to: struct members, struct, union,
728 in C++ also: class. */
729 #ifndef _GL_ATTRIBUTE_PACKED
730 /* Oracle Studio 12.6 miscompiles code with __attribute__ ((__packed__)) despite
731 __has_attribute OK. */
732 # if _GL_HAS_ATTRIBUTE (packed) && !defined __SUNPRO_C
733 # define _GL_ATTRIBUTE_PACKED __attribute__ ((__packed__))
734 # else
735 # define _GL_ATTRIBUTE_PACKED
736 # endif
737 #endif
738
739 /* _GL_ATTRIBUTE_PURE declares:
740 It is OK for a compiler to move calls to the function and to omit
741 calls to the function if another call has the same arguments or the
742 result is not used, and if observable state is the same.
743 This attribute is safe for a function that does not affect observable state
744 and always returns exactly once.
745 (This attribute is looser than _GL_ATTRIBUTE_CONST because the function
746 can depend on observable state. It is stricter than
747 _GL_ATTRIBUTE_REPRODUCIBLE because the function must return exactly
748 once and cannot affect state addressed by its arguments.) */
749 /* Applies to: functions. */
750 #ifndef _GL_ATTRIBUTE_PURE
751 # if _GL_HAS_ATTRIBUTE (pure)
752 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
753 # else
754 # define _GL_ATTRIBUTE_PURE _GL_ATTRIBUTE_REPRODUCIBLE
755 # endif
756 #endif
757
758 /* _GL_ATTRIBUTE_REPRODUCIBLE declares:
759 It is OK for a compiler to move calls to the function and to omit duplicate
760 calls to the function with the same arguments, so long as the state
761 addressed by its arguments is the same and is updated in time for
762 the rest of the program.
763 This attribute is safe for a function that is effectless and idempotent; see
764 ISO C 23 § 6.7.12.7 for a definition of these terms.
765 (This attribute is looser than _GL_ATTRIBUTE_UNSEQUENCED because
766 the function need not be stateless and idempotent. It is looser
767 than _GL_ATTRIBUTE_PURE because the function need not return
768 exactly once and can affect state addressed by its arguments.)
769 See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
770 <https://stackoverflow.com/questions/76847905/>.
771 ATTENTION! Efforts are underway to change the meaning of this attribute.
772 See <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3424.htm>. */
773 /* Applies to: functions, pointer to functions, function types. */
774 #ifndef _GL_ATTRIBUTE_REPRODUCIBLE
775 /* This may be revisited when gcc and clang support [[reproducible]] or possibly
776 __attribute__ ((__reproducible__)). */
777 # ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
778 # if _GL_HAS_ATTRIBUTE (reproducible)
779 # define _GL_ATTRIBUTE_REPRODUCIBLE [[reproducible]]
780 # endif
781 # endif
782 # ifndef _GL_ATTRIBUTE_REPRODUCIBLE
783 # define _GL_ATTRIBUTE_REPRODUCIBLE
784 # endif
785 #endif
786
787 /* _GL_ATTRIBUTE_RETURNS_NONNULL declares that the function's return value is
788 a non-NULL pointer. */
789 /* Applies to: functions. */
790 #ifndef _GL_ATTRIBUTE_RETURNS_NONNULL
791 # if _GL_HAS_ATTRIBUTE (returns_nonnull)
792 # define _GL_ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
793 # else
794 # define _GL_ATTRIBUTE_RETURNS_NONNULL
795 # endif
796 #endif
797
798 /* _GL_ATTRIBUTE_SENTINEL(pos) declares that the variadic function expects a
799 trailing NULL argument.
800 _GL_ATTRIBUTE_SENTINEL () - The last argument is NULL (requires C99).
801 _GL_ATTRIBUTE_SENTINEL ((N)) - The (N+1)st argument from the end is NULL. */
802 /* Applies to: functions. */
803 #ifndef _GL_ATTRIBUTE_SENTINEL
804 # if _GL_HAS_ATTRIBUTE (sentinel)
805 # define _GL_ATTRIBUTE_SENTINEL(pos) __attribute__ ((__sentinel__ pos))
806 # else
807 # define _GL_ATTRIBUTE_SENTINEL(pos)
808 # endif
809 #endif
810
811 /* _GL_ATTRIBUTE_UNSEQUENCED declares:
812 It is OK for a compiler to move calls to the function and to omit duplicate
813 calls to the function with the same arguments, so long as the state
814 addressed by its arguments is the same.
815 This attribute is safe for a function that is effectless, idempotent,
816 stateless, and independent; see ISO C 23 § 6.7.12.7 for a definition of
817 these terms.
818 (This attribute is stricter than _GL_ATTRIBUTE_REPRODUCIBLE because
819 the function must be stateless and independent. It is looser than
820 _GL_ATTRIBUTE_CONST because the function need not return exactly
821 once and can depend on state addressed by its arguments.)
822 See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
823 <https://stackoverflow.com/questions/76847905/>.
824 ATTENTION! Efforts are underway to change the meaning of this attribute.
825 See <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3424.htm>. */
826 /* Applies to: functions, pointer to functions, function types. */
827 #ifndef _GL_ATTRIBUTE_UNSEQUENCED
828 /* This may be revisited when gcc and clang support [[unsequenced]] or possibly
829 __attribute__ ((__unsequenced__)). */
830 # ifndef _GL_BRACKET_BEFORE_ATTRIBUTE
831 # if _GL_HAS_ATTRIBUTE (unsequenced)
832 # define _GL_ATTRIBUTE_UNSEQUENCED [[unsequenced]]
833 # endif
834 # endif
835 # ifndef _GL_ATTRIBUTE_UNSEQUENCED
836 # define _GL_ATTRIBUTE_UNSEQUENCED
837 # endif
838 #endif
839
840 /* A helper macro. Don't use it directly. */
841 #ifndef _GL_ATTRIBUTE_UNUSED
842 # if _GL_HAS_ATTRIBUTE (unused)
843 # define _GL_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
844 # else
845 # define _GL_ATTRIBUTE_UNUSED
846 # endif
847 #endif
848
849 ]dnl There is no _GL_ATTRIBUTE_VISIBILITY; see m4/visibility.m4 instead.
850 [
851 /* _GL_UNUSED_LABEL; declares that it is not a programming mistake if the
852 immediately preceding label is not used. The compiler should not warn
853 if the label is not used. */
854 /* Applies to: label (both in C and C++). */
855 /* Note that g++ < 4.5 does not support the '__attribute__ ((__unused__)) ;'
856 syntax. But clang does. */
857 #ifndef _GL_UNUSED_LABEL
858 # if !(defined __cplusplus && !_GL_GNUC_PREREQ (4, 5)) || defined __clang__
859 # define _GL_UNUSED_LABEL _GL_ATTRIBUTE_UNUSED
860 # else
861 # define _GL_UNUSED_LABEL
862 # endif
863 #endif
864
865 /* The following attributes enable detection of multithread-safety problems
866 and resource leaks at compile-time, by clang ≥ 15, when the warning option
867 -Wthread-safety is enabled. For usage, see
868 <https://clang.llvm.org/docs/ThreadSafetyAnalysis.html>. */
869 #ifndef _GL_ATTRIBUTE_CAPABILITY_TYPE
870 # if __clang_major__ >= 15
871 # define _GL_ATTRIBUTE_CAPABILITY_TYPE(concept) \
872 __attribute__ ((__capability__ (concept)))
873 # else
874 # define _GL_ATTRIBUTE_CAPABILITY_TYPE(concept)
875 # endif
876 #endif
877 #ifndef _GL_ATTRIBUTE_ACQUIRE_CAPABILITY
878 # if __clang_major__ >= 15
879 # define _GL_ATTRIBUTE_ACQUIRE_CAPABILITY(resource) \
880 __attribute__ ((__acquire_capability__ (resource)))
881 # else
882 # define _GL_ATTRIBUTE_ACQUIRE_CAPABILITY(resource)
883 # endif
884 #endif
885 #ifndef _GL_ATTRIBUTE_RELEASE_CAPABILITY
886 # if __clang_major__ >= 15
887 # define _GL_ATTRIBUTE_RELEASE_CAPABILITY(resource) \
888 __attribute__ ((__release_capability__ (resource)))
889 # else
890 # define _GL_ATTRIBUTE_RELEASE_CAPABILITY(resource)
891 # endif
892 #endif
893 ])
894 AH_VERBATIM([c_linkage],
895 [/* In C++, there is the concept of "language linkage", that encompasses
896 name mangling and function calling conventions.
897 The following macros start and end a block of "C" linkage. */
898 #ifdef __cplusplus
899 # define _GL_BEGIN_C_LINKAGE extern "C" {
900 # define _GL_END_C_LINKAGE }
901 #else
902 # define _GL_BEGIN_C_LINKAGE
903 # define _GL_END_C_LINKAGE
904 #endif
905 ])
906 AH_VERBATIM([async_safe],
907 [/* The _GL_ASYNC_SAFE marker should be attached to functions that are
908 signal handlers (for signals other than SIGABRT, SIGPIPE) or can be
909 invoked from such signal handlers. Such functions have some restrictions:
910 * All functions that it calls should be marked _GL_ASYNC_SAFE as well,
911 or should be listed as async-signal-safe in POSIX
912 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04>
913 section 2.4.3. Note that malloc(), sprintf(), and fwrite(), in
914 particular, are NOT async-signal-safe.
915 * All memory locations (variables and struct fields) that these functions
916 access must be marked 'volatile'. This holds for both read and write
917 accesses. Otherwise the compiler might optimize away stores to and
918 reads from such locations that occur in the program, depending on its
919 data flow analysis. For example, when the program contains a loop
920 that is intended to inspect a variable set from within a signal handler
921 while (!signal_occurred)
922 ;
923 the compiler is allowed to transform this into an endless loop if the
924 variable 'signal_occurred' is not declared 'volatile'.
925 Additionally, recall that:
926 * A signal handler should not modify errno (except if it is a handler
927 for a fatal signal and ends by raising the same signal again, thus
928 provoking the termination of the process). If it invokes a function
929 that may clobber errno, it needs to save and restore the value of
930 errno. */
931 #define _GL_ASYNC_SAFE
932 ])
933 AH_VERBATIM([micro_optimizations],
934 [/* _GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2, where
935 n1 and n2 are expressions without side effects, that evaluate to real
936 numbers (excluding NaN).
937 It returns
938 1 if n1 > n2
939 0 if n1 == n2
940 -1 if n1 < n2
941 The naïve code (n1 > n2 ? 1 : n1 < n2 ? -1 : 0) produces a conditional
942 jump with nearly all GCC versions up to GCC 10.
943 This variant (n1 < n2 ? -1 : n1 > n2) produces a conditional with many
944 GCC versions up to GCC 9.
945 The better code (n1 > n2) - (n1 < n2) from Hacker's Delight § 2-9
946 avoids conditional jumps in all GCC versions >= 3.4. */
947 #define _GL_CMP(n1, n2) (((n1) > (n2)) - ((n1) < (n2)))
948 ])
949 dnl Hint which direction to take regarding cross-compilation guesses:
950 dnl When a user installs a program on a platform they are not intimately
951 dnl familiar with, --enable-cross-guesses=conservative is the appropriate
952 dnl choice. It implements the "If we don't know, assume the worst" principle.
953 dnl However, when an operating system developer (on a platform which is not
954 dnl yet known to gnulib) builds packages for their platform, they want to
955 dnl expose, not hide, possible platform bugs; in this case,
956 dnl --enable-cross-guesses=risky is the appropriate choice.
957 dnl Sets the variables
958 dnl gl_cross_guess_normal (to be used when 'yes' is good and 'no' is bad),
959 dnl gl_cross_guess_inverted (to be used when 'no' is good and 'yes' is bad).
960 AC_ARG_ENABLE([cross-guesses],
961 [AS_HELP_STRING([[--enable-cross-guesses={conservative|risky}]],
962 [specify policy for cross-compilation guesses])],
963 [if test "x$enableval" != xconservative && test "x$enableval" != xrisky; then
964 AC_MSG_WARN([invalid argument supplied to --enable-cross-guesses])
965 enableval=conservative
966 fi
967 gl_cross_guesses="$enableval"],
968 [gl_cross_guesses=conservative])
969 if test $gl_cross_guesses = risky; then
970 gl_cross_guess_normal="guessing yes"
971 gl_cross_guess_inverted="guessing no"
972 else
973 gl_cross_guess_normal="guessing no"
974 gl_cross_guess_inverted="guessing yes"
975 fi
976 dnl Preparation for running test programs:
977 dnl Tell glibc to write diagnostics from -D_FORTIFY_SOURCE=2 to stderr, not
978 dnl to /dev/tty, so they can be redirected to log files. Such diagnostics
979 dnl arise e.g., in the macros gl_PRINTF_DIRECTIVE_N, gl_SNPRINTF_DIRECTIVE_N.
980 LIBC_FATAL_STDERR_=1
981 export LIBC_FATAL_STDERR_
982 ])
983
984 # gl_MODULE_INDICATOR_INIT_VARIABLE([variablename])
985 # gl_MODULE_INDICATOR_INIT_VARIABLE([variablename], [initialvalue])
986 # initializes the shell variable that indicates the presence of the given module
987 # as a C preprocessor expression.
988 AC_DEFUN([gl_MODULE_INDICATOR_INIT_VARIABLE],
989 [
990 GL_MODULE_INDICATOR_PREFIX[]_[$1]=m4_if([$2], , [0], [$2])
991 AC_SUBST(GL_MODULE_INDICATOR_PREFIX[]_[$1])
992 ])
993
994 # gl_MODULE_INDICATOR_CONDITION
995 # expands to a C preprocessor expression that evaluates to 1 or 0, depending
996 # whether a gnulib module that has been requested shall be considered present
997 # or not.
998 m4_define([gl_MODULE_INDICATOR_CONDITION], [1])
999
1000 # gl_MODULE_INDICATOR_SET_VARIABLE([modulename])
1001 # sets the shell variable that indicates the presence of the given module to
1002 # a C preprocessor expression that will evaluate to 1.
1003 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE],
1004 [
1005 gl_MODULE_INDICATOR_SET_VARIABLE_AUX(
1006 [GL_MODULE_INDICATOR_PREFIX[]_GNULIB_[]m4_translit([[$1]],
1007 [abcdefghijklmnopqrstuvwxyz./-],
1008 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])],
1009 [gl_MODULE_INDICATOR_CONDITION])
1010 ])
1011
1012 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX([variable])
1013 # modifies the shell variable to include the gl_MODULE_INDICATOR_CONDITION.
1014 # The shell variable's value is a C preprocessor expression that evaluates
1015 # to 0 or 1.
1016 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX],
1017 [
1018 m4_if(m4_defn([gl_MODULE_INDICATOR_CONDITION]), [1],
1019 [
1020 dnl Simplify the expression VALUE || 1 to 1.
1021 $1=1
1022 ],
1023 [gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([$1],
1024 [gl_MODULE_INDICATOR_CONDITION])])
1025 ])
1026
1027 # gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR([variable], [condition])
1028 # modifies the shell variable to include the given condition. The shell
1029 # variable's value is a C preprocessor expression that evaluates to 0 or 1.
1030 AC_DEFUN([gl_MODULE_INDICATOR_SET_VARIABLE_AUX_OR],
1031 [
1032 dnl Simplify the expression 1 || CONDITION to 1.
1033 if test "$[]$1" != 1; then
1034 dnl Simplify the expression 0 || CONDITION to CONDITION.
1035 if test "$[]$1" = 0; then
1036 $1=$2
1037 else
1038 $1="($[]$1 || $2)"
1039 fi
1040 fi
1041 ])
1042
1043 # gl_MODULE_INDICATOR([modulename])
1044 # defines a C macro indicating the presence of the given module
1045 # in a location where it can be used.
1046 # | Value | Value |
1047 # | in lib/ | in tests/ |
1048 # --------------------------------------------+---------+-----------+
1049 # Module present among main modules: | 1 | 1 |
1050 # --------------------------------------------+---------+-----------+
1051 # Module present among tests-related modules: | 0 | 1 |
1052 # --------------------------------------------+---------+-----------+
1053 # Module not present at all: | 0 | 0 |
1054 # --------------------------------------------+---------+-----------+
1055 AC_DEFUN([gl_MODULE_INDICATOR],
1056 [
1057 AC_DEFINE_UNQUOTED([GNULIB_]m4_translit([[$1]],
1058 [abcdefghijklmnopqrstuvwxyz./-],
1059 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]),
1060 [gl_MODULE_INDICATOR_CONDITION],
1061 [Define to a C preprocessor expression that evaluates to 1 or 0,
1062 depending whether the gnulib module $1 shall be considered present.])
1063 ])
1064
1065 # gl_MODULE_INDICATOR_FOR_TESTS([modulename])
1066 # defines a C macro indicating the presence of the given module
1067 # in lib or tests. This is useful to determine whether the module
1068 # should be tested.
1069 # | Value | Value |
1070 # | in lib/ | in tests/ |
1071 # --------------------------------------------+---------+-----------+
1072 # Module present among main modules: | 1 | 1 |
1073 # --------------------------------------------+---------+-----------+
1074 # Module present among tests-related modules: | 1 | 1 |
1075 # --------------------------------------------+---------+-----------+
1076 # Module not present at all: | 0 | 0 |
1077 # --------------------------------------------+---------+-----------+
1078 AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS],
1079 [
1080 AC_DEFINE([GNULIB_TEST_]m4_translit([[$1]],
1081 [abcdefghijklmnopqrstuvwxyz./-],
1082 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___]), [1],
1083 [Define to 1 when the gnulib module $1 should be tested.])
1084 ])
1085
1086 # gl_ASSERT_NO_GNULIB_POSIXCHECK
1087 # asserts that there will never be a need to #define GNULIB_POSIXCHECK.
1088 # and thereby enables an optimization of configure and config.h.
1089 # Used by Emacs.
1090 AC_DEFUN([gl_ASSERT_NO_GNULIB_POSIXCHECK],
1091 [
1092 dnl Override gl_WARN_ON_USE_PREPARE.
1093 dnl But hide this definition from 'aclocal'.
1094 AC_DEFUN([gl_W][ARN_ON_USE_PREPARE], [])
1095 ])
1096
1097 # gl_ASSERT_NO_GNULIB_TESTS
1098 # asserts that there will be no gnulib tests in the scope of the configure.ac
1099 # and thereby enables an optimization of config.h.
1100 # Used by Emacs.
1101 AC_DEFUN([gl_ASSERT_NO_GNULIB_TESTS],
1102 [
1103 dnl Override gl_MODULE_INDICATOR_FOR_TESTS.
1104 AC_DEFUN([gl_MODULE_INDICATOR_FOR_TESTS], [])
1105 ])
1106
1107 # Test whether <features.h> exists.
1108 # Set HAVE_FEATURES_H.
1109 AC_DEFUN([gl_FEATURES_H],
1110 [
1111 AC_CHECK_HEADERS_ONCE([features.h])
1112 if test $ac_cv_header_features_h = yes; then
1113 HAVE_FEATURES_H=1
1114 else
1115 HAVE_FEATURES_H=0
1116 fi
1117 AC_SUBST([HAVE_FEATURES_H])
1118 ])
1119
1120 # gl_PROG_CC_C99
1121 # Modifies the value of the shell variable CC in an attempt to make $CC
1122 # understand ISO C99 source code.
1123 AC_DEFUN([gl_PROG_CC_C99],
1124 [
1125 dnl Just use AC_PROG_CC_C99.
1126 dnl When AC_PROG_CC_C99 and AC_PROG_CC_STDC are used together, the substituted
1127 dnl value of CC will contain the C99 enabling options twice. But this is only
1128 dnl a cosmetic problem.
1129 dnl With Autoconf >= 2.70, use AC_PROG_CC since it implies AC_PROG_CC_C99;
1130 dnl this avoids a "warning: The macro `AC_PROG_CC_C99' is obsolete."
1131 m4_version_prereq([2.70],
1132 [AC_REQUIRE([AC_PROG_CC])],
1133 [AC_REQUIRE([AC_PROG_CC_C99])])
1134 ])
1135
1136 # gl_PROG_AR_RANLIB
1137 # Determines the values for AR, ARFLAGS, RANLIB that fit with the compiler.
1138 # The user can set the variables AR, ARFLAGS, RANLIB if he wants to override
1139 # the values.
1140 AC_DEFUN([gl_PROG_AR_RANLIB],
1141 [
1142 dnl Minix 3 comes with two toolchains: The Amsterdam Compiler Kit compiler
1143 dnl as "cc", and GCC as "gcc". They have different object file formats and
1144 dnl library formats. In particular, the GNU binutils programs ar and ranlib
1145 dnl produce libraries that work only with gcc, not with cc.
1146 AC_REQUIRE([AC_PROG_CC])
1147 dnl The '][' hides this use from 'aclocal'.
1148 AC_BEFORE([$0], [A][M_PROG_AR])
1149 AC_CACHE_CHECK([for Minix Amsterdam compiler], [gl_cv_c_amsterdam_compiler],
1150 [
1151 AC_EGREP_CPP([Amsterdam],
1152 [
1153 #ifdef __ACK__
1154 Amsterdam
1155 #endif
1156 ],
1157 [gl_cv_c_amsterdam_compiler=yes],
1158 [gl_cv_c_amsterdam_compiler=no])
1159 ])
1160
1161 dnl Don't compete with AM_PROG_AR's decision about AR/ARFLAGS if we are not
1162 dnl building with __ACK__.
1163 if test $gl_cv_c_amsterdam_compiler = yes; then
1164 if test -z "$AR"; then
1165 AR='cc -c.a'
1166 fi
1167 if test -z "$ARFLAGS"; then
1168 ARFLAGS='-o'
1169 fi
1170 else
1171 dnl AM_PROG_AR was added in automake v1.11.2. AM_PROG_AR does not AC_SUBST
1172 dnl ARFLAGS variable (it is filed into Makefile.in directly by automake
1173 dnl script on-demand, if not specified by ./configure of course).
1174 dnl Don't AC_REQUIRE the AM_PROG_AR otherwise the code for __ACK__ above
1175 dnl will be ignored. Also, pay attention to call AM_PROG_AR in else block
1176 dnl because AM_PROG_AR is written so it could re-set AR variable even for
1177 dnl __ACK__. It may seem like its easier to avoid calling the macro here,
1178 dnl but we need to AC_SUBST both AR/ARFLAGS (thus those must have some good
1179 dnl default value and automake should usually know them).
1180 dnl
1181 dnl The '][' hides this use from 'aclocal'.
1182 m4_ifdef([A][M_PROG_AR], [A][M_PROG_AR], [:])
1183 fi
1184
1185 dnl In case the code above has not helped with setting AR/ARFLAGS, use
1186 dnl Automake-documented default values for AR and ARFLAGS, but prefer
1187 dnl ${host}-ar over ar (useful for cross-compiling).
1188 AC_CHECK_TOOL([AR], [ar], [ar])
1189 if test -z "$ARFLAGS"; then
1190 ARFLAGS='cr'
1191 fi
1192
1193 AC_SUBST([AR])
1194 AC_SUBST([ARFLAGS])
1195 if test -z "$RANLIB"; then
1196 if test $gl_cv_c_amsterdam_compiler = yes; then
1197 RANLIB=':'
1198 else
1199 dnl Use the ranlib program if it is available.
1200 AC_PROG_RANLIB
1201 fi
1202 fi
1203 AC_SUBST([RANLIB])
1204 ])
1205
1206 # AC_C_RESTRICT
1207 # This definition is copied from post-2.70 Autoconf and overrides the
1208 # AC_C_RESTRICT macro from autoconf 2.60..2.70.
1209 m4_version_prereq([2.70.1], [], [
1210 AC_DEFUN([AC_C_RESTRICT],
1211 [AC_CACHE_CHECK([for C/C++ restrict keyword], [ac_cv_c_restrict],
1212 [ac_cv_c_restrict=no
1213 # Put '__restrict__' first, to avoid problems with glibc and non-GCC; see:
1214 # https://lists.gnu.org/archive/html/bug-autoconf/2016-02/msg00006.html
1215 # Put 'restrict' last, because C++ lacks it.
1216 for ac_kw in __restrict__ __restrict _Restrict restrict; do
1217 AC_COMPILE_IFELSE(
1218 [AC_LANG_PROGRAM(
1219 [[typedef int *int_ptr;
1220 int foo (int_ptr $ac_kw ip) { return ip[0]; }
1221 int bar (int [$ac_kw]); /* Catch GCC bug 14050. */
1222 int bar (int ip[$ac_kw]) { return ip[0]; }
1223 ]],
1224 [[int s[1];
1225 int *$ac_kw t = s;
1226 t[0] = 0;
1227 return foo (t) + bar (t);
1228 ]])],
1229 [ac_cv_c_restrict=$ac_kw])
1230 test "$ac_cv_c_restrict" != no && break
1231 done
1232 ])
1233 AH_VERBATIM([restrict],
1234 [/* Define to the equivalent of the C99 'restrict' keyword, or to
1235 nothing if this is not supported. Do not define if restrict is
1236 supported only directly. */
1237 #undef restrict
1238 /* Work around a bug in older versions of Sun C++, which did not
1239 #define __restrict__ or support _Restrict or __restrict__
1240 even though the corresponding Sun C compiler ended up with
1241 "#define restrict _Restrict" or "#define restrict __restrict__"
1242 in the previous line. This workaround can be removed once
1243 we assume Oracle Developer Studio 12.5 (2016) or later. */
1244 #if defined __SUNPRO_CC && !defined __RESTRICT && !defined __restrict__
1245 # define _Restrict
1246 # define __restrict__
1247 #endif])
1248 case $ac_cv_c_restrict in
1249 restrict) ;;
1250 no) AC_DEFINE([restrict], []) ;;
1251 *) AC_DEFINE_UNQUOTED([restrict], [$ac_cv_c_restrict]) ;;
1252 esac
1253 ])# AC_C_RESTRICT
1254 ])
1255
1256 # gl_BIGENDIAN
1257 # is like AC_C_BIGENDIAN, except that it can be AC_REQUIREd.
1258 # Note that AC_REQUIRE([AC_C_BIGENDIAN]) does not work reliably because some
1259 # macros invoke AC_C_BIGENDIAN with arguments.
1260 AC_DEFUN([gl_BIGENDIAN],
1261 [
1262 AC_C_BIGENDIAN
1263 ])
1264
1265 # A temporary file descriptor.
1266 # Must be less than 10, because dash 0.5.8 does not support redirections
1267 # with multi-digit file descriptors.
1268 m4_define([GL_TMP_FD], 9)
1269
1270 # gl_SILENT(command)
1271 # executes command, but without the normal configure output.
1272 # This is useful when you want to invoke AC_CACHE_CHECK (or AC_CHECK_FUNC etc.)
1273 # inside another AC_CACHE_CHECK.
1274 AC_DEFUN([gl_SILENT],
1275 [
1276 exec GL_TMP_FD>&AS_MESSAGE_FD AS_MESSAGE_FD>/dev/null
1277 $1
1278 exec AS_MESSAGE_FD>&GL_TMP_FD GL_TMP_FD>&-
1279 ])
1280
1281 # gl_CACHE_VAL_SILENT(cache-id, command-to-set-it)
1282 # is like AC_CACHE_VAL(cache-id, command-to-set-it), except that it does not
1283 # output a spurious "(cached)" mark in the midst of other configure output.
1284 # This macro should be used instead of AC_CACHE_VAL when it is not surrounded
1285 # by an AC_MSG_CHECKING/AC_MSG_RESULT pair.
1286 AC_DEFUN([gl_CACHE_VAL_SILENT],
1287 [
1288 gl_SILENT([
1289 AC_CACHE_VAL([$1], [$2])
1290 ])
1291 ])
1292
1293 # gl_CONDITIONAL(conditional, condition)
1294 # is like AM_CONDITIONAL(conditional, condition), except that it does not
1295 # produce an error
1296 # configure: error: conditional "..." was never defined.
1297 # Usually this means the macro was only invoked conditionally.
1298 # when only invoked conditionally. Instead, in that case, both the _TRUE
1299 # and the _FALSE case are disabled.
1300 AC_DEFUN([gl_CONDITIONAL],
1301 [
1302 pushdef([AC_CONFIG_COMMANDS_PRE], [:])dnl
1303 AM_CONDITIONAL([$1], [$2])
1304 popdef([AC_CONFIG_COMMANDS_PRE])dnl
1305 if test -z "${[$1]_TRUE}" && test -z "${[$1]_FALSE}"; then
1306 [$1]_TRUE='#'
1307 [$1]_FALSE='#'
1308 fi
1309 ])
1310
1311 # gl_CC_ALLOW_WARNINGS
1312 # sets and substitutes a variable GL_CFLAG_ALLOW_WARNINGS, to a $(CC) option
1313 # that reverts a preceding '-Werror' option, if available.
1314 # This is expected to be '-Wno-error' on gcc, clang (except clang/MSVC), xlclang
1315 # and empty otherwise.
1316 AC_DEFUN([gl_CC_ALLOW_WARNINGS],
1317 [
1318 AC_REQUIRE([AC_PROG_CC])
1319 AC_CACHE_CHECK([for C compiler option to allow warnings],
1320 [gl_cv_cc_wallow],
1321 [rm -fr conftest*
1322 echo 'int dummy;' > conftest.c
1323 AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -c conftest.c 2>conftest1.err]) >/dev/null
1324 AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -Wno-error -c conftest.c 2>conftest2.err]) >/dev/null
1325 dnl Test the number of error output lines, because AIX xlc accepts the
1326 dnl option '-Wno-error', just to produce a warning
1327 dnl "Option -Wno-error was incorrectly specified. The option will be ignored."
1328 dnl afterwards.
1329 if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
1330 gl_cv_cc_wallow='-Wno-error'
1331 else
1332 gl_cv_cc_wallow=none
1333 fi
1334 rm -fr conftest*
1335 ])
1336 case "$gl_cv_cc_wallow" in
1337 none) GL_CFLAG_ALLOW_WARNINGS='' ;;
1338 *) GL_CFLAG_ALLOW_WARNINGS="$gl_cv_cc_wallow" ;;
1339 esac
1340 AC_SUBST([GL_CFLAG_ALLOW_WARNINGS])
1341 ])
1342
1343 # gl_CXX_ALLOW_WARNINGS
1344 # sets and substitutes a variable GL_CXXFLAG_ALLOW_WARNINGS, to a $(CC) option
1345 # that reverts a preceding '-Werror' option, if available.
1346 AC_DEFUN([gl_CXX_ALLOW_WARNINGS],
1347 [
1348 dnl Requires AC_PROG_CXX or gl_PROG_ANSI_CXX.
1349 if test -n "$CXX" && test "$CXX" != no; then
1350 AC_CACHE_CHECK([for C++ compiler option to allow warnings],
1351 [gl_cv_cxx_wallow],
1352 [rm -fr conftest*
1353 echo 'int dummy;' > conftest.cc
1354 AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -c conftest.cc 2>conftest1.err]) >/dev/null
1355 AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -Wno-error -c conftest.cc 2>conftest2.err]) >/dev/null
1356 dnl Test the number of error output lines, because AIX xlC accepts the
1357 dnl option '-Wno-error', just to produce a warning
1358 dnl "Option -Wno-error was incorrectly specified. The option will be ignored."
1359 dnl afterwards.
1360 if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then
1361 gl_cv_cxx_wallow='-Wno-error'
1362 else
1363 gl_cv_cxx_wallow=none
1364 fi
1365 rm -fr conftest*
1366 ])
1367 case "$gl_cv_cxx_wallow" in
1368 none) GL_CXXFLAG_ALLOW_WARNINGS='' ;;
1369 *) GL_CXXFLAG_ALLOW_WARNINGS="$gl_cv_cxx_wallow" ;;
1370 esac
1371 else
1372 GL_CXXFLAG_ALLOW_WARNINGS=''
1373 fi
1374 AC_SUBST([GL_CXXFLAG_ALLOW_WARNINGS])
1375 ])
1376
1377 # gl_CC_GNULIB_WARNINGS
1378 # sets and substitutes a variable GL_CFLAG_GNULIB_WARNINGS, to a $(CC) option
1379 # set that enables or disables warnings as suitable for the Gnulib coding style.
1380 AC_DEFUN([gl_CC_GNULIB_WARNINGS],
1381 [
1382 AC_REQUIRE([gl_CC_ALLOW_WARNINGS])
1383 dnl Assume that the compiler supports -Wno-* options only if it also supports
1384 dnl -Wno-error.
1385 GL_CFLAG_GNULIB_WARNINGS=''
1386 if test -n "$GL_CFLAG_ALLOW_WARNINGS"; then
1387 dnl Enable these warning options:
1388 dnl
1389 dnl GCC clang
1390 dnl -Wno-cast-qual >= 3 >= 3.9
1391 dnl -Wno-conversion >= 3 >= 3.9
1392 dnl -Wno-float-conversion >= 4.9 >= 3.9
1393 dnl -Wno-float-equal >= 3 >= 3.9
1394 dnl -Wimplicit-fallthrough >= 7 >= 3.9
1395 dnl -Wno-pedantic >= 4.8 >= 3.9
1396 dnl -Wno-sign-compare >= 3 >= 3.9
1397 dnl -Wno-sign-conversion >= 4.3 >= 3.9
1398 dnl -Wno-tautological-out-of-range-compare - >= 3.9
1399 dnl -Wno-type-limits >= 4.3 >= 3.9
1400 dnl -Wno-undef >= 3 >= 3.9
1401 dnl -Wno-unsuffixed-float-constants >= 4.5
1402 dnl -Wno-unused-const-variable >= 4.4 >= 3.9
1403 dnl -Wno-unused-function >= 3 >= 3.9
1404 dnl -Wno-unused-parameter >= 3 >= 3.9
1405 dnl
1406 cat > conftest.c <<\EOF
1407 #if (__GNUC__ >= 3 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1408 -Wno-cast-qual
1409 -Wno-conversion
1410 -Wno-float-equal
1411 -Wno-sign-compare
1412 -Wno-undef
1413 -Wno-unused-function
1414 -Wno-unused-parameter
1415 #endif
1416 #if (__GNUC__ + (__GNUC_MINOR__ >= 9) > 4 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1417 -Wno-float-conversion
1418 #endif
1419 #if (__GNUC__ >= 7 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1420 -Wimplicit-fallthrough
1421 #endif
1422 #if (__GNUC__ + (__GNUC_MINOR__ >= 8) > 4 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1423 -Wno-pedantic
1424 #endif
1425 #if 3 < __clang_major__ + (9 <= __clang_minor__)
1426 -Wno-tautological-constant-out-of-range-compare
1427 #endif
1428 #if (__GNUC__ + (__GNUC_MINOR__ >= 3) > 4 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1429 -Wno-sign-conversion
1430 -Wno-type-limits
1431 #endif
1432 #if (__GNUC__ + (__GNUC_MINOR__ >= 4) > 4 && !defined __clang__) || (__clang_major__ + (__clang_minor__ >= 9) > 3)
1433 -Wno-unused-const-variable
1434 #endif
1435 #if (__GNUC__ + (__GNUC_MINOR__ >= 5) > 4 && !defined __clang__)
1436 -Wno-unsuffixed-float-constants
1437 #endif
1438 EOF
1439 gl_command="$CC $CFLAGS $CPPFLAGS -E conftest.c > conftest.out"
1440 if AC_TRY_EVAL([gl_command]); then
1441 gl_options=`grep -v '#' conftest.out`
1442 for word in $gl_options; do
1443 GL_CFLAG_GNULIB_WARNINGS="$GL_CFLAG_GNULIB_WARNINGS $word"
1444 done
1445 fi
1446 rm -f conftest.c conftest.out
1447 fi
1448 AC_SUBST([GL_CFLAG_GNULIB_WARNINGS])
1449 ])
1450
1451 dnl gl_CONDITIONAL_HEADER([foo.h])
1452 dnl takes a shell variable GL_GENERATE_FOO_H (with value true or false) as input
1453 dnl and produces
1454 dnl - an AC_SUBSTed variable FOO_H that is either a file name or empty, based
1455 dnl on whether GL_GENERATE_FOO_H is true or false,
1456 dnl - an Automake conditional GL_GENERATE_FOO_H that evaluates to the value of
1457 dnl the shell variable GL_GENERATE_FOO_H.
1458 AC_DEFUN([gl_CONDITIONAL_HEADER],
1459 [
1460 m4_pushdef([gl_header_name], AS_TR_SH(m4_toupper($1)))
1461 m4_pushdef([gl_generate_var], [GL_GENERATE_]AS_TR_SH(m4_toupper($1)))
1462 m4_pushdef([gl_generate_cond], [GL_GENERATE_]AS_TR_SH(m4_toupper($1)))
1463 case "$gl_generate_var" in
1464 false) gl_header_name='' ;;
1465 true)
1466 dnl It is OK to use a .h file in lib/ from within tests/, but not vice
1467 dnl versa.
1468 if test -z "$gl_header_name"; then
1469 gl_header_name="${gl_source_base_prefix}$1"
1470 fi
1471 ;;
1472 *) echo "*** gl_generate_var is not set correctly" 1>&2; exit 1 ;;
1473 esac
1474 AC_SUBST(gl_header_name)
1475 gl_CONDITIONAL(gl_generate_cond, [$gl_generate_var])
1476 m4_popdef([gl_generate_cond])
1477 m4_popdef([gl_generate_var])
1478 m4_popdef([gl_header_name])
1479 ])
1480
1481 dnl Preparations for gl_CHECK_FUNCS_MACOS.
1482 AC_DEFUN([gl_PREPARE_CHECK_FUNCS_MACOS],
1483 [
1484 AC_REQUIRE([AC_CANONICAL_HOST])
1485 AC_REQUIRE([gl_COMPILER_CLANG])
1486 AC_CACHE_CHECK([for compiler option needed when checking for future declarations],
1487 [gl_cv_compiler_check_future_option],
1488 [case "$host_os" in
1489 dnl This is only needed on macOS.
1490 darwin*)
1491 if test $gl_cv_compiler_clang = yes; then
1492 dnl Test whether the compiler supports the option
1493 dnl '-Werror=unguarded-availability-new'.
1494 saved_ac_compile="$ac_compile"
1495 ac_compile="$ac_compile -Werror=unguarded-availability-new"
1496 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]],[[]])],
1497 [gl_cv_compiler_check_future_option='-Werror=unguarded-availability-new'],
1498 [gl_cv_compiler_check_future_option=none])
1499 ac_compile="$saved_ac_compile"
1500 else
1501 gl_cv_compiler_check_future_option=none
1502 fi
1503 ;;
1504 *) gl_cv_compiler_check_future_option=none ;;
1505 esac
1506 ])
1507 ])
1508
1509 dnl Pieces of the expansion of
1510 dnl gl_CHECK_FUNCS_ANDROID
1511 dnl gl_CHECK_FUNCS_MACOS
1512 dnl gl_CHECK_FUNCS_ANDROID_MACOS
1513
1514 AC_DEFUN([gl_CHECK_FUNCS_DEFAULT_CASE],
1515 [
1516 *)
1517 AC_CHECK_FUNC([$1])
1518 [gl_cv_onwards_func_][$1]=$[ac_cv_func_][$1]
1519 ;;
1520 ])
1521
1522 AC_DEFUN([gl_CHECK_FUNCS_CASE_FOR_ANDROID],
1523 [
1524 linux*-android*)
1525 AC_CHECK_DECL([$1], , , [$2])
1526 if test $[ac_cv_have_decl_][$1] = yes; then
1527 AC_CHECK_FUNC([[$1]])
1528 if test $[ac_cv_func_][$1] = yes; then
1529 [gl_cv_onwards_func_][$1]=yes
1530 else
1531 dnl The function is declared but does not exist. This should not
1532 dnl happen normally. But anyway, we know that a future version
1533 dnl of Android will have the function.
1534 [gl_cv_onwards_func_][$1]='future OS version'
1535 fi
1536 else
1537 [gl_cv_onwards_func_][$1]='future OS version'
1538 fi
1539 ;;
1540 ])
1541
1542 AC_DEFUN([gl_CHECK_FUNCS_CASE_FOR_MACOS],
1543 [
1544 darwin*)
1545 if test "x$gl_cv_compiler_check_future_option" != "xnone"; then
1546 dnl Use a compile test, not a link test.
1547 saved_ac_compile="$ac_compile"
1548 ac_compile="$ac_compile $gl_cv_compiler_check_future_option"
1549 saved_ac_compile_for_check_decl="$ac_compile_for_check_decl"
1550 ac_compile_for_check_decl="$ac_compile_for_check_decl $gl_cv_compiler_check_future_option"
1551 unset [ac_cv_have_decl_][$1]
1552 AC_CHECK_DECL([$1], , , [$2])
1553 ac_compile="$saved_ac_compile"
1554 ac_compile_for_check_decl="$saved_ac_compile_for_check_decl"
1555 [ac_cv_func_][$1]="$[ac_cv_have_decl_][$1]"
1556 if test $[ac_cv_func_][$1] = yes; then
1557 [gl_cv_onwards_func_][$1]=yes
1558 else
1559 unset [ac_cv_have_decl_][$1]
1560 AC_CHECK_DECL([$1], , , [$2])
1561 if test $[ac_cv_have_decl_][$1] = yes; then
1562 [gl_cv_onwards_func_][$1]='future OS version'
1563 else
1564 [gl_cv_onwards_func_][$1]=no
1565 fi
1566 fi
1567 else
1568 AC_CHECK_FUNC([$1])
1569 [gl_cv_onwards_func_][$1]=$[ac_cv_func_][$1]
1570 fi
1571 ;;
1572 ])
1573
1574 AC_DEFUN([gl_CHECK_FUNCS_SET_RESULTS],
1575 [
1576 case "$[gl_cv_onwards_func_][$1]" in
1577 future*) [ac_cv_func_][$1]=no ;;
1578 *) [ac_cv_func_][$1]=$[gl_cv_onwards_func_][$1] ;;
1579 esac
1580 if test $[ac_cv_func_][$1] = yes; then
1581 AC_DEFINE([HAVE_]m4_translit([[$1]],
1582 [abcdefghijklmnopqrstuvwxyz],
1583 [ABCDEFGHIJKLMNOPQRSTUVWXYZ]),
1584 [1], [Define to 1 if you have the `$1' function.])
1585 fi
1586 ])
1587
1588 dnl gl_CHECK_FUNCS_ANDROID([func], [[#include <foo.h>]])
1589 dnl is like AC_CHECK_FUNCS([func]), taking into account a portability problem
1590 dnl on Android.
1591 dnl
1592 dnl When code is compiled on Android, it is in the context of a certain
1593 dnl "Android API level", which indicates the minimum version of Android on
1594 dnl which the app can be installed. In other words, you don't compile for a
1595 dnl specific version of Android. You compile for all versions of Android,
1596 dnl onwards from the given API level.
1597 dnl Thus, the question "does the OS have the function func" has three possible
1598 dnl answers:
1599 dnl - yes, in all versions starting from the given API level,
1600 dnl - no, in no version,
1601 dnl - not in the given API level, but in a later version of Android.
1602 dnl
1603 dnl In detail, this works as follows:
1604 dnl If func was added to Android API level, say, 28, then the libc.so has the
1605 dnl symbol func always, whereas the header file <foo.h> declares func
1606 dnl conditionally:
1607 dnl #if __ANDROID_API__ >= 28
1608 dnl ... func (...) __INTRODUCED_IN(28);
1609 dnl #endif
1610 dnl Thus, when compiling with "clang -target armv7a-unknown-linux-android28",
1611 dnl the function func is declared and exists in libc.
1612 dnl Whereas when compiling with "clang -target armv7a-unknown-linux-android27",
1613 dnl the function func is not declared but exists in libc.
1614 dnl
1615 dnl This macro sets two variables:
1616 dnl - gl_cv_onwards_func_<func> to yes / no / "future OS version"
1617 dnl - ac_cv_func_<func> to yes / no / no
1618 dnl The first variable allows to distinguish all three cases.
1619 dnl The second variable is set, so that an invocation
1620 dnl gl_CHECK_FUNCS_ANDROID([func], [[#include <foo.h>]])
1621 dnl can be used as a drop-in replacement for
1622 dnl AC_CHECK_FUNCS([func]).
1623 AC_DEFUN([gl_CHECK_FUNCS_ANDROID],
1624 [
1625 AC_REQUIRE([AC_CANONICAL_HOST])
1626 AC_CACHE_CHECK([for [$1]],
1627 [[gl_cv_onwards_func_][$1]],
1628 [gl_SILENT([
1629 case "$host_os" in
1630 gl_CHECK_FUNCS_CASE_FOR_ANDROID([$1], [$2])
1631 gl_CHECK_FUNCS_DEFAULT_CASE([$1])
1632 esac
1633 ])
1634 ])
1635 gl_CHECK_FUNCS_SET_RESULTS([$1])
1636 ])
1637
1638 dnl gl_CHECK_FUNCS_MACOS([func], [[#include <foo.h>]])
1639 dnl is like AC_CHECK_FUNCS([func]), taking into account a portability problem
1640 dnl on macOS.
1641 dnl
1642 dnl When code is compiled on macOS, it is in the context of a certain minimum
1643 dnl macOS version, that can be set through the option '-mmacosx-version-min='.
1644 dnl In other words, you don't compile for a specific version of macOS. You
1645 dnl compile for all versions of macOS, onwards from the given version.
1646 dnl Thus, the question "does the OS have the function func" has three possible
1647 dnl answers:
1648 dnl - yes, in all versions starting from the given version,
1649 dnl - no, in no version,
1650 dnl - not in the given version, but in a later version of macOS.
1651 dnl
1652 dnl In detail, this works as follows:
1653 dnl If func was added to, say, macOS version 13, then the libc has the
1654 dnl symbol func always, whereas the header file <foo.h> declares func
1655 dnl conditionally with a special availability attribute:
1656 dnl ... func (...) __attribute__((availability(macos,introduced=13.0)));
1657 dnl Thus, when compiling with "clang mmacosx-version-min=13", there is no
1658 dnl warning about the use of func, and the resulting binary
1659 dnl - runs fine on macOS 13,
1660 dnl - aborts with a dyld "Symbol not found" message on macOS 12.
1661 dnl Whereas, when compiling with "clang mmacosx-version-min=12", there is a
1662 dnl warning: 'func' is only available on macOS 13.0 or newer
1663 dnl [-Wunguarded-availability-new],
1664 dnl and the resulting binary
1665 dnl - runs fine on macOS 13,
1666 dnl - crashes with a SIGSEGV (signal 11) on macOS 12.
1667 dnl
1668 dnl This macro sets two variables:
1669 dnl - gl_cv_onwards_func_<func> to yes / no / "future OS version"
1670 dnl - ac_cv_func_<func> to yes / no / no
1671 dnl The first variable allows to distinguish all three cases.
1672 dnl The second variable is set, so that an invocation
1673 dnl gl_CHECK_FUNCS_MACOS([func], [[#include <foo.h>]])
1674 dnl can be used as a drop-in replacement for
1675 dnl AC_CHECK_FUNCS([func]).
1676 AC_DEFUN([gl_CHECK_FUNCS_MACOS],
1677 [
1678 AC_REQUIRE([AC_CANONICAL_HOST])
1679 AC_REQUIRE([gl_PREPARE_CHECK_FUNCS_MACOS])
1680 AC_CACHE_CHECK([for [$1]],
1681 [[gl_cv_onwards_func_][$1]],
1682 [gl_SILENT([
1683 case "$host_os" in
1684 gl_CHECK_FUNCS_CASE_FOR_MACOS([$1], [$2])
1685 gl_CHECK_FUNCS_DEFAULT_CASE([$1])
1686 esac
1687 ])
1688 ])
1689 gl_CHECK_FUNCS_SET_RESULTS([$1])
1690 ])
1691
1692 dnl gl_CHECK_FUNCS_ANDROID_MACOS([func], [[#include <foo.h>]])
1693 dnl is like AC_CHECK_FUNCS([func]), taking into account a portability problem
1694 dnl on Android and on macOS.
1695 dnl It is the combination of gl_CHECK_FUNCS_ANDROID and gl_CHECK_FUNCS_MACOS.
1696 AC_DEFUN([gl_CHECK_FUNCS_ANDROID_MACOS],
1697 [
1698 AC_REQUIRE([AC_CANONICAL_HOST])
1699 AC_REQUIRE([gl_PREPARE_CHECK_FUNCS_MACOS])
1700 AC_CACHE_CHECK([for [$1]],
1701 [[gl_cv_onwards_func_][$1]],
1702 [gl_SILENT([
1703 case "$host_os" in
1704 gl_CHECK_FUNCS_CASE_FOR_ANDROID([$1], [$2])
1705 gl_CHECK_FUNCS_CASE_FOR_MACOS([$1], [$2])
1706 gl_CHECK_FUNCS_DEFAULT_CASE([$1])
1707 esac
1708 ])
1709 ])
1710 gl_CHECK_FUNCS_SET_RESULTS([$1])
1711 ])
1712
1713 dnl Expands to some code for use in .c programs that, on native Windows, defines
1714 dnl the Microsoft deprecated alias function names to the underscore-prefixed
1715 dnl actual function names. With this macro, these function names are available
1716 dnl without linking with '-loldnames' and without generating warnings.
1717 dnl Usage: Use it after all system header files are included.
1718 dnl #include <...>
1719 dnl #include <...>
1720 dnl ]GL_MDA_DEFINES[
1721 dnl ...
1722 AC_DEFUN([GL_MDA_DEFINES],[
1723 AC_REQUIRE([_GL_MDA_DEFINES])
1724 [$gl_mda_defines]
1725 ])
1726 AC_DEFUN([_GL_MDA_DEFINES],
1727 [gl_mda_defines='
1728 #if defined _WIN32 && !defined __CYGWIN__
1729 #define access _access
1730 #define chdir _chdir
1731 #define chmod _chmod
1732 #define close _close
1733 #define creat _creat
1734 #define dup _dup
1735 #define dup2 _dup2
1736 #define ecvt _ecvt
1737 #define execl _execl
1738 #define execle _execle
1739 #define execlp _execlp
1740 #define execv _execv
1741 #define execve _execve
1742 #define execvp _execvp
1743 #define execvpe _execvpe
1744 #define fcloseall _fcloseall
1745 #define fcvt _fcvt
1746 #define fdopen _fdopen
1747 #define fileno _fileno
1748 #define gcvt _gcvt
1749 #define getcwd _getcwd
1750 #define getpid _getpid
1751 #define getw _getw
1752 #define isatty _isatty
1753 #define j0 _j0
1754 #define j1 _j1
1755 #define jn _jn
1756 #define lfind _lfind
1757 #define lsearch _lsearch
1758 #define lseek _lseek
1759 #define memccpy _memccpy
1760 #define mkdir _mkdir
1761 #define mktemp _mktemp
1762 #define open _open
1763 #define putenv _putenv
1764 #define putw _putw
1765 #define read _read
1766 #define rmdir _rmdir
1767 #define strdup _strdup
1768 #define swab _swab
1769 #define tempnam _tempnam
1770 #define tzset _tzset
1771 #define umask _umask
1772 #define unlink _unlink
1773 #define utime _utime
1774 #define wcsdup _wcsdup
1775 #define write _write
1776 #define y0 _y0
1777 #define y1 _y1
1778 #define yn _yn
1779 #endif
1780 '
1781 ])