Debian Codegraph @

m4/gnulib-common.m4

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