std-gnu11.m4 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. # Prefer GNU C11 and C++11 to earlier versions. -*- coding: utf-8 -*-
  2. # This implementation is taken from GNU Autoconf lib/autoconf/c.m4
  3. # commit 017d5ddd82854911f0119691d91ea8a1438824d6
  4. # dated Sun Apr 3 13:57:17 2016 -0700
  5. # This implementation will be obsolete once we can assume Autoconf 2.70
  6. # or later is installed everywhere a Gnulib program might be developed.
  7. m4_version_prereq([2.70], [], [
  8. # Copyright (C) 2001-2023 Free Software Foundation, Inc.
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. # Written by David MacKenzie, with help from
  22. # Akim Demaille, Paul Eggert,
  23. # François Pinard, Karl Berry, Richard Pixley, Ian Lance Taylor,
  24. # Roland McGrath, Noah Friedman, david d zuhn, and many others.
  25. # AC_PROG_CC([COMPILER ...])
  26. # --------------------------
  27. # COMPILER ... is a space separated list of C compilers to search for.
  28. # This just gives the user an opportunity to specify an alternative
  29. # search list for the C compiler.
  30. AC_DEFUN_ONCE([AC_PROG_CC],
  31. [AC_LANG_PUSH(C)dnl
  32. AC_ARG_VAR([CC], [C compiler command])dnl
  33. AC_ARG_VAR([CFLAGS], [C compiler flags])dnl
  34. _AC_ARG_VAR_LDFLAGS()dnl
  35. _AC_ARG_VAR_LIBS()dnl
  36. _AC_ARG_VAR_CPPFLAGS()dnl
  37. m4_ifval([$1],
  38. [AC_CHECK_TOOLS(CC, [$1])],
  39. [AC_CHECK_TOOL(CC, gcc)
  40. if test -z "$CC"; then
  41. dnl Here we want:
  42. dnl AC_CHECK_TOOL(CC, cc)
  43. dnl but without the check for a tool without the prefix.
  44. dnl Until the check is removed from there, copy the code:
  45. if test -n "$ac_tool_prefix"; then
  46. AC_CHECK_PROG(CC, [${ac_tool_prefix}cc], [${ac_tool_prefix}cc])
  47. fi
  48. fi
  49. if test -z "$CC"; then
  50. AC_CHECK_PROG(CC, cc, cc, , , /usr/ucb/cc)
  51. fi
  52. if test -z "$CC"; then
  53. AC_CHECK_TOOLS(CC, cl.exe)
  54. fi
  55. if test -z "$CC"; then
  56. AC_CHECK_TOOL(CC, clang)
  57. fi
  58. ])
  59. test -z "$CC" && AC_MSG_FAILURE([no acceptable C compiler found in \$PATH])
  60. # Provide some information about the compiler.
  61. _AS_ECHO_LOG([checking for _AC_LANG compiler version])
  62. set X $ac_compile
  63. ac_compiler=$[2]
  64. for ac_option in --version -v -V -qversion -version; do
  65. _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
  66. done
  67. m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
  68. m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
  69. _AC_LANG_COMPILER_GNU
  70. if test $ac_compiler_gnu = yes; then
  71. GCC=yes
  72. else
  73. GCC=
  74. fi
  75. _AC_PROG_CC_G
  76. dnl
  77. dnl Set ac_prog_cc_stdc to the supported C version.
  78. dnl Also set the documented variable ac_cv_prog_cc_stdc;
  79. dnl its name was chosen when it was cached, but it is no longer cached.
  80. _AC_PROG_CC_C11([ac_prog_cc_stdc=c11
  81. ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c11],
  82. [_AC_PROG_CC_C99([ac_prog_cc_stdc=c99
  83. ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c99],
  84. [_AC_PROG_CC_C89([ac_prog_cc_stdc=c89
  85. ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c89],
  86. [ac_prog_cc_stdc=no
  87. ac_cv_prog_cc_stdc=no])])])
  88. dnl
  89. AC_LANG_POP(C)dnl
  90. ])# AC_PROG_CC
  91. # AC_PROG_CXX([LIST-OF-COMPILERS])
  92. # --------------------------------
  93. # LIST-OF-COMPILERS is a space separated list of C++ compilers to search
  94. # for (if not specified, a default list is used). This just gives the
  95. # user an opportunity to specify an alternative search list for the C++
  96. # compiler.
  97. # aCC HP-UX C++ compiler much better than `CC', so test before.
  98. # FCC Fujitsu C++ compiler
  99. # KCC KAI C++ compiler
  100. # RCC Rational C++
  101. # xlC_r AIX C Set++ (with support for reentrant code)
  102. # xlC AIX C Set++
  103. AC_DEFUN([AC_PROG_CXX],
  104. [AC_LANG_PUSH(C++)dnl
  105. AC_ARG_VAR([CXX], [C++ compiler command])dnl
  106. AC_ARG_VAR([CXXFLAGS], [C++ compiler flags])dnl
  107. _AC_ARG_VAR_LDFLAGS()dnl
  108. _AC_ARG_VAR_LIBS()dnl
  109. _AC_ARG_VAR_CPPFLAGS()dnl
  110. _AC_ARG_VAR_PRECIOUS([CCC])dnl
  111. if test -z "$CXX"; then
  112. if test -n "$CCC"; then
  113. CXX=$CCC
  114. else
  115. AC_CHECK_TOOLS(CXX,
  116. [m4_default([$1],
  117. [g++ c++ gpp aCC CC cxx cc++ cl.exe FCC KCC RCC xlC_r xlC clang++])],
  118. g++)
  119. fi
  120. fi
  121. # Provide some information about the compiler.
  122. _AS_ECHO_LOG([checking for _AC_LANG compiler version])
  123. set X $ac_compile
  124. ac_compiler=$[2]
  125. for ac_option in --version -v -V -qversion; do
  126. _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
  127. done
  128. m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
  129. m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
  130. _AC_LANG_COMPILER_GNU
  131. if test $ac_compiler_gnu = yes; then
  132. GXX=yes
  133. else
  134. GXX=
  135. fi
  136. _AC_PROG_CXX_G
  137. _AC_PROG_CXX_CXX11([ac_prog_cxx_stdcxx=cxx11
  138. ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx11
  139. ac_cv_prog_cxx_cxx98=$ac_cv_prog_cxx_cxx11],
  140. [_AC_PROG_CXX_CXX98([ac_prog_cxx_stdcxx=cxx98
  141. ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx98],
  142. [ac_prog_cxx_stdcxx=no
  143. ac_cv_prog_cxx_stdcxx=no])])
  144. AC_LANG_POP(C++)dnl
  145. ])# AC_PROG_CXX
  146. # _AC_C_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
  147. # ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
  148. # --------------------------------------------------------------
  149. # Check whether the C compiler accepts features of STANDARD (e.g `c89', `c99')
  150. # by trying to compile a program of TEST-PROLOGUE and TEST-BODY. If this fails,
  151. # try again with each compiler option in the space-separated OPTION-LIST; if one
  152. # helps, append it to CC. If eventually successful, run ACTION-IF-AVAILABLE,
  153. # else ACTION-IF-UNAVAILABLE.
  154. AC_DEFUN([_AC_C_STD_TRY],
  155. [AC_MSG_CHECKING([for $CC option to enable ]m4_translit($1, [c], [C])[ features])
  156. AC_CACHE_VAL(ac_cv_prog_cc_$1,
  157. [ac_cv_prog_cc_$1=no
  158. ac_save_CC=$CC
  159. AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
  160. for ac_arg in '' $4
  161. do
  162. CC="$ac_save_CC $ac_arg"
  163. _AC_COMPILE_IFELSE([], [ac_cv_prog_cc_$1=$ac_arg])
  164. test "x$ac_cv_prog_cc_$1" != "xno" && break
  165. done
  166. rm -f conftest.$ac_ext
  167. CC=$ac_save_CC
  168. ])# AC_CACHE_VAL
  169. ac_prog_cc_stdc_options=
  170. case "x$ac_cv_prog_cc_$1" in
  171. x)
  172. AC_MSG_RESULT([none needed]) ;;
  173. xno)
  174. AC_MSG_RESULT([unsupported]) ;;
  175. *)
  176. ac_prog_cc_stdc_options=" $ac_cv_prog_cc_$1"
  177. CC=$CC$ac_prog_cc_stdc_options
  178. AC_MSG_RESULT([$ac_cv_prog_cc_$1]) ;;
  179. esac
  180. AS_IF([test "x$ac_cv_prog_cc_$1" != xno], [$5], [$6])
  181. ])# _AC_C_STD_TRY
  182. # _AC_C_C99_TEST_HEADER
  183. # ---------------------
  184. # A C header suitable for testing for C99.
  185. AC_DEFUN([_AC_C_C99_TEST_HEADER],
  186. [[#include <stdarg.h>
  187. #include <stdbool.h>
  188. #include <stddef.h>
  189. #include <stdlib.h>
  190. #include <wchar.h>
  191. #include <stdio.h>
  192. // Check varargs macros. These examples are taken from C99 6.10.3.5.
  193. #define debug(...) fprintf (stderr, __VA_ARGS__)
  194. #define showlist(...) puts (#__VA_ARGS__)
  195. #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
  196. static void
  197. test_varargs_macros (void)
  198. {
  199. int x = 1234;
  200. int y = 5678;
  201. debug ("Flag");
  202. debug ("X = %d\n", x);
  203. showlist (The first, second, and third items.);
  204. report (x>y, "x is %d but y is %d", x, y);
  205. }
  206. // Check long long types.
  207. #define BIG64 18446744073709551615ull
  208. #define BIG32 4294967295ul
  209. #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
  210. #if !BIG_OK
  211. your preprocessor is broken;
  212. #endif
  213. #if BIG_OK
  214. #else
  215. your preprocessor is broken;
  216. #endif
  217. static long long int bignum = -9223372036854775807LL;
  218. static unsigned long long int ubignum = BIG64;
  219. struct incomplete_array
  220. {
  221. int datasize;
  222. double data[];
  223. };
  224. struct named_init {
  225. int number;
  226. const wchar_t *name;
  227. double average;
  228. };
  229. typedef const char *ccp;
  230. static inline int
  231. test_restrict (ccp restrict text)
  232. {
  233. // See if C++-style comments work.
  234. // Iterate through items via the restricted pointer.
  235. // Also check for declarations in for loops.
  236. for (unsigned int i = 0; *(text+i) != '\0'; ++i)
  237. continue;
  238. return 0;
  239. }
  240. // Check varargs and va_copy.
  241. static bool
  242. test_varargs (const char *format, ...)
  243. {
  244. va_list args;
  245. va_start (args, format);
  246. va_list args_copy;
  247. va_copy (args_copy, args);
  248. const char *str = "";
  249. int number = 0;
  250. float fnumber = 0;
  251. while (*format)
  252. {
  253. switch (*format++)
  254. {
  255. case 's': // string
  256. str = va_arg (args_copy, const char *);
  257. break;
  258. case 'd': // int
  259. number = va_arg (args_copy, int);
  260. break;
  261. case 'f': // float
  262. fnumber = va_arg (args_copy, double);
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. va_end (args_copy);
  269. va_end (args);
  270. return *str && number && fnumber;
  271. }]])# _AC_C_C99_TEST_HEADER
  272. # _AC_C_C99_TEST_BODY
  273. # -------------------
  274. # A C body suitable for testing for C99, assuming the corresponding header.
  275. AC_DEFUN([_AC_C_C99_TEST_BODY],
  276. [[
  277. // Check bool.
  278. _Bool success = false;
  279. // Check restrict.
  280. if (test_restrict ("String literal") == 0)
  281. success = true;
  282. char *restrict newvar = "Another string";
  283. // Check varargs.
  284. success &= test_varargs ("s, d' f .", "string", 65, 34.234);
  285. test_varargs_macros ();
  286. // Check flexible array members.
  287. struct incomplete_array *ia =
  288. malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
  289. ia->datasize = 10;
  290. for (int i = 0; i < ia->datasize; ++i)
  291. ia->data[i] = i * 1.234;
  292. // Check named initializers.
  293. struct named_init ni = {
  294. .number = 34,
  295. .name = L"Test wide string",
  296. .average = 543.34343,
  297. };
  298. ni.number = 58;
  299. int dynamic_array[ni.number];
  300. dynamic_array[ni.number - 1] = 543;
  301. // work around unused variable warnings
  302. return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
  303. || dynamic_array[ni.number - 1] != 543);
  304. ]])
  305. # _AC_PROG_CC_C99 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
  306. # ----------------------------------------------------------------
  307. # If the C compiler is not in ISO C99 mode by default, try to add an
  308. # option to output variable CC to make it so. This macro tries
  309. # various options that select ISO C99 on some system or another. It
  310. # considers the compiler to be in ISO C99 mode if it handles _Bool,
  311. # // comments, flexible array members, inline, long long int, mixed
  312. # code and declarations, named initialization of structs, restrict,
  313. # va_copy, varargs macros, variable declarations in for loops and
  314. # variable length arrays.
  315. AC_DEFUN([_AC_PROG_CC_C99],
  316. [_AC_C_STD_TRY([c99],
  317. [_AC_C_C99_TEST_HEADER],
  318. [_AC_C_C99_TEST_BODY],
  319. dnl Try
  320. dnl GCC -std=gnu99 (unused restrictive modes: -std=c99 -std=iso9899:1999)
  321. dnl IBM XL C -qlanglvl=extc1x (V12.1; does not pass C11 test)
  322. dnl IBM XL C -qlanglvl=extc99
  323. dnl (pre-V12.1; unused restrictive mode: -qlanglvl=stdc99)
  324. dnl HP cc -AC99
  325. dnl Intel ICC -std=c99, -c99 (deprecated)
  326. dnl IRIX -c99
  327. dnl Solaris -D_STDC_C99=
  328. dnl cc's -xc99 option uses linker magic to define the external
  329. dnl symbol __xpg4 as if by "int __xpg4 = 1;", which enables C99
  330. dnl behavior for C library functions. This is not wanted here,
  331. dnl because it means that a single module compiled with -xc99
  332. dnl alters C runtime behavior for the entire program, not for
  333. dnl just the module. Instead, define the (private) symbol
  334. dnl _STDC_C99, which suppresses a bogus failure in <stdbool.h>.
  335. dnl The resulting compiler passes the test case here, and that's
  336. dnl good enough. For more, please see the thread starting at:
  337. dnl https://lists.gnu.org/r/autoconf/2010-12/msg00059.html
  338. dnl Tru64 -c99
  339. dnl with extended modes being tried first.
  340. [[-std=gnu99 -std=c99 -c99 -AC99 -D_STDC_C99= -qlanglvl=extc1x -qlanglvl=extc99]], [$1], [$2])[]dnl
  341. ])# _AC_PROG_CC_C99
  342. # _AC_PROG_CC_C11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
  343. # ----------------------------------------------------------------
  344. # If the C compiler is not in ISO C11 mode by default, try to add an
  345. # option to output variable CC to make it so. This macro tries
  346. # various options that select ISO C11 on some system or another. It
  347. # considers the compiler to be in ISO C11 mode if it handles _Alignas,
  348. # _Alignof, _Noreturn, _Static_assert, UTF-8 string literals,
  349. # duplicate typedefs, and anonymous structures and unions.
  350. AC_DEFUN([_AC_PROG_CC_C11],
  351. [_AC_C_STD_TRY([c11],
  352. [_AC_C_C99_TEST_HEADER[
  353. // Check _Alignas.
  354. char _Alignas (double) aligned_as_double;
  355. char _Alignas (0) no_special_alignment;
  356. extern char aligned_as_int;
  357. char _Alignas (0) _Alignas (int) aligned_as_int;
  358. // Check _Alignof.
  359. enum
  360. {
  361. int_alignment = _Alignof (int),
  362. int_array_alignment = _Alignof (int[100]),
  363. char_alignment = _Alignof (char)
  364. };
  365. _Static_assert (0 < -_Alignof (int), "_Alignof is signed");
  366. // Check _Noreturn.
  367. int _Noreturn does_not_return (void) { for (;;) continue; }
  368. // Check _Static_assert.
  369. struct test_static_assert
  370. {
  371. int x;
  372. _Static_assert (sizeof (int) <= sizeof (long int),
  373. "_Static_assert does not work in struct");
  374. long int y;
  375. };
  376. // Check UTF-8 literals.
  377. #define u8 syntax error!
  378. char const utf8_literal[] = u8"happens to be ASCII" "another string";
  379. // Check duplicate typedefs.
  380. typedef long *long_ptr;
  381. typedef long int *long_ptr;
  382. typedef long_ptr long_ptr;
  383. // Anonymous structures and unions -- taken from C11 6.7.2.1 Example 1.
  384. struct anonymous
  385. {
  386. union {
  387. struct { int i; int j; };
  388. struct { int k; long int l; } w;
  389. };
  390. int m;
  391. } v1;
  392. ]],
  393. [_AC_C_C99_TEST_BODY[
  394. v1.i = 2;
  395. v1.w.k = 5;
  396. _Static_assert ((offsetof (struct anonymous, i)
  397. == offsetof (struct anonymous, w.k)),
  398. "Anonymous union alignment botch");
  399. ]],
  400. dnl Try
  401. dnl GCC -std=gnu11 (unused restrictive mode: -std=c11)
  402. dnl with extended modes being tried first.
  403. dnl
  404. dnl Do not try -qlanglvl=extc1x, because IBM XL C V12.1 (the latest version as
  405. dnl of September 2012) does not pass the C11 test. For now, try extc1x when
  406. dnl compiling the C99 test instead, since it enables _Static_assert and
  407. dnl _Noreturn, which is a win. If -qlanglvl=extc11 or -qlanglvl=extc1x passes
  408. dnl the C11 test in some future version of IBM XL C, we'll add it here,
  409. dnl preferably extc11.
  410. [[-std=gnu11]], [$1], [$2])[]dnl
  411. ])# _AC_PROG_CC_C11
  412. # AC_PROG_CC_C89
  413. # --------------
  414. # Do not use AU_ALIAS here and in AC_PROG_CC_C99 and AC_PROG_CC_STDC,
  415. # as that'd be incompatible with how Automake redefines AC_PROG_CC. See
  416. # <https://lists.gnu.org/r/autoconf/2012-10/msg00048.html>.
  417. AU_DEFUN([AC_PROG_CC_C89],
  418. [AC_REQUIRE([AC_PROG_CC])],
  419. [$0 is obsolete; use AC_PROG_CC]
  420. )
  421. # AC_PROG_CC_C99
  422. # --------------
  423. AU_DEFUN([AC_PROG_CC_C99],
  424. [AC_REQUIRE([AC_PROG_CC])],
  425. [$0 is obsolete; use AC_PROG_CC]
  426. )
  427. # AC_PROG_CC_STDC
  428. # ---------------
  429. AU_DEFUN([AC_PROG_CC_STDC],
  430. [AC_REQUIRE([AC_PROG_CC])],
  431. [$0 is obsolete; use AC_PROG_CC]
  432. )
  433. # AC_C_PROTOTYPES
  434. # ---------------
  435. # Check if the C compiler supports prototypes, included if it needs
  436. # options.
  437. AC_DEFUN([AC_C_PROTOTYPES],
  438. [AC_REQUIRE([AC_PROG_CC])dnl
  439. if test "$ac_prog_cc_stdc" != no; then
  440. AC_DEFINE(PROTOTYPES, 1,
  441. [Define to 1 if the C compiler supports function prototypes.])
  442. AC_DEFINE(__PROTOTYPES, 1,
  443. [Define like PROTOTYPES; this can be used by system headers.])
  444. fi
  445. ])# AC_C_PROTOTYPES
  446. # _AC_CXX_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
  447. # ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
  448. # ----------------------------------------------------------------
  449. # Check whether the C++ compiler accepts features of STANDARD (e.g
  450. # `cxx98', `cxx11') by trying to compile a program of TEST-PROLOGUE
  451. # and TEST-BODY. If this fails, try again with each compiler option
  452. # in the space-separated OPTION-LIST; if one helps, append it to CXX.
  453. # If eventually successful, run ACTION-IF-AVAILABLE, else
  454. # ACTION-IF-UNAVAILABLE.
  455. AC_DEFUN([_AC_CXX_STD_TRY],
  456. [AC_MSG_CHECKING([for $CXX option to enable ]m4_translit(m4_translit($1, [x], [+]), [a-z], [A-Z])[ features])
  457. AC_LANG_PUSH(C++)dnl
  458. AC_CACHE_VAL(ac_cv_prog_cxx_$1,
  459. [ac_cv_prog_cxx_$1=no
  460. ac_save_CXX=$CXX
  461. AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
  462. for ac_arg in '' $4
  463. do
  464. CXX="$ac_save_CXX $ac_arg"
  465. _AC_COMPILE_IFELSE([], [ac_cv_prog_cxx_$1=$ac_arg])
  466. test "x$ac_cv_prog_cxx_$1" != "xno" && break
  467. done
  468. rm -f conftest.$ac_ext
  469. CXX=$ac_save_CXX
  470. ])# AC_CACHE_VAL
  471. ac_prog_cxx_stdcxx_options=
  472. case "x$ac_cv_prog_cxx_$1" in
  473. x)
  474. AC_MSG_RESULT([none needed]) ;;
  475. xno)
  476. AC_MSG_RESULT([unsupported]) ;;
  477. *)
  478. ac_prog_cxx_stdcxx_options=" $ac_cv_prog_cxx_$1"
  479. CXX=$CXX$ac_prog_cxx_stdcxx_options
  480. AC_MSG_RESULT([$ac_cv_prog_cxx_$1]) ;;
  481. esac
  482. AC_LANG_POP(C++)dnl
  483. AS_IF([test "x$ac_cv_prog_cxx_$1" != xno], [$5], [$6])
  484. ])# _AC_CXX_STD_TRY
  485. # _AC_CXX_CXX98_TEST_HEADER
  486. # -------------------------
  487. # A C++ header suitable for testing for CXX98.
  488. AC_DEFUN([_AC_CXX_CXX98_TEST_HEADER],
  489. [[
  490. #include <algorithm>
  491. #include <cstdlib>
  492. #include <fstream>
  493. #include <iomanip>
  494. #include <iostream>
  495. #include <list>
  496. #include <map>
  497. #include <set>
  498. #include <sstream>
  499. #include <stdexcept>
  500. #include <string>
  501. #include <utility>
  502. #include <vector>
  503. namespace test {
  504. typedef std::vector<std::string> string_vec;
  505. typedef std::pair<int,bool> map_value;
  506. typedef std::map<std::string,map_value> map_type;
  507. typedef std::set<int> set_type;
  508. template<typename T>
  509. class printer {
  510. public:
  511. printer(std::ostringstream& os): os(os) {}
  512. void operator() (T elem) { os << elem << std::endl; }
  513. private:
  514. std::ostringstream& os;
  515. };
  516. }
  517. ]])# _AC_CXX_CXX98_TEST_HEADER
  518. # _AC_CXX_CXX98_TEST_BODY
  519. # -----------------------
  520. # A C++ body suitable for testing for CXX98, assuming the corresponding header.
  521. AC_DEFUN([_AC_CXX_CXX98_TEST_BODY],
  522. [[
  523. try {
  524. // Basic string.
  525. std::string teststr("ASCII text");
  526. teststr += " string";
  527. // Simple vector.
  528. test::string_vec testvec;
  529. testvec.push_back(teststr);
  530. testvec.push_back("foo");
  531. testvec.push_back("bar");
  532. if (testvec.size() != 3) {
  533. throw std::runtime_error("vector size is not 1");
  534. }
  535. // Dump vector into stringstream and obtain string.
  536. std::ostringstream os;
  537. for (test::string_vec::const_iterator i = testvec.begin();
  538. i != testvec.end(); ++i) {
  539. if (i + 1 != testvec.end()) {
  540. os << teststr << '\n';
  541. }
  542. }
  543. // Check algorithms work.
  544. std::for_each(testvec.begin(), testvec.end(), test::printer<std::string>(os));
  545. std::string os_out = os.str();
  546. // Test pair and map.
  547. test::map_type testmap;
  548. testmap.insert(std::make_pair(std::string("key"),
  549. std::make_pair(53,false)));
  550. // Test set.
  551. int values[] = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
  552. test::set_type testset(values, values + sizeof(values)/sizeof(values[0]));
  553. std::list<int> testlist(testset.begin(), testset.end());
  554. std::copy(testset.begin(), testset.end(), std::back_inserter(testlist));
  555. } catch (const std::exception& e) {
  556. std::cerr << "Caught exception: " << e.what() << std::endl;
  557. // Test fstream
  558. std::ofstream of("test.txt");
  559. of << "Test ASCII text\n" << std::flush;
  560. of << "N= " << std::hex << std::setw(8) << std::left << 534 << std::endl;
  561. of.close();
  562. }
  563. std::exit(0);
  564. ]])
  565. # _AC_CXX_CXX11_TEST_HEADER
  566. # -------------------------
  567. # A C++ header suitable for testing for CXX11.
  568. AC_DEFUN([_AC_CXX_CXX11_TEST_HEADER],
  569. [[
  570. #include <deque>
  571. #include <functional>
  572. #include <memory>
  573. #include <tuple>
  574. #include <array>
  575. #include <regex>
  576. #include <iostream>
  577. namespace cxx11test
  578. {
  579. typedef std::shared_ptr<std::string> sptr;
  580. typedef std::weak_ptr<std::string> wptr;
  581. typedef std::tuple<std::string,int,double> tp;
  582. typedef std::array<int, 20> int_array;
  583. constexpr int get_val() { return 20; }
  584. struct testinit
  585. {
  586. int i;
  587. double d;
  588. };
  589. class delegate {
  590. public:
  591. delegate(int n) : n(n) {}
  592. delegate(): delegate(2354) {}
  593. virtual int getval() { return this->n; };
  594. protected:
  595. int n;
  596. };
  597. class overridden : public delegate {
  598. public:
  599. overridden(int n): delegate(n) {}
  600. virtual int getval() override final { return this->n * 2; }
  601. };
  602. class nocopy {
  603. public:
  604. nocopy(int i): i(i) {}
  605. nocopy() = default;
  606. nocopy(const nocopy&) = delete;
  607. nocopy & operator=(const nocopy&) = delete;
  608. private:
  609. int i;
  610. };
  611. }
  612. ]])# _AC_CXX_CXX11_TEST_HEADER
  613. # _AC_CXX_CXX11_TEST_BODY
  614. # -----------------------
  615. # A C++ body suitable for testing for CXX11, assuming the corresponding header.
  616. AC_DEFUN([_AC_CXX_CXX11_TEST_BODY],
  617. [[
  618. {
  619. // Test auto and decltype
  620. std::deque<int> d;
  621. d.push_front(43);
  622. d.push_front(484);
  623. d.push_front(3);
  624. d.push_front(844);
  625. int total = 0;
  626. for (auto i = d.begin(); i != d.end(); ++i) { total += *i; }
  627. auto a1 = 6538;
  628. auto a2 = 48573953.4;
  629. auto a3 = "String literal";
  630. decltype(a2) a4 = 34895.034;
  631. }
  632. {
  633. // Test constexpr
  634. short sa[cxx11test::get_val()] = { 0 };
  635. }
  636. {
  637. // Test initializer lists
  638. cxx11test::testinit il = { 4323, 435234.23544 };
  639. }
  640. {
  641. // Test range-based for and lambda
  642. cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
  643. for (int &x : array) { x += 23; }
  644. std::for_each(array.begin(), array.end(), [](int v1){ std::cout << v1; });
  645. }
  646. {
  647. using cxx11test::sptr;
  648. using cxx11test::wptr;
  649. sptr sp(new std::string("ASCII string"));
  650. wptr wp(sp);
  651. sptr sp2(wp);
  652. }
  653. {
  654. cxx11test::tp tuple("test", 54, 45.53434);
  655. double d = std::get<2>(tuple);
  656. std::string s;
  657. int i;
  658. std::tie(s,i,d) = tuple;
  659. }
  660. {
  661. static std::regex filename_regex("^_?([a-z0-9_.]+-)+[a-z0-9]+$");
  662. std::string testmatch("Test if this string matches");
  663. bool match = std::regex_search(testmatch, filename_regex);
  664. }
  665. {
  666. cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
  667. cxx11test::int_array::size_type size = array.size();
  668. }
  669. {
  670. // Test constructor delegation
  671. cxx11test::delegate d1;
  672. cxx11test::delegate d2();
  673. cxx11test::delegate d3(45);
  674. }
  675. {
  676. // Test override and final
  677. cxx11test::overridden o1(55464);
  678. }
  679. {
  680. // Test nullptr
  681. char *c = nullptr;
  682. }
  683. {
  684. // Test template brackets
  685. std::vector<std::pair<int,char*>> v1;
  686. }
  687. {
  688. // Unicode literals
  689. char const *utf8 = u8"UTF-8 string \u2500";
  690. char16_t const *utf16 = u"UTF-8 string \u2500";
  691. char32_t const *utf32 = U"UTF-32 string \u2500";
  692. }
  693. ]])
  694. # _AC_PROG_CXX_CXX98 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
  695. # -------------------------------------------------------------------
  696. # If the C++ compiler is not in ISO C++98 mode by default, try to add
  697. # an option to output variable CXX to make it so. This macro tries
  698. # various options that select ISO C++98 on some system or another. It
  699. # considers the compiler to be in ISO C++98 mode if it handles basic
  700. # features of the std namespace including: string, containers (list,
  701. # map, set, vector), streams (fstreams, iostreams, stringstreams,
  702. # iomanip), pair, exceptions and algorithms.
  703. AC_DEFUN([_AC_PROG_CXX_CXX98],
  704. [_AC_CXX_STD_TRY([cxx98],
  705. [_AC_CXX_CXX98_TEST_HEADER],
  706. [_AC_CXX_CXX98_TEST_BODY],
  707. dnl Try
  708. dnl GCC -std=gnu++98 (unused restrictive mode: -std=c++98)
  709. dnl IBM XL C -qlanglvl=extended
  710. dnl HP aC++ -AA
  711. dnl Intel ICC -std=gnu++98
  712. dnl Solaris N/A (default)
  713. dnl Tru64 N/A (default, but -std gnu could be used)
  714. dnl with extended modes being tried first.
  715. [[-std=gnu++98 -std=c++98 -qlanglvl=extended -AA]], [$1], [$2])[]dnl
  716. ])# _AC_PROG_CXX_CXX98
  717. # _AC_PROG_CXX_CXX11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
  718. # -------------------------------------------------------------------
  719. # If the C++ compiler is not in ISO CXX11 mode by default, try to add
  720. # an option to output variable CXX to make it so. This macro tries
  721. # various options that select ISO C++11 on some system or another. It
  722. # considers the compiler to be in ISO C++11 mode if it handles all the
  723. # tests from the C++98 checks, plus the following: Language features
  724. # (auto, constexpr, decltype, default/deleted constructors, delegate
  725. # constructors, final, initializer lists, lambda functions, nullptr,
  726. # override, range-based for loops, template brackets without spaces,
  727. # unicode literals) and library features (array, memory (shared_ptr,
  728. # weak_ptr), regex and tuple types).
  729. AC_DEFUN([_AC_PROG_CXX_CXX11],
  730. [_AC_CXX_STD_TRY([cxx11],
  731. [_AC_CXX_CXX11_TEST_HEADER
  732. _AC_CXX_CXX98_TEST_HEADER],
  733. [_AC_CXX_CXX11_TEST_BODY
  734. _AC_CXX_CXX98_TEST_BODY],
  735. dnl Try
  736. dnl GCC -std=gnu++11 (unused restrictive mode: -std=c++11) [and 0x variants]
  737. dnl IBM XL C -qlanglvl=extended0x
  738. dnl (pre-V12.1; unused restrictive mode: -qlanglvl=stdcxx11)
  739. dnl HP aC++ -AA
  740. dnl Intel ICC -std=c++11 -std=c++0x
  741. dnl Solaris N/A (no support)
  742. dnl Tru64 N/A (no support)
  743. dnl with extended modes being tried first.
  744. [[-std=gnu++11 -std=c++11 -std=gnu++0x -std=c++0x -qlanglvl=extended0x -AA]], [$1], [$2])[]dnl
  745. ])# _AC_PROG_CXX_CXX11
  746. ])# m4_version_prereq