c_backported.m4 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. # Backported macros from autoconf git master + a few custom patches
  2. # This file is part of Autoconf. -*- Autoconf -*-
  3. # Programming languages support.
  4. # Copyright (C) 2001-2017, 2020-2023 Free Software Foundation, Inc.
  5. # This file is part of Autoconf. This program is free
  6. # software; you can redistribute it and/or modify it under the
  7. # terms of the GNU General Public License as published by the
  8. # Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # Under Section 7 of GPL version 3, you are granted additional
  17. # permissions described in the Autoconf Configure Script Exception,
  18. # version 3.0, as published by the Free Software Foundation.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # and a copy of the Autoconf Configure Script Exception along with
  22. # this program; see the files COPYINGv3 and COPYING.EXCEPTION
  23. # respectively. If not, see <https://www.gnu.org/licenses/>.
  24. # Written by David MacKenzie, with help from
  25. # Akim Demaille, Paul Eggert,
  26. # François Pinard, Karl Berry, Richard Pixley, Ian Lance Taylor,
  27. # Roland McGrath, Noah Friedman, david d zuhn, and many others.
  28. # ---- Backported macros only ----
  29. AC_DEFUN([_AC_C_C89_TEST_GLOBALS],
  30. [m4_divert_text([INIT_PREPARE],
  31. [[# Test code for whether the C compiler supports C89 (global declarations)
  32. ac_c_conftest_c89_globals='
  33. /* Does the compiler advertise C89 conformance?
  34. Do not test the value of __STDC__, because some compilers set it to 0
  35. while being otherwise adequately conformant. */
  36. #if !defined __STDC__
  37. # error "Compiler does not advertise C89 conformance"
  38. #endif
  39. #include <stddef.h>
  40. #include <stdarg.h>
  41. struct stat;
  42. /* Most of the following tests are stolen from RCS 5.7 src/conf.sh. */
  43. struct buf { int x; };
  44. struct buf * (*rcsopen) (struct buf *, struct stat *, int);
  45. static char *e (char **p, int i)
  46. {
  47. return p[i];
  48. }
  49. static char *f (char * (*g) (char **, int), char **p, ...)
  50. {
  51. char *s;
  52. va_list v;
  53. va_start (v,p);
  54. s = g (p, va_arg (v,int));
  55. va_end (v);
  56. return s;
  57. }
  58. /* C89 style stringification. */
  59. #define noexpand_stringify(a) #a
  60. const char *stringified = noexpand_stringify(arbitrary+token=sequence);
  61. /* C89 style token pasting. Exercises some of the corner cases that
  62. e.g. old MSVC gets wrong, but not very hard. */
  63. #define noexpand_concat(a,b) a##b
  64. #define expand_concat(a,b) noexpand_concat(a,b)
  65. extern int vA;
  66. extern int vbee;
  67. #define aye A
  68. #define bee B
  69. int *pvA = &expand_concat(v,aye);
  70. int *pvbee = &noexpand_concat(v,bee);
  71. /* OSF 4.0 Compaq cc is some sort of almost-ANSI by default. It has
  72. function prototypes and stuff, but not \xHH hex character constants.
  73. These do not provoke an error unfortunately, instead are silently treated
  74. as an "x". The following induces an error, until -std is added to get
  75. proper ANSI mode. Curiously \x00 != x always comes out true, for an
  76. array size at least. It is necessary to write \x00 == 0 to get something
  77. that is true only with -std. */
  78. int osf4_cc_array ['\''\x00'\'' == 0 ? 1 : -1];
  79. /* IBM C 6 for AIX is almost-ANSI by default, but it replaces macro parameters
  80. inside strings and character constants. */
  81. #define FOO(x) '\''x'\''
  82. int xlc6_cc_array[FOO(a) == '\''x'\'' ? 1 : -1];
  83. int test (int i, double x);
  84. struct s1 {int (*f) (int a);};
  85. struct s2 {int (*f) (double a);};
  86. int pairnames (int, char **, int *(*)(struct buf *, struct stat *, int),
  87. int, int);'
  88. ]])])
  89. AC_DEFUN([_AC_C_C99_TEST_GLOBALS],
  90. [m4_divert_text([INIT_PREPARE],
  91. [[# Test code for whether the C compiler supports C99 (global declarations)
  92. ac_c_conftest_c99_globals='
  93. // Does the compiler advertise C99 conformance?
  94. #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
  95. # error "Compiler does not advertise C99 conformance"
  96. #endif
  97. #include <stdbool.h>
  98. extern int puts (const char *);
  99. extern int printf (const char *, ...);
  100. extern int dprintf (int, const char *, ...);
  101. extern void *malloc (size_t);
  102. extern void free (void *);
  103. // Check varargs macros. These examples are taken from C99 6.10.3.5.
  104. // dprintf is used instead of fprintf to avoid needing to declare
  105. // FILE and stderr.
  106. #define debug(...) dprintf (2, __VA_ARGS__)
  107. #define showlist(...) puts (#__VA_ARGS__)
  108. #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
  109. static void
  110. test_varargs_macros (void)
  111. {
  112. int x = 1234;
  113. int y = 5678;
  114. debug ("Flag");
  115. debug ("X = %d\n", x);
  116. showlist (The first, second, and third items.);
  117. report (x>y, "x is %d but y is %d", x, y);
  118. }
  119. // Check long long types.
  120. #define BIG64 18446744073709551615ull
  121. #define BIG32 4294967295ul
  122. #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
  123. #if !BIG_OK
  124. #error "your preprocessor is broken"
  125. #endif
  126. #if BIG_OK
  127. #else
  128. #error "your preprocessor is broken"
  129. #endif
  130. static long long int bignum = -9223372036854775807LL;
  131. static unsigned long long int ubignum = BIG64;
  132. struct incomplete_array
  133. {
  134. int datasize;
  135. double data[];
  136. };
  137. struct named_init {
  138. int number;
  139. const wchar_t *name;
  140. double average;
  141. };
  142. typedef const char *ccp;
  143. static inline int
  144. test_restrict (ccp restrict text)
  145. {
  146. // See if C++-style comments work.
  147. // Iterate through items via the restricted pointer.
  148. // Also check for declarations in for loops.
  149. for (unsigned int i = 0; *(text+i) != '\''\0'\''; ++i)
  150. continue;
  151. return 0;
  152. }
  153. // Check varargs and va_copy.
  154. static bool
  155. test_varargs (const char *format, ...)
  156. {
  157. va_list args;
  158. va_start (args, format);
  159. va_list args_copy;
  160. va_copy (args_copy, args);
  161. const char *str = "";
  162. int number = 0;
  163. float fnumber = 0;
  164. while (*format)
  165. {
  166. switch (*format++)
  167. {
  168. case '\''s'\'': // string
  169. str = va_arg (args_copy, const char *);
  170. break;
  171. case '\''d'\'': // int
  172. number = va_arg (args_copy, int);
  173. break;
  174. case '\''f'\'': // float
  175. fnumber = va_arg (args_copy, double);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. va_end (args_copy);
  182. va_end (args);
  183. return *str && number && fnumber;
  184. }
  185. '
  186. ]])])
  187. AC_DEFUN([_AC_C_C11_TEST_GLOBALS],
  188. [m4_divert_text([INIT_PREPARE],
  189. [[# Test code for whether the C compiler supports C11 (global declarations)
  190. ac_c_conftest_c11_globals='
  191. // Does the compiler advertise C11 conformance?
  192. #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112L
  193. # error "Compiler does not advertise C11 conformance"
  194. #endif
  195. // Check _Alignas.
  196. char _Alignas (double) aligned_as_double;
  197. char _Alignas (0) no_special_alignment;
  198. extern char aligned_as_int;
  199. char _Alignas (0) _Alignas (int) aligned_as_int;
  200. // Check _Alignof.
  201. enum
  202. {
  203. int_alignment = _Alignof (int),
  204. int_array_alignment = _Alignof (int[100]),
  205. char_alignment = _Alignof (char)
  206. };
  207. _Static_assert (0 < -_Alignof (int), "_Alignof is signed");
  208. // Check _Noreturn.
  209. _Noreturn int does_not_return (void) { for (;;) continue; }
  210. // Check _Static_assert.
  211. struct test_static_assert
  212. {
  213. int x;
  214. _Static_assert (sizeof (int) <= sizeof (long int),
  215. "_Static_assert does not work in struct");
  216. long int y;
  217. };
  218. // Check UTF-8 literals.
  219. #define u8 syntax error!
  220. char const utf8_literal[] = u8"happens to be ASCII" "another string";
  221. // Check duplicate typedefs.
  222. typedef long *long_ptr;
  223. typedef long int *long_ptr;
  224. typedef long_ptr long_ptr;
  225. // Anonymous structures and unions -- taken from C11 6.7.2.1 Example 1.
  226. struct anonymous
  227. {
  228. union {
  229. struct { int i; int j; };
  230. struct { int k; long int l; } w;
  231. };
  232. int m;
  233. } v1;
  234. '
  235. ]])])
  236. # AC_LANG_CALL(C)(PROLOGUE, FUNCTION)
  237. # -----------------------------------
  238. # Avoid conflicting decl of main.
  239. m4_define([AC_LANG_CALL(C)],
  240. [AC_LANG_PROGRAM([$1
  241. m4_if([$2], [main], ,
  242. [/* Override any GCC internal prototype to avoid an error.
  243. Use char because int might match the return type of a GCC
  244. builtin and then its argument prototype would still apply.
  245. The 'extern "C"' is for builds by C++ compilers;
  246. although this is not generally supported in C code supporting it here
  247. has little cost and some practical benefit (sr 110532). */
  248. #ifdef __cplusplus
  249. extern "C"
  250. #endif
  251. char $2 (void);])], [return $2 ();])])
  252. # AC_LANG_FUNC_LINK_TRY(C)(FUNCTION)
  253. # ----------------------------------
  254. # Don't include <ctype.h> because on OSF/1 3.0 it includes
  255. # <sys/types.h> which includes <sys/select.h> which contains a
  256. # prototype for select. Similarly for bzero.
  257. #
  258. # This test used to merely assign f=$1 in main(), but that was
  259. # optimized away by HP unbundled cc A.05.36 for ia64 under +O3,
  260. # presumably on the basis that there's no need to do that store if the
  261. # program is about to exit. Conversely, the AIX linker optimizes an
  262. # unused external declaration that initializes f=$1. So this test
  263. # program has both an external initialization of f, and a use of f in
  264. # main that affects the exit status.
  265. #
  266. m4_define([AC_LANG_FUNC_LINK_TRY(C)],
  267. [AC_LANG_PROGRAM(
  268. [/* Define $1 to an innocuous variant, in case <limits.h> declares $1.
  269. For example, HP-UX 11i <limits.h> declares gettimeofday. */
  270. #define $1 innocuous_$1
  271. /* System header to define __stub macros and hopefully few prototypes,
  272. which can conflict with char $1 (void); below. */
  273. #include <limits.h>
  274. #undef $1
  275. /* Override any GCC internal prototype to avoid an error.
  276. Use char because int might match the return type of a GCC
  277. builtin and then its argument prototype would still apply. */
  278. #ifdef __cplusplus
  279. extern "C"
  280. #endif
  281. char $1 (void);
  282. /* The GNU C library defines this for functions which it implements
  283. to always fail with ENOSYS. Some functions are actually named
  284. something starting with __ and the normal name is an alias. */
  285. #if defined __stub_$1 || defined __stub___$1
  286. choke me
  287. #endif
  288. ], [return $1 ();])])
  289. # AC_C_BIGENDIAN ([ACTION-IF-TRUE], [ACTION-IF-FALSE], [ACTION-IF-UNKNOWN],
  290. # [ACTION-IF-UNIVERSAL])
  291. # -------------------------------------------------------------------------
  292. AC_DEFUN([AC_C_BIGENDIAN],
  293. [AH_VERBATIM([WORDS_BIGENDIAN],
  294. [/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
  295. significant byte first (like Motorola and SPARC, unlike Intel). */
  296. #if defined AC_APPLE_UNIVERSAL_BUILD
  297. # if defined __BIG_ENDIAN__
  298. # define WORDS_BIGENDIAN 1
  299. # endif
  300. #else
  301. # ifndef WORDS_BIGENDIAN
  302. # undef WORDS_BIGENDIAN
  303. # endif
  304. #endif])dnl
  305. AC_CACHE_CHECK([whether byte ordering is bigendian], [ac_cv_c_bigendian],
  306. [ac_cv_c_bigendian=unknown
  307. # See if we're dealing with a universal compiler.
  308. AC_COMPILE_IFELSE(
  309. [AC_LANG_SOURCE(
  310. [[#ifndef __APPLE_CC__
  311. not a universal capable compiler
  312. #endif
  313. typedef int dummy;
  314. ]])],
  315. [
  316. # Check for potential -arch flags. It is not universal unless
  317. # there are at least two -arch flags with different values.
  318. ac_arch=
  319. ac_prev=
  320. for ac_word in $CC $CFLAGS $CPPFLAGS $LDFLAGS; do
  321. if test -n "$ac_prev"; then
  322. case $ac_word in
  323. i?86 | x86_64 | ppc | ppc64)
  324. if test -z "$ac_arch" || test "$ac_arch" = "$ac_word"; then
  325. ac_arch=$ac_word
  326. else
  327. ac_cv_c_bigendian=universal
  328. break
  329. fi
  330. ;;
  331. esac
  332. ac_prev=
  333. elif test "x$ac_word" = "x-arch"; then
  334. ac_prev=arch
  335. fi
  336. done])
  337. if test $ac_cv_c_bigendian = unknown; then
  338. # See if sys/param.h defines the BYTE_ORDER macro.
  339. AC_COMPILE_IFELSE(
  340. [AC_LANG_PROGRAM(
  341. [[#include <sys/types.h>
  342. #include <sys/param.h>
  343. ]],
  344. [[#if ! (defined BYTE_ORDER && defined BIG_ENDIAN \\
  345. && defined LITTLE_ENDIAN && BYTE_ORDER && BIG_ENDIAN \\
  346. && LITTLE_ENDIAN)
  347. bogus endian macros
  348. #endif
  349. ]])],
  350. [# It does; now see whether it defined to BIG_ENDIAN or not.
  351. AC_COMPILE_IFELSE(
  352. [AC_LANG_PROGRAM(
  353. [[#include <sys/types.h>
  354. #include <sys/param.h>
  355. ]],
  356. [[#if BYTE_ORDER != BIG_ENDIAN
  357. not big endian
  358. #endif
  359. ]])],
  360. [ac_cv_c_bigendian=yes],
  361. [ac_cv_c_bigendian=no])])
  362. fi
  363. if test $ac_cv_c_bigendian = unknown; then
  364. # See if <limits.h> defines _LITTLE_ENDIAN or _BIG_ENDIAN (e.g., Solaris).
  365. AC_COMPILE_IFELSE(
  366. [AC_LANG_PROGRAM(
  367. [[#include <limits.h>
  368. ]],
  369. [[#if ! (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
  370. bogus endian macros
  371. #endif
  372. ]])],
  373. [# It does; now see whether it defined to _BIG_ENDIAN or not.
  374. AC_COMPILE_IFELSE(
  375. [AC_LANG_PROGRAM(
  376. [[#include <limits.h>
  377. ]],
  378. [[#ifndef _BIG_ENDIAN
  379. not big endian
  380. #endif
  381. ]])],
  382. [ac_cv_c_bigendian=yes],
  383. [ac_cv_c_bigendian=no])])
  384. fi
  385. if test $ac_cv_c_bigendian = unknown; then
  386. # Compile a test program.
  387. AC_RUN_IFELSE(
  388. [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
  389. [[
  390. /* Are we little or big endian? From Harbison&Steele. */
  391. union
  392. {
  393. long int l;
  394. char c[sizeof (long int)];
  395. } u;
  396. u.l = 1;
  397. return u.c[sizeof (long int) - 1] == 1;
  398. ]])],
  399. [ac_cv_c_bigendian=no],
  400. [ac_cv_c_bigendian=yes],
  401. [# Try to guess by grepping values from an object file.
  402. AC_LINK_IFELSE(
  403. [AC_LANG_SOURCE(
  404. [[unsigned short int ascii_mm[] =
  405. { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };
  406. unsigned short int ascii_ii[] =
  407. { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };
  408. int use_ascii (int i) {
  409. return ascii_mm[i] + ascii_ii[i];
  410. }
  411. unsigned short int ebcdic_ii[] =
  412. { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };
  413. unsigned short int ebcdic_mm[] =
  414. { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };
  415. int use_ebcdic (int i) {
  416. return ebcdic_mm[i] + ebcdic_ii[i];
  417. }
  418. int
  419. main (int argc, char **argv)
  420. {
  421. /* Intimidate the compiler so that it does not
  422. optimize the arrays away. */
  423. char *p = argv[0];
  424. ascii_mm[1] = *p++; ebcdic_mm[1] = *p++;
  425. ascii_ii[1] = *p++; ebcdic_ii[1] = *p++;
  426. return use_ascii (argc) == use_ebcdic (*p);
  427. }]])],
  428. [if grep BIGenDianSyS conftest$ac_exeext >/dev/null; then
  429. ac_cv_c_bigendian=yes
  430. fi
  431. if grep LiTTleEnDian conftest$ac_exeext >/dev/null ; then
  432. if test "$ac_cv_c_bigendian" = unknown; then
  433. ac_cv_c_bigendian=no
  434. else
  435. # finding both strings is unlikely to happen, but who knows?
  436. ac_cv_c_bigendian=unknown
  437. fi
  438. fi])])
  439. fi])
  440. case $ac_cv_c_bigendian in #(
  441. yes)
  442. m4_default([$1],
  443. [AC_DEFINE([WORDS_BIGENDIAN], 1)]);; #(
  444. no)
  445. $2 ;; #(
  446. universal)
  447. dnl Note that AC_APPLE_UNIVERSAL_BUILD sorts less than WORDS_BIGENDIAN;
  448. dnl this is a necessity for proper config header operation. Warn if
  449. dnl the user did not specify a config header but is relying on the
  450. dnl default behavior for universal builds.
  451. m4_default([$4],
  452. [AC_CONFIG_COMMANDS_PRE([m4_ifset([AH_HEADER], [],
  453. [m4_warn([obsolete],
  454. [AC_C_BIGENDIAN should be used with AC_CONFIG_HEADERS])])])dnl
  455. AC_DEFINE([AC_APPLE_UNIVERSAL_BUILD],1,
  456. [Define if building universal (internal helper macro)])])
  457. ;; #(
  458. *)
  459. m4_default([$3],
  460. [AC_MSG_ERROR([unknown endianness
  461. presetting ac_cv_c_bigendian=no (or yes) will help])]) ;;
  462. esac
  463. ])# AC_C_BIGENDIAN
  464. # AC_C_VARARRAYS
  465. # --------------
  466. # Check whether the C compiler supports variable-length arrays.
  467. AC_DEFUN([AC_C_VARARRAYS],
  468. [
  469. AC_CACHE_CHECK([for variable-length arrays],
  470. ac_cv_c_vararrays,
  471. [AC_COMPILE_IFELSE([AC_LANG_SOURCE(
  472. [[ #ifndef __STDC_NO_VLA__
  473. #error __STDC_NO_VLA__ not defined
  474. choke me now
  475. #endif
  476. ]])],
  477. [ac_cv_c_vararrays='no: __STDC_NO_VLA__ is defined'],
  478. [AC_COMPILE_IFELSE(
  479. [AC_LANG_PROGRAM(
  480. [[/* Test for VLA support. This test is partly inspired
  481. from examples in the C standard. Use at least two VLA
  482. functions to detect the GCC 3.4.3 bug described in:
  483. https://lists.gnu.org/archive/html/bug-gnulib/2014-08/msg00014.html
  484. */
  485. #ifdef __STDC_NO_VLA__
  486. syntax error;
  487. #else
  488. extern int n;
  489. static int B[100];
  490. int fvla (int m, int C[m][m]);
  491. static int
  492. simple (int count, int all[static count])
  493. {
  494. return all[count - 1];
  495. }
  496. int
  497. fvla (int m, int C[m][m])
  498. {
  499. typedef int VLA[m][m];
  500. VLA x;
  501. int D[m];
  502. static int (*q)[m] = &B;
  503. int (*s)[n] = q;
  504. (void) simple;
  505. return C && &x[0][0] == &D[0] && &D[0] == s[0];
  506. }
  507. #endif
  508. ]])],
  509. [ac_cv_c_vararrays=yes],
  510. [ac_cv_c_vararrays=no])])])
  511. if test "$ac_cv_c_vararrays" = yes; then
  512. dnl This is for compatibility with Autoconf 2.61-2.69.
  513. AC_DEFINE([HAVE_C_VARARRAYS], 1,
  514. [Define to 1 if C supports variable-length arrays.])
  515. elif test "$ac_cv_c_vararrays" = no; then
  516. AC_DEFINE([__STDC_NO_VLA__], 1,
  517. [Define to 1 if C does not support variable-length arrays, and
  518. if the compiler does not already define this.])
  519. fi
  520. ])