ax_cxx_compile_stdcxx_11.m4 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # ============================================================================
  2. # http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html
  3. # ============================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_CXX_COMPILE_STDCXX_11([ext|noext],[mandatory|optional])
  8. #
  9. # DESCRIPTION
  10. #
  11. # Check for baseline language coverage in the compiler for the C++11
  12. # standard; if necessary, add switches to CXXFLAGS to enable support.
  13. #
  14. # The first argument, if specified, indicates whether you insist on an
  15. # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
  16. # -std=c++11). If neither is specified, you get whatever works, with
  17. # preference for an extended mode.
  18. #
  19. # The second argument, if specified 'mandatory' or if left unspecified,
  20. # indicates that baseline C++11 support is required and that the macro
  21. # should error out if no mode with that support is found. If specified
  22. # 'optional', then configuration proceeds regardless, after defining
  23. # HAVE_CXX11 if and only if a supporting mode is found.
  24. #
  25. # LICENSE
  26. #
  27. # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
  28. # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
  29. # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
  30. #
  31. # Copying and distribution of this file, with or without modification, are
  32. # permitted in any medium without royalty provided the copyright notice
  33. # and this notice are preserved. This file is offered as-is, without any
  34. # warranty.
  35. #serial 3
  36. m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody], [
  37. template <typename T>
  38. struct check
  39. {
  40. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  41. };
  42. typedef check<check<bool>> right_angle_brackets;
  43. int a;
  44. decltype(a) b;
  45. typedef check<int> check_type;
  46. check_type c;
  47. check_type&& cr = static_cast<check_type&&>(c);
  48. auto d = a;
  49. ])
  50. AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [dnl
  51. m4_if([$1], [], [],
  52. [$1], [ext], [],
  53. [$1], [noext], [],
  54. [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_11])])dnl
  55. m4_if([$2], [], [ax_cxx_compile_cxx11_required=true],
  56. [$2], [mandatory], [ax_cxx_compile_cxx11_required=true],
  57. [$2], [optional], [ax_cxx_compile_cxx11_required=false],
  58. [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX_11])])
  59. AC_LANG_PUSH([C++])dnl
  60. ac_success=no
  61. AC_CACHE_CHECK(whether $CXX supports C++11 features by default,
  62. ax_cv_cxx_compile_cxx11,
  63. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  64. [ax_cv_cxx_compile_cxx11=yes],
  65. [ax_cv_cxx_compile_cxx11=no])])
  66. if test x$ax_cv_cxx_compile_cxx11 = xyes; then
  67. ac_success=yes
  68. fi
  69. m4_if([$1], [noext], [], [dnl
  70. if test x$ac_success = xno; then
  71. for switch in -std=gnu++11 -std=gnu++0x; do
  72. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch])
  73. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch,
  74. $cachevar,
  75. [ac_save_CXXFLAGS="$CXXFLAGS"
  76. CXXFLAGS="$CXXFLAGS $switch"
  77. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  78. [eval $cachevar=yes],
  79. [eval $cachevar=no])
  80. CXXFLAGS="$ac_save_CXXFLAGS"])
  81. if eval test x\$$cachevar = xyes; then
  82. CXXFLAGS="$CXXFLAGS $switch"
  83. ac_success=yes
  84. break
  85. fi
  86. done
  87. fi])
  88. m4_if([$1], [ext], [], [dnl
  89. if test x$ac_success = xno; then
  90. for switch in -std=c++11 -std=c++0x; do
  91. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch])
  92. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch,
  93. $cachevar,
  94. [ac_save_CXXFLAGS="$CXXFLAGS"
  95. CXXFLAGS="$CXXFLAGS $switch"
  96. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  97. [eval $cachevar=yes],
  98. [eval $cachevar=no])
  99. CXXFLAGS="$ac_save_CXXFLAGS"])
  100. if eval test x\$$cachevar = xyes; then
  101. CXXFLAGS="$CXXFLAGS $switch"
  102. ac_success=yes
  103. break
  104. fi
  105. done
  106. fi])
  107. AC_LANG_POP([C++])
  108. if test x$ax_cxx_compile_cxx11_required = xtrue; then
  109. if test x$ac_success = xno; then
  110. AC_MSG_ERROR([*** A compiler with support for C++11 language features is required.])
  111. fi
  112. else
  113. if test x$ac_success = xno; then
  114. HAVE_CXX11=0
  115. AC_MSG_NOTICE([No compiler with C++11 support was found])
  116. else
  117. HAVE_CXX11=1
  118. AC_DEFINE(HAVE_CXX11,1,
  119. [define if the compiler supports basic C++11 syntax])
  120. fi
  121. AC_SUBST(HAVE_CXX11)
  122. fi
  123. ])