Debian Codegraph @

build-aux/m4/zw_automodern.m4

raw
1 dnl Copyright 2017 Zack Weinberg <zackw at panix.com>.
2 dnl Partially based on Autoconf, copyright 1992-2017 Free Software Foundation.
3 dnl
4 dnl This program is free software; you can redistribute it and/or
5 dnl modify it under the terms of the GNU General Public License as
6 dnl published by the Free Software Foundation, either version 3 of the
7 dnl License, or (at your option) any later version.
8 dnl
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 dnl GNU General Public License for more details.
13 dnl
14 dnl Under Section 7 of GPL version 3, you are granted additional
15 dnl permissions described in the Autoconf Configure Script Exception,
16 dnl version 3.0, as published by the Free Software Foundation.
17 dnl
18 dnl Because only two files in this source tree are released
19 dnl under GPLv3 with exceptions, neither the GPLv3 nor the exception are
20 dnl distributed with this source tree. Copies can be retrieved from
21 dnl https://www.gnu.org/licenses/
22 dnl
23 dnl As of this writing (September 2017), Autoconf 2.70 is not yet released.
24 dnl Backport some improvements:
25 dnl - switch AC_CHECK_HEADER to compile-only
26 dnl - eliminate unnecessary tests in AC_INCLUDES_DEFAULT
27 dnl - Darwin (OSX) support in AC_USE_SYSTEM_EXTENSIONS
28 dnl - C11 mode by default in AC_PROG_CC, falling back to C99
29 AC_PREREQ([2.62])dnl earliest version with working m4_version_prereq
30 m4_version_prereq([2.70], [], [
31
32 m4_define([AC_CHECK_HEADER], [_AC_CHECK_HEADER_COMPILE($@)])
33
34 AC_DEFUN_ONCE([_AC_INCLUDES_DEFAULT_REQUIREMENTS],
35 [m4_divert_text([DEFAULTS],
36 [# Factoring default headers for most tests.
37 ac_includes_default="\
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
47 #endif
48 #ifdef HAVE_STRINGS_H
49 # include <strings.h>
50 #endif
51 #ifdef HAVE_INTTYPES_H
52 # include <inttypes.h>
53 #endif
54 #ifdef HAVE_STDINT_H
55 # include <stdint.h>
56 #endif
57 #ifdef HAVE_UNISTD_H
58 # include <unistd.h>
59 #endif"
60 ])]dnl
61 [AC_CHECK_HEADERS(
62 [sys/types.h sys/stat.h strings.h inttypes.h stdint.h unistd.h],,,[/**/])]dnl
63 dnl For backward compatibility, provide unconditional AC_DEFINEs of
64 dnl HAVE_STDLIB_H, HAVE_STRING_H, and STDC_HEADERS.
65 [AC_DEFINE([HAVE_STDLIB_H], [1],
66 [Always define to 1, for backward compatibility.
67 You can assume <stdlib.h> exists.])]dnl
68 [AC_DEFINE([HAVE_STRING_H], [1],
69 [Always define to 1, for backward compatibility.
70 You can assume <string.h> exists.])]dnl
71 [AC_DEFINE([STDC_HEADERS], [1],
72 [Always define to 1, for backward compatibility.
73 You can assume the C90 standard headers exist.])])
74
75 m4_define([AC_USE_SYSTEM_EXTENSIONS],
76 m4_defn([AC_USE_SYSTEM_EXTENSIONS])[
77 AH_VERBATIM([USE_SYSTEM_EXTENSIONS_270],
78 [/* Enable general extensions on OSX. */
79 #ifndef _DARWIN_C_SOURCE
80 # undef _DARWIN_C_SOURCE
81 #endif
82 ])
83 AC_DEFINE([_DARWIN_C_SOURCE])
84 ])
85
86 dnl Prior to 2.70, AC_PROG_CC ends with an unconditional call to
87 dnl _AC_PROG_CC_C89. Use this as an extension hook, replacing it with
88 dnl the logic that will be used in 2.70.
89 m4_define([_AC_PROG_CC_C89_original], m4_defn([_AC_PROG_CC_C89]))
90 m4_define([_AC_PROG_CC_C89], [dnl
91 dnl Set ac_prog_cc_stdc to the supported C version.
92 dnl Also set the documented variable ac_cv_prog_cc_stdc;
93 dnl its name was chosen when it was cached, but it is no longer cached.
94 _AC_PROG_CC_C11([ac_prog_cc_stdc=c11
95 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c11],
96 [_AC_PROG_CC_C99([ac_prog_cc_stdc=c99
97 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c99],
98 [_AC_PROG_CC_C89_original([ac_prog_cc_stdc=c89
99 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c89],
100 [ac_prog_cc_stdc=no
101 ac_cv_prog_cc_stdc=no])])])
102 ])
103 dnl Must also supply the definition of _AC_PROG_CC_C11.
104 dnl This is also taken verbatim from Autoconf trunk.
105 dnl I regret the bulk.
106 AC_DEFUN([_AC_PROG_CC_C11],
107 [_AC_C_STD_TRY([c11],
108 [[#include <stdarg.h>
109 #include <stdbool.h>
110 #include <stddef.h>
111 #include <stdlib.h>
112 #include <wchar.h>
113 #include <stdio.h>
114
115 // Check varargs macros. These examples are taken from C99 6.10.3.5.
116 #define debug(...) fprintf (stderr, __VA_ARGS__)
117 #define showlist(...) puts (#__VA_ARGS__)
118 #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
119 static void
120 test_varargs_macros (void)
121 {
122 int x = 1234;
123 int y = 5678;
124 debug ("Flag");
125 debug ("X = %d\n", x);
126 showlist (The first, second, and third items.);
127 report (x>y, "x is %d but y is %d", x, y);
128 }
129
130 // Check long long types.
131 #define BIG64 18446744073709551615ull
132 #define BIG32 4294967295ul
133 #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
134 #if !BIG_OK
135 your preprocessor is broken;
136 #endif
137 #if BIG_OK
138 #else
139 your preprocessor is broken;
140 #endif
141 static long long int bignum = -9223372036854775807LL;
142 static unsigned long long int ubignum = BIG64;
143
144 struct incomplete_array
145 {
146 int datasize;
147 double data[];
148 };
149
150 struct named_init {
151 int number;
152 const wchar_t *name;
153 double average;
154 };
155
156 typedef const char *ccp;
157
158 static inline int
159 test_restrict (ccp restrict text)
160 {
161 // See if C++-style comments work.
162 // Iterate through items via the restricted pointer.
163 // Also check for declarations in for loops.
164 for (unsigned int i = 0; *(text+i) != '\0'; ++i)
165 continue;
166 return 0;
167 }
168
169 // Check varargs and va_copy.
170 static bool
171 test_varargs (const char *format, ...)
172 {
173 va_list args;
174 va_start (args, format);
175 va_list args_copy;
176 va_copy (args_copy, args);
177
178 const char *str = "";
179 int number = 0;
180 float fnumber = 0;
181
182 while (*format)
183 {
184 switch (*format++)
185 {
186 case 's': // string
187 str = va_arg (args_copy, const char *);
188 break;
189 case 'd': // int
190 number = va_arg (args_copy, int);
191 break;
192 case 'f': // float
193 fnumber = va_arg (args_copy, double);
194 break;
195 default:
196 break;
197 }
198 }
199 va_end (args_copy);
200 va_end (args);
201
202 return *str && number && fnumber;
203 }
204
205 // Check _Alignas.
206 char _Alignas (double) aligned_as_double;
207 char _Alignas (0) no_special_alignment;
208 extern char aligned_as_int;
209 char _Alignas (0) _Alignas (int) aligned_as_int;
210
211 // Check _Alignof.
212 enum
213 {
214 int_alignment = _Alignof (int),
215 int_array_alignment = _Alignof (int[100]),
216 char_alignment = _Alignof (char)
217 };
218 _Static_assert (0 < -_Alignof (int), "_Alignof is signed");
219
220 // Check _Noreturn.
221 int _Noreturn does_not_return (void) { for (;;) continue; }
222
223 // Check _Static_assert.
224 struct test_static_assert
225 {
226 int x;
227 _Static_assert (sizeof (int) <= sizeof (long int),
228 "_Static_assert does not work in struct");
229 long int y;
230 };
231
232 // Check UTF-8 literals.
233 #define u8 syntax error!
234 char const utf8_literal[] = u8"happens to be ASCII" "another string";
235
236 // Check duplicate typedefs.
237 typedef long *long_ptr;
238 typedef long int *long_ptr;
239 typedef long_ptr long_ptr;
240
241 // Anonymous structures and unions -- taken from C11 6.7.2.1 Example 1.
242 struct anonymous
243 {
244 union {
245 struct { int i; int j; };
246 struct { int k; long int l; } w;
247 };
248 int m;
249 } v1;
250 ]],
251 [[
252 // Check bool.
253 _Bool success = false;
254
255 // Check restrict.
256 if (test_restrict ("String literal") == 0)
257 success = true;
258 char *restrict newvar = "Another string";
259
260 // Check varargs.
261 success &= test_varargs ("s, d' f .", "string", 65, 34.234);
262 test_varargs_macros ();
263
264 // Check flexible array members.
265 struct incomplete_array *ia =
266 malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
267 ia->datasize = 10;
268 for (int i = 0; i < ia->datasize; ++i)
269 ia->data[i] = i * 1.234;
270
271 // Check named initializers.
272 struct named_init ni = {
273 .number = 34,
274 .name = L"Test wide string",
275 .average = 543.34343,
276 };
277
278 ni.number = 58;
279
280 int dynamic_array[ni.number];
281 dynamic_array[ni.number - 1] = 543;
282
283 // work around unused variable warnings
284 return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
285 || dynamic_array[ni.number - 1] != 543);
286
287 v1.i = 2;
288 v1.w.k = 5;
289 _Static_assert ((offsetof (struct anonymous, i)
290 == offsetof (struct anonymous, w.k)),
291 "Anonymous union alignment botch");
292 ]],
293 dnl Try
294 dnl GCC -std=gnu11 (unused restrictive mode: -std=c11)
295 dnl with extended modes being tried first.
296 dnl
297 dnl Do not try -qlanglvl=extc1x, because IBM XL C V12.1 (the latest version as
298 dnl of September 2012) does not pass the C11 test. For now, try extc1x when
299 dnl compiling the C99 test instead, since it enables _Static_assert and
300 dnl _Noreturn, which is a win. If -qlanglvl=extc11 or -qlanglvl=extc1x passes
301 dnl the C11 test in some future version of IBM XL C, we'll add it here,
302 dnl preferably extc11.
303 [[-std=gnu11]], [$1], [$2])[]dnl
304 ])# _AC_PROG_CC_C11
305
306
307 ])dnl m4_version_prereq([2.70], ...)