Debian Codegraph @

m4/gnulib-common.m4

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