ax_cxx_compile_stdcxx_11.m4 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <[email protected]>
  28. # Copyright (c) 2012 Zack Weinberg <[email protected]>
  29. # Copyright (c) 2013 Roy Stogner <[email protected]>
  30. # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <[email protected]>
  31. #
  32. # Copying and distribution of this file, with or without modification, are
  33. # permitted in any medium without royalty provided the copyright notice
  34. # and this notice are preserved. This file is offered as-is, without any
  35. # warranty.
  36. #serial 9
  37. m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody], [[
  38. template <typename T>
  39. struct check
  40. {
  41. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  42. };
  43. struct Base {
  44. virtual void f() {}
  45. };
  46. struct Child : public Base {
  47. virtual void f() override {}
  48. };
  49. typedef check<check<bool>> right_angle_brackets;
  50. int a;
  51. decltype(a) b;
  52. typedef check<int> check_type;
  53. check_type c;
  54. check_type&& cr = static_cast<check_type&&>(c);
  55. auto d = a;
  56. auto l = [](){};
  57. // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
  58. // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function because of this
  59. namespace test_template_alias_sfinae {
  60. struct foo {};
  61. template<typename T>
  62. using member = typename T::member_type;
  63. template<typename T>
  64. void func(...) {}
  65. template<typename T>
  66. void func(member<T>*) {}
  67. void test();
  68. void test() {
  69. func<foo>(0);
  70. }
  71. }
  72. ]])
  73. AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [dnl
  74. m4_if([$1], [], [],
  75. [$1], [ext], [],
  76. [$1], [noext], [],
  77. [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_11])])dnl
  78. m4_if([$2], [], [ax_cxx_compile_cxx11_required=true],
  79. [$2], [mandatory], [ax_cxx_compile_cxx11_required=true],
  80. [$2], [optional], [ax_cxx_compile_cxx11_required=false],
  81. [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX_11])])
  82. AC_LANG_PUSH([C++])dnl
  83. ac_success=no
  84. AC_CACHE_CHECK(whether $CXX supports C++11 features by default,
  85. ax_cv_cxx_compile_cxx11,
  86. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  87. [ax_cv_cxx_compile_cxx11=yes],
  88. [ax_cv_cxx_compile_cxx11=no])])
  89. if test x$ax_cv_cxx_compile_cxx11 = xyes; then
  90. ac_success=yes
  91. fi
  92. m4_if([$1], [noext], [], [dnl
  93. if test x$ac_success = xno; then
  94. for switch in -std=gnu++11 -std=gnu++0x; do
  95. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch])
  96. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch,
  97. $cachevar,
  98. [ac_save_CXXFLAGS="$CXXFLAGS"
  99. CXXFLAGS="$CXXFLAGS $switch"
  100. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  101. [eval $cachevar=yes],
  102. [eval $cachevar=no])
  103. CXXFLAGS="$ac_save_CXXFLAGS"])
  104. if eval test x\$$cachevar = xyes; then
  105. CXXFLAGS="$CXXFLAGS $switch"
  106. ac_success=yes
  107. break
  108. fi
  109. done
  110. fi])
  111. m4_if([$1], [ext], [], [dnl
  112. if test x$ac_success = xno; then
  113. for switch in -std=c++11 -std=c++0x; do
  114. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch])
  115. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch,
  116. $cachevar,
  117. [ac_save_CXXFLAGS="$CXXFLAGS"
  118. CXXFLAGS="$CXXFLAGS $switch"
  119. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  120. [eval $cachevar=yes],
  121. [eval $cachevar=no])
  122. CXXFLAGS="$ac_save_CXXFLAGS"])
  123. if eval test x\$$cachevar = xyes; then
  124. CXXFLAGS="$CXXFLAGS $switch"
  125. ac_success=yes
  126. break
  127. fi
  128. done
  129. fi])
  130. AC_LANG_POP([C++])
  131. if test x$ax_cxx_compile_cxx11_required = xtrue; then
  132. if test x$ac_success = xno; then
  133. AC_MSG_ERROR([*** A compiler with support for C++11 language features is required.])
  134. fi
  135. else
  136. if test x$ac_success = xno; then
  137. HAVE_CXX11=0
  138. AC_MSG_NOTICE([No compiler with C++11 support was found])
  139. else
  140. HAVE_CXX11=1
  141. AC_DEFINE(HAVE_CXX11,1,
  142. [define if the compiler supports basic C++11 syntax])
  143. fi
  144. AC_SUBST(HAVE_CXX11)
  145. fi
  146. ])