std-gnu11.m4 24 KB

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